From 66da5f82347d0022cfbaea405c9db50eac4a84d8 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 7 May 2021 09:44:55 +0200 Subject: Cleanup, internal renames. API: c_strhash() -> c_string_hash(), added adv() and ind() to cvec and cdeq. --- benchmarks/others/clist_v1.h | 2 +- benchmarks/others/csmap_v1.h | 10 ++-- docs/cdeq_api.md | 9 ++-- docs/clist_api.md | 2 +- docs/cmap_api.md | 5 +- docs/csmap_api.md | 2 +- docs/cstr_api.md | 9 ++-- docs/cvec_api.md | 17 ++++--- examples/advanced.c | 2 +- examples/csset_erase.c | 2 +- stc/cdeq.h | 107 ++++++++++++++++++++----------------------- stc/clist.h | 24 +++++----- stc/cmap.h | 19 ++++---- stc/csmap.h | 12 ++--- stc/cstr.h | 19 ++++---- stc/cvec.h | 23 +++++----- 16 files changed, 132 insertions(+), 132 deletions(-) diff --git a/benchmarks/others/clist_v1.h b/benchmarks/others/clist_v1.h index 0c438ad0..ff777325 100644 --- a/benchmarks/others/clist_v1.h +++ b/benchmarks/others/clist_v1.h @@ -69,7 +69,7 @@ _c_using_clist(clist_##X, Value, valueCompare, valueDel, valueFromRaw, valueToRaw, RawValue) #define using_clist_str() \ - _c_using_clist(clist_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_clist(clist_str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) #define _c_using_clist_types(CX, Value) \ diff --git a/benchmarks/others/csmap_v1.h b/benchmarks/others/csmap_v1.h index f93ff1cd..be5f929a 100644 --- a/benchmarks/others/csmap_v1.h +++ b/benchmarks/others/csmap_v1.h @@ -77,8 +77,8 @@ int main(void) { #define using_csset_str() \ _c_using_aatree_strkey(str, csset_, cstr, @@, @@) #define using_csmap_str() \ - _c_using_aatree(csmap_str, csmap_, cstr, cstr, cstr_compare_raw, cstr_del, cstr_del, \ - cstr_from, cstr_c_str, const char*, cstr_from, cstr_c_str, const char*) + _c_using_aatree(csmap_str, csmap_, cstr, cstr, c_rstr_compare, cstr_del, cstr_del, \ + cstr_from, cstr_toraw, const char*, cstr_from, cstr_toraw, const char*) #define using_csmap_strkey(...) c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__) @@ -88,8 +88,8 @@ int main(void) { #define using_csmap_strkey_4(X, Mapped, mappedDel, mappedClone) \ _c_using_aatree_strkey(X, csmap_, Mapped, mappedDel, mappedClone) #define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedClone) \ - _c_using_aatree(C##X, C, cstr, Mapped, cstr_compare_raw, mappedDel, cstr_del, \ - cstr_from, cstr_c_str, const char*, mappedClone, c_trivial_toraw, Mapped) + _c_using_aatree(C##X, C, cstr, Mapped, c_rstr_compare, mappedDel, cstr_del, \ + cstr_from, cstr_toraw, const char*, mappedClone, c_trivial_toraw, Mapped) #define using_csmap_strval(...) c_MACRO_OVERLOAD(using_csmap_strval, __VA_ARGS__) @@ -102,7 +102,7 @@ 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) \ _c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompare, cstr_del, keyDel, \ - keyFromRaw, keyToRaw, RawKey, cstr_from, cstr_c_str, const char*) + keyFromRaw, keyToRaw, RawKey, cstr_from, cstr_toraw, const char*) #define SET_ONLY_csset_(...) __VA_ARGS__ #define SET_ONLY_csmap_(...) diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 555976b5..61b0e164 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -23,7 +23,7 @@ be replaced by `i` in all of the following documentation. `using_cdeq_str()` is a shorthand for: ``` -using_cdeq(str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) +using_cdeq(str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) ``` ## Methods @@ -63,7 +63,10 @@ cdeq_X_iter_t cdeq_X_insert_at(cdeq_X* self, cdeq_X_iter_t it, Value value cdeq_X_iter_t cdeq_X_emplace(cdeq_X* self, size_t idx, RawValue raw); cdeq_X_iter_t cdeq_X_emplace_n(cdeq_X* self, size_t idx, const RawValue[] arr, size_t n); cdeq_X_iter_t cdeq_X_emplace_at(cdeq_X* self, cdeq_X_iter_t it, RawValue raw); -cdeq_X_iter_t cdeq_X_emplace_range(cdeq_X* self, cdeq_X_iter_t it1, cdeq_X_iter_t it2); // will clone +cdeq_X_iter_t cdeq_X_emplace_range(cdeq_X* self, cdeq_X_iter_t it, + cdeq_X_iter_t it1, cdeq_X_iter_t it2); // will clone +cdeq_X_iter_t cdeq_X_emplace_range_p(cdeq_X* self, Value* pos, + const Value* p1, const Value* p2); cdeq_X_iter_t cdeq_X_erase(cdeq_X* self, size_t idx); cdeq_X_iter_t cdeq_X_erase_n(cdeq_X* self, size_t idx, size_t n); @@ -76,7 +79,7 @@ cdeq_X_value_t* cdeq_X_get(const cdeq_X* self, RawValue raw); void cdeq_X_sort(cdeq_X* self); void cdeq_X_sort_range(cdeq_X_iter_t i1, cdeq_X_iter_t i2, - int(*cmp)(const cdeq_X_value_t*, const cdeq_X_value_t*)); + int(*cmp)(const Value*, const Value*)); cdeq_X_iter_t cdeq_X_begin(const cdeq_X* self); cdeq_X_iter_t cdeq_X_end(const cdeq_X* self); diff --git a/docs/clist_api.md b/docs/clist_api.md index 7807b677..93384a10 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -38,7 +38,7 @@ The macro `using_clist()` must be instantiated in the global scope. `X` is a typ will affect the names of all clist types and methods. E.g. declaring `using_clist(i, int);`, `X` should be replaced by `i` in all of the following documentation. `using_clist_str()` is a shorthand for ```c -using_clist(str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) +using_clist(str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) ``` ## Methods diff --git a/docs/cmap_api.md b/docs/cmap_api.md index bac1a9b2..30a7b84a 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -87,6 +87,7 @@ cmap_X_value_t cmap_X_value_clone(cmap_X_value_t val); ``` ```c uint64_t c_default_hash(const void *data, size_t len); // key any trivial type +uint64_t c_string_hash(const char* str); // uses c_default_hash() uint64_t c_default_hash32(const void* data, size_t); // key one 32bit int uint64_t c_default_hash64(const void* data, size_t); // key one 64bit int int c_default_equals(const RawKey* a, const RawKey* b); // the == operator @@ -273,7 +274,7 @@ static int Viking_equals(const Viking* a, const Viking* b) { } static uint32_t Viking_hash(const Viking* a, int ignored) { - return c_strhash(a->name.str) ^ (c_strhash(a->country.str) >> 15); + return cstr_hash(a->name) ^ (cstr_hash(a->country) >> 15); } static void Viking_del(Viking* v) { @@ -339,7 +340,7 @@ static int RViking_equals(const RViking* r1, const RViking* r2) { } static uint32_t RViking_hash(const RViking* r, int ignored) { - return c_strhash(r->name) ^ (c_strhash(r->country) >> 15); + return c_string_hash(r->name) ^ (c_string_hash(r->country) >> 15); } static Viking Viking_fromR(RViking r) {return (Viking){cstr_from(r.name), cstr_from(r.country)};} diff --git a/docs/csmap_api.md b/docs/csmap_api.md index e602c9f8..5b81f2e6 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -63,7 +63,7 @@ csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_it csmap_X_result_t csmap_X_insert(csmap_X* self, Key key, Mapped mapped); // no change if key in map csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, Key key, Mapped mapped); // always update mapped -csmap_X_result_t csmap_X_put(csmap_X* self, Key key, Mapped mapped); // alias for insert_or_assign +csmap_X_result_t csmap_X_put(csmap_X* self, Key key, Mapped mapped); // same as insert_or_assign() csmap_X_result_t csmap_X_emplace(csmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map csmap_X_result_t csmap_X_emplace_or_assign(csmap_X* self, RawKey rkey, RawMapped rmapped); // always update rmapped diff --git a/docs/cstr_api.md b/docs/cstr_api.md index df457c0b..29e16b17 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -84,14 +84,13 @@ bool cstr_getdelim(cstr *self, int delim, FILE *stream); ``` Helper methods: ```c -const char* cstr_c_str(const cstr* x); -int cstr_compare_raw(const char** x, const char** y); -bool cstr_equals_raw(const char** x, const char** y); -uint64_t cstr_hash_raw(const char* const* x, size_t ignored); +const char* cstr_toraw(const cstr* x); +int c_rstr_compare(const char** x, const char** y); +bool c_rstr_equals(const char** x, const char** y); +uint64_t c_rstr_hash(const char* const* x, size_t ignored); int c_strncasecmp(const char* str1, const char* str2, size_t n); char* c_strnstr(const char* str, const char* needle, size_t n); char* c_strncasestr(const char* str, const char* needle, size_t n); -uint64_t c_strhash(const char* str); ``` ## Types diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 0efd7a62..e6288336 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -27,7 +27,7 @@ be replaced by `i` in all of the following documentation. `using_cvec_str()` is a shorthand for: ``` -using_cvec(str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) +using_cvec(str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) ``` ## Methods @@ -64,7 +64,10 @@ cvec_X_iter_t cvec_X_insert_at(cvec_X* self, cvec_X_iter_t it, Value value cvec_X_iter_t cvec_X_emplace(cvec_X* self, size_t idx, RawValue raw); cvec_X_iter_t cvec_X_emplace_n(cvec_X* self, size_t idx, const RawValue[] arr, size_t n); cvec_X_iter_t cvec_X_emplace_at(cvec_X* self, cvec_X_iter_t it, RawValue raw); -cvec_X_iter_t cvec_X_emplace_range(cvec_X* self, cvec_X_iter_t it1, cvec_X_iter_t it2); // will clone +cvec_X_iter_t cvec_X_emplace_range(cvec_X* self, cvec_X_iter_t it, + cvec_X_iter_t it1, cvec_X_iter_t it2); // will clone +cvec_X_iter_t cvec_X_emplace_range_p(cvec_X* self, Value* pos, + const Value* p1, const Value* p2); cvec_X_iter_t cvec_X_erase(cvec_X* self, size_t idx); cvec_X_iter_t cvec_X_erase_n(cvec_X* self, size_t idx, size_t n); @@ -79,7 +82,7 @@ cvec_X_iter_t cvec_X_bsearch_in(cvec_X_iter_t i1, cvec_X_iter_t i2, RawVal void cvec_X_sort(cvec_X* self); void cvec_X_sort_range(cvec_X_iter_t i1, cvec_X_iter_t i2, - int(*cmp)(const cvec_X_value_t*, const cvec_X_value_t*)); + int(*cmp)(const Value*, const Value*)); cvec_X_iter_t cvec_X_begin(const cvec_X* self); cvec_X_iter_t cvec_X_end(const cvec_X* self); @@ -116,7 +119,7 @@ int main() // Append a set of numbers c_emplace(cvec_i, vec, {7, 5, 16, 8}); - printf("initial: "); + printf("initial:"); c_foreach (k, cvec_i, vec) { printf(" %d", *k.ref); } @@ -124,7 +127,7 @@ int main() // Sort the vector cvec_i_sort(&vec); - printf("\nsorted: "); + printf("\nsorted:"); c_foreach (k, cvec_i, vec) { printf(" %d", *k.ref); } @@ -134,8 +137,8 @@ int main() ``` Output: ``` -initial: 7 5 16 8 25 13 -sorted: 5 7 8 13 16 25 +initial: 25 13 7 5 16 8 +sorted: 5 7 8 13 16 25 ``` ### Example 2 ```c diff --git a/examples/advanced.c b/examples/advanced.c index 84e148ac..73fcb470 100644 --- a/examples/advanced.c +++ b/examples/advanced.c @@ -20,7 +20,7 @@ typedef struct VikingRaw { } VikingRaw; uint64_t vikingraw_hash(const VikingRaw* raw, size_t ignore) { - uint64_t hash = c_strhash(raw->name) ^ (c_strhash(raw->country) >> 15); + uint64_t hash = c_string_hash(raw->name) ^ (c_string_hash(raw->country) >> 15); return hash; } static inline int vikingraw_equals(const VikingRaw* rx, const VikingRaw* ry) { diff --git a/examples/csset_erase.c b/examples/csset_erase.c index 9a05290d..1ddd8f03 100644 --- a/examples/csset_erase.c +++ b/examples/csset_erase.c @@ -18,7 +18,7 @@ int main() while (it.ref) it = csset_i_erase_at(&set, it); c_foreach (k, csset_i, set) printf(" %d", *k.ref); puts(""); - val = 35; + val = 40; printf("Erase values < %d:\n", val); it = csset_i_lower_bound(&set, val); csset_i_erase_range(&set, csset_i_begin(&set), it); diff --git a/stc/cdeq.h b/stc/cdeq.h index 6bd85cf3..f8b63f1a 100644 --- a/stc/cdeq.h +++ b/stc/cdeq.h @@ -41,7 +41,7 @@ _c_using_cdeq(cdeq_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) #define using_cdeq_str() \ - _c_using_cdeq(cdeq_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_cdeq(cdeq_str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) struct cdeq_rep { size_t size, cap; void* base[]; }; @@ -50,49 +50,53 @@ struct cdeq_rep { size_t size, cap; void* base[]; }; #define _c_using_cdeq(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ \ - typedef Value CX##_value_t; \ - typedef RawValue CX##_rawvalue_t; \ - typedef struct {CX##_value_t *ref; } CX##_iter_t; \ - typedef struct {CX##_value_t *_base, *data;} CX; \ + typedef Value CX##_value_t; \ + typedef RawValue CX##_rawvalue_t; \ + typedef struct {CX##_value_t *ref; } CX##_iter_t; \ + typedef struct {CX##_value_t *_base, *data;} CX; \ \ - STC_API CX CX##_init(void); \ - STC_API CX CX##_clone(CX deq); \ - STC_API void CX##_clear(CX* self); \ - STC_API void CX##_del(CX* self); \ - STC_API void CX##_expand_left_(CX* self, size_t idx, size_t n); \ - STC_API void CX##_expand_right_(CX* self, size_t idx, size_t n); \ - STC_API CX##_iter_t CX##_find_in(CX##_iter_t p1, CX##_iter_t p2, RawValue raw); \ - STC_API int CX##_value_compare(const CX##_value_t* x, const CX##_value_t* y); \ - STC_API void CX##_push_back(CX* self, Value value); \ - STC_API void CX##_push_front(CX* self, Value value); \ - STC_API CX##_iter_t CX##_erase_range_p(CX* self, CX##_value_t* p1, CX##_value_t* p2); \ - STC_API CX##_iter_t CX##_insert_range_p(CX* self, CX##_value_t* pos, \ - const CX##_value_t* p1, const CX##_value_t* p2, bool clone); \ - STC_API CX##_iter_t CX##_emplace_range_p(CX* self, CX##_value_t* pos, \ + STC_API CX CX##_init(void); \ + STC_API CX CX##_clone(CX cx); \ + STC_API void CX##_clear(CX* self); \ + STC_API void CX##_del(CX* self); \ + STC_API CX##_iter_t CX##_find_in(CX##_iter_t p1, CX##_iter_t p2, RawValue raw); \ + STC_API int CX##_value_compare(const CX##_value_t* x, const CX##_value_t* y); \ + STC_API void CX##_push_back(CX* self, Value value); \ + STC_API void CX##_push_front(CX* self, Value value); \ + STC_API CX##_iter_t CX##_erase_range_p(CX* self, CX##_value_t* p1, CX##_value_t* p2); \ + STC_API CX##_iter_t CX##_insert_range_p(CX* self, CX##_value_t* pos, \ + const CX##_value_t* p1, const CX##_value_t* p2, bool clone); \ + STC_API CX##_iter_t CX##_emplace_range_p(CX* self, CX##_value_t* pos, \ const CX##_rawvalue_t* p1, const CX##_rawvalue_t* p2); \ - STC_INLINE bool CX##_empty(CX deq) {return !_cdeq_rep(&deq)->size;} \ - STC_INLINE size_t CX##_size(CX deq) {return _cdeq_rep(&deq)->size;} \ - STC_INLINE size_t CX##_capacity(CX deq) {return _cdeq_rep(&deq)->cap;} \ - STC_INLINE void CX##_swap(CX* a, CX* b) {c_swap(CX, *a, *b);} \ - STC_INLINE Value CX##_value_fromraw(RawValue raw) {return valueFromRaw(raw);} \ - STC_INLINE Value CX##_value_clone(Value val) \ - {return valueFromRaw(valueToRaw(&val));} \ - STC_INLINE void CX##_emplace_back(CX* self, RawValue raw) \ - {CX##_push_back(self, valueFromRaw(raw));} \ - STC_INLINE void CX##_emplace_front(CX* self, RawValue raw) \ - {CX##_push_front(self, valueFromRaw(raw));} \ - STC_INLINE void CX##_pop_back(CX* self) \ - {valueDel(&self->data[--_cdeq_rep(self)->size]);} \ - STC_INLINE void CX##_pop_front(CX* self) \ - {valueDel(self->data++); --_cdeq_rep(self)->size;} \ - STC_INLINE \ - CX##_value_t* CX##_front(const CX* self) {return self->data;} \ - STC_INLINE \ - CX##_value_t* CX##_back(const CX* self) \ - {return self->data + _cdeq_rep(self)->size - 1;} \ - STC_INLINE \ - CX##_value_t* CX##_at(const CX* self, size_t idx) \ - {assert(idx < _cdeq_rep(self)->size); return self->data + idx;} \ + STC_API void CX##_expand_right_(CX* self, size_t idx, size_t n); \ +\ + STC_INLINE bool CX##_empty(CX cx) {return !_cdeq_rep(&cx)->size;} \ + STC_INLINE size_t CX##_size(CX cx) {return _cdeq_rep(&cx)->size;} \ + STC_INLINE size_t CX##_capacity(CX cx) {return _cdeq_rep(&cx)->cap;} \ + STC_INLINE void CX##_swap(CX* a, CX* b) {c_swap(CX, *a, *b);} \ + STC_INLINE Value CX##_value_fromraw(RawValue raw) {return valueFromRaw(raw);} \ + STC_INLINE Value CX##_value_clone(Value val) \ + {return valueFromRaw(valueToRaw(&val));} \ + STC_INLINE void CX##_emplace_back(CX* self, RawValue raw) \ + {CX##_push_back(self, valueFromRaw(raw));} \ + STC_INLINE void CX##_emplace_front(CX* self, RawValue raw) \ + {CX##_push_front(self, valueFromRaw(raw));} \ + STC_INLINE void CX##_pop_back(CX* self) \ + {valueDel(&self->data[--_cdeq_rep(self)->size]);} \ + STC_INLINE void CX##_pop_front(CX* self) \ + {valueDel(self->data++); --_cdeq_rep(self)->size;} \ + STC_INLINE CX##_value_t* CX##_front(const CX* self) {return self->data;} \ + STC_INLINE CX##_value_t* CX##_back(const CX* self) \ + {return self->data + _cdeq_rep(self)->size - 1;} \ + STC_INLINE CX##_value_t* CX##_at(const CX* self, size_t idx) \ + {assert(idx < _cdeq_rep(self)->size); return self->data + idx;} \ + STC_INLINE CX##_iter_t CX##_begin(const CX* self) \ + {CX##_iter_t it = {self->data}; return it;} \ + STC_INLINE CX##_iter_t CX##_end(const CX* self) \ + {CX##_iter_t it = {self->data + _cdeq_rep(self)->size}; return it;} \ + STC_INLINE void CX##_next(CX##_iter_t* it) {++it->ref;} \ + STC_INLINE CX##_iter_t CX##_adv(CX##_iter_t it, intptr_t offs) {it.ref += offs; return it;} \ + STC_INLINE size_t CX##_idx(CX cx, CX##_iter_t it) {return it.ref - cx.data;} \ \ STC_INLINE CX \ CX##_with_capacity(size_t n) { \ @@ -163,17 +167,6 @@ struct cdeq_rep { size_t size, cap; void* base[]; }; CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2) { \ return CX##_erase_range_p(self, it1.ref, it2.ref); \ } \ -\ - STC_INLINE CX##_iter_t \ - CX##_begin(const CX* self) { \ - CX##_iter_t it = {self->data}; return it; \ - } \ - STC_INLINE CX##_iter_t \ - CX##_end(const CX* self) { \ - CX##_iter_t it = {self->data + _cdeq_rep(self)->size}; return it; \ - } \ - STC_INLINE void \ - CX##_next(CX##_iter_t* it) {++it->ref;} \ \ STC_INLINE CX##_iter_t \ CX##_find(const CX* self, RawValue raw) { \ @@ -213,7 +206,7 @@ static struct cdeq_rep _cdeq_inits = {0, 0}; STC_DEF CX \ CX##_init(void) { \ CX##_value_t *b = (CX##_value_t *) _cdeq_inits.base; \ - CX deq = {b, b}; return deq; \ + CX cx = {b, b}; return cx; \ } \ \ STC_DEF void \ @@ -305,10 +298,10 @@ static struct cdeq_rep _cdeq_inits = {0, 0}; } \ \ STC_DEF CX \ - CX##_clone(CX deq) { \ - size_t sz = _cdeq_rep(&deq)->size; \ + CX##_clone(CX cx) { \ + size_t sz = _cdeq_rep(&cx)->size; \ CX out = CX##_with_capacity(sz); \ - CX##_insert_range_p(&out, out.data, deq.data, deq.data + sz, true); \ + CX##_insert_range_p(&out, out.data, cx.data, cx.data + sz, true); \ return out; \ } \ \ diff --git a/stc/clist.h b/stc/clist.h index 2bebf58e..4d6b8c1b 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -69,7 +69,7 @@ _c_using_clist(clist_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) #define using_clist_str() \ - _c_using_clist(clist_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_clist(clist_str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) #define _c_using_clist_types(CX, Value) \ @@ -99,7 +99,7 @@ STC_API size_t _clist_count(const clist_VOID* self); _c_using_clist_types(CX, Value); \ typedef RawValue CX##_rawvalue_t; \ \ - STC_API CX CX##_clone(CX list); \ + STC_API CX CX##_clone(CX cx); \ STC_API void CX##_del(CX* self); \ STC_API void CX##_push_back(CX* self, Value value); \ STC_API void CX##_push_front(CX* self, Value value); \ @@ -114,10 +114,10 @@ STC_API size_t _clist_count(const clist_VOID* self); STC_API CX##_iter_t CX##_find_in(CX##_iter_t it1, CX##_iter_t it2, RawValue val); \ STC_API CX##_node_t* CX##_erase_after_(CX* self, CX##_node_t* node); \ \ - STC_INLINE CX CX##_init(void) {CX lst = {NULL}; return lst;} \ - STC_INLINE bool CX##_empty(CX lst) {return lst.last == NULL;} \ - STC_INLINE size_t CX##_count(CX lst) \ - {return _clist_count((const clist_VOID*) &lst);} \ + STC_INLINE CX CX##_init(void) {CX cx = {NULL}; return cx;} \ + STC_INLINE bool CX##_empty(CX cx) {return cx.last == NULL;} \ + STC_INLINE size_t CX##_count(CX cx) \ + {return _clist_count((const clist_VOID*) &cx);} \ STC_INLINE void CX##_clear(CX* self) {CX##_del(self);} \ STC_INLINE Value CX##_value_clone(Value val) \ {return valueFromRaw(valueToRaw(&val));} \ @@ -190,9 +190,9 @@ STC_API size_t _clist_count(const clist_VOID* self); #define _c_implement_clist(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ \ STC_DEF CX \ - CX##_clone(CX list) { \ + CX##_clone(CX cx) { \ CX out = CX##_init(); \ - c_foreach_3 (it, CX, list) CX##_emplace_back(&out, valueToRaw(it.ref)); \ + c_foreach_3 (it, CX, cx) CX##_emplace_back(&out, valueToRaw(it.ref)); \ return out; \ } \ \ @@ -297,14 +297,14 @@ STC_API size_t _clist_count(const clist_VOID* self); \ STC_DEF CX \ CX##_split(CX* self, CX##_iter_t it1, CX##_iter_t it2) { \ - CX list = {NULL}; \ - if (it1.ref == it2.ref) return list; \ + CX cx = {NULL}; \ + if (it1.ref == it2.ref) return cx; \ CX##_node_t *p1 = it1.prev, \ *p2 = it2.ref ? it2.prev : self->last; \ p1->next = p2->next, p2->next = _clist_node(CX, it1.ref); \ if (self->last == p2) self->last = (p1 == p2) ? NULL : p1; \ - list.last = p2; \ - return list; \ + cx.last = p2; \ + return cx; \ } \ \ STC_DEF int \ diff --git a/stc/cmap.h b/stc/cmap.h index e0e86d5c..45694708 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -90,9 +90,9 @@ int main(void) { keyDel, keyFromRaw, keyToRaw, RawKey) #define using_cmap_str() \ - _c_using_chash(cmap_str, cmap_, cstr, cstr, cstr_equals_raw, cstr_hash_raw, \ - cstr_del, cstr_from, cstr_c_str, const char*, \ - cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_chash(cmap_str, cmap_, cstr, cstr, c_rstr_equals, c_rstr_hash, \ + cstr_del, cstr_from, cstr_toraw, const char*, \ + cstr_del, cstr_from, cstr_toraw, const char*) #define using_cmap_strkey(...) c_MACRO_OVERLOAD(using_cmap_strkey, __VA_ARGS__) @@ -107,9 +107,9 @@ int main(void) { _c_using_chash_strkey(X, cmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) #define _c_using_chash_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ - _c_using_chash(C##X, C, cstr, Mapped, cstr_equals_raw, cstr_hash_raw, \ + _c_using_chash(C##X, C, cstr, Mapped, c_rstr_equals, c_rstr_hash, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ - cstr_del, cstr_from, cstr_c_str, const char*) + cstr_del, cstr_from, cstr_toraw, const char*) #define using_cmap_strval(...) c_MACRO_OVERLOAD(using_cmap_strval, __VA_ARGS__) @@ -125,7 +125,7 @@ int main(void) { #define using_cmap_strval_8(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ _c_using_chash(cmap_##X, cmap_, Key, cstr, keyEqualsRaw, keyHashRaw, \ - cstr_del, cstr_from, cstr_c_str, const char*, \ + cstr_del, cstr_from, cstr_toraw, const char*, \ keyDel, keyFromRaw, keyToRaw, RawKey) #define SET_ONLY_cmap_(...) @@ -138,11 +138,12 @@ int main(void) { typedef struct {size_t idx; uint_fast8_t hx;} chash_bucket_t; \ STC_API uint64_t c_default_hash(const void *data, size_t len); +STC_INLINE uint64_t c_string_hash(const char *s) + {return c_default_hash(s, strlen(s));} STC_INLINE uint64_t c_default_hash32(const void* data, size_t ignored) - {return *(const uint32_t *)data * 0xc6a4a7935bd1e99d;} + {return *(const uint32_t *)data * 0xc6a4a7935bd1e99d;} STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) - {return *(const uint64_t *)data * 0xc6a4a7935bd1e99d;} - + {return *(const uint64_t *)data * 0xc6a4a7935bd1e99d;} #define _c_using_chash(CX, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ diff --git a/stc/csmap.h b/stc/csmap.h index b8240fd2..9cffac0d 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -85,9 +85,9 @@ int main(void) { keyDel, keyFromRaw, keyToRaw, RawKey) #define using_csmap_str() \ - _c_using_aatree(csmap_str, csmap_, cstr, cstr, cstr_compare_raw, \ - cstr_del, cstr_from, cstr_c_str, const char*, \ - cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_aatree(csmap_str, csmap_, cstr, cstr, c_rstr_compare, \ + cstr_del, cstr_from, cstr_toraw, const char*, \ + cstr_del, cstr_from, cstr_toraw, const char*) #define using_csmap_strkey(...) c_MACRO_OVERLOAD(using_csmap_strkey, __VA_ARGS__) @@ -102,9 +102,9 @@ int main(void) { _c_using_aatree_strkey(X, csmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) #define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \ - _c_using_aatree(C##X, C, cstr, Mapped, cstr_compare_raw, \ + _c_using_aatree(C##X, C, cstr, Mapped, c_rstr_compare, \ mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ - cstr_del, cstr_from, cstr_c_str, const char*) + cstr_del, cstr_from, cstr_toraw, const char*) #define using_csmap_strval(...) c_MACRO_OVERLOAD(using_csmap_strval, __VA_ARGS__) @@ -120,7 +120,7 @@ int main(void) { #define using_csmap_strval_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \ _c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompareRaw, \ - cstr_del, cstr_from, cstr_c_str, const char*, \ + cstr_del, cstr_from, cstr_toraw, const char*, \ keyDel, keyFromRaw, keyToRaw, RawKey) #define SET_ONLY_csmap_(...) diff --git a/stc/cstr.h b/stc/cstr.h index 2bdcf3a0..8270bbf6 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -167,16 +167,15 @@ cstr_iends_with(cstr s, const char* needle) { } /* cvec/cmap adaption functions: */ -#define cstr_c_str(x) ((x)->str) -#define cstr_compare_raw(x, y) strcmp(*(x), *(y)) -#define cstr_equals_raw(x, y) (strcmp(*(x), *(y)) == 0) -#define cstr_hash_raw(p, none) c_default_hash(*(p), strlen(*(p))) - -#define cstr_compare_ref(x, y) strcmp((x)->str, (y)->str) -#define cstr_equals_ref(x, y) (strcmp((x)->str, (y)->str) == 0) -#define cstr_hash_ref(p, none) c_default_hash((p)->str, cstr_size(*(p))) - -#define c_strhash(s) c_default_hash(s, strlen(s)) +#define c_rstr_compare(rx, ry) strcmp(*(rx), *(ry)) +#define c_rstr_equals(rx, ry) (strcmp(*(rx), *(ry)) == 0) +#define c_rstr_hash(rp, none) c_default_hash(*(rp), strlen(*(rp))) + +#define cstr_toraw(xp) ((xp)->str) +#define cstr_compare_ref(xp, yp) strcmp((xp)->str, (yp)->str) +#define cstr_equals_ref(xp, yp) (strcmp((xp)->str, (yp)->str) == 0) +#define cstr_hash_ref(xp, none) c_default_hash((xp)->str, cstr_size(*(xp))) +#define cstr_hash(x) c_default_hash((x).str, cstr_size(x)) /* -------------------------- IMPLEMENTATION ------------------------- */ diff --git a/stc/cvec.h b/stc/cvec.h index 3856ec9b..da138b66 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -41,7 +41,7 @@ _c_using_cvec(cvec_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) #define using_cvec_str() \ - _c_using_cvec(cvec_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) + _c_using_cvec(cvec_str, cstr, c_rstr_compare, cstr_del, cstr_from, cstr_toraw, const char*) struct cvec_rep { size_t size, cap; void* data[]; }; @@ -56,7 +56,7 @@ struct cvec_rep { size_t size, cap; void* data[]; }; typedef struct {CX##_value_t *data;} CX; \ \ STC_API CX CX##_init(void); \ - STC_API CX CX##_clone(CX vec); \ + STC_API CX CX##_clone(CX cx); \ STC_API void CX##_del(CX* self); \ STC_API void CX##_clear(CX* self); \ STC_API void CX##_reserve(CX* self, size_t cap); \ @@ -71,9 +71,9 @@ struct cvec_rep { size_t size, cap; void* data[]; }; STC_API CX##_iter_t CX##_emplace_range_p(CX* self, CX##_value_t* pos, \ const CX##_rawvalue_t* p1, const CX##_rawvalue_t* p2); \ \ - STC_INLINE size_t CX##_size(CX vec) { return _cvec_rep(&vec)->size; } \ - STC_INLINE size_t CX##_capacity(CX vec) { return _cvec_rep(&vec)->cap; } \ - STC_INLINE bool CX##_empty(CX vec) {return !_cvec_rep(&vec)->size;} \ + STC_INLINE size_t CX##_size(CX cx) { return _cvec_rep(&cx)->size; } \ + STC_INLINE size_t CX##_capacity(CX cx) { return _cvec_rep(&cx)->cap; } \ + STC_INLINE bool CX##_empty(CX cx) {return !_cvec_rep(&cx)->size;} \ STC_INLINE Value CX##_value_fromraw(RawValue raw) {return valueFromRaw(raw);} \ STC_INLINE void CX##_swap(CX* a, CX* b) {c_swap(CX, *a, *b);} \ STC_INLINE CX##_value_t*CX##_front(const CX* self) {return self->data;} \ @@ -90,7 +90,8 @@ struct cvec_rep { size_t size, cap; void* data[]; }; STC_INLINE CX##_iter_t CX##_end(const CX* self) \ {CX##_iter_t it = {self->data + _cvec_rep(self)->size}; return it;} \ STC_INLINE void CX##_next(CX##_iter_t* it) {++it->ref;} \ - STC_INLINE size_t CX##_index(CX vec, CX##_iter_t it) {return it.ref - vec.data;} \ + STC_INLINE CX##_iter_t CX##_adv(CX##_iter_t it, intptr_t offs) {it.ref += offs; return it;} \ + STC_INLINE size_t CX##_idx(CX cx, CX##_iter_t it) {return it.ref - cx.data;} \ \ STC_INLINE CX \ CX##_with_size(size_t size, Value null_val) { \ @@ -207,8 +208,8 @@ static struct cvec_rep _cvec_inits = {0, 0}; \ STC_DEF CX \ CX##_init(void) { \ - CX vec = {(CX##_value_t *) _cvec_inits.data}; \ - return vec; \ + CX cx = {(CX##_value_t *) _cvec_inits.data}; \ + return cx; \ } \ \ STC_DEF void \ @@ -259,10 +260,10 @@ static struct cvec_rep _cvec_inits = {0, 0}; } \ \ STC_DEF CX \ - CX##_clone(CX vec) { \ - size_t len = _cvec_rep(&vec)->size; \ + CX##_clone(CX cx) { \ + size_t len = _cvec_rep(&cx)->size; \ CX out = CX##_with_capacity(len); \ - CX##_insert_range_p(&out, out.data, vec.data, vec.data + len, true); \ + CX##_insert_range_p(&out, out.data, cx.data, cx.data + len, true); \ return out; \ } \ \ -- cgit v1.2.3