diff options
| author | Tyge Løvset <[email protected]> | 2021-02-19 08:14:39 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-02-19 08:14:39 +0100 |
| commit | cfd14e93d275e5b4daa95321058ef5a13e1e4ff4 (patch) | |
| tree | 485e1570f8978f0d004628c5963f44d53d0b0636 | |
| parent | b87f71d04b1c8c26df55540124f4940a8f87b81b (diff) | |
| download | STC-modified-cfd14e93d275e5b4daa95321058ef5a13e1e4ff4.tar.gz STC-modified-cfd14e93d275e5b4daa95321058ef5a13e1e4ff4.zip | |
Minor docs. Updated csmap_v1.h
| -rw-r--r-- | docs/cmap_api.md | 2 | ||||
| -rw-r--r-- | docs/csmap_api.md | 2 | ||||
| -rw-r--r-- | examples/csmap_v1.h | 108 | ||||
| -rw-r--r-- | stc/csmap.h | 1 |
4 files changed, 39 insertions, 74 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 7cb88177..04cfccf8 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -177,7 +177,7 @@ int main() /* put replaces existing mapped value: */ cmap_id_put(&idnames, 110, "White"); /* put a constructed mapped value into map: */ - cmap_id_put_mapped(&idnames, 120, cstr_from_fmt("#%08x", col)); + cmap_id_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); /* emplace inserts only when key does not exist: */ cmap_id_emplace(&idnames, 100, "Green"); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 1d0e9708..72f90f08 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -154,7 +154,7 @@ int main() /* put replaces existing mapped value: */ csmap_id_put(&idnames, 110, "White"); /* put a constructed mapped value into map: */ - csmap_id_put_mapped(&idnames, 120, cstr_from_fmt("#%08x", col)); + csmap_id_insert_or_assign(&idnames, 120, cstr_from_fmt("#%08x", col)); /* emplace inserts only when key does not exist: */ csmap_id_emplace(&idnames, 100, "Green"); diff --git a/examples/csmap_v1.h b/examples/csmap_v1.h index bc7a978d..14abecbf 100644 --- a/examples/csmap_v1.h +++ b/examples/csmap_v1.h @@ -219,46 +219,52 @@ int main(void) { } \
\
STC_API C##_##X##_result_t \
- C##_##X##_insert_key(C##_##X* self, RawKey rkey); \
+ C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \
\
STC_INLINE C##_##X##_result_t \
- C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped) ) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- MAP_ONLY_##C( if (res.second) res.first->second = mappedFromRaw(rmapped); ) \
+ C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \
+ if (res.second) { \
+ *KEY_REF_##C(res.first) = keyFromRaw(rkey); \
+ MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \
+ } \
return res; \
} \
STC_INLINE C##_##X##_result_t \
- C##_##X##_insert(C##_##X* self, C##_##X##_rawvalue_t raw) { \
- return SET_ONLY_##C( C##_##X##_insert_key(self, raw) ) \
- MAP_ONLY_##C( C##_##X##_emplace(self, raw.first, raw.second) ); \
+ C##_##X##_put(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \
+ if (res.second) *KEY_REF_##C(res.first) = keyFromRaw(rkey); \
+ MAP_ONLY_##C( else mappedDel(&res.first->second); \
+ res.first->second = mappedFromRaw(rmapped); ) \
+ return res; \
} \
STC_INLINE void \
C##_##X##_push_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \
- for (size_t i=0; i<n; ++i) C##_##X##_insert(self, arr[i]); \
+ for (size_t i=0; i<n; ++i) SET_ONLY_##C( C##_##X##_emplace(self, arr[i]); ) \
+ MAP_ONLY_##C( C##_##X##_put(self, arr[i].first, arr[i].second); ) \
} \
\
- MAP_ONLY_##C( \
STC_INLINE C##_##X##_result_t \
- C##_##X##_put(C##_##X* self, RawKey rkey, RawMapped rmapped) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- if (!res.second) mappedDel(&res.first->second); \
- res.first->second = mappedFromRaw(rmapped); return res; \
- } \
- STC_INLINE C##_##X##_result_t \
- C##_##X##_insert_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \
- return C##_##X##_put(self, rkey, rmapped); \
- } \
- STC_INLINE C##_##X##_result_t \
- C##_##X##_put_mapped(C##_##X* self, RawKey rkey, Mapped mapped) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- if (!res.second) mappedDel(&res.first->second); \
- res.first->second = mapped; return res; \
+ C##_##X##_insert(C##_##X* self, Key key MAP_ONLY_##C(, Mapped mapped)) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(&key)); \
+ if (res.second) {*KEY_REF_##C(res.first) = key; MAP_ONLY_##C( res.first->second = mapped; )} \
+ else {keyDel(&key); MAP_ONLY_##C( mappedDel(&mapped); )} \
+ return res; \
} \
- STC_INLINE C##_##X##_mapped_t* \
- C##_##X##_at(const C##_##X* self, RawKey rkey) { \
- C##_##X##_iter_t it; \
- return &C##_##X##_find_it(self, rkey, &it)->second; \
- }) \
+\
+ MAP_ONLY_##C( \
+ STC_INLINE C##_##X##_result_t \
+ C##_##X##_insert_or_assign(C##_##X* self, Key key, Mapped mapped) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(&key)); \
+ if (res.second) res.first->first = key; else keyDel(&key); \
+ mappedDel(&res.first->second); res.first->second = mapped; \
+ return res; \
+ } \
+ STC_INLINE C##_##X##_mapped_t* \
+ C##_##X##_at(const C##_##X* self, RawKey rkey) { \
+ C##_##X##_iter_t it; \
+ return &C##_##X##_find_it(self, rkey, &it)->second; \
+ }) \
\
STC_INLINE C##_##X##_value_t* \
C##_##X##_front(C##_##X* self) { \
@@ -371,47 +377,8 @@ int main(void) { return tn; \
} \
\
-/*\
- static C##_##X##_node_t* ** recursive version ** \
- C##_##X##_insert_key_r_(C##_##X##_node_t* tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \
- if (tn->level == 0) { \
- tn = c_new_1(C##_##X##_node_t); \
- res->first = &tn->value, res->second = true; \
- tn->link[0] = tn->link[1] = (C##_##X##_node_t*) &cbst_nil, tn->level = 1; \
- *KEY_REF_##C(&tn->value) = keyFromRaw(*rkey); \
- return tn; \
- } \
- C##_##X##_rawkey_t r = keyToRaw(KEY_REF_##C(&tn->value)); \
- int c = keyCompareRaw(&r, rkey); \
- if (c == 0) { res->first = &tn->value; return tn; } \
- tn->link[c == -1] = C##_##X##_insert_key_r_(tn->link[c == -1], rkey, res); \
- tn = C##_##X##_skew_(tn); \
- tn = C##_##X##_split_(tn); \
- return tn; \
- } \
-\
- static C##_##X##_size_t ** recursive version, array based ** \
- C##_##X##_insert_key_r_(C##_##X* self, C##_##X##_size_t tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \
- if (tn == 0) { \
- tn = C##_##X##_node_new_(self); \
- *KEY_REF_##C(&self->data[tn].value) = keyFromRaw(*rkey); \
- res->first = &self->data[tn].value, res->second = true; \
- return tn; \
- } \
- C##_##X##_rawkey_t r = keyToRaw(KEY_REF_##C(&self->data[tn].value)); \
- int c = keyCompareRaw(&r, rkey), dir; \
- if (c == 0) { res->first = &self->data[tn].value; return tn; } \
- dir = (c == -1); \
- C##_##X##_size_t node = C##_##X##_insert_key_r_(self, self->data[tn].link[dir], rkey, res); \
- C##_##X##_node_t *d = self->data; \
- d[tn].link[dir] = node; \
- tn = C##_##X##_skew_(d, tn); \
- tn = C##_##X##_split_(d, tn); \
- return tn; \
- } \
-*/\
static inline C##_##X##_node_t* \
- C##_##X##_insert_key_i_(C##_##X##_node_t* tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \
+ C##_##X##_insert_entry_i_(C##_##X##_node_t* tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \
C##_##X##_node_t *up[64], *it = tn; \
int c, top = 0, dir = 0; \
while (it->level) { \
@@ -421,7 +388,6 @@ int main(void) { it = it->link[(dir = (c == -1))]; \
} \
tn = c_new_1(C##_##X##_node_t); \
- *KEY_REF_##C(&tn->value) = keyFromRaw(*rkey); \
res->first = &tn->value, res->second = true; \
tn->link[0] = tn->link[1] = (C##_##X##_node_t*) &cbst_nil, tn->level = 1; \
if (top == 0) return tn; \
@@ -436,9 +402,9 @@ int main(void) { } \
\
STC_DEF C##_##X##_result_t \
- C##_##X##_insert_key(C##_##X* self, RawKey rkey) { \
+ C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \
C##_##X##_result_t res = {NULL, false}; \
- self->root = C##_##X##_insert_key_i_(self->root, &rkey, &res); \
+ self->root = C##_##X##_insert_entry_i_(self->root, &rkey, &res); \
self->size += res.second; \
return res; \
} \
diff --git a/stc/csmap.h b/stc/csmap.h index 952ee375..751b9845 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -224,7 +224,6 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; STC_API C##_##X##_result_t \
C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \
\
-\
STC_INLINE C##_##X##_result_t \
C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \
C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \
|
