From 00a6264d9e888e1fac3c51f14fdaaaf20633b84d Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 10 Mar 2021 23:35:09 +0100 Subject: Reduced macro token-pasting a lot in maps. --- examples/csmap_v1.h | 314 ++++++++++++++++++++++---------------------- stc/cmap.h | 284 ++++++++++++++++++++-------------------- stc/csmap.h | 364 ++++++++++++++++++++++++++-------------------------- 3 files changed, 481 insertions(+), 481 deletions(-) diff --git a/examples/csmap_v1.h b/examples/csmap_v1.h index c2489df4..5adbed7d 100644 --- a/examples/csmap_v1.h +++ b/examples/csmap_v1.h @@ -61,7 +61,7 @@ int main(void) { #define using_csmap_10(X, Key, Mapped, keyCompareRaw, mappedDel, mappedClone, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CBST(X, csmap, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ + _using_CBST(X, csmap_, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ keyFromRaw, keyToRaw, RawKey, mappedClone, c_trivial_toraw, Mapped) /* csset: */ @@ -78,24 +78,24 @@ int main(void) { using_csset_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key) #define using_csset_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CBST(X, csset, Key, Key, keyCompareRaw, @@, keyDel, \ + _using_CBST(X, csset_, Key, Key, keyCompareRaw, @@, keyDel, \ keyFromRaw, keyToRaw, RawKey, @@, @@, void) /* csset_str, csmap_str, csmap_strkey, csmap_strval: */ #define using_csset_str() \ - _using_CBST_strkey(str, csset, cstr_t, @@, @@) + _using_CBST_strkey(str, csset_, cstr_t, @@, @@) #define using_csmap_str() \ - _using_CBST(str, csmap, cstr_t, cstr_t, cstr_compare_raw, cstr_del, cstr_del, \ + _using_CBST(str, csmap_, cstr_t, cstr_t, cstr_compare_raw, cstr_del, cstr_del, \ cstr_from, cstr_c_str, const char*, cstr_from, cstr_c_str, const char*) #define using_csmap_strkey(...) \ c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__) #define using_csmap_strkey_2(X, Mapped) \ - _using_CBST_strkey(X, csmap, Mapped, c_trivial_del, c_trivial_fromraw) + _using_CBST_strkey(X, csmap_, Mapped, c_trivial_del, c_trivial_fromraw) #define using_csmap_strkey_4(X, Mapped, mappedDel, mappedClone) \ - _using_CBST_strkey(X, csmap, Mapped, mappedDel, mappedClone) + _using_CBST_strkey(X, csmap_, Mapped, mappedDel, mappedClone) #define _using_CBST_strkey(X, C, Mapped, mappedDel, mappedClone) \ _using_CBST(X, C, cstr_t, Mapped, cstr_compare_raw, mappedDel, cstr_del, \ @@ -114,36 +114,36 @@ int main(void) { using_csmap_strval_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key) #define using_csmap_strval_7(X, Key, keyCompare, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CBST(X, csmap, Key, cstr_t, keyCompare, cstr_del, keyDel, \ + _using_CBST(X, csmap_, Key, cstr_t, keyCompare, cstr_del, keyDel, \ keyFromRaw, keyToRaw, RawKey, cstr_from, cstr_c_str, const char*) -#define SET_ONLY_csset(...) __VA_ARGS__ -#define SET_ONLY_csmap(...) -#define MAP_ONLY_csset(...) -#define MAP_ONLY_csmap(...) __VA_ARGS__ -#define KEY_REF_csset(vp) (vp) -#define KEY_REF_csmap(vp) (&(vp)->first) +#define SET_ONLY_csset_(...) __VA_ARGS__ +#define SET_ONLY_csmap_(...) +#define MAP_ONLY_csset_(...) +#define MAP_ONLY_csmap_(...) __VA_ARGS__ +#define KEY_REF_csset_(vp) (vp) +#define KEY_REF_csmap_(vp) (&(vp)->first) #define _using_CBST_types(X, C, Key, Mapped) \ - typedef Key C##_##X##_key_t; \ - typedef Mapped C##_##X##_mapped_t; \ + typedef Key C##X##_key_t; \ + typedef Mapped C##X##_mapped_t; \ \ - typedef SET_ONLY_##C( C##_##X##_key_t ) \ - MAP_ONLY_##C( struct {C##_##X##_key_t first; \ - C##_##X##_mapped_t second;} ) \ - C##_##X##_value_t; \ + typedef SET_ONLY_##C( C##X##_key_t ) \ + MAP_ONLY_##C( struct {C##X##_key_t first; \ + C##X##_mapped_t second;} ) \ + C##X##_value_t; \ \ - typedef struct C##_##X##_node { \ - struct C##_##X##_node *link[2]; \ + typedef struct C##X##_node { \ + struct C##X##_node *link[2]; \ uint8_t level; \ - C##_##X##_value_t value; \ - } C##_##X##_node_t; \ + C##X##_value_t value; \ + } C##X##_node_t; \ \ typedef struct { \ - C##_##X##_value_t *ref; \ + C##X##_value_t *ref; \ int _top; \ - C##_##X##_node_t *_tn, *_st[48]; \ - } C##_##X##_iter_t + C##X##_node_t *_tn, *_st[48]; \ + } C##X##_iter_t #define _using_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ @@ -151,79 +151,79 @@ int main(void) { _using_CBST_types(X, C, Key, Mapped); \ \ typedef struct { \ - C##_##X##_node_t* root; \ + C##X##_node_t* root; \ size_t size; \ - } C##_##X; \ + } C##X; \ \ - typedef RawKey C##_##X##_rawkey_t; \ - typedef RawMapped C##_##X##_rawmapped_t; \ - typedef SET_ONLY_##C( C##_##X##_rawkey_t ) \ - MAP_ONLY_##C( struct {C##_##X##_rawkey_t first; \ - C##_##X##_rawmapped_t second;} ) \ - C##_##X##_rawvalue_t; \ + typedef RawKey C##X##_rawkey_t; \ + typedef RawMapped C##X##_rawmapped_t; \ + typedef SET_ONLY_##C( C##X##_rawkey_t ) \ + MAP_ONLY_##C( struct {C##X##_rawkey_t first; \ + C##X##_rawmapped_t second;} ) \ + C##X##_rawvalue_t; \ \ typedef struct { \ - C##_##X##_value_t *first; \ + C##X##_value_t *first; \ bool second; \ - } C##_##X##_result_t; \ + } C##X##_result_t; \ \ - STC_API C##_##X \ - C##_##X##_init(void); \ + STC_API C##X \ + C##X##_init(void); \ STC_INLINE bool \ - C##_##X##_empty(C##_##X m) {return m.size == 0;} \ + C##X##_empty(C##X m) {return m.size == 0;} \ STC_INLINE size_t \ - C##_##X##_size(C##_##X m) {return m.size;} \ + C##X##_size(C##X m) {return m.size;} \ \ STC_API void \ - C##_##X##_del_r_(C##_##X##_node_t* tn); \ + C##X##_del_r_(C##X##_node_t* tn); \ \ STC_INLINE void \ - C##_##X##_del(C##_##X* self) {C##_##X##_del_r_(self->root);} \ + C##X##_del(C##X* self) {C##X##_del_r_(self->root);} \ STC_INLINE void \ - C##_##X##_clear(C##_##X* self) {C##_##X##_del(self); *self = C##_##X##_init();} \ + C##X##_clear(C##X* self) {C##X##_del(self); *self = C##X##_init();} \ STC_INLINE void \ - C##_##X##_swap(C##_##X* a, C##_##X* b) {c_swap(C##_##X, *a, *b);} \ + C##X##_swap(C##X* a, C##X* b) {c_swap(C##X, *a, *b);} \ \ STC_INLINE void \ - C##_##X##_value_del(C##_##X##_value_t* val) { \ + C##X##_value_del(C##X##_value_t* val) { \ keyDel(KEY_REF_##C(val)); \ MAP_ONLY_##C( mappedDel(&val->second); ) \ } \ - STC_INLINE C##_##X##_value_t \ - C##_##X##_value_clone(C##_##X##_value_t val) { \ + STC_INLINE C##X##_value_t \ + C##X##_value_clone(C##X##_value_t val) { \ *KEY_REF_##C(&val) = keyFromRaw(keyToRaw(KEY_REF_##C(&val))); \ MAP_ONLY_##C( val.second = mappedFromRaw(mappedToRaw(&val.second)); ) \ return val; \ } \ \ - STC_API C##_##X##_node_t* C##_##X##_clone_r_(C##_##X##_node_t *tn); \ - STC_INLINE C##_##X \ - C##_##X##_clone(C##_##X bst) { \ - C##_##X clone = {C##_##X##_clone_r_(bst.root), bst.size}; \ + STC_API C##X##_node_t* C##X##_clone_r_(C##X##_node_t *tn); \ + STC_INLINE C##X \ + C##X##_clone(C##X bst) { \ + C##X clone = {C##X##_clone_r_(bst.root), bst.size}; \ return clone; \ } \ \ - STC_API C##_##X##_value_t* \ - C##_##X##_find_it(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* out); \ + STC_API C##X##_value_t* \ + C##X##_find_it(const C##X* self, RawKey rkey, C##X##_iter_t* out); \ \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_find(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it; \ - C##_##X##_find_it(self, rkey, &it); \ + STC_INLINE C##X##_iter_t \ + C##X##_find(const C##X* self, RawKey rkey) { \ + C##X##_iter_t it; \ + C##X##_find_it(self, rkey, &it); \ return it; \ } \ STC_INLINE bool \ - C##_##X##_contains(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it; \ - return C##_##X##_find_it(self, rkey, &it) != NULL; \ + C##X##_contains(const C##X* self, RawKey rkey) { \ + C##X##_iter_t it; \ + return C##X##_find_it(self, rkey, &it) != NULL; \ } \ \ - STC_API C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \ + 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); \ + 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); \ if (res.second) { \ *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \ @@ -231,108 +231,108 @@ int main(void) { return res; \ } \ STC_INLINE void \ - C##_##X##_emplace_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ - for (size_t i=0; isecond = mapped; )} \ else {keyDel(&key); MAP_ONLY_##C( mappedDel(&mapped); )} \ return res; \ } \ \ 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)); \ + 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##_result_t \ - C##_##X##_put(C##_##X* self, Key k, Mapped m) { \ - return C##_##X##_insert_or_assign(self, k, m); \ + STC_INLINE C##X##_result_t \ + C##X##_put(C##X* self, Key k, Mapped m) { \ + return C##X##_insert_or_assign(self, k, m); \ } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ - C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + STC_INLINE C##X##_result_t \ + C##X##_emplace_or_assign(C##X* self, RawKey rkey, RawMapped rmapped) { \ + C##X##_result_t res = C##X##_insert_entry_(self, rkey); \ if (res.second) res.first->first = keyFromRaw(rkey); \ else mappedDel(&res.first->second); \ res.first->second = mappedFromRaw(rmapped); 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##_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(const C##_##X* self) { \ - C##_##X##_node_t *tn = self->root; \ + STC_INLINE C##X##_value_t* \ + C##X##_front(const C##X* self) { \ + C##X##_node_t *tn = self->root; \ while (tn->link[0]->level) tn = tn->link[0]; \ return &tn->value; \ } \ - STC_INLINE C##_##X##_value_t* \ - C##_##X##_back(const C##_##X* self) { \ - C##_##X##_node_t *tn = self->root; \ + STC_INLINE C##X##_value_t* \ + C##X##_back(const C##X* self) { \ + C##X##_node_t *tn = self->root; \ while (tn->link[1]->level) tn = tn->link[1]; \ return &tn->value; \ } \ \ STC_API void \ - C##_##X##_next(C##_##X##_iter_t* it); \ + C##X##_next(C##X##_iter_t* it); \ \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_begin(const C##_##X* self) { \ - C##_##X##_iter_t it = {NULL, 0, self->root}; \ - C##_##X##_next(&it); return it; \ + STC_INLINE C##X##_iter_t \ + C##X##_begin(const C##X* self) { \ + C##X##_iter_t it = {NULL, 0, self->root}; \ + C##X##_next(&it); return it; \ } \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_end(const C##_##X* self) {\ - C##_##X##_iter_t it = {NULL}; return it; \ + STC_INLINE C##X##_iter_t \ + C##X##_end(const C##X* self) {\ + C##X##_iter_t it = {NULL}; return it; \ } \ - STC_INLINE C##_##X##_mapped_t* \ - C##_##X##_itval(C##_##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ + STC_INLINE C##X##_mapped_t* \ + C##X##_itval(C##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ MAP_ONLY_##C( &it.ref->second );} \ \ - STC_API C##_##X##_node_t* \ - C##_##X##_erase_r_(C##_##X##_node_t *tn, const C##_##X##_rawkey_t* rkey, int *erased); \ + STC_API C##X##_node_t* \ + C##X##_erase_r_(C##X##_node_t *tn, const C##X##_rawkey_t* rkey, int *erased); \ \ STC_INLINE size_t \ - C##_##X##_erase(C##_##X* self, RawKey rkey) { \ + C##X##_erase(C##X* self, RawKey rkey) { \ int erased = 0; \ - self->root = C##_##X##_erase_r_(self->root, &rkey, &erased); \ + self->root = C##X##_erase_r_(self->root, &rkey, &erased); \ self->size -= erased; return erased; \ } \ STC_INLINE size_t \ - C##_##X##_erase_at(C##_##X* self, C##_##X##_iter_t pos) { \ - return C##_##X##_erase(self, keyToRaw(KEY_REF_##C(pos.ref))); \ + C##X##_erase_at(C##X* self, C##X##_iter_t pos) { \ + return C##X##_erase(self, keyToRaw(KEY_REF_##C(pos.ref))); \ } \ \ _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \ - typedef C##_##X C##_##X##_t + typedef C##X C##X##_t /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) #define _implement_CBST(X, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \ keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \ - STC_DEF C##_##X \ - C##_##X##_init(void) { \ - C##_##X m = {(C##_##X##_node_t *) &cbst_nil, 0}; \ + STC_DEF C##X \ + C##X##_init(void) { \ + C##X m = {(C##X##_node_t *) &cbst_nil, 0}; \ return m; \ } \ \ - STC_DEF C##_##X##_value_t* \ - C##_##X##_find_it(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* out) { \ - C##_##X##_node_t *tn = self->root; \ + STC_DEF C##X##_value_t* \ + C##X##_find_it(const C##X* self, C##X##_rawkey_t rkey, C##X##_iter_t* out) { \ + C##X##_node_t *tn = self->root; \ out->_top = 0; \ while (tn->level) { \ - int c; C##_##X##_rawkey_t rx = keyToRaw(KEY_REF_##C(&tn->value)); \ + int c; C##X##_rawkey_t rx = keyToRaw(KEY_REF_##C(&tn->value)); \ if ((c = keyCompareRaw(&rx, &rkey)) < 0) tn = tn->link[1]; \ else if (c > 0) {out->_st[out->_top++] = tn; tn = tn->link[0];} \ else {out->_tn = tn->link[1]; return (out->ref = &tn->value);} \ @@ -341,8 +341,8 @@ int main(void) { } \ \ STC_DEF void \ - C##_##X##_next(C##_##X##_iter_t *it) { \ - C##_##X##_node_t *tn = it->_tn; \ + C##X##_next(C##X##_iter_t *it) { \ + C##X##_node_t *tn = it->_tn; \ if (it->_top || tn->level) { \ while (tn->level) { \ it->_st[it->_top++] = tn; \ @@ -355,10 +355,10 @@ int main(void) { it->ref = NULL; \ } \ \ - static C##_##X##_node_t * \ - C##_##X##_skew_(C##_##X##_node_t *tn) { \ + static C##X##_node_t * \ + C##X##_skew_(C##X##_node_t *tn) { \ if (tn && tn->link[0]->level == tn->level && tn->level) { \ - C##_##X##_node_t *tmp = tn->link[0]; \ + C##X##_node_t *tmp = tn->link[0]; \ tn->link[0] = tmp->link[1]; \ tmp->link[1] = tn; \ tn = tmp; \ @@ -366,10 +366,10 @@ int main(void) { return tn; \ } \ \ - static C##_##X##_node_t * \ - C##_##X##_split_(C##_##X##_node_t *tn) { \ + static C##X##_node_t * \ + C##X##_split_(C##X##_node_t *tn) { \ if (tn->link[1]->link[1]->level == tn->level && tn->level) { \ - C##_##X##_node_t *tmp = tn->link[1]; \ + C##X##_node_t *tmp = tn->link[1]; \ tn->link[1] = tmp->link[0]; \ tmp->link[0] = tn; \ tn = tmp; \ @@ -378,55 +378,55 @@ int main(void) { return tn; \ } \ \ - static inline C##_##X##_node_t* \ - 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; \ + static inline C##X##_node_t* \ + 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) { \ up[top++] = it; \ - C##_##X##_rawkey_t r = keyToRaw(KEY_REF_##C(&it->value)); \ + C##X##_rawkey_t r = keyToRaw(KEY_REF_##C(&it->value)); \ if ((c = keyCompareRaw(&r, rkey)) == 0) {res->first = &it->value; return tn;} \ it = it->link[(dir = (c == -1))]; \ } \ - tn = c_new_1(C##_##X##_node_t); \ + 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; \ + tn->link[0] = tn->link[1] = (C##X##_node_t*) &cbst_nil, tn->level = 1; \ if (top == 0) return tn; \ up[top - 1]->link[dir] = tn; \ while (top--) { \ if (top) dir = (up[top - 1]->link[1] == up[top]); \ - up[top] = C##_##X##_skew_(up[top]); \ - up[top] = C##_##X##_split_(up[top]); \ + up[top] = C##X##_skew_(up[top]); \ + up[top] = C##X##_split_(up[top]); \ if (top) up[top - 1]->link[dir] = up[top]; \ } \ return up[0]; \ } \ \ - STC_DEF C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \ - C##_##X##_result_t res = {NULL, false}; \ - self->root = C##_##X##_insert_entry_i_(self->root, &rkey, &res); \ + STC_DEF C##X##_result_t \ + C##X##_insert_entry_(C##X* self, RawKey rkey) { \ + C##X##_result_t res = {NULL, false}; \ + self->root = C##X##_insert_entry_i_(self->root, &rkey, &res); \ self->size += res.second; \ return res; \ } \ \ - STC_DEF C##_##X##_node_t* \ - C##_##X##_erase_r_(C##_##X##_node_t *tn, const C##_##X##_rawkey_t* rkey, int *erased) { \ + STC_DEF C##X##_node_t* \ + C##X##_erase_r_(C##X##_node_t *tn, const C##X##_rawkey_t* rkey, int *erased) { \ if (tn->level == 0) \ return tn; \ - C##_##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&tn->value)); \ - C##_##X##_node_t *tx; int c = keyCompareRaw(&raw, rkey); \ + C##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&tn->value)); \ + C##X##_node_t *tx; int c = keyCompareRaw(&raw, rkey); \ if (c != 0) \ - tn->link[c == -1] = C##_##X##_erase_r_(tn->link[c == -1], rkey, erased); \ + tn->link[c == -1] = C##X##_erase_r_(tn->link[c == -1], rkey, erased); \ else { \ - if (!*erased) {C##_##X##_value_del(&tn->value); *erased = 1;} \ + if (!*erased) {C##X##_value_del(&tn->value); *erased = 1;} \ if (tn->link[0]->level && tn->link[1]->level) { \ tx = tn->link[0]; \ while (tx->link[1]->level) \ tx = tx->link[1]; \ tn->value = tx->value; \ raw = keyToRaw(KEY_REF_##C(&tn->value)); \ - tn->link[0] = C##_##X##_erase_r_(tn->link[0], &raw, erased); \ + tn->link[0] = C##X##_erase_r_(tn->link[0], &raw, erased); \ } else { \ tx = tn; \ tn = tn->link[tn->link[0]->level == 0]; \ @@ -436,38 +436,38 @@ int main(void) { if (tn->link[0]->level < tn->level - 1 || tn->link[1]->level < tn->level - 1) { \ if (tn->link[1]->level > --tn->level) \ tn->link[1]->level = tn->level; \ - tn = C##_##X##_skew_(tn); \ - tx = tn->link[0] = C##_##X##_skew_(tn->link[0]); \ - tx->link[0] = C##_##X##_skew_(tx->link[0]); \ - tn = C##_##X##_split_(tn); \ - tn->link[0] = C##_##X##_split_(tn->link[0]); \ + tn = C##X##_skew_(tn); \ + tx = tn->link[0] = C##X##_skew_(tn->link[0]); \ + tx->link[0] = C##X##_skew_(tx->link[0]); \ + tn = C##X##_split_(tn); \ + tn->link[0] = C##X##_split_(tn->link[0]); \ } \ return tn; \ } \ \ - STC_DEF C##_##X##_node_t* \ - C##_##X##_clone_r_(C##_##X##_node_t *tn) { \ + STC_DEF C##X##_node_t* \ + C##X##_clone_r_(C##X##_node_t *tn) { \ if (! tn->level) return tn; \ - C##_##X##_node_t *cn = c_new_1(C##_##X##_node_t); \ - cn->link[0] = C##_##X##_clone_r_(tn->link[0]); \ - cn->link[1] = C##_##X##_clone_r_(tn->link[1]); \ + C##X##_node_t *cn = c_new_1(C##X##_node_t); \ + cn->link[0] = C##X##_clone_r_(tn->link[0]); \ + cn->link[1] = C##X##_clone_r_(tn->link[1]); \ cn->level = tn->level; \ - cn->value = C##_##X##_value_clone(tn->value); \ + cn->value = C##X##_value_clone(tn->value); \ return cn; \ } \ \ STC_DEF void \ - C##_##X##_del_r_(C##_##X##_node_t* tn) { \ + C##X##_del_r_(C##X##_node_t* tn) { \ if (tn->level != 0) { \ - C##_##X##_del_r_(tn->link[0]); \ - C##_##X##_del_r_(tn->link[1]); \ - C##_##X##_value_del(&tn->value); \ + C##X##_del_r_(tn->link[0]); \ + C##X##_del_r_(tn->link[1]); \ + C##X##_value_del(&tn->value); \ c_free(tn); \ } \ } -_using_CBST_types(_, csmap, int, int); +_using_CBST_types(_, csmap_, int, int); static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0}; #else diff --git a/stc/cmap.h b/stc/cmap.h index 822f48e4..78413064 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -73,14 +73,14 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; #define using_cmap_9(X, Key, Mapped, keyEquals, keyHash, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ - _using_CHASH(X, cmap, Key, Mapped, keyEquals, keyHash, \ + _using_CHASH(X, cmap_, Key, Mapped, keyEquals, keyHash, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Key) #define using_cmap_13(X, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CHASH(X, cmap, Key, Mapped, keyEqualsRaw, keyHashRaw, \ + _using_CHASH(X, cmap_, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) @@ -90,7 +90,7 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; keyDel, keyClone, c_trivial_toraw, Key) #define using_cmap_keydef_9(X, Key, Mapped, keyEqualsRaw, keyHashRaw, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CHASH(X, cmap, Key, Mapped, keyEqualsRaw, keyHashRaw, \ + _using_CHASH(X, cmap_, Key, Mapped, keyEqualsRaw, keyHashRaw, \ c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) @@ -108,15 +108,15 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; using_cset_8(X, Key, keyEquals, keyHash, keyDel, keyClone, c_trivial_toraw, Key) #define using_cset_8(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CHASH(X, cset, Key, Key, keyEqualsRaw, keyHashRaw, \ + _using_CHASH(X, cset_, Key, Key, keyEqualsRaw, keyHashRaw, \ @@, @@, @@, void, \ keyDel, keyFromRaw, keyToRaw, RawKey) /* cset_str, cmap_str, cmap_strkey, cmap_strval: */ #define using_cset_str() \ - _using_CHASH_strkey(str, cset, cstr_t, @@, @@, @@, void) + _using_CHASH_strkey(str, cset_, cstr_t, @@, @@, @@, void) #define using_cmap_str() \ - _using_CHASH(str, cmap, cstr_t, cstr_t, cstr_equals_raw, cstr_hash_raw, \ + _using_CHASH(str, cmap_, cstr_t, cstr_t, cstr_equals_raw, cstr_hash_raw, \ cstr_del, cstr_from, cstr_c_str, const char*, \ cstr_del, cstr_from, cstr_c_str, const char*) @@ -124,13 +124,13 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; c_MACRO_OVERLOAD(using_cmap_strkey, __VA_ARGS__) #define using_cmap_strkey_2(X, Mapped) \ - _using_CHASH_strkey(X, cmap, Mapped, c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped) + _using_CHASH_strkey(X, cmap_, Mapped, c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped) #define using_cmap_strkey_4(X, Mapped, mappedDel, mappedClone) \ - _using_CHASH_strkey(X, cmap, Mapped, mappedDel, mappedClone, c_trivial_toraw, Mapped) + _using_CHASH_strkey(X, cmap_, Mapped, mappedDel, mappedClone, c_trivial_toraw, Mapped) #define using_cmap_strkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ - _using_CHASH_strkey(X, cmap, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) + _using_CHASH_strkey(X, cmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) #define _using_CHASH_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ _using_CHASH(X, C, cstr_t, Mapped, cstr_equals_raw, cstr_hash_raw, \ @@ -150,16 +150,16 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; using_cmap_strval_8(X, Key, keyEquals, keyHash, keyDel, keyClone, c_trivial_toraw, Key) #define using_cmap_strval_8(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_CHASH(X, cmap, Key, cstr_t, keyEqualsRaw, keyHashRaw, \ + _using_CHASH(X, cmap_, Key, cstr_t, keyEqualsRaw, keyHashRaw, \ cstr_del, cstr_from, cstr_c_str, const char*, \ keyDel, keyFromRaw, keyToRaw, RawKey) -#define SET_ONLY_cset(...) __VA_ARGS__ -#define SET_ONLY_cmap(...) -#define MAP_ONLY_cset(...) -#define MAP_ONLY_cmap(...) __VA_ARGS__ -#define KEY_REF_cset(vp) (vp) -#define KEY_REF_cmap(vp) (&(vp)->first) +#define SET_ONLY_cset_(...) __VA_ARGS__ +#define SET_ONLY_cmap_(...) +#define MAP_ONLY_cset_(...) +#define MAP_ONLY_cmap_(...) __VA_ARGS__ +#define KEY_REF_cset_(vp) (vp) +#define KEY_REF_cmap_(vp) (&(vp)->first) #ifndef CMAP_SIZE_T #define CMAP_SIZE_T uint32_t #endif @@ -167,94 +167,94 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; #define _using_CHASH(X, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - typedef Key C##_##X##_key_t; \ - typedef Mapped C##_##X##_mapped_t; \ - typedef RawKey C##_##X##_rawkey_t; \ - typedef RawMapped C##_##X##_rawmapped_t; \ - typedef CMAP_SIZE_T C##_##X##_size_t; \ + typedef Key C##X##_key_t; \ + typedef Mapped C##X##_mapped_t; \ + typedef RawKey C##X##_rawkey_t; \ + typedef RawMapped C##X##_rawmapped_t; \ + typedef CMAP_SIZE_T C##X##_size_t; \ \ - typedef SET_ONLY_##C( C##_##X##_key_t ) \ - MAP_ONLY_##C( struct {C##_##X##_key_t first; \ - C##_##X##_mapped_t second;} ) \ - C##_##X##_value_t; \ + typedef SET_ONLY_##C( C##X##_key_t ) \ + MAP_ONLY_##C( struct {C##X##_key_t first; \ + C##X##_mapped_t second;} ) \ + C##X##_value_t; \ \ - typedef SET_ONLY_##C( C##_##X##_rawkey_t ) \ - MAP_ONLY_##C( struct {C##_##X##_rawkey_t first; \ - C##_##X##_rawmapped_t second;} ) \ - C##_##X##_rawvalue_t; \ + typedef SET_ONLY_##C( C##X##_rawkey_t ) \ + MAP_ONLY_##C( struct {C##X##_rawkey_t first; \ + C##X##_rawmapped_t second;} ) \ + C##X##_rawvalue_t; \ \ typedef struct { \ - C##_##X##_value_t *first; \ + C##X##_value_t *first; \ bool second; /* inserted */ \ - } C##_##X##_result_t; \ + } C##X##_result_t; \ \ typedef struct { \ - C##_##X##_value_t* table; \ + C##X##_value_t* table; \ uint8_t* _hashx; \ - C##_##X##_size_t size, bucket_count; \ + C##X##_size_t size, bucket_count; \ float min_load_factor; \ float max_load_factor; \ - } C##_##X; \ + } C##X; \ \ typedef struct { \ - C##_##X##_value_t *ref; \ + C##X##_value_t *ref; \ uint8_t* _hx; \ - } C##_##X##_iter_t; \ + } C##X##_iter_t; \ \ - STC_INLINE C##_##X \ - C##_##X##_init(void) {C##_##X m = _cmap_inits; return m;} \ + STC_INLINE C##X \ + C##X##_init(void) {C##X m = _cmap_inits; return m;} \ STC_INLINE bool \ - C##_##X##_empty(C##_##X m) {return m.size == 0;} \ + C##X##_empty(C##X m) {return m.size == 0;} \ STC_INLINE size_t \ - C##_##X##_size(C##_##X m) {return (size_t) m.size;} \ - STC_INLINE C##_##X##_value_t \ - C##_##X##_value_clone(C##_##X##_value_t val) { \ + C##X##_size(C##X m) {return (size_t) m.size;} \ + STC_INLINE C##X##_value_t \ + C##X##_value_clone(C##X##_value_t val) { \ *KEY_REF_##C(&val) = keyFromRaw(keyToRaw(KEY_REF_##C(&val))); \ MAP_ONLY_##C( val.second = mappedFromRaw(mappedToRaw(&val.second)); ) \ return val; \ } \ STC_INLINE void \ - C##_##X##_value_del(C##_##X##_value_t* val) { \ + C##X##_value_del(C##X##_value_t* val) { \ keyDel(KEY_REF_##C(val)); \ MAP_ONLY_##C( mappedDel(&val->second); ) \ } \ STC_INLINE size_t \ - C##_##X##_bucket_count(C##_##X map) {return (size_t) map.bucket_count;} \ + C##X##_bucket_count(C##X map) {return (size_t) map.bucket_count;} \ STC_INLINE size_t \ - C##_##X##_capacity(C##_##X map) {return (size_t) (map.bucket_count*map.max_load_factor);} \ + C##X##_capacity(C##X map) {return (size_t) (map.bucket_count*map.max_load_factor);} \ STC_INLINE void \ - C##_##X##_swap(C##_##X *map1, C##_##X *map2) {c_swap(C##_##X, *map1, *map2);} \ + C##X##_swap(C##X *map1, C##X *map2) {c_swap(C##X, *map1, *map2);} \ STC_INLINE void \ - C##_##X##_set_load_factors(C##_##X* self, float min_load, float max_load) { \ + C##X##_set_load_factors(C##X* self, float min_load, float max_load) { \ self->min_load_factor = min_load; \ self->max_load_factor = max_load; \ } \ - STC_API C##_##X \ - C##_##X##_with_capacity(size_t cap); \ - STC_API C##_##X \ - C##_##X##_clone(C##_##X map); \ + STC_API C##X \ + C##X##_with_capacity(size_t cap); \ + STC_API C##X \ + C##X##_clone(C##X map); \ STC_API void \ - C##_##X##_reserve(C##_##X* self, size_t capacity); \ + C##X##_reserve(C##X* self, size_t capacity); \ STC_API void \ - C##_##X##_del(C##_##X* self); \ + C##X##_del(C##X* self); \ STC_API void \ - C##_##X##_clear(C##_##X* self); \ + C##X##_clear(C##X* self); \ \ - STC_API C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \ + STC_API C##X##_result_t \ + C##X##_insert_entry_(C##X* self, RawKey rkey); \ STC_API chash_bucket_t \ - C##_##X##_bucket_(const C##_##X* self, const C##_##X##_rawkey_t* rkeyptr); \ + C##X##_bucket_(const C##X* self, const C##X##_rawkey_t* rkeyptr); \ \ - STC_API C##_##X##_iter_t \ - C##_##X##_find(const C##_##X* self, RawKey rkey); \ + STC_API C##X##_iter_t \ + C##X##_find(const C##X* self, RawKey rkey); \ STC_INLINE bool \ - C##_##X##_contains(const C##_##X* self, RawKey rkey) { \ - return self->size && self->_hashx[C##_##X##_bucket_(self, &rkey).idx]; \ + C##X##_contains(const C##X* self, RawKey rkey) { \ + return self->size && self->_hashx[C##X##_bucket_(self, &rkey).idx]; \ } \ \ - 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); \ + 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); \ if (res.second) { \ *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \ @@ -262,81 +262,81 @@ typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; return res; \ } \ STC_INLINE void \ - C##_##X##_emplace_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ - for (size_t i=0; isecond = mapped; )} \ else {keyDel(&key); MAP_ONLY_##C( mappedDel(&mapped); )} \ return res; \ } \ \ 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)); \ + 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##_result_t \ - C##_##X##_put(C##_##X* self, Key k, Mapped m) { /* shorter, like operator[] */ \ - return C##_##X##_insert_or_assign(self, k, m); \ + STC_INLINE C##X##_result_t \ + C##X##_put(C##X* self, Key k, Mapped m) { /* shorter, like operator[] */ \ + return C##X##_insert_or_assign(self, k, m); \ } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ - C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + STC_INLINE C##X##_result_t \ + C##X##_emplace_or_assign(C##X* self, RawKey rkey, RawMapped rmapped) { \ + C##X##_result_t res = C##X##_insert_entry_(self, rkey); \ if (res.second) res.first->first = keyFromRaw(rkey); \ else mappedDel(&res.first->second); \ res.first->second = mappedFromRaw(rmapped); return res; \ } \ - STC_INLINE C##_##X##_mapped_t* \ - C##_##X##_at(const C##_##X* self, RawKey rkey) { \ - chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \ + STC_INLINE C##X##_mapped_t* \ + C##X##_at(const C##X* self, RawKey rkey) { \ + chash_bucket_t b = C##X##_bucket_(self, &rkey); \ assert(self->_hashx[b.idx]); \ return &self->table[b.idx].second; \ }) \ \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_begin(const C##_##X* self) { \ - C##_##X##_iter_t it = {self->table, self->_hashx}; \ + STC_INLINE C##X##_iter_t \ + C##X##_begin(const C##X* self) { \ + C##X##_iter_t it = {self->table, self->_hashx}; \ if (it._hx) while (*it._hx == 0) ++it.ref, ++it._hx; \ return it; \ } \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_end(const C##_##X* self) {\ - C##_##X##_iter_t it = {self->table + self->bucket_count}; return it; \ + STC_INLINE C##X##_iter_t \ + C##X##_end(const C##X* self) {\ + C##X##_iter_t it = {self->table + self->bucket_count}; return it; \ } \ STC_INLINE void \ - C##_##X##_next(C##_##X##_iter_t* it) { \ + C##X##_next(C##X##_iter_t* it) { \ while ((++it->ref, *++it->_hx == 0)) ; \ } \ - STC_INLINE C##_##X##_mapped_t* \ - C##_##X##_itval(C##_##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ + STC_INLINE C##X##_mapped_t* \ + C##X##_itval(C##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ MAP_ONLY_##C( &it.ref->second );} \ \ STC_API void \ - C##_##X##_erase_entry(C##_##X* self, C##_##X##_value_t* val); \ + C##X##_erase_entry(C##X* self, C##X##_value_t* val); \ STC_INLINE size_t \ - C##_##X##_erase(C##_##X* self, RawKey rkey) { \ + C##X##_erase(C##X* self, RawKey rkey) { \ if (self->size == 0) return 0; \ - chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \ - return self->_hashx[b.idx] ? C##_##X##_erase_entry(self, self->table + b.idx), 1 : 0; \ + chash_bucket_t b = C##X##_bucket_(self, &rkey); \ + return self->_hashx[b.idx] ? C##X##_erase_entry(self, self->table + b.idx), 1 : 0; \ } \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_erase_at(C##_##X* self, C##_##X##_iter_t pos) { \ - C##_##X##_erase_entry(self, pos.ref); \ - C##_##X##_next(&pos); return pos; \ + STC_INLINE C##X##_iter_t \ + C##X##_erase_at(C##X* self, C##X##_iter_t pos) { \ + C##X##_erase_entry(self, pos.ref); \ + C##X##_next(&pos); return pos; \ } \ \ _implement_CHASH(X, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - typedef C##_##X C##_##X##_t + typedef C##X C##X##_t STC_API uint64_t c_default_hash(const void *data, size_t len); STC_INLINE uint64_t c_default_hash32(const void* data, size_t ignored) @@ -357,41 +357,41 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_um #define _implement_CHASH(X, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - STC_DEF C##_##X \ - C##_##X##_with_capacity(size_t cap) { \ - C##_##X h = _cmap_inits; \ - C##_##X##_reserve(&h, cap); \ + STC_DEF C##X \ + C##X##_with_capacity(size_t cap) { \ + C##X h = _cmap_inits; \ + C##X##_reserve(&h, cap); \ return h; \ } \ \ - STC_INLINE void C##_##X##_wipe_(C##_##X* self) { \ + STC_INLINE void C##X##_wipe_(C##X* self) { \ if (self->size == 0) return; \ - C##_##X##_value_t* e = self->table, *end = e + self->bucket_count; \ + C##X##_value_t* e = self->table, *end = e + self->bucket_count; \ uint8_t *hx = self->_hashx; \ - for (; e != end; ++e) if (*hx++) C##_##X##_value_del(e); \ + for (; e != end; ++e) if (*hx++) C##X##_value_del(e); \ } \ \ - STC_DEF void C##_##X##_del(C##_##X* self) { \ - C##_##X##_wipe_(self); \ + STC_DEF void C##X##_del(C##X* self) { \ + C##X##_wipe_(self); \ c_free(self->_hashx); \ c_free(self->table); \ } \ \ - STC_DEF void C##_##X##_clear(C##_##X* self) { \ - C##_##X##_wipe_(self); \ + STC_DEF void C##X##_clear(C##X* self) { \ + C##X##_wipe_(self); \ self->size = 0; \ memset(self->_hashx, 0, self->bucket_count); \ } \ \ STC_DEF chash_bucket_t \ - C##_##X##_bucket_(const C##_##X* self, const C##_##X##_rawkey_t* rkeyptr) { \ - const uint64_t hash = keyHashRaw(rkeyptr, sizeof(C##_##X##_rawkey_t)); \ + C##X##_bucket_(const C##X* self, const C##X##_rawkey_t* rkeyptr) { \ + const uint64_t hash = keyHashRaw(rkeyptr, sizeof(C##X##_rawkey_t)); \ uint_fast8_t sx; size_t cap = self->bucket_count; \ chash_bucket_t b = {_c_SELECT(fastrange,CMAP_SIZE_T)(hash, cap), (uint_fast8_t)(hash | 0x80)}; \ const uint8_t* hashx = self->_hashx; \ while ((sx = hashx[b.idx])) { \ if (sx == b.hx) { \ - C##_##X##_rawkey_t r = keyToRaw(KEY_REF_##C(self->table + b.idx)); \ + C##X##_rawkey_t r = keyToRaw(KEY_REF_##C(self->table + b.idx)); \ if (keyEqualsRaw(&r, rkeyptr)) break; \ } \ if (++b.idx == cap) b.idx = 0; \ @@ -399,24 +399,24 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_um return b; \ } \ \ - STC_DEF C##_##X##_iter_t \ - C##_##X##_find(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it = {NULL}; \ + STC_DEF C##X##_iter_t \ + C##X##_find(const C##X* self, RawKey rkey) { \ + C##X##_iter_t it = {NULL}; \ if (self->size == 0) return it; \ - chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \ + chash_bucket_t b = C##X##_bucket_(self, &rkey); \ if (*(it._hx = self->_hashx+b.idx)) it.ref = self->table+b.idx; \ return it; \ } \ \ - STC_INLINE void C##_##X##_reserve_expand_(C##_##X* self) { \ + STC_INLINE void C##X##_reserve_expand_(C##X* self) { \ if (self->size + 1 >= self->bucket_count*self->max_load_factor) \ - C##_##X##_reserve(self, 5 + self->size * 3 / 2); \ + C##X##_reserve(self, 5 + self->size * 3 / 2); \ } \ - STC_DEF C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \ - C##_##X##_reserve_expand_(self); \ - chash_bucket_t b = C##_##X##_bucket_(self, &rkey); \ - C##_##X##_result_t res = {&self->table[b.idx], !self->_hashx[b.idx]}; \ + STC_DEF C##X##_result_t \ + C##X##_insert_entry_(C##X* self, RawKey rkey) { \ + C##X##_reserve_expand_(self); \ + chash_bucket_t b = C##X##_bucket_(self, &rkey); \ + C##X##_result_t res = {&self->table[b.idx], !self->_hashx[b.idx]}; \ if (res.second) { \ self->_hashx[b.idx] = b.hx; \ ++self->size; \ @@ -424,38 +424,38 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_um return res; \ } \ \ - STC_DEF C##_##X \ - C##_##X##_clone(C##_##X m) { \ - C##_##X clone = { \ - c_new_2(C##_##X##_value_t, m.bucket_count), \ + STC_DEF C##X \ + C##X##_clone(C##X m) { \ + C##X clone = { \ + c_new_2(C##X##_value_t, m.bucket_count), \ (uint8_t *) memcpy(c_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1), \ m.size, m.bucket_count, m.min_load_factor, m.max_load_factor \ }; \ - C##_##X##_value_t *e = m.table, *end = e + m.bucket_count, *dst = clone.table; \ + C##X##_value_t *e = m.table, *end = e + m.bucket_count, *dst = clone.table; \ for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst) \ - if (*hx) *dst = C##_##X##_value_clone(*e); \ + if (*hx) *dst = C##X##_value_clone(*e); \ return clone; \ } \ \ STC_DEF void \ - C##_##X##_reserve(C##_##X* self, size_t newcap) { \ + C##X##_reserve(C##X* self, size_t newcap) { \ if (newcap < self->size) return; \ size_t oldcap = self->bucket_count; \ newcap = (size_t) (newcap / self->max_load_factor) | 1; \ - C##_##X tmp = { \ - c_new_2 (C##_##X##_value_t, newcap), \ + C##X tmp = { \ + c_new_2 (C##X##_value_t, newcap), \ (uint8_t *) c_calloc(newcap + 1, sizeof(uint8_t)), \ - self->size, (C##_##X##_size_t) newcap, \ + self->size, (C##X##_size_t) newcap, \ self->min_load_factor, self->max_load_factor \ }; \ /* Rehash: */ \ - tmp._hashx[newcap] = 0xff; c_swap(C##_##X, *self, tmp); \ - C##_##X##_value_t* e = tmp.table, *slot = self->table; \ + tmp._hashx[newcap] = 0xff; c_swap(C##X, *self, tmp); \ + C##X##_value_t* e = tmp.table, *slot = self->table; \ uint8_t* hashx = self->_hashx; \ for (size_t i = 0; i < oldcap; ++i, ++e) \ if (tmp._hashx[i]) { \ RawKey r = keyToRaw(KEY_REF_##C(e)); \ - chash_bucket_t b = C##_##X##_bucket_(self, &r); \ + chash_bucket_t b = C##X##_bucket_(self, &r); \ slot[b.idx] = *e, \ hashx[b.idx] = (uint8_t) b.hx; \ } \ @@ -464,11 +464,11 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_um } \ \ STC_DEF void \ - C##_##X##_erase_entry(C##_##X* self, C##_##X##_value_t* val) { \ + C##X##_erase_entry(C##X* self, C##X##_value_t* val) { \ size_t i = chash_index_(*self, val), j = i, k, cap = self->bucket_count; \ - C##_##X##_value_t* slot = self->table; \ + C##X##_value_t* slot = self->table; \ uint8_t* hashx = self->_hashx; \ - C##_##X##_value_del(&slot[i]); \ + C##X##_value_del(&slot[i]); \ for (;;) { /* delete without leaving tombstone */ \ if (++j == cap) j = 0; \ if (! hashx[j]) \ @@ -480,7 +480,7 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) {uint64_t l,h; c_um } \ hashx[i] = 0, k = --self->size; \ if (k < cap*self->min_load_factor && k > 512) \ - C##_##X##_reserve(self, k*1.2); \ + C##X##_reserve(self, k*1.2); \ } STC_DEF uint64_t c_default_hash(const void *key, size_t len) { diff --git a/stc/csmap.h b/stc/csmap.h index cdd27c4d..64fa1e65 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -62,7 +62,7 @@ int main(void) { #define using_csmap_12(X, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_AATREE(X, csmap, Key, Mapped, keyCompareRaw, \ + _using_AATREE(X, csmap_, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) @@ -72,7 +72,7 @@ int main(void) { keyDel, keyClone, c_trivial_toraw, Key) #define using_csmap_keydef_8(X, Key, Mapped, keyCompareRaw, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_AATREE(X, csmap, Key, Mapped, keyCompareRaw, \ + _using_AATREE(X, csmap_, Key, Mapped, keyCompareRaw, \ c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) @@ -90,15 +90,15 @@ int main(void) { using_csset_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key) #define using_csset_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_AATREE(X, csset, Key, Key, keyCompareRaw, \ + _using_AATREE(X, csset_, Key, Key, keyCompareRaw, \ @@, @@, @@, void, \ keyDel, keyFromRaw, keyToRaw, RawKey) /* csset_str, csmap_str, csmap_strkey, csmap_strval: */ #define using_csset_str() \ - _using_AATREE_strkey(str, csset, cstr_t, @@, @@, @@, void) + _using_AATREE_strkey(str, csset_, cstr_t, @@, @@, @@, void) #define using_csmap_str() \ - _using_AATREE(str, csmap, cstr_t, cstr_t, cstr_compare_raw, \ + _using_AATREE(str, csmap_, cstr_t, cstr_t, cstr_compare_raw, \ cstr_del, cstr_from, cstr_c_str, const char*, \ cstr_del, cstr_from, cstr_c_str, const char*) @@ -106,13 +106,13 @@ int main(void) { c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__) #define using_csmap_strkey_2(X, Mapped) \ - _using_AATREE_strkey(X, csmap, Mapped, c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped) + _using_AATREE_strkey(X, csmap_, Mapped, c_trivial_del, c_trivial_fromraw, c_trivial_toraw, Mapped) #define using_csmap_strkey_4(X, Mapped, mappedDel, mappedClone) \ - _using_AATREE_strkey(X, csmap, Mapped, mappedDel, mappedClone, c_trivial_toraw, Mapped) + _using_AATREE_strkey(X, csmap_, Mapped, mappedDel, mappedClone, c_trivial_toraw, Mapped) #define using_csmap_strkey_6(X, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ - _using_AATREE_strkey(X, csmap, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) + _using_AATREE_strkey(X, csmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) #define _using_AATREE_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ _using_AATREE(X, C, cstr_t, Mapped, cstr_compare_raw, \ @@ -132,16 +132,16 @@ int main(void) { using_csmap_strval_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key) #define using_csmap_strval_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ - _using_AATREE(X, csmap, Key, cstr_t, keyCompareRaw, \ + _using_AATREE(X, csmap_, Key, cstr_t, keyCompareRaw, \ cstr_del, cstr_from, cstr_c_str, const char*, \ keyDel, keyFromRaw, keyToRaw, RawKey) -#define SET_ONLY_csset(...) __VA_ARGS__ -#define SET_ONLY_csmap(...) -#define MAP_ONLY_csset(...) -#define MAP_ONLY_csmap(...) __VA_ARGS__ -#define KEY_REF_csset(vp) (vp) -#define KEY_REF_csmap(vp) (&(vp)->first) +#define SET_ONLY_csset_(...) __VA_ARGS__ +#define SET_ONLY_csmap_(...) +#define MAP_ONLY_csset_(...) +#define MAP_ONLY_csmap_(...) __VA_ARGS__ +#define KEY_REF_csset_(vp) (vp) +#define KEY_REF_csmap_(vp) (&(vp)->first) #ifndef CSMAP_SIZE_T #define CSMAP_SIZE_T uint32_t #endif @@ -152,101 +152,101 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; #define _using_AATREE(X, C, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - typedef Key C##_##X##_key_t; \ - typedef Mapped C##_##X##_mapped_t; \ - typedef RawKey C##_##X##_rawkey_t; \ - typedef RawMapped C##_##X##_rawmapped_t; \ - typedef CSMAP_SIZE_T C##_##X##_size_t; \ + typedef Key C##X##_key_t; \ + typedef Mapped C##X##_mapped_t; \ + typedef RawKey C##X##_rawkey_t; \ + typedef RawMapped C##X##_rawmapped_t; \ + typedef CSMAP_SIZE_T C##X##_size_t; \ \ - typedef SET_ONLY_##C( C##_##X##_key_t ) \ - MAP_ONLY_##C( struct {C##_##X##_key_t first; \ - C##_##X##_mapped_t second;} ) \ - C##_##X##_value_t; \ + typedef SET_ONLY_##C( C##X##_key_t ) \ + MAP_ONLY_##C( struct {C##X##_key_t first; \ + C##X##_mapped_t second;} ) \ + C##X##_value_t; \ \ - typedef SET_ONLY_##C( C##_##X##_rawkey_t ) \ - MAP_ONLY_##C( struct {C##_##X##_rawkey_t first; \ - C##_##X##_rawmapped_t second;} ) \ - C##_##X##_rawvalue_t; \ + typedef SET_ONLY_##C( C##X##_rawkey_t ) \ + MAP_ONLY_##C( struct {C##X##_rawkey_t first; \ + C##X##_rawmapped_t second;} ) \ + C##X##_rawvalue_t; \ \ typedef struct { \ - C##_##X##_value_t *first; \ + C##X##_value_t *first; \ bool second; /* inserted */ \ - } C##_##X##_result_t; \ + } C##X##_result_t; \ \ - typedef struct C##_##X##_node { \ - C##_##X##_size_t link[2]; \ + typedef struct C##X##_node { \ + C##X##_size_t link[2]; \ int8_t level; \ - C##_##X##_value_t value; \ - } C##_##X##_node_t; \ + C##X##_value_t value; \ + } C##X##_node_t; \ \ typedef struct { \ - C##_##X##_node_t* nodes; \ - } C##_##X; \ + C##X##_node_t* nodes; \ + } C##X; \ \ typedef struct { \ - C##_##X##_value_t *ref; \ - C##_##X##_node_t *_d; \ + C##X##_value_t *ref; \ + C##X##_node_t *_d; \ int _top; \ - C##_##X##_size_t _tn, _st[48]; \ - } C##_##X##_iter_t; \ + C##X##_size_t _tn, _st[48]; \ + } C##X##_iter_t; \ \ - STC_API C##_##X C##_##X##_init(void); \ - STC_API C##_##X C##_##X##_clone(C##_##X tree); \ + STC_API C##X C##X##_init(void); \ + STC_API C##X C##X##_clone(C##X tree); \ \ STC_API void \ - C##_##X##_reserve(C##_##X* self, size_t cap); \ - STC_INLINE C##_##X \ - C##_##X##_with_capacity(size_t size) { \ - C##_##X x = C##_##X##_init(); \ - C##_##X##_reserve(&x, size); \ + C##X##_reserve(C##X* self, size_t cap); \ + STC_INLINE C##X \ + C##X##_with_capacity(size_t size) { \ + C##X x = C##X##_init(); \ + C##X##_reserve(&x, size); \ return x; \ } \ STC_INLINE bool \ - C##_##X##_empty(C##_##X tree) {return _csmap_rep(&tree)->size == 0;} \ + C##X##_empty(C##X tree) {return _csmap_rep(&tree)->size == 0;} \ STC_INLINE size_t \ - C##_##X##_size(C##_##X tree) {return _csmap_rep(&tree)->size;} \ + C##X##_size(C##X tree) {return _csmap_rep(&tree)->size;} \ STC_INLINE size_t \ - C##_##X##_capacity(C##_##X tree) {return _csmap_rep(&tree)->cap;} \ + C##X##_capacity(C##X tree) {return _csmap_rep(&tree)->cap;} \ STC_API void \ - C##_##X##_del(C##_##X* self); \ + C##X##_del(C##X* self); \ STC_INLINE void \ - C##_##X##_clear(C##_##X* self) {C##_##X##_del(self); *self = C##_##X##_init();} \ + C##X##_clear(C##X* self) {C##X##_del(self); *self = C##X##_init();} \ STC_INLINE void \ - C##_##X##_swap(C##_##X* a, C##_##X* b) {c_swap(C##_##X, *a, *b);} \ + C##X##_swap(C##X* a, C##X* b) {c_swap(C##X, *a, *b);} \ \ STC_INLINE void \ - C##_##X##_value_del(C##_##X##_value_t* val) { \ + C##X##_value_del(C##X##_value_t* val) { \ keyDel(KEY_REF_##C(val)); \ MAP_ONLY_##C( mappedDel(&val->second); ) \ } \ - STC_INLINE C##_##X##_value_t \ - C##_##X##_value_clone(C##_##X##_value_t val) { \ + STC_INLINE C##X##_value_t \ + C##X##_value_clone(C##X##_value_t val) { \ *KEY_REF_##C(&val) = keyFromRaw(keyToRaw(KEY_REF_##C(&val))); \ MAP_ONLY_##C( val.second = mappedFromRaw(mappedToRaw(&val.second)); ) \ return val; \ } \ \ - STC_API C##_##X##_value_t* \ - C##_##X##_find_it(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* out); \ + STC_API C##X##_value_t* \ + C##X##_find_it(const C##X* self, RawKey rkey, C##X##_iter_t* out); \ \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_find(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it; \ - C##_##X##_find_it(self, rkey, &it); \ + STC_INLINE C##X##_iter_t \ + C##X##_find(const C##X* self, RawKey rkey) { \ + C##X##_iter_t it; \ + C##X##_find_it(self, rkey, &it); \ return it; \ } \ STC_INLINE bool \ - C##_##X##_contains(const C##_##X* self, RawKey rkey) { \ - C##_##X##_iter_t it; \ - return C##_##X##_find_it(self, rkey, &it) != NULL; \ + C##X##_contains(const C##X* self, RawKey rkey) { \ + C##X##_iter_t it; \ + return C##X##_find_it(self, rkey, &it) != NULL; \ } \ \ - STC_API C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \ + 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); \ + 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); \ if (res.second) { \ *KEY_REF_##C(res.first) = keyFromRaw(rkey); \ MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \ @@ -254,73 +254,73 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; return res; \ } \ STC_INLINE void \ - C##_##X##_emplace_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \ - for (size_t i=0; isecond = mapped; )} \ else {keyDel(&key); MAP_ONLY_##C( mappedDel(&mapped); )} \ return res; \ } \ \ 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)); \ + 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##_result_t \ - C##_##X##_put(C##_##X* self, Key k, Mapped m) { \ - return C##_##X##_insert_or_assign(self, k, m); \ + STC_INLINE C##X##_result_t \ + C##X##_put(C##X* self, Key k, Mapped m) { \ + return C##X##_insert_or_assign(self, k, m); \ } \ - STC_INLINE C##_##X##_result_t \ - C##_##X##_emplace_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \ - C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \ + STC_INLINE C##X##_result_t \ + C##X##_emplace_or_assign(C##X* self, RawKey rkey, RawMapped rmapped) { \ + C##X##_result_t res = C##X##_insert_entry_(self, rkey); \ if (res.second) res.first->first = keyFromRaw(rkey); \ else mappedDel(&res.first->second); \ res.first->second = mappedFromRaw(rmapped); 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##_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_API C##_##X##_value_t* C##_##X##_front(const C##_##X* self); \ - STC_API C##_##X##_value_t* C##_##X##_back(const C##_##X* self); \ - STC_API void C##_##X##_next(C##_##X##_iter_t* it); \ + STC_API C##X##_value_t* C##X##_front(const C##X* self); \ + STC_API C##X##_value_t* C##X##_back(const C##X* self); \ + STC_API void C##X##_next(C##X##_iter_t* it); \ \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_begin(const C##_##X* self) { \ - C##_##X##_iter_t it = {NULL, self->nodes, 0, (C##_##X##_size_t) _csmap_rep(self)->root}; \ - if (it._tn) C##_##X##_next(&it); \ + STC_INLINE C##X##_iter_t \ + C##X##_begin(const C##X* self) { \ + C##X##_iter_t it = {NULL, self->nodes, 0, (C##X##_size_t) _csmap_rep(self)->root}; \ + if (it._tn) C##X##_next(&it); \ return it; \ } \ - STC_INLINE C##_##X##_iter_t \ - C##_##X##_end(const C##_##X* self) {\ - C##_##X##_iter_t it = {NULL}; return it; \ + STC_INLINE C##X##_iter_t \ + C##X##_end(const C##X* self) {\ + C##X##_iter_t it = {NULL}; return it; \ } \ - STC_INLINE C##_##X##_mapped_t* \ - C##_##X##_itval(C##_##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ + STC_INLINE C##X##_mapped_t* \ + C##X##_itval(C##X##_iter_t it) {return SET_ONLY_##C( it.ref ) \ MAP_ONLY_##C( &it.ref->second );} \ STC_API int \ - C##_##X##_erase(C##_##X* self, RawKey rkey); \ + C##X##_erase(C##X* self, RawKey rkey); \ \ STC_INLINE int \ - C##_##X##_erase_at(C##_##X* self, C##_##X##_iter_t pos) { \ - return C##_##X##_erase(self, keyToRaw(KEY_REF_##C(pos.ref))); \ + C##X##_erase_at(C##X* self, C##X##_iter_t pos) { \ + return C##X##_erase(self, keyToRaw(KEY_REF_##C(pos.ref))); \ } \ \ _implement_AATREE(X, C, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - typedef C##_##X C##_##X##_t + typedef C##X C##X##_t /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -330,63 +330,63 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; #define _implement_AATREE(X, C, Key, Mapped, keyCompareRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ keyDel, keyFromRaw, keyToRaw, RawKey) \ - STC_DEF C##_##X \ - C##_##X##_init(void) { \ - C##_##X tree = {(C##_##X##_node_t *) _smap_inits.nodes}; \ + STC_DEF C##X \ + C##X##_init(void) { \ + C##X tree = {(C##X##_node_t *) _smap_inits.nodes}; \ return tree; \ } \ \ - STC_DEF C##_##X##_value_t* \ - C##_##X##_front(const C##_##X* self) { \ - C##_##X##_node_t *d = self->nodes; \ - C##_##X##_size_t tn = (C##_##X##_size_t) _csmap_rep(self)->root; \ + STC_DEF C##X##_value_t* \ + C##X##_front(const C##X* self) { \ + C##X##_node_t *d = self->nodes; \ + C##X##_size_t tn = (C##X##_size_t) _csmap_rep(self)->root; \ while (d[tn].link[0]) tn = d[tn].link[0]; \ return &d[tn].value; \ } \ - STC_DEF C##_##X##_value_t* \ - C##_##X##_back(const C##_##X* self) { \ - C##_##X##_node_t *d = self->nodes; \ - C##_##X##_size_t tn = (C##_##X##_size_t) _csmap_rep(self)->root; \ + STC_DEF C##X##_value_t* \ + C##X##_back(const C##X* self) { \ + C##X##_node_t *d = self->nodes; \ + C##X##_size_t tn = (C##X##_size_t) _csmap_rep(self)->root; \ while (d[tn].link[1]) tn = d[tn].link[1]; \ return &d[tn].value; \ } \ \ STC_DEF void \ - C##_##X##_reserve(C##_##X* self, size_t cap) { \ + C##X##_reserve(C##X* self, size_t cap) { \ struct csmap_rep* rep = _csmap_rep(self); \ - C##_##X##_size_t oldcap = rep->cap; \ + C##X##_size_t oldcap = rep->cap; \ if (cap > oldcap) { \ rep = (struct csmap_rep*) c_realloc(oldcap ? rep : NULL, \ - sizeof(struct csmap_rep) + (cap + 1)*sizeof(C##_##X##_node_t)); \ + sizeof(struct csmap_rep) + (cap + 1)*sizeof(C##X##_node_t)); \ if (oldcap == 0) \ - memset(rep, 0, sizeof(struct csmap_rep) + sizeof(C##_##X##_node_t)); \ + memset(rep, 0, sizeof(struct csmap_rep) + sizeof(C##X##_node_t)); \ rep->cap = cap; \ - self->nodes = (C##_##X##_node_t *) rep->nodes; \ + self->nodes = (C##X##_node_t *) rep->nodes; \ } \ } \ \ - STC_DEF C##_##X##_size_t \ - C##_##X##_node_new_(C##_##X* self, int level) { \ + STC_DEF C##X##_size_t \ + C##X##_node_new_(C##X* self, int level) { \ size_t tn; struct csmap_rep *rep = _csmap_rep(self); \ if (rep->disp) { \ tn = rep->disp; \ rep->disp = self->nodes[tn].link[1]; \ } else { \ - if ((tn = rep->head + 1) > rep->cap) C##_##X##_reserve(self, 4 + tn*3/2); \ + if ((tn = rep->head + 1) > rep->cap) C##X##_reserve(self, 4 + tn*3/2); \ ++_csmap_rep(self)->head; /* do after reserve */ \ } \ - C##_##X##_node_t* dn = &self->nodes[tn]; \ + C##X##_node_t* dn = &self->nodes[tn]; \ dn->link[0] = dn->link[1] = 0; dn->level = level; \ - return (C##_##X##_size_t) tn; \ + return (C##X##_size_t) tn; \ } \ \ - STC_DEF C##_##X##_value_t* \ - C##_##X##_find_it(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* out) { \ - C##_##X##_size_t tn = _csmap_rep(self)->root; \ - C##_##X##_node_t *d = out->_d = self->nodes; \ + STC_DEF C##X##_value_t* \ + C##X##_find_it(const C##X* self, C##X##_rawkey_t rkey, C##X##_iter_t* out) { \ + C##X##_size_t tn = _csmap_rep(self)->root; \ + C##X##_node_t *d = out->_d = self->nodes; \ out->_top = 0; \ while (tn) { \ - int c; C##_##X##_rawkey_t rx = keyToRaw(KEY_REF_##C(&d[tn].value)); \ + int c; C##X##_rawkey_t rx = keyToRaw(KEY_REF_##C(&d[tn].value)); \ if ((c = keyCompareRaw(&rx, &rkey)) < 0) tn = d[tn].link[1]; \ else if (c > 0) {out->_st[out->_top++] = tn; tn = d[tn].link[0];} \ else {out->_tn = d[tn].link[1]; return (out->ref = &d[tn].value);} \ @@ -395,8 +395,8 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; } \ \ STC_DEF void \ - C##_##X##_next(C##_##X##_iter_t *it) { \ - C##_##X##_size_t tn = it->_tn; \ + C##X##_next(C##X##_iter_t *it) { \ + C##X##_size_t tn = it->_tn; \ if (it->_top || tn) { \ while (tn) { \ it->_st[it->_top++] = tn; \ @@ -409,20 +409,20 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; it->ref = NULL; \ } \ \ - static C##_##X##_size_t \ - C##_##X##_skew_(C##_##X##_node_t *d, C##_##X##_size_t tn) { \ + static C##X##_size_t \ + C##X##_skew_(C##X##_node_t *d, C##X##_size_t tn) { \ if (tn && d[d[tn].link[0]].level == d[tn].level) { \ - C##_##X##_size_t tmp = d[tn].link[0]; \ + C##X##_size_t tmp = d[tn].link[0]; \ d[tn].link[0] = d[tmp].link[1]; \ d[tmp].link[1] = tn; \ tn = tmp; \ } \ return tn; \ } \ - static C##_##X##_size_t \ - C##_##X##_split_(C##_##X##_node_t *d, C##_##X##_size_t tn) { \ + static C##X##_size_t \ + C##X##_split_(C##X##_node_t *d, C##X##_size_t tn) { \ if (d[d[d[tn].link[1]].link[1]].level == d[tn].level) { \ - C##_##X##_size_t tmp = d[tn].link[1]; \ + C##X##_size_t tmp = d[tn].link[1]; \ d[tn].link[1] = d[tmp].link[0]; \ d[tmp].link[0] = tn; \ tn = tmp; \ @@ -431,62 +431,62 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; return tn; \ } \ \ - static inline C##_##X##_size_t \ - C##_##X##insert_entry_i_(C##_##X* self, C##_##X##_size_t tn, const C##_##X##_rawkey_t* rkey, C##_##X##_result_t* res) { \ - C##_##X##_size_t up[64], it = tn; \ - C##_##X##_node_t* d = self->nodes; \ + static inline C##X##_size_t \ + C##X##insert_entry_i_(C##X* self, C##X##_size_t tn, const C##X##_rawkey_t* rkey, C##X##_result_t* res) { \ + C##X##_size_t up[64], it = tn; \ + C##X##_node_t* d = self->nodes; \ int c, top = 0, dir = 0; \ while (it) { \ up[top++] = it; \ - C##_##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&d[it].value)); \ + C##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&d[it].value)); \ if ((c = keyCompareRaw(&raw, rkey)) == 0) {res->first = &d[it].value; return tn;} \ dir = (c == -1); \ it = d[it].link[dir]; \ } \ - it = C##_##X##_node_new_(self, 1); d = self->nodes; \ + it = C##X##_node_new_(self, 1); d = self->nodes; \ res->first = &d[it].value, res->second = true; \ if (top == 0) return it; \ d[up[top - 1]].link[dir] = it; \ while (top--) { \ if (top) dir = (d[up[top - 1]].link[1] == up[top]); \ - up[top] = C##_##X##_skew_(d, up[top]); \ - up[top] = C##_##X##_split_(d, up[top]); \ + up[top] = C##X##_skew_(d, up[top]); \ + up[top] = C##X##_split_(d, up[top]); \ if (top) d[up[top - 1]].link[dir] = up[top]; \ } \ return up[0]; \ } \ \ - STC_DEF C##_##X##_result_t \ - C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \ - C##_##X##_result_t res = {NULL, false}; \ - C##_##X##_size_t tn = C##_##X##insert_entry_i_(self, (C##_##X##_size_t) _csmap_rep(self)->root, &rkey, &res); \ + STC_DEF C##X##_result_t \ + C##X##_insert_entry_(C##X* self, RawKey rkey) { \ + C##X##_result_t res = {NULL, false}; \ + C##X##_size_t tn = C##X##insert_entry_i_(self, (C##X##_size_t) _csmap_rep(self)->root, &rkey, &res); \ _csmap_rep(self)->root = tn; \ _csmap_rep(self)->size += res.second; \ return res; \ } \ \ - static C##_##X##_size_t \ - C##_##X##_erase_r_(C##_##X##_node_t *d, C##_##X##_size_t tn, const C##_##X##_rawkey_t* rkey, int *erased) { \ + static C##X##_size_t \ + C##X##_erase_r_(C##X##_node_t *d, C##X##_size_t tn, const C##X##_rawkey_t* rkey, int *erased) { \ if (tn == 0) return 0; \ - C##_##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&d[tn].value)); \ - C##_##X##_size_t tx; int c = keyCompareRaw(&raw, rkey); \ + C##X##_rawkey_t raw = keyToRaw(KEY_REF_##C(&d[tn].value)); \ + C##X##_size_t tx; int c = keyCompareRaw(&raw, rkey); \ if (c != 0) \ - d[tn].link[c == -1] = C##_##X##_erase_r_(d, d[tn].link[c == -1], rkey, erased); \ + d[tn].link[c == -1] = C##X##_erase_r_(d, d[tn].link[c == -1], rkey, erased); \ else { \ - if (!*erased) {C##_##X##_value_del(&d[tn].value); *erased = 1;} \ + if (!*erased) {C##X##_value_del(&d[tn].value); *erased = 1;} \ if (d[tn].link[0] && d[tn].link[1]) { \ tx = d[tn].link[0]; \ while (d[tx].link[1]) \ tx = d[tx].link[1]; \ d[tn].value = d[tx].value; /* move */ \ raw = keyToRaw(KEY_REF_##C(&d[tn].value)); \ - d[tn].link[0] = C##_##X##_erase_r_(d, d[tn].link[0], &raw, erased); \ + d[tn].link[0] = C##X##_erase_r_(d, d[tn].link[0], &raw, erased); \ } else { /* unlink node */ \ tx = tn; \ tn = d[tn].link[ d[tn].link[0] == 0 ]; \ /* move it to disposed nodes list */ \ struct csmap_rep *rep = c_container_of(d, struct csmap_rep, nodes); \ - d[tx].link[1] = (C##_##X##_size_t) rep->disp; \ + d[tx].link[1] = (C##X##_size_t) rep->disp; \ rep->disp = tx; \ } \ } \ @@ -494,53 +494,53 @@ static struct csmap_rep _smap_inits = {0, 0, 0, 0}; if (d[d[tn].link[0]].level < d[tn].level - 1 || d[tx].level < d[tn].level - 1) { \ if (d[tx].level > --d[tn].level) \ d[tx].level = d[tn].level; \ - tn = C##_##X##_skew_(d, tn); \ - tx = d[tn].link[1] = C##_##X##_skew_(d, d[tn].link[1]); \ - d[tx].link[1] = C##_##X##_skew_(d, d[tx].link[1]); \ - tn = C##_##X##_split_(d, tn); \ - d[tn].link[1] = C##_##X##_split_(d, d[tn].link[1]); \ + tn = C##X##_skew_(d, tn); \ + tx = d[tn].link[1] = C##X##_skew_(d, d[tn].link[1]); \ + d[tx].link[1] = C##X##_skew_(d, d[tx].link[1]); \ + tn = C##X##_split_(d, tn); \ + d[tn].link[1] = C##X##_split_(d, d[tn].link[1]); \ } \ return tn; \ } \ \ STC_DEF int \ - C##_##X##_erase(C##_##X* self, RawKey rkey) { \ + C##X##_erase(C##X* self, RawKey rkey) { \ int erased = 0; \ - C##_##X##_size_t root = C##_##X##_erase_r_(self->nodes, (C##_##X##_size_t) _csmap_rep(self)->root, &rkey, &erased); \ + C##X##_size_t root = C##X##_erase_r_(self->nodes, (C##X##_size_t) _csmap_rep(self)->root, &rkey, &erased); \ if (erased) {_csmap_rep(self)->root = root; --_csmap_rep(self)->size;} \ return erased; \ } \ \ - static C##_##X##_size_t \ - C##_##X##_clone_r_(C##_##X* self, const C##_##X##_node_t* src, C##_##X##_size_t sn) { \ + static C##X##_size_t \ + C##X##_clone_r_(C##X* self, const C##X##_node_t* src, C##X##_size_t sn) { \ if (sn == 0) return 0; \ - C##_##X##_size_t tx, tn = C##_##X##_node_new_(self, src[sn].level); \ - self->nodes[tn].value = C##_##X##_value_clone(src[sn].value); \ - tx = C##_##X##_clone_r_(self, src, src[sn].link[0]); self->nodes[tn].link[0] = tx; \ - tx = C##_##X##_clone_r_(self, src, src[sn].link[1]); self->nodes[tn].link[1] = tx; \ + C##X##_size_t tx, tn = C##X##_node_new_(self, src[sn].level); \ + self->nodes[tn].value = C##X##_value_clone(src[sn].value); \ + tx = C##X##_clone_r_(self, src, src[sn].link[0]); self->nodes[tn].link[0] = tx; \ + tx = C##X##_clone_r_(self, src, src[sn].link[1]); self->nodes[tn].link[1] = tx; \ return tn; \ } \ - STC_DEF C##_##X \ - C##_##X##_clone(C##_##X tree) { \ - C##_##X clone = C##_##X##_with_capacity(_csmap_rep(&tree)->size); \ - C##_##X##_size_t root = C##_##X##_clone_r_(&clone, tree.nodes, (C##_##X##_size_t) _csmap_rep(&tree)->root); \ + STC_DEF C##X \ + C##X##_clone(C##X tree) { \ + C##X clone = C##X##_with_capacity(_csmap_rep(&tree)->size); \ + C##X##_size_t root = C##X##_clone_r_(&clone, tree.nodes, (C##X##_size_t) _csmap_rep(&tree)->root); \ _csmap_rep(&clone)->root = root; \ _csmap_rep(&clone)->size = _csmap_rep(&tree)->size; \ return clone; \ } \ \ static void \ - C##_##X##_del_r_(C##_##X##_node_t* d, C##_##X##_size_t tn) { \ + C##X##_del_r_(C##X##_node_t* d, C##X##_size_t tn) { \ if (tn) { \ - C##_##X##_del_r_(d, d[tn].link[0]); \ - C##_##X##_del_r_(d, d[tn].link[1]); \ - C##_##X##_value_del(&d[tn].value); \ + C##X##_del_r_(d, d[tn].link[0]); \ + C##X##_del_r_(d, d[tn].link[1]); \ + C##X##_value_del(&d[tn].value); \ } \ } \ STC_DEF void \ - C##_##X##_del(C##_##X* self) { \ + C##X##_del(C##X* self) { \ if (_csmap_rep(self)->root) { \ - C##_##X##_del_r_(self->nodes, (C##_##X##_size_t) _csmap_rep(self)->root); \ + C##X##_del_r_(self->nodes, (C##X##_size_t) _csmap_rep(self)->root); \ c_free(_csmap_rep(self)); \ } \ } -- cgit v1.2.3