From 92b950333c6c7002bdbf1b60af44a249dc0cef9c Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 19 Dec 2021 12:21:44 +0100 Subject: First commit for Version 3 of STC. Main changes are consistent rename of '_del' to '_drop' and '_compare' to '_cmp'. Also i_key_ref (earlier i_key_sptr) and i_val_ref replaced by more general i_key_bind/i_val_bind. --- include/stc/alt/clist.h | 18 +-- include/stc/alt/csmap.h | 30 ++-- include/stc/alt/cstr.h | 27 ++-- include/stc/alt/sstr.h | 406 ------------------------------------------------ include/stc/carr2.h | 13 +- include/stc/carr3.h | 13 +- include/stc/cbits.h | 6 +- include/stc/cbox.h | 63 ++++---- include/stc/ccommon.h | 34 ++-- include/stc/cdeq.h | 33 ++-- include/stc/clist.h | 34 ++-- include/stc/cmap.h | 37 ++--- include/stc/cpque.h | 10 +- include/stc/cset.h | 2 +- include/stc/csmap.h | 45 +++--- include/stc/csptr.h | 60 +++---- include/stc/csset.h | 2 +- include/stc/cstack.h | 8 +- include/stc/cstr.h | 7 +- include/stc/csview.h | 8 +- include/stc/cvec.h | 41 ++--- include/stc/template.h | 162 +++++++++---------- 22 files changed, 346 insertions(+), 713 deletions(-) delete mode 100644 include/stc/alt/sstr.h (limited to 'include') diff --git a/include/stc/alt/clist.h b/include/stc/alt/clist.h index 6695a26e..14abc7e4 100644 --- a/include/stc/alt/clist.h +++ b/include/stc/alt/clist.h @@ -49,7 +49,7 @@ puts("sorted"); c_foreach (i, clist_ix, list) if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.ref->value); - clist_ix_del(&list); + clist_ix_drop(&list); } */ #include @@ -61,13 +61,13 @@ _c_clist_types(clist_VOID, int); STC_API size_t _clist_count(const clist_VOID* self); #define _clist_node(_cx_self, vp) c_container_of(vp, _cx_node, value) -#define _c_using_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw, defTypes) \ +#define _c_using_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw, defTypes) \ \ defTypes( _c_clist_types(_cx_self, i_val); ) \ typedef i_valraw _cx_rawvalue; \ \ STC_API _cx_self _cx_memb(_clone)(_cx_self lst); \ - STC_API void _cx_memb(_del)(_cx_self* self); \ + STC_API void _cx_memb(_drop)(_cx_self* self); \ STC_API void _cx_memb(_push_back)(_cx_self* self, i_val value); \ STC_API void _cx_memb(_push_front)(_cx_self* self, i_val value); \ STC_API void _cx_memb(_emplace_items)(_cx_self *self, const _cx_rawvalue arr[], size_t n); \ @@ -89,7 +89,7 @@ STC_API size_t _clist_count(const clist_VOID* self); STC_INLINE size_t _cx_memb(_count)(_cx_self lst) { return _clist_count((const clist_VOID*) &lst); } \ STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } \ STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } \ - STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); } \ + STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } \ STC_INLINE void _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) \ { _cx_memb(_push_back)(self, i_valfrom(raw)); } \ STC_INLINE void _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw) \ @@ -143,13 +143,13 @@ STC_API size_t _clist_count(const clist_VOID* self); while (n-- && it.ref) _cx_memb(_next)(&it); return it; \ } \ \ - _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw) \ + _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw) \ struct stc_trailing_semicolon /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw) \ +#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw) \ \ STC_DEF _cx_self \ _cx_memb(_clone)(_cx_self lst) { \ @@ -160,7 +160,7 @@ STC_API size_t _clist_count(const clist_VOID* self); } \ \ STC_DEF void \ - _cx_memb(_del)(_cx_self* self) { \ + _cx_memb(_drop)(_cx_self* self) { \ while (self->last) _cx_memb(_erase_after_)(self, self->last); \ } \ \ @@ -232,7 +232,7 @@ STC_API size_t _clist_count(const clist_VOID* self); node->next = next; \ if (del == next) self->last = node = NULL; \ else if (self->last == del) self->last = node, node = NULL; \ - i_valdel(&del->value); c_free(del); \ + i_valdrop(&del->value); c_free(del); \ return node; \ } \ \ @@ -361,7 +361,7 @@ _clist_mergesort(clist_VOID_node *list, int (*cmp)(const void*, const void*)) { } #else -#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw) +#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw) #endif #endif diff --git a/include/stc/alt/csmap.h b/include/stc/alt/csmap.h index 5b331efb..beccf559 100644 --- a/include/stc/alt/csmap.h +++ b/include/stc/alt/csmap.h @@ -30,7 +30,7 @@ using_csmap(mx, int, char); // Sorted map int main(void) { - c_autovar (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m)) + c_autovar (csmap_mx m = csmap_mx_init(), csmap_mx_drop(&m)) { csmap_mx_insert(&m, 5, 'a'); csmap_mx_insert(&m, 8, 'b'); @@ -91,8 +91,8 @@ int main(void) { \ STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.size == 0; } \ STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cx.size; } \ - STC_INLINE void _cx_memb(_del)(_cx_self* self) { _cx_memb(_del_r_)(self->root); } \ - STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); *self = _cx_memb(_init)(); } \ + STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_del_r_)(self->root); } \ + STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); *self = _cx_memb(_init)(); } \ STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) {c_swap(_cx_self, *a, *b); } \ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self cx) { return c_make(_cx_self){ _cx_memb(_clone_r_)(cx.root), cx.size}; } \ STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) \ @@ -103,9 +103,9 @@ int main(void) { {_cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); } \ \ STC_INLINE void \ - _cx_memb(_value_del)(_cx_value* val) { \ - i_keydel(_i_keyref(val)); \ - _i_MAP_ONLY( i_valdel(&val->second); ) \ + _cx_memb(_value_drop)(_cx_value* val) { \ + i_keydrop(_i_keyref(val)); \ + _i_MAP_ONLY( i_valdrop(&val->second); ) \ } \ \ STC_INLINE void \ @@ -134,7 +134,7 @@ int main(void) { _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped)) { \ _cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key)); \ if (res.inserted) {*_i_keyref(res.ref) = key; _i_MAP_ONLY( res.ref->second = mapped; )} \ - else {i_keydel(&key); _i_MAP_ONLY( i_valdel(&mapped); )} \ + else {i_keydrop(&key); _i_MAP_ONLY( i_valdrop(&mapped); )} \ return res; \ } \ \ @@ -143,7 +143,7 @@ int main(void) { _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped) { \ _cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key)); \ if (res.inserted) res.ref->first = key; \ - else {i_keydel(&key); i_valdel(&res.ref->second); } \ + else {i_keydrop(&key); i_valdrop(&res.ref->second); } \ res.ref->second = mapped; return res; \ } \ \ @@ -156,7 +156,7 @@ int main(void) { _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { \ _cx_result res = _cx_memb(_insert_entry_)(self, rkey); \ if (res.inserted) res.ref->first = i_keyfrom(rkey); \ - else i_valdel(&res.ref->second); \ + else i_valdrop(&res.ref->second); \ res.ref->second = i_valfrom(rmapped); return res; \ } \ \ @@ -191,8 +191,8 @@ int main(void) { } \ \ _c_implement_aatree(_cx_self, C, i_key, i_val, i_cmp, \ - i_valdel, i_valfrom, i_valto, i_valraw, \ - i_keydel, i_keyfrom, i_keyto, i_keyraw) \ + i_valdrop, i_valfrom, i_valto, i_valraw, \ + i_keydrop, i_keyfrom, i_keyto, i_keyraw) \ struct stc_trailing_semicolon /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -204,8 +204,8 @@ _c_aatree_complete_types(csmap_SENTINEL, csmap_); static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_sentinel, 0}; #define _c_implement_aatree(_cx_self, C, i_key, i_val, i_cmp, \ - i_valdel, i_valfrom, i_valto, i_valraw, \ - i_keydel, i_keyfrom, i_keyto, i_keyraw) \ + i_valdrop, i_valfrom, i_valto, i_valraw, \ + i_keydrop, i_keyfrom, i_keyto, i_keyraw) \ STC_DEF _cx_self \ _cx_memb(_init)(void) { \ _cx_self cx = {(_cx_node *) &_aatree_sentinel, 0}; \ @@ -330,7 +330,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti if (c != 0) \ tn->link[c < 0] = _cx_memb(_erase_r_)(tn->link[c < 0], rkey, erased); \ else { \ - if (!*erased) { _cx_memb(_value_del)(&tn->value); *erased = 1; } \ + if (!*erased) { _cx_memb(_value_drop)(&tn->value); *erased = 1; } \ if (tn->link[0]->level && tn->link[1]->level) { \ tx = tn->link[0]; \ while (tx->link[1]->level) \ @@ -395,7 +395,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti if (tn->level != 0) { \ _cx_memb(_del_r_)(tn->link[0]); \ _cx_memb(_del_r_)(tn->link[1]); \ - _cx_memb(_value_del)(&tn->value); \ + _cx_memb(_value_drop)(&tn->value); \ c_free(tn); \ } \ } diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h index 76b3a800..bd135b7e 100644 --- a/include/stc/alt/cstr.h +++ b/include/stc/alt/cstr.h @@ -41,7 +41,7 @@ typedef union { /**************************** PRIVATE API **********************************/ -enum { SSO_CAP = offsetof(cstr, lon.ncap) + sizeof((cstr){{0}}.lon.ncap) - 1 }; +enum { SSO_CAP = sizeof(_cstr_rep_t) - 1 }; #define cstr_is_long(s) (bool)((s)->sso.cap_len & 128) #define cstr_select_(s, memb) (cstr_is_long(s) ? cstr_l_##memb : cstr_s_##memb) @@ -52,18 +52,18 @@ enum { SSO_CAP = offsetof(cstr, lon.ncap) + sizeof((cstr){{0}}.lon.ncap) - 1 }; #define cstr_s_end(s) ((s)->sso.data + cstr_s_size(s)) #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -#define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8) -#define cstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) -#define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1)) + #define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8) + #define cstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1)) #else -#define cstr_l_cap(s) (~(s)->lon.ncap) -#define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap)) + #define cstr_l_cap(s) (~(s)->lon.ncap) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap)) #endif #define cstr_l_size(s) ((s)->lon.size) #define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0) #define cstr_l_data(s) (s)->lon.data #define cstr_l_end(s) ((s)->lon.data + cstr_l_size(s)) -#define cstr_l_del(s) free((s)->lon.data) +#define cstr_l_drop(s) free((s)->lon.data) STC_API char* cstr_init_(cstr* self, size_t len, size_t cap); STC_API void cstr_internal_move_(cstr* self, size_t pos1, size_t pos2); @@ -74,15 +74,16 @@ STC_INLINE void cstr_set_size_(cstr* self, size_t len) { STC_INLINE _cstr_rep_t cstr_rep_(cstr* self) { return cstr_is_long(self) - ? c_make(_cstr_rep_t){self->lon.data, cstr_l_size(self), cstr_l_cap(self)} - : c_make(_cstr_rep_t){self->sso.data, cstr_s_size(self), cstr_s_cap(self)}; + ? c_make(_cstr_rep_t){self->lon.data, cstr_l_size(self), cstr_l_cap(self)} + : c_make(_cstr_rep_t){self->sso.data, cstr_s_size(self), cstr_s_cap(self)}; } /**************************** PUBLIC API **********************************/ #define cstr_new(literal) cstr_from_n(literal, sizeof((c_strlit){literal}) - 1) #define cstr_npos (SIZE_MAX >> 1) -#define cstr_null (cstr){.sso = {.cap_len = SSO_CAP}} +#define cstr_null (c_make(cstr){.sso = {.cap_len = SSO_CAP}}) +#define cstr_toraw(self) cstr_str(self) STC_API char* cstr_reserve(cstr* self, size_t cap); STC_API void cstr_shrink_to_fit(cstr* self); @@ -126,8 +127,8 @@ STC_INLINE cstr cstr_clone(cstr s) { return cstr_from_n(rep.data, rep.size); } -STC_INLINE void cstr_del(cstr* self) { - if (cstr_is_long(self)) cstr_l_del(self); +STC_INLINE void cstr_drop(cstr* self) { + if (cstr_is_long(self)) cstr_l_drop(self); } STC_INLINE void cstr_clear(cstr* self) { @@ -170,7 +171,7 @@ STC_INLINE bool cstr_equalto(const cstr* s1, const cstr* s2) { return strcmp(cstr_str(s1), cstr_str(s2)) == 0; } -STC_INLINE int cstr_compare(const cstr* s1, const cstr* s2) { +STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2) { return strcmp(cstr_str(s1), cstr_str(s2)); } diff --git a/include/stc/alt/sstr.h b/include/stc/alt/sstr.h deleted file mode 100644 index 2ea1033d..00000000 --- a/include/stc/alt/sstr.h +++ /dev/null @@ -1,406 +0,0 @@ -/* MIT License - * - * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -/* A string type with short string optimization in C99 with optimal short string - * utilization (23 characters with 24 bytes string representation). - */ -#ifndef SSTR_INCLUDED -#define SSTR_INCLUDED - -#include -#include -#include -#include - -#ifndef sstr_size_t -typedef size_t sstr_size_t; -#endif -typedef struct { char* data; sstr_size_t size, cap; } sstr_rep_t; -typedef const char sstr_literal_t[]; - -typedef union { - struct { char* data; sstr_size_t size, ncap; } lon; - struct { char data[sizeof(sstr_rep_t)]; } sso; -} sstr; - -/**************************** PRIVATE API **********************************/ - -enum { SSO_CAP = offsetof(sstr, lon.ncap) + sizeof((sstr){{0}}.lon.ncap) - 1 }; -#define sstr_is_long(s) (bool)((s)->sso.data[SSO_CAP] & 128) -#define sstr_select_(s, memb) (sstr_is_long(s) ? sstr_l_##memb : sstr_s_##memb) - -#define sstr_s_cap(s) SSO_CAP -#define sstr_s_size(s) ((sstr_size_t)(SSO_CAP - (s)->sso.data[SSO_CAP])) -#define sstr_s_set_size(s, len) ((s)->sso.data[SSO_CAP] = SSO_CAP - (len), (s)->sso.data[len] = 0) -#define sstr_s_data(s) (s)->sso.data -#define sstr_s_end(s) ((s)->sso.data + sstr_s_size(s)) - -#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ -#define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8) -#define sstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) -#define sstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1)) -#else -#define sstr_l_cap(s) (~(s)->lon.ncap) -#define sstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap)) -#endif -#define sstr_l_size(s) ((s)->lon.size) -#define sstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0) -#define sstr_l_data(s) (s)->lon.data -#define sstr_l_end(s) ((s)->lon.data + sstr_l_size(s)) -#define sstr_l_del(s) free((s)->lon.data) - -STC_API char* sstr_init_(sstr* self, sstr_size_t len, sstr_size_t cap); -STC_API void sstr_internal_move_(sstr* self, size_t pos1, size_t pos2); - -STC_INLINE void sstr_set_size_(sstr* self, sstr_size_t len) { - sstr_select_(self, set_size(self, len)); -} - -STC_INLINE sstr_rep_t sstr_rep_(sstr* self) { - return sstr_is_long(self) - ? c_make(sstr_rep_t){self->lon.data, sstr_l_size(self), sstr_l_cap(self)} - : c_make(sstr_rep_t){self->sso.data, sstr_s_size(self), sstr_s_cap(self)}; -} - -/**************************** PUBLIC API **********************************/ - -#define sstr_lit(lit) sstr_from_n(lit, sizeof((sstr_literal_t){lit}) - 1) -#define sstr_npos (~(sstr_size_t)0 >> 1) - -STC_API char* sstr_reserve(sstr* self, sstr_size_t cap); -STC_API void sstr_shrink_to_fit(sstr* self); -STC_API void sstr_resize(sstr* self, sstr_size_t size, char value); -STC_API char* strnstrn(const char *s, const char *needle, size_t slen, size_t nlen); -STC_API sstr_size_t sstr_find_n(sstr s, const char* needle, sstr_size_t pos, sstr_size_t nmax); -STC_API void sstr_assign_n(sstr* self, const char* str, sstr_size_t n); -STC_API void sstr_append_n(sstr* self, const char* str, sstr_size_t n); -STC_API bool sstr_getdelim(sstr *self, int delim, FILE *fp); -STC_API void sstr_erase_n(sstr* self, size_t pos, size_t n); - - -STC_INLINE sstr sstr_init(void) { - sstr s; - sstr_s_set_size(&s, 0); - return s; -} - -STC_INLINE sstr sstr_from_n(const char* str, sstr_size_t n) { - sstr s; - memcpy(sstr_init_(&s, n, n), str, n); - return s; -} - -STC_INLINE sstr sstr_from(const char* str) { - return sstr_from_n(str, strlen(str)); -} - -STC_INLINE sstr sstr_with_size(sstr_size_t size, char value) { - sstr s; - memset(sstr_init_(&s, size, size), value, size); - return s; -} - -STC_INLINE sstr sstr_with_capacity(sstr_size_t cap) { - sstr s; - sstr_init_(&s, 0, cap); - return s; -} - -STC_INLINE sstr sstr_clone(sstr s) { - sstr_rep_t rep = sstr_rep_(&s); - return sstr_from_n(rep.data, rep.size); -} - -STC_INLINE void sstr_del(sstr* self) { - if (sstr_is_long(self)) sstr_l_del(self); -} - -STC_INLINE void sstr_clear(sstr* self) { - sstr_set_size_(self, 0); -} - -STC_INLINE char* sstr_data(sstr* self) { - return sstr_select_(self, data(self)); -} - -STC_INLINE const char* sstr_str(const sstr* self) { - return sstr_select_(self, data(self)); -} - -STC_INLINE bool sstr_empty(sstr s) { - return sstr_select_(&s, size(&s)) == 0; -} - -STC_INLINE sstr_size_t sstr_size(sstr s) { - return sstr_select_(&s, size(&s)); -} - -STC_INLINE sstr_size_t sstr_length(sstr s) { - return sstr_select_(&s, size(&s)); -} - -STC_INLINE sstr_size_t sstr_capacity(sstr s) { - return sstr_select_(&s, cap(&s)); -} - -STC_INLINE bool sstr_equals(sstr s1, const char* str) { - return strcmp(sstr_str(&s1), str) == 0; -} - -STC_INLINE bool sstr_equals_s(sstr s1, sstr s2) { - return strcmp(sstr_str(&s1), sstr_str(&s2)) == 0; -} - -STC_INLINE bool sstr_equalto(const sstr* s1, const sstr* s2) { - return strcmp(sstr_str(s1), sstr_str(s2)) == 0; -} - -STC_INLINE int sstr_compare(const sstr* s1, const sstr* s2) { - return strcmp(sstr_str(s1), sstr_str(s2)); -} - -STC_INLINE sstr_size_t sstr_find(sstr s, const char* needle) { - const char *str = sstr_str(&s), *res = strstr(str, needle); - return res ? res - str : sstr_npos; -} - -STC_INLINE bool sstr_find_s(sstr s, sstr needle) { - return sstr_find(s, sstr_str(&needle)); -} - -STC_INLINE bool sstr_contains(sstr s, const char* needle) { - return strstr(sstr_str(&s), needle) != NULL; -} - -STC_INLINE bool sstr_contains_s(sstr s, sstr needle) { - return strstr(sstr_str(&s), sstr_str(&needle)) != NULL; -} - -STC_INLINE bool sstr_starts_with(sstr s, const char* sub) { - const char* str = sstr_str(&s); - while (*sub && *str == *sub) ++str, ++sub; - return *sub == 0; -} - -STC_INLINE bool sstr_starts_with_s(sstr s, sstr sub) { - return sstr_starts_with(s, sstr_str(&sub)); -} - -STC_INLINE bool sstr_ends_with(sstr s, const char* sub) { - sstr_rep_t rep = sstr_rep_(&s); sstr_size_t n = strlen(sub); - return n <= rep.size && memcmp(rep.data + rep.size - n, sub, n) == 0; -} - -STC_INLINE bool sstr_ends_with_s(sstr s, sstr sub) { - return sstr_ends_with(s, sstr_str(&sub)); -} - -STC_INLINE void sstr_assign(sstr* self, const char* str) { - sstr_assign_n(self, str, strlen(str)); -} - -STC_INLINE void sstr_copy(sstr* self, sstr s) { - sstr_rep_t rep = sstr_rep_(&s); - sstr_assign_n(self, rep.data, rep.size); -} - -STC_INLINE void sstr_append(sstr* self, const char* str) { - sstr_append_n(self, str, strlen(str)); -} - -STC_INLINE void sstr_append_s(sstr* self, sstr s) { - sstr_rep_t rep = sstr_rep_(&s); - sstr_append_n(self, rep.data, rep.size); -} - -STC_INLINE void sstr_replace_n(sstr* self, size_t pos, size_t len, const char* str, size_t n) { - sstr_internal_move_(self, pos + len, pos + n); - memcpy(&sstr_data(self)[pos], str, n); -} - -STC_INLINE void sstr_replace(sstr* self, size_t pos, size_t len, const char* str) { - sstr_replace_n(self, pos, len, str, strlen(str)); -} - -STC_INLINE void sstr_replace_s(sstr* self, size_t pos, size_t len, sstr s) { - sstr_rep_t rep = sstr_rep_(&s); - sstr_replace_n(self, pos, len, rep.data, rep.size); -} - -STC_INLINE void sstr_insert_n(sstr* self, size_t pos, const char* str, size_t n) { - sstr_replace_n(self, pos, 0, str, n); -} - -STC_INLINE void sstr_insert(sstr* self, size_t pos, const char* str) { - sstr_replace_n(self, pos, 0, str, strlen(str)); -} - -STC_INLINE void sstr_insert_s(sstr* self, size_t pos, sstr s) { - sstr_rep_t rep = sstr_rep_(&s); - sstr_replace_n(self, pos, 0, rep.data, rep.size); -} - -STC_INLINE bool sstr_getline(sstr *self, FILE *fp) { - return sstr_getdelim(self, '\n', fp); -} - -/* -------------------------- IMPLEMENTATION ------------------------- */ - -#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) - -STC_DEF void sstr_internal_move_(sstr* self, size_t pos1, size_t pos2) { - if (pos1 == pos2) - return; - sstr_rep_t rep = sstr_rep_(self); - sstr_size_t newlen = rep.size + pos2 - pos1; - if (newlen > rep.cap) - rep.data = sstr_reserve(self, (rep.size*3 >> 1) + pos2 - pos1); - memmove(&rep.data[pos2], &rep.data[pos1], rep.size - pos1); - sstr_set_size_(self, newlen); -} - -STC_DEF char* sstr_init_(sstr* self, sstr_size_t len, sstr_size_t cap) { - if (cap > SSO_CAP) { - self->lon.data = (char *)malloc(cap + 1); - sstr_l_set_size(self, len); - sstr_l_set_cap(self, cap); - return self->lon.data; - } - sstr_s_set_size(self, len); - return self->sso.data; -} - -STC_DEF void sstr_shrink_to_fit(sstr* self) { - sstr_rep_t rep = sstr_rep_(self); - if (rep.size == rep.cap) - return; - if (rep.size > SSO_CAP) { - self->lon.data = (char *)realloc(self->lon.data, sstr_l_size(self) + 1); - sstr_l_set_cap(self, sstr_l_size(self)); - } else if (rep.cap > SSO_CAP) { - memcpy(self->sso.data, rep.data, rep.size + 1); - sstr_s_set_size(self, rep.size); - free(rep.data); - } -} - -STC_DEF char* sstr_reserve(sstr* self, sstr_size_t cap) { - if (sstr_is_long(self)) { - if (cap > sstr_l_cap(self)) { - self->lon.data = (char *)realloc(self->lon.data, cap + 1); - sstr_l_set_cap(self, cap); - } - return self->lon.data; - } - /* from short to long: */ - if (cap > sstr_s_cap(self)) { - char* data = (char *)malloc(cap + 1); - sstr_size_t len = sstr_s_size(self); - memcpy(data, self->sso.data, len); - self->lon.data = data; - sstr_l_set_size(self, len); - sstr_l_set_cap(self, cap); - return data; - } - return self->sso.data; -} - -STC_DEF void sstr_resize(sstr* self, sstr_size_t size, char value) { - sstr_rep_t rep = sstr_rep_(self); - if (size > rep.size) { - if (size > rep.cap) rep.data = sstr_reserve(self, size); - memset(rep.data + rep.size, value, size - rep.size); - } - sstr_set_size_(self, size); -} - -STC_DEF char* strnstrn(const char *s, const char *needle, size_t slen, size_t nlen) { - if (!nlen) return (char *)s; - if (nlen > slen) return NULL; - slen -= nlen; - do { - if (*s == *needle && !memcmp(s, needle, nlen)) return (char *)s; - ++s; - } while (slen--); - return NULL; -} - -STC_DEF sstr_size_t -sstr_find_n(sstr s, const char* needle, sstr_size_t pos, sstr_size_t nmax) { - sstr_rep_t rep = sstr_rep_(&s); - sstr_size_t nlen = (sstr_size_t) strlen(needle); - if (pos > rep.size) return sstr_npos; - char* res = strnstrn(rep.data + pos, needle, rep.size, nmax < nlen ? nmax : nlen); - return res ? res - rep.data : sstr_npos; -} - -STC_DEF void sstr_assign_n(sstr* self, const char* str, sstr_size_t n) { - sstr_rep_t rep = sstr_rep_(self); - if (n > rep.cap) { - rep.data = (char *)realloc(sstr_is_long(self) ? rep.data : NULL, n + 1); - sstr_l_set_cap(self, n); - } - memmove(rep.data, str, n); - sstr_set_size_(self, n); -} - -STC_DEF void sstr_append_n(sstr* self, const char* str, sstr_size_t n) { - sstr_rep_t rep = sstr_rep_(self); - if (rep.size + n > rep.cap) { - sstr_size_t off = (sstr_size_t)(str - rep.data); /* handle self append */ - rep.data = sstr_reserve(self, (rep.size*3 >> 1) + n); - if (off <= rep.size) str = rep.data + off; - } - memcpy(rep.data + rep.size, str, n); - sstr_set_size_(self, rep.size + n); -} - -STC_DEF bool sstr_getdelim(sstr *self, int delim, FILE *fp) { - int c = fgetc(fp); - if (c == EOF) - return false; - sstr_size_t pos = 0; - sstr_rep_t rep = sstr_rep_(self); - for (;;) { - if (c == delim || c == EOF) { - sstr_set_size_(self, pos); - return true; - } - if (pos == rep.cap) { - sstr_set_size_(self, pos); - rep.data = sstr_reserve(self, (rep.cap = (rep.cap*3 >> 1) + 16)); - } - rep.data[pos++] = (char) c; - c = fgetc(fp); - } -} - -STC_DEF void sstr_erase_n(sstr* self, size_t pos, size_t n) { - sstr_rep_t rep = sstr_rep_(self); - if (n > rep.size - pos) n = rep.size - pos; - memmove(&rep.data[pos], &rep.data[pos + n], rep.size - (pos + n)); - sstr_set_size_(self, rep.size - n); -} - -#endif -#endif \ No newline at end of file diff --git a/include/stc/carr2.h b/include/stc/carr2.h index d1feb90b..92964d4d 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -34,7 +34,7 @@ int main() { int w = 7, h = 5; - c_autovar (carr2_int image = carr2_int_init(w, h), carr2_int_del(&image)) + c_autovar (carr2_int image = carr2_int_init(w, h), carr2_int_drop(&image)) { int *dat = carr2_int_data(&image); for (int i = 0; i < carr2_int_size(image); ++i) @@ -63,7 +63,7 @@ _cx_deftypes(_c_carr2_types, _cx_self, i_val); STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value); STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* storage); STC_API _cx_value* _cx_memb(_release)(_cx_self* self); -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); #if !c_option(c_no_clone) STC_API _cx_self _cx_memb(_clone)(_cx_self src); STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); @@ -124,7 +124,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { STC_DEF void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } #endif @@ -135,10 +135,11 @@ STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) { return values; } -STC_DEF void _cx_memb(_del)(_cx_self* self) { +STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (!self->data) return; - for (_cx_value* p = self->data[0], *e = p + _cx_memb(_size)(*self); p != e; ++p) - i_valdel(p); + for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(*self); p != q; ) { + --q; i_valdrop(q); + } c_free(self->data[0]); /* values */ c_free(self->data); /* pointers */ } diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 4a4a6484..31df5bdf 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -34,7 +34,7 @@ int main() { int w = 7, h = 5, d = 3; - c_autovar (carr3_int image = carr3_int_init(w, h, d), carr3_int_del(&image)) + c_autovar (carr3_int image = carr3_int_init(w, h, d), carr3_int_drop(&image)) { int *dat = carr3_int_data(&image); for (int i = 0; i < carr3_int_size(image); ++i) @@ -65,7 +65,7 @@ _cx_deftypes(_c_carr3_types, _cx_self, i_val); STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value); STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage); STC_API _cx_value* _cx_memb(_release)(_cx_self* self); -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); #if !c_option(c_no_clone) STC_API _cx_self _cx_memb(_clone)(_cx_self src); STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); @@ -130,7 +130,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } #endif @@ -141,10 +141,11 @@ STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) { return values; } -STC_DEF void _cx_memb(_del)(_cx_self* self) { +STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (!self->data) return; - for (_cx_value* p = **self->data, *e = p + _cx_memb(_size)(*self); p != e; ++p) - i_valdel(p); + for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(*self); p != q; ) { + --q; i_valdrop(q); + } c_free(self->data[0][0]); /* data */ c_free(self->data); /* pointers */ } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index b3a70edc..a6ee3948 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -30,7 +30,7 @@ Similar to boost::dynamic_bitset / std::bitset #include "cbits.h" int main() { - c_autovar (cbits bset = cbits_with_size(23, true), cbits_del(&bset)) + c_autovar (cbits bset = cbits_with_size(23, true), cbits_drop(&bset)) { cbits_reset(&bset, 9); cbits_resize(&bset, 43, false); @@ -75,14 +75,14 @@ STC_API bool cbits_disjoint(cbits set, cbits other); STC_INLINE cbits cbits_init() { return c_make(cbits){NULL, 0}; } STC_INLINE cbits cbits_from(const char* s) { return cbits_from_n(s, strlen(s)); } STC_INLINE void cbits_clear(cbits* self) { self->size = 0; } -STC_INLINE void cbits_del(cbits* self) { c_free(self->data64); } +STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } STC_INLINE size_t cbits_size(cbits set) { return set.size; } #define cbits_new(literal) \ cbits_from_n(literal, sizeof c_make(c_strlit){literal} - 1) STC_INLINE cbits* cbits_take(cbits* self, cbits other) { - if (self->data64 != other.data64) {cbits_del(self); *self = other;} + if (self->data64 != other.data64) {cbits_drop(self); *self = other;} return self; } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 89bd272e..51bc81a4 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -26,7 +26,7 @@ typedef struct { cstr name, last; } Person; -Person Person_from(const char* name, const char* last) { +Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } Person Person_clone(Person p) { @@ -34,21 +34,21 @@ Person Person_clone(Person p) { p.last = cstr_clone(p.last); return p; } -void Person_del(Person* p) { - printf("del: %s %s\n", p->name.str, p->last.str); - c_del(cstr, &p->name, &p->last); +void Person_drop(Person* p) { + printf("drop: %s %s\n", p->name.str, p->last.str); + c_drop(cstr, &p->name, &p->last); } #define i_val Person -#define i_valdel Person_del +#define i_valdrop Person_drop #define i_valfrom Person_clone -#define i_opt c_no_compare // compare by .get addresses only +#define i_opt c_no_cmp // compare by .get addresses only #define i_tag prs #include int main() { - c_autovar (cbox_prs p = cbox_prs_new(Person_from("John", "Smiths")), cbox_prs_del(&p)) - c_autovar (cbox_prs q = cbox_prs_clone(p), cbox_prs_del(&q)) + c_autovar (cbox_prs p = cbox_prs_new(Person_new("John", "Smiths")), cbox_prs_drop(&p)) + c_autovar (cbox_prs q = cbox_prs_clone(p), cbox_prs_drop(&q)) { cstr_assign(&q.get->name, "Joe"); @@ -63,6 +63,7 @@ int main() { #include "ccommon.h" #include "forward.h" #include +#include #define cbox_null {NULL} #endif // CBOX_H_INCLUDED @@ -71,6 +72,7 @@ int main() { #define _i_prefix cbox_ #endif #include "template.h" +typedef i_valraw _cx_rawvalue; #if !c_option(c_is_fwd) _cx_deftypes(_c_cbox_types, _cx_self, i_val); @@ -90,8 +92,8 @@ _cx_memb(_new)(i_val val) { // destructor STC_INLINE void -_cx_memb(_del)(_cx_self* self) { - if (self->get) { i_valdel(self->get); c_free(self->get); } +_cx_memb(_drop)(_cx_self* self) { + if (self->get) { i_valdrop(self->get); c_free(self->get); } } STC_INLINE _cx_self @@ -102,19 +104,19 @@ _cx_memb(_move)(_cx_self* self) { STC_INLINE void _cx_memb(_reset)(_cx_self* self) { - _cx_memb(_del)(self); self->get = NULL; + _cx_memb(_drop)(self); self->get = NULL; } // take ownership of *p STC_INLINE void _cx_memb(_reset_with)(_cx_self* self, _cx_value* p) { - _cx_memb(_del)(self); self->get = p; + _cx_memb(_drop)(self); self->get = p; } // take ownership of val STC_INLINE void _cx_memb(_reset_new)(_cx_self* self, i_val val) { - if (self->get) { i_valdel(self->get); *self->get = val; } + if (self->get) { i_valdrop(self->get); *self->get = val; } else self->get = c_new(i_val, val); } @@ -123,11 +125,10 @@ _cx_memb(_reset_new)(_cx_self* self, i_val val) { _cx_memb(_from)(i_valraw raw) { return c_make(_cx_self){c_new(i_val, i_valfrom(raw))}; } - - STC_INLINE void - _cx_memb(_reset_from)(_cx_self* self, i_valraw raw) { - _cx_memb(_reset_new)(self, i_valfrom(raw)); - } + STC_INLINE i_valraw + _cx_memb(_toraw)(const _cx_self* self) { + return i_valto(self->get); + } STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { @@ -145,30 +146,32 @@ _cx_memb(_reset_new)(_cx_self* self, i_val val) { STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self other) { - if (other.get != self->get) _cx_memb(_del)(self); + if (other.get != self->get) _cx_memb(_drop)(self); *self = other; } STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self, size_t n) { - #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32 + #if c_option(c_no_cmp) && SIZE_MAX >> 32 return c_hash64(&self->get, 8); - #elif (c_option(c_no_compare) || defined _i_default_compare) + #elif c_option(c_no_cmp) return c_hash32(&self->get, 4); #else - i_valraw raw = i_valto(self->get); - return i_hash(&raw, sizeof raw); + return i_hash(self->get, sizeof *self->get); #endif } STC_INLINE int -_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) { - #if (c_option(c_no_compare) || defined _i_default_compare) - return (int)(x->get - y->get); +_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { + #if c_option(c_no_cmp) + return c_default_cmp(&x->get, &y->get); #else - i_valraw rx = i_valto(x->get); - i_valraw ry = i_valto(y->get); - return i_cmp(&rx, &ry); + return i_cmp(x->get, y->get); #endif } -#include "template.h" \ No newline at end of file + +STC_INLINE bool +_cx_memb(_equalto)(const _cx_self* x, const _cx_self* y) { + return !_cx_memb(_cmp)(x, y); +} +#include "template.h" diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 2942873f..d3664077 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -93,33 +93,37 @@ #endif typedef const char c_strlit[]; -#define c_delete(T, ptr) do { T *_c_p = ptr; T##_del(_c_p); c_free(_c_p); } while (0) +#define c_delete(T, ptr) do { T *_c_p = ptr; T##_drop(_c_p); c_free(_c_p); } while (0) #define c_swap(T, x, y) do { T _c_t = x; x = y; y = _c_t; } while (0) #define c_arraylen(a) (sizeof (a)/sizeof (a)[0]) -#define c_less_compare(less, x, y) (less(y, x) - less(x, y)) +#define c_less_cmp(less, x, y) (less(y, x) - less(x, y)) #define c_default_less(x, y) (*(x) < *(y)) -#define c_default_compare(x, y) c_less_compare(c_default_less, x, y) +#define c_default_cmp(x, y) c_less_cmp(c_default_less, x, y) #define c_default_equalto(x, y) (*(x) == *(y)) #define c_memcmp_equalto(x, y) (memcmp(x, y, sizeof *(x)) == 0) -#define c_rawstr_compare(x, y) strcmp(*(x), *(y)) -#define c_rawstr_equalto(x, y) (strcmp(*(x), *(y)) == 0) -#define c_rawstr_hash(x, dummy) c_strhash(*(x)) - #define c_default_clone(x) (x) #define c_default_fromraw(x) (x) // [deprecated] #define c_default_toraw(ptr) (*(ptr)) -#define c_default_del(ptr) ((void) (ptr)) +#define c_default_drop(ptr) ((void) (ptr)) #define c_option(flag) ((i_opt) & (flag)) #define c_is_fwd 1 #define c_no_atomic 2 #define c_no_clone 4 -#define c_no_compare 8 +#define c_no_cmp 8 /* Generic algorithms */ +typedef char* c_mutstr; +typedef const char* c_rawstr; +#define c_rawstr_cmp(x, y) strcmp(*(x), *(y)) +#define c_rawstr_equalto(x, y) (strcmp(*(x), *(y)) == 0) +#define c_rawstr_hash(x, dummy) c_strhash(*(x)) +#define c_rawstr_clone(s) strcpy((char*)c_malloc(strlen(s) + 1), s) +#define c_rawstr_drop(x) c_free((char *) &**(x)) + #define _c_ROTL(x, k) (x << (k) | x >> (8*sizeof(x) - (k))) STC_INLINE uint64_t c_strhash(const char *s) { @@ -168,16 +172,16 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { #define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__) #define c_auto_2(C, a) \ - c_autovar(C a = C##_init(), C##_del(&a)) + c_autovar(C a = C##_init(), C##_drop(&a)) #define c_auto_3(C, a, b) \ c_autovar(c_EXPAND(C a = C##_init(), b = C##_init()), \ - C##_del(&b), C##_del(&a)) + C##_drop(&b), C##_drop(&a)) #define c_auto_4(C, a, b, c) \ c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \ - C##_del(&c), C##_del(&b), C##_del(&a)) + C##_drop(&c), C##_drop(&b), C##_drop(&a)) #define c_auto_5(C, a, b, c, d) \ c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \ - C##_del(&d), C##_del(&c), C##_del(&b), C##_del(&a)) + C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a)) #define c_autobuf(b, type, n) c_autobuf_N(b, type, n, 256) #define c_autobuf_N(b, type, n, BYTES) \ @@ -203,10 +207,10 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { C##_##method(_c_cx, *_c_i); \ } while (0) -#define c_del(C, ...) do { \ +#define c_drop(C, ...) do { \ C* _c_arr[] = {__VA_ARGS__}; \ for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \ - C##_del(_c_arr[_c_i]); \ + C##_drop(_c_arr[_c_i]); \ } while (0) #if defined(__SIZEOF_INT128__) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index f8cc1d52..050f9e67 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -42,7 +42,7 @@ typedef i_valraw _cx_rawvalue; STC_API _cx_self _cx_memb(_init)(void); STC_API void _cx_memb(_clear)(_cx_self* self); -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); STC_API bool _cx_memb(_expand_right_half_)(_cx_self* self, size_t idx, size_t n); #ifndef _i_queue @@ -54,9 +54,9 @@ STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* po const _cx_rawvalue* p1, const _cx_rawvalue* p2); #endif // !c_no_clone -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, i_valraw raw); -STC_API int _cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y); +STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); #endif STC_API _cx_value* _cx_memb(_push_front)(_cx_self* self, i_val value); STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2); @@ -72,7 +72,7 @@ STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } #endif STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return !cdeq_rep_(&cx)->size; } @@ -83,7 +83,7 @@ STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfro STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_valto(pval); } STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) - { i_valdel(self->data); ++self->data; --cdeq_rep_(self)->size; } + { i_valdrop(self->data); ++self->data; --cdeq_rep_(self)->size; } STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return self->data + cdeq_rep_(self)->size - 1; } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } @@ -106,7 +106,7 @@ _cx_memb(_with_capacity)(const size_t n) { STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_value* p = &self->data[--cdeq_rep_(self)->size]; - i_valdel(p); + i_valdrop(p); } STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { @@ -175,7 +175,7 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) { } #endif // !c_no_clone -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_valraw raw) { @@ -201,9 +201,9 @@ _cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, STC_INLINE void _cx_memb(_sort)(_cx_self* self) { - _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_compare)); + _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp)); } -#endif // !c_no_compare +#endif // !c_no_cmp #endif // _i_queue /* -------------------------- IMPLEMENTATION ------------------------- */ @@ -224,8 +224,9 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { struct cdeq_rep* rep = cdeq_rep_(self); if (rep->cap) { - for (_cx_value *p = self->data, *q = p + rep->size; p != q; ++p) - i_valdel(p); + for (_cx_value *p = self->data, *q = p + rep->size; p != q; ) { + --q; i_valdrop(q); + } rep->size = 0; } } @@ -242,7 +243,7 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { } STC_DEF void -_cx_memb(_del)(_cx_self* self) { +_cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); if (cdeq_rep_(self)->cap) c_free(cdeq_rep_(self)); @@ -357,7 +358,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { const size_t n = p2 - p1; if (n > 0) { _cx_value* p = p1, *end = self->data + cdeq_rep_(self)->size; - for (; p != p2; ++p) i_valdel(p); + for (; p != p2; ++p) { i_valdrop(p); } if (p1 == self->data) self->data += n; else memmove(p1, p2, (end - p2) * sizeof(i_val)); cdeq_rep_(self)->size -= n; @@ -385,7 +386,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, } #endif // !c_option(c_no_clone) -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { @@ -397,12 +398,12 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { } STC_DEF int -_cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y) { +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { i_valraw rx = i_valto(x); i_valraw ry = i_valto(y); return i_cmp(&rx, &ry); } -#endif // !c_no_compare +#endif // !c_no_cmp #endif // !_i_queue #endif // IMPLEMENTATION #include "template.h" diff --git a/include/stc/clist.h b/include/stc/clist.h index 7cd2df50..c239fdd9 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -91,15 +91,16 @@ typedef i_valraw _cx_rawvalue; STC_API size_t _clist_count(const clist_VOID* self); -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); STC_API _cx_value* _cx_memb(_push_front)(_cx_self* self, i_val value); STC_API _cx_iter _cx_memb(_insert)(_cx_self* self, _cx_iter it, i_val value); STC_API _cx_iter _cx_memb(_erase_at)(_cx_self* self, _cx_iter it); STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2); -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_API size_t _cx_memb(_remove)(_cx_self* self, i_valraw val); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw val); +STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); #endif STC_API _cx_iter _cx_memb(_splice)(_cx_self* self, _cx_iter it, _cx_self* other); STC_API _cx_self _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2); @@ -114,14 +115,13 @@ STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw) { return _cx_memb(_push_front)(self, i_valfrom(raw)); } STC_INLINE _cx_iter _cx_memb(_emplace)(_cx_self* self, _cx_iter it, i_valraw raw) { return _cx_memb(_insert)(self, it, i_valfrom(raw)); } -STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) - { return i_valfrom(raw); } STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } + STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->last == other.last) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } #endif STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; } @@ -129,8 +129,7 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return tr STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.last == NULL; } STC_INLINE size_t _cx_memb(_count)(_cx_self cx) { return _clist_count((const clist_VOID*) &cx); } -STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); } -STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_valto(pval); } +STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) { _cx_memb(_erase_after_)(self, self->last); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return &self->last->next->value; } @@ -166,7 +165,7 @@ _cx_memb(_splice_range)(_cx_self* self, _cx_iter it, return _cx_memb(_splice)(self, it, &tmp); } -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_valraw val) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val); @@ -197,7 +196,7 @@ _cx_memb(_clone)(_cx_self cx) { #endif STC_DEF void -_cx_memb(_del)(_cx_self* self) { +_cx_memb(_drop)(_cx_self* self) { while (self->last) _cx_memb(_erase_after_)(self, self->last); } @@ -250,7 +249,7 @@ _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node) { node->next = next; if (del == next) self->last = node = NULL; else if (self->last == del) self->last = node, node = NULL; - i_valdel(&del->value); c_free(del); + i_valdrop(&del->value); c_free(del); return node; } @@ -280,7 +279,7 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) { return cx; } -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw val) { @@ -321,7 +320,14 @@ _cx_memb(_sort)(_cx_self* self) { if (self->last) self->last = (_cx_node *) _clist_mergesort((clist_VOID_node *) self->last->next, _cx_memb(_sort_cmp_)); } -#endif // !c_no_compare + +STC_DEF int +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { + i_valraw rx = i_valto(x); + i_valraw ry = i_valto(y); + return i_cmp(&rx, &ry); +} +#endif // !c_no_cmp #endif // TEMPLATE IMPLEMENTATION @@ -336,7 +342,7 @@ _clist_count(const clist_VOID* self) { return n; } -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) // Singly linked list Mergesort implementation by Simon Tatham. O(n*log n). // https://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html @@ -387,7 +393,7 @@ _clist_mergesort(clist_VOID_node *list, int (*cmp)(const clist_VOID_node*, const insize *= 2; } } -#endif // !c_no_compare +#endif // !c_no_cmp #endif // NON-TEMPLATE IMPLEMENTATION #include "template.h" #define CLIST_H_INCLUDED diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 3d1915d3..10e9c586 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -31,7 +31,7 @@ #include int main(void) { - c_autovar (cmap_ichar m = cmap_ichar_init(), cmap_ichar_del(&m)) + c_autovar (cmap_ichar m = cmap_ichar_init(), cmap_ichar_drop(&m)) { cmap_ichar_emplace(&m, 5, 'a'); cmap_ichar_emplace(&m, 8, 'b'); @@ -91,7 +91,7 @@ STC_API _cx_self _cx_memb(_with_capacity)(size_t cap); #if !c_option(c_no_clone) STC_API _cx_self _cx_memb(_clone)(_cx_self map); #endif -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t capacity); STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr); @@ -121,7 +121,7 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) return _cx_memb(_insert_or_assign)(self, key, mapped); } - STC_INLINE const _cx_mapped* + STC_INLINE _cx_mapped* _cx_memb(_at)(const _cx_self* self, i_keyraw rkey) { chash_bucket_t b = _cx_memb(_bucket_)(self, &rkey); assert(self->_hashx[b.idx]); @@ -132,13 +132,14 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) #if !c_option(c_no_clone) STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->table == other.table) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -STC_INLINE void -_cx_memb(_value_clone)(_cx_value* _dst, _cx_value* _val) { - *_i_keyref(_dst) = i_keyfrom(i_keyto(_i_keyref(_val))); - _i_MAP_ONLY( _dst->second = i_valfrom(i_valto(&_val->second)); ) +STC_INLINE _cx_value +_cx_memb(_value_clone)(_cx_value _val) { + *_i_keyref(&_val) = i_keyfrom(i_keyto(_i_keyref(&_val))); + _i_MAP_ONLY( _val.second = i_valfrom(i_valto(&_val.second)); ) + return _val; } STC_INLINE _cx_result @@ -159,16 +160,16 @@ _cx_memb(_value_toraw)(_cx_value* val) { } STC_INLINE void -_cx_memb(_value_del)(_cx_value* _val) { - i_keydel(_i_keyref(_val)); - _i_MAP_ONLY( i_valdel(&_val->second); ) +_cx_memb(_value_drop)(_cx_value* _val) { + i_keydrop(_i_keyref(_val)); + _i_MAP_ONLY( i_valdrop(&_val->second); ) } STC_INLINE _cx_result _cx_memb(_insert)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { _cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto(&_key)); if (_res.inserted) { *_i_keyref(_res.ref) = _key; _i_MAP_ONLY( _res.ref->second = _mapped; )} - else { i_keydel(&_key); _i_MAP_ONLY( i_valdel(&_mapped); )} + else { i_keydrop(&_key); _i_MAP_ONLY( i_valdrop(&_mapped); )} return _res; } @@ -249,10 +250,10 @@ STC_INLINE void _cx_memb(_wipe_)(_cx_self* self) { if (self->size == 0) return; _cx_value* e = self->table, *end = e + self->bucket_count; uint8_t *hx = self->_hashx; - for (; e != end; ++e) if (*hx++) _cx_memb(_value_del)(e); + for (; e != end; ++e) if (*hx++) _cx_memb(_value_drop)(e); } -STC_DEF void _cx_memb(_del)(_cx_self* self) { +STC_DEF void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_wipe_)(self); c_free(self->_hashx); c_free((void *) self->table); @@ -269,7 +270,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) { _cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto(&_key)); if (_res.inserted) _res.ref->first = _key; - else { i_keydel(&_key); i_valdel(&_res.ref->second); } + else { i_keydrop(&_key); i_valdrop(&_res.ref->second); } _res.ref->second = _mapped; return _res; } #if !c_option(c_no_clone) @@ -277,7 +278,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); if (_res.inserted) _res.ref->first = i_keyfrom(rkey); - else i_valdel(&_res.ref->second); + else i_valdrop(&_res.ref->second); _res.ref->second = i_valfrom(rmapped); return _res; } #endif @@ -323,7 +324,7 @@ _cx_memb(_clone)(_cx_self m) { }; _cx_value *e = m.table, *end = e + m.bucket_count, *dst = clone.table; for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst) - if (*hx) _cx_memb(_value_clone)(dst, e); + if (*hx) *dst = _cx_memb(_value_clone)(*e); return clone; } #endif @@ -363,7 +364,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { const _cx_size _cap = self->bucket_count; _cx_value* _slot = self->table; uint8_t* _hashx = self->_hashx; - _cx_memb(_value_del)(&_slot[i]); + _cx_memb(_value_drop)(&_slot[i]); for (;;) { /* delete without leaving tombstone */ if (++j == _cap) j = 0; if (! _hashx[j]) diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 49aa2406..87be1e73 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -55,7 +55,7 @@ STC_INLINE bool _cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { if (!_cx_memb(_reserve)(self, len)) return false; const size_t n = self->size; - for (size_t i = len; i < n; ++i) i_valdel(&self->data[i]); + for (size_t i = len; i < n; ++i) { i_valdrop(&self->data[i]); } for (size_t i = n; i < len; ++i) self->data[i] = null; self->size = len; return true; @@ -71,10 +71,10 @@ STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { STC_INLINE void _cx_memb(_clear)(_cx_self* self) { size_t i = self->size; self->size = 0; - while (i--) i_valdel(&self->data[i]); + while (i--) { i_valdrop(&self->data[i]); } } -STC_INLINE void _cx_memb(_del)(_cx_self* self) +STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); c_free(self->data); } STC_INLINE size_t _cx_memb(_size)(_cx_self q) @@ -97,7 +97,7 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self q); STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_value val) @@ -139,7 +139,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { STC_DEF void _cx_memb(_erase_at)(_cx_self* self, size_t idx) { - i_valdel(&self->data[idx]); + i_valdrop(&self->data[idx]); size_t n = --self->size; self->data[idx] = self->data[n]; _cx_memb(_sift_down_)(self->data - 1, idx + 1, n); diff --git a/include/stc/cset.h b/include/stc/cset.h index d74a8ccc..91e703da 100644 --- a/include/stc/cset.h +++ b/include/stc/cset.h @@ -35,7 +35,7 @@ int main(void) { c_foreach (i, cset_sx, s) printf("set %d\n", *i.ref); - cset_sx_del(&s); + cset_sx_drop(&s); } */ diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 101e7399..6ba220ab 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -32,7 +32,7 @@ #include int main(void) { - c_autovar (csmap_sx m = csmap_sx_init(), csmap_sx_del(&m)) + c_autovar (csmap_sx m = csmap_sx_init(), csmap_sx_drop(&m)) { csmap_sx_emplace(&m, "Testing one", 1.234); csmap_sx_emplace(&m, "Testing two", 12.34); @@ -97,7 +97,7 @@ STC_API _cx_self _cx_memb(_init)(void); #if !c_option(c_no_clone) STC_API _cx_self _cx_memb(_clone)(_cx_self tree); #endif -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, i_keyraw rkey, _cx_iter* out); STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, i_keyraw rkey); @@ -112,7 +112,7 @@ STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE bool _cx_memb(_empty)(_cx_self tree) { return _csmap_rep(&tree)->size == 0; } STC_INLINE size_t _cx_memb(_size)(_cx_self tree) { return _csmap_rep(&tree)->size; } STC_INLINE size_t _cx_memb(_capacity)(_cx_self tree) { return _csmap_rep(&tree)->cap; } -STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); *self = _cx_memb(_init)(); } +STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); *self = _cx_memb(_init)(); } STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_self, *a, *b); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey) { _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it) != NULL; } @@ -134,10 +134,16 @@ _cx_memb(_value_toraw)(_cx_value* val) { _i_MAP_ONLY( c_make(_cx_rawvalue){i_keyto(&val->first), i_valto(&val->second)} ); } +STC_INLINE int +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { + _cx_rawkey rx = i_keyto(_i_keyref(x)), ry = i_keyto(_i_keyref(y)); + return i_cmp(&rx, &ry); +} + STC_INLINE void -_cx_memb(_value_del)(_cx_value* val) { - i_keydel(_i_keyref(val)); - _i_MAP_ONLY( i_valdel(&val->second); ) +_cx_memb(_value_drop)(_cx_value* val) { + i_keydrop(_i_keyref(val)); + _i_MAP_ONLY( i_valdrop(&val->second); ) } #ifndef _i_isset @@ -150,7 +156,7 @@ _cx_memb(_value_del)(_cx_value* val) { _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped) { return _cx_memb(_insert_or_assign)(self, key, mapped); } - STC_INLINE const _cx_mapped* + STC_INLINE _cx_mapped* _cx_memb(_at)(const _cx_self* self, i_keyraw rkey) { _cx_iter it; return &_cx_memb(_find_it)(self, rkey, &it)->second; } #endif @@ -166,13 +172,14 @@ _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) { STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->nodes == other.nodes) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -STC_INLINE void -_cx_memb(_value_clone)(_cx_value* dst, _cx_value* val) { - *_i_keyref(dst) = i_keyfrom(i_keyto(_i_keyref(val))); - _i_MAP_ONLY( dst->second = i_valfrom(i_valto(&val->second)); ) +STC_INLINE _cx_value +_cx_memb(_value_clone)(_cx_value _val) { + *_i_keyref(&_val) = i_keyfrom(i_keyto(_i_keyref(&_val))); + _i_MAP_ONLY( _val.second = i_valfrom(i_valto(&_val.second)); ) + return _val; } STC_INLINE _cx_result @@ -190,7 +197,7 @@ STC_INLINE _cx_result _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped)) { _cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key)); if (res.inserted) { *_i_keyref(res.ref) = key; _i_MAP_ONLY( res.ref->second = mapped; )} - else { i_keydel(&key); _i_MAP_ONLY( i_valdel(&mapped); )} + else { i_keydrop(&key); _i_MAP_ONLY( i_valdrop(&mapped); )} return res; } @@ -279,7 +286,7 @@ _cx_memb(_node_new_)(_cx_self* self, int level) { _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped) { _cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key)); if (res.inserted) res.ref->first = key; - else { i_keydel(&key); i_valdel(&res.ref->second); } + else { i_keydrop(&key); i_valdrop(&res.ref->second); } res.ref->second = mapped; return res; } #if !c_option(c_no_clone) @@ -287,7 +294,7 @@ _cx_memb(_node_new_)(_cx_self* self, int level) { _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { _cx_result res = _cx_memb(_insert_entry_)(self, rkey); if (res.inserted) res.ref->first = i_keyfrom(rkey); - else i_valdel(&res.ref->second); + else i_valdrop(&res.ref->second); res.ref->second = i_valfrom(rmapped); return res; } #endif @@ -404,7 +411,7 @@ _cx_memb(_erase_r_)(_cx_node *d, _cx_size tn, const _cx_rawkey* rkey, int *erase d[tn].link[c < 0] = _cx_memb(_erase_r_)(d, d[tn].link[c < 0], rkey, erased); else { if (!(*erased)++) - _cx_memb(_value_del)(&d[tn].value); + _cx_memb(_value_drop)(&d[tn].value); if (d[tn].link[0] && d[tn].link[1]) { tx = d[tn].link[0]; while (d[tx].link[1]) @@ -470,7 +477,7 @@ STC_DEF _cx_size _cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, _cx_size sn) { if (sn == 0) return 0; _cx_size tx, tn = _cx_memb(_node_new_)(self, src[sn].level); - _cx_memb(_value_clone)(&self->nodes[tn].value, &src[sn].value); + self->nodes[tn].value = _cx_memb(_value_clone)(src[sn].value); tx = _cx_memb(_clone_r_)(self, src, src[sn].link[0]); self->nodes[tn].link[0] = tx; tx = _cx_memb(_clone_r_)(self, src, src[sn].link[1]); self->nodes[tn].link[1] = tx; return tn; @@ -491,12 +498,12 @@ _cx_memb(_del_r_)(_cx_node* d, _cx_size tn) { if (tn) { _cx_memb(_del_r_)(d, d[tn].link[0]); _cx_memb(_del_r_)(d, d[tn].link[1]); - _cx_memb(_value_del)(&d[tn].value); + _cx_memb(_value_drop)(&d[tn].value); } } STC_DEF void -_cx_memb(_del)(_cx_self* self) { +_cx_memb(_drop)(_cx_self* self) { if (_csmap_rep(self)->root) { _cx_memb(_del_r_)(self->nodes, (_cx_size) _csmap_rep(self)->root); c_free(_csmap_rep(self)); diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 4753ef12..b66cd4d7 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -26,25 +26,25 @@ typedef struct { cstr name, last; } Person; -Person Person_from(const char* name, const char* last) { +Person Person_new(const char* name, const char* last) { return (Person){.name = cstr_from(name), .last = cstr_from(last)}; } -void Person_del(Person* p) { - printf("del: %s %s\n", p->name.str, p->last.str); - c_del(cstr, &p->name, &p->last); +void Person_drop(Person* p) { + printf("drop: %s %s\n", p->name.str, p->last.str); + c_drop(cstr, &p->name, &p->last); } #define i_tag person #define i_val Person -#define i_valdel Person_del +#define i_valdrop Person_drop #include int main() { - csptr_person p = csptr_person_new(Person_from("John", "Smiths")); + csptr_person p = csptr_person_new(Person_new("John", "Smiths")); csptr_person q = csptr_person_clone(p); // share the pointer printf("%s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count); - c_del(csptr_person, &p, &q); + c_drop(csptr_person, &p, &q); } */ @@ -76,6 +76,7 @@ int main() { #endif #define _i_has_internal_clone #include "template.h" +typedef i_valraw _cx_rawvalue; #if !c_option(c_no_atomic) #define _i_atomic_inc(v) c_atomic_inc(v) @@ -132,9 +133,9 @@ _cx_memb(_move)(_cx_self* self) { } STC_INLINE void -_cx_memb(_del)(_cx_self* self) { +_cx_memb(_drop)(_cx_self* self) { if (self->use_count && _i_atomic_dec_and_test(self->use_count)) { - i_valdel(self->get); + i_valdrop(self->get); if (self->get != &((_cx_csptr_rep *)self->use_count)->value) c_free(self->get); c_free(self->use_count); @@ -143,19 +144,19 @@ _cx_memb(_del)(_cx_self* self) { STC_INLINE void _cx_memb(_reset)(_cx_self* self) { - _cx_memb(_del)(self); + _cx_memb(_drop)(self); self->use_count = NULL, self->get = NULL; } STC_INLINE void _cx_memb(_reset_with)(_cx_self* self, _cx_value* p) { - _cx_memb(_del)(self); + _cx_memb(_drop)(self); *self = _cx_memb(_with)(p); } STC_INLINE void _cx_memb(_reset_new)(_cx_self* self, i_val val) { - _cx_memb(_del)(self); + _cx_memb(_drop)(self); *self = _cx_memb(_new)(val); } @@ -164,47 +165,48 @@ _cx_memb(_reset_new)(_cx_self* self, i_val val) { return _cx_memb(_new)(i_valfrom(raw)); } - STC_INLINE void - _cx_memb(_reset_from)(_cx_self* self, i_valraw raw) { - _cx_memb(_del)(self); - *self = _cx_memb(_new)(i_valfrom(raw)); + STC_INLINE i_valraw + _cx_memb(_toraw)(const _cx_self* self) { + return i_valto(self->get); } #endif STC_INLINE void _cx_memb(_copy)(_cx_self* self, _cx_self ptr) { if (ptr.use_count) _i_atomic_inc(ptr.use_count); - _cx_memb(_del)(self); *self = ptr; + _cx_memb(_drop)(self); *self = ptr; } STC_INLINE void _cx_memb(_take)(_cx_self* self, _cx_self ptr) { - if (self->get != ptr.get) _cx_memb(_del)(self); + if (self->get != ptr.get) _cx_memb(_drop)(self); *self = ptr; } STC_INLINE uint64_t _cx_memb(_hash)(const _cx_self* self, size_t n) { - #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32 + #if c_option(c_no_cmp) && SIZE_MAX >> 32 return c_hash64(&self->get, 8); - #elif (c_option(c_no_compare) || defined _i_default_compare) + #elif c_option(c_no_cmp) return c_hash32(&self->get, 4); #else - i_valraw raw = i_valto(self->get); - return i_hash(&raw, sizeof raw); + return i_hash(self->get, sizeof *self->get); #endif } STC_INLINE int -_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) { - #if (c_option(c_no_compare) || defined _i_default_compare) - return (int)(x->get - y->get); +_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { + #if c_option(c_no_cmp) + return c_default_cmp(&x->get, &y->get); #else - i_valraw rx = i_valto(x->get); - i_valraw ry = i_valto(y->get); - return i_cmp(&rx, &ry); + return i_cmp(x->get, y->get); #endif } + +STC_INLINE bool +_cx_memb(_equalto)(const _cx_self* x, const _cx_self* y) { + return !_cx_memb(_cmp)(x, y); +} #undef _i_atomic_inc #undef _i_atomic_dec_and_test -#include "template.h" \ No newline at end of file +#include "template.h" diff --git a/include/stc/csset.h b/include/stc/csset.h index e4558192..07cf4b17 100644 --- a/include/stc/csset.h +++ b/include/stc/csset.h @@ -38,7 +38,7 @@ int main(void) { c_foreach (k, csset_i, s) printf("set %d\n", *k.ref); - csset_i_del(&s); + csset_i_drop(&s); } */ diff --git a/include/stc/cstack.h b/include/stc/cstack.h index c1170f79..d43afe29 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -54,11 +54,11 @@ STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val fill) { STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_value *p = self->data + self->size; - while (p-- != self->data) i_valdel(p); + while (p-- != self->data) { i_valdrop(p); } self->size = 0; } -STC_INLINE void _cx_memb(_del)(_cx_self* self) +STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); c_free(self->data); } STC_INLINE size_t _cx_memb(_size)(_cx_self v) @@ -74,7 +74,7 @@ STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) { return &self->data[self->size - 1]; } STC_INLINE void _cx_memb(_pop)(_cx_self* self) - { _cx_value* p = &self->data[--self->size]; i_valdel(p); } + { _cx_value* p = &self->data[--self->size]; i_valdrop(p); } STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { if (n < self->size) return true; @@ -107,7 +107,7 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) diff --git a/include/stc/cstr.h b/include/stc/cstr.h index a46de3c3..730f4d16 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -68,6 +68,7 @@ STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nma STC_INLINE cstr cstr_init() { return cstr_null; } #define cstr_str(self) (self)->str +#define cstr_toraw(self) (self)->str #define cstr_new(literal) \ cstr_from_n(literal, sizeof c_make(c_strlit){literal} - 1) STC_INLINE cstr cstr_from(const char* str) @@ -77,7 +78,7 @@ STC_INLINE size_t cstr_size(cstr s) { return _cstr_rep(&s)->size; } STC_INLINE size_t cstr_length(cstr s) { return _cstr_rep(&s)->size; } STC_INLINE size_t cstr_capacity(cstr s) { return _cstr_rep(&s)->cap; } STC_INLINE bool cstr_empty(cstr s) { return _cstr_rep(&s)->size == 0; } -STC_INLINE void cstr_del(cstr* self) +STC_INLINE void cstr_drop(cstr* self) { if (_cstr_rep(self)->cap) c_free(_cstr_rep(self)); } STC_INLINE cstr cstr_clone(cstr s) { return cstr_from_n(s.str, _cstr_rep(&s)->size); } @@ -166,7 +167,7 @@ cstr_ends_with(cstr s, const char* sub) { } /* container adaptor functions: */ -#define cstr_compare(xp, yp) strcmp((xp)->str, (yp)->str) +#define cstr_cmp(xp, yp) strcmp((xp)->str, (yp)->str) #define cstr_equalto(xp, yp) (strcmp((xp)->str, (yp)->str) == 0) #define cstr_hash(xp, dummy) c_strhash((xp)->str) @@ -249,7 +250,7 @@ cstr_printf(cstr* self, const char* fmt, ...) { va_start(args, fmt); int n = cstr_vfmt(&ret, fmt, args); va_end(args); - cstr_del(self); + cstr_drop(self); *self = ret; return n; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 872b6e1e..ac120e15 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -114,9 +114,13 @@ STC_INLINE bool cstr_ends_with_v(cstr s, csview sub) /* ---- Container helper functions ---- */ -#define csview_compare(xp, yp) strcmp((xp)->str, (yp)->str) +STC_INLINE int csview_cmp(const csview* x, const csview* y) { + const size_t m = x->size < y->size ? x->size : y->size; + const int c = memcmp(x->str, y->str, m); + return c ? c : x->size - y->size; + } #define csview_hash(xp, dummy) c_strhash((xp)->str) -#define csview_equalto(xp, yp) (strcmp((xp)->str, (yp)->str) == 0) +#define csview_equalto(xp, yp) (csview_cmp(xp, yp) == 0) /* -------------------------- IMPLEMENTATION ------------------------- */ diff --git a/include/stc/cvec.h b/include/stc/cvec.h index f64f7840..01143d25 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -46,15 +46,15 @@ struct MyStruct { int main() { cvec_i32 vec = cvec_i32_init(); cvec_i32_push_back(&vec, 123); - cvec_i32_del(&vec); + cvec_i32_drop(&vec); cvec_float fvec = cvec_float_init(); cvec_float_push_back(&fvec, 123.3); - cvec_float_del(&fvec); + cvec_float_drop(&fvec); cvec_str svec = cvec_str_init(); cvec_str_emplace_back(&svec, "Hello, friend"); - cvec_str_del(&svec); + cvec_str_drop(&svec); } */ @@ -79,7 +79,7 @@ struct cvec_rep { size_t size, cap; void* data[]; }; typedef i_valraw _cx_rawvalue; STC_API _cx_self _cx_memb(_init)(void); -STC_API void _cx_memb(_del)(_cx_self* self); +STC_API void _cx_memb(_drop)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_val null); @@ -87,8 +87,8 @@ STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value); STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2); STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -#if !c_option(c_no_compare) -STC_API int _cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y); +#if !c_option(c_no_cmp) +STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw raw); STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_valraw raw); #endif @@ -107,7 +107,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; - _cx_memb(_del)(self); *self = _cx_memb(_clone)(other); + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } STC_INLINE _cx_iter _cx_memb(_emplace)(_cx_self* self, const size_t idx, i_valraw raw) { @@ -136,7 +136,7 @@ STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->da STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return self->data + cvec_rep_(self)->size - 1; } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) - { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdel(p); } + { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { return c_make(_cx_iter){self->data}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) @@ -197,7 +197,7 @@ _cx_memb(_at)(const _cx_self* self, const size_t idx) { return self->data + idx; } -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_valraw raw) { @@ -226,9 +226,9 @@ _cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2, } STC_INLINE void _cx_memb(_sort)(_cx_self* self) { - _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_compare)); + _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp)); } -#endif // !c_no_compare +#endif // !c_no_cmp /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) @@ -246,14 +246,15 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { struct cvec_rep* rep = cvec_rep_(self); if (rep->cap) { - for (_cx_value *p = self->data, *q = p + rep->size; p != q; ++p) - i_valdel(p); + for (_cx_value *p = self->data, *q = p + rep->size; p != q; ) { + --q; i_valdrop(q); + } rep->size = 0; } } STC_DEF void -_cx_memb(_del)(_cx_self* self) { +_cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); if (cvec_rep_(self)->cap) c_free(cvec_rep_(self)); @@ -263,7 +264,7 @@ STC_DEF bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { struct cvec_rep* rep = cvec_rep_(self); const size_t len = rep->size; - if (cap > rep->cap || cap && cap == len) { + if (cap > rep->cap || (cap && cap == len)) { rep = (struct cvec_rep*) c_realloc(rep->cap ? rep : NULL, offsetof(struct cvec_rep, data) + cap*sizeof(i_val)); if (!rep) return false; @@ -279,7 +280,7 @@ _cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { if (!_cx_memb(_reserve)(self, len)) return false; struct cvec_rep *rep = cvec_rep_(self); const size_t n = rep->size; - for (size_t i = len; i < n; ++i) i_valdel(&self->data[i]); + for (size_t i = len; i < n; ++i) { i_valdrop(&self->data[i]); } for (size_t i = n; i < len; ++i) self->data[i] = null; if (rep->cap) rep->size = len; return true; @@ -320,7 +321,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { intptr_t len = p2 - p1; if (len > 0) { _cx_value* p = p1, *end = self->data + cvec_rep_(self)->size; - for (; p != p2; ++p) i_valdel(p); + for (; p != p2; ++p) { i_valdrop(p); } memmove(p1, p2, (end - p2) * sizeof(i_val)); cvec_rep_(self)->size -= len; } @@ -356,7 +357,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, } #endif -#if !c_option(c_no_compare) +#if !c_option(c_no_cmp) STC_DEF _cx_iter _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { @@ -381,12 +382,12 @@ _cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { } STC_DEF int -_cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y) { +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { i_valraw rx = i_valto(x); i_valraw ry = i_valto(y); return i_cmp(&rx, &ry); } -#endif // !c_no_compare +#endif // !c_no_cmp #endif #include "template.h" #define CVEC_H_INCLUDED diff --git a/include/stc/template.h b/include/stc/template.h index 85c4a940..61befb2b 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -40,19 +40,13 @@ #define _cx_size _cx_memb(_size_t) #endif -#if defined i_valraw && !(defined i_valto && defined i_valfrom) - #error "if i_valraw or i_valto defined, i_valfrom must be defined" -#endif -#if defined i_keyraw && !(defined i_keyto && defined i_keyfrom) - #error "if i_keyraw or i_keyto defined, i_keyfrom a must be defined" -#endif -#ifdef i_key_csptr // [deprecated] - #define i_key_ref i_key_csptr - #error "i_key_csptr no longer supported: use new name i_key_ref" +#if defined i_key_csptr || defined i_key_ref // [deprecated] + #define i_key_bind i_key_csptr + #error "i_key_csptr/ref no longer supported: use new name i_key_bind" #endif -#ifdef i_val_csptr // [deprecated] - #define i_val_ref i_val_csptr - #error "i_val_csptr no longer supported: use new name i_val_ref" +#if defined i_val_csptr || defined i_val_ref // [deprecated] + #define i_val_bind i_val_csptr + #error "i_val_sptr/ref no longer supported: use new name i_val_bind" #endif #ifdef i_cnt // [deprecated] #define i_type i_cnt @@ -69,46 +63,53 @@ #include "cstr.h" #endif -#if defined i_key_ref - #define i_key i_key_ref - #define i_keyfrom c_PASTE(i_key, _clone) - #ifndef i_cmp - #define i_cmp c_PASTE(i_key, _compare) - #endif - #ifndef i_hash - #define i_hash c_PASTE(i_key, _hash) - #endif - #ifndef i_keydel - #define i_keydel c_PASTE(i_key, _del) - #endif - -#elif defined i_key_str - - #define i_key cstr - #define i_keyfrom cstr_from - #define i_keyto cstr_str - #define i_keyraw const char* +#ifdef i_key_str + #define i_key_bind cstr + #define i_keyraw c_rawstr #ifndef i_tag #define i_tag str #endif - #ifndef i_cmp - #define i_cmp c_rawstr_compare +#endif + +#ifdef i_key_bind + #define i_key i_key_bind + #ifndef i_keyraw + #ifndef i_keyfrom + #define i_keyfrom c_PASTE(i_key, _clone) + #endif + #else + #ifndef i_keyfrom + #define i_keyfrom c_PASTE(i_key, _from) + #endif + #ifndef i_keyto + #define i_keyto c_PASTE(i_key, _toraw) + #endif + #endif + #ifndef i_cmp + #define i_cmp c_PASTE(i_keyraw, _cmp) + #endif + #ifndef i_equ + #define i_equ c_PASTE(i_keyraw, _equalto) #endif #ifndef i_hash - #define i_hash c_rawstr_hash + #define i_hash c_PASTE(i_keyraw, _hash) #endif - #if !defined i_keydel - #define i_keydel cstr_del + #ifndef i_keydrop + #define i_keydrop c_PASTE(i_key, _drop) #endif #endif -/* Resolve i_del and i_from here */ -#if defined i_del && defined i_isset - #define i_keydel i_del -#elif defined i_del && !defined i_key - #define i_valdel i_del -#elif defined i_del - #error "i_del not supported for maps, define i_keydel / i_valdel instead." +#if defined i_keyraw && !(defined i_keyto && defined i_keyfrom) + #error "if i_keyraw defined, i_keyfrom and i_keyto must be defined" +#endif + +/* Resolve i_drop and i_from here */ +#if defined i_drop && defined i_isset + #define i_keydrop i_drop +#elif defined i_drop && !defined i_key + #define i_valdrop i_drop +#elif defined i_drop + #error "i_drop not supported for maps, define i_keydrop / i_valdrop instead." #endif #if defined i_from && defined i_isset #define i_keyfrom i_from @@ -118,33 +119,40 @@ #error "i_from not supported for maps, define i_keyfrom / i_valfrom instead." #endif -#if defined i_val_ref - #define i_val i_val_ref - #define i_valfrom c_PASTE(i_val, _clone) - #if !defined i_cmp && !defined i_key - #define i_cmp c_PASTE(i_val, _compare) - #endif - #if !defined i_valdel - #define i_valdel c_PASTE(i_val, _del) +#ifdef i_val_str + #define i_val_bind cstr + #define i_valraw c_rawstr + #if !defined i_tag && !defined i_key + #define i_tag str #endif +#endif -#elif defined i_val_str - - #define i_val cstr - #define i_valfrom cstr_from - #define i_valto cstr_str - #define i_valraw const char* - #if !defined i_tag && !defined i_key - #define i_tag str +#ifdef i_val_bind + #define i_val i_val_bind + #ifndef i_valraw + #ifndef i_valfrom + #define i_valfrom c_PASTE(i_val, _clone) + #endif + #else + #ifndef i_valfrom + #define i_valfrom c_PASTE(i_val, _from) + #endif + #ifndef i_valto + #define i_valto c_PASTE(i_val, _toraw) + #endif #endif #if !defined i_cmp && !defined i_key - #define i_cmp c_rawstr_compare + #define i_cmp c_PASTE(i_valraw, _cmp) #endif - #if !defined i_valdel - #define i_valdel cstr_del + #ifndef i_valdrop + #define i_valdrop c_PASTE(i_val, _drop) #endif #endif +#if defined i_valraw && !(defined i_valto && defined i_valfrom) + #error "if i_valraw defined, i_valfrom and i_valto must be defined" +#endif + #ifdef i_key #ifdef _i_isset #define i_val i_key @@ -152,8 +160,8 @@ #ifndef i_tag #define i_tag i_key #endif - #if !defined _i_has_internal_clone && defined i_keydel && !defined i_keyfrom && !c_option(c_no_clone) - #error "i_keydel defined but not i_keyfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'" + #if !defined _i_has_internal_clone && defined i_keydrop && !defined i_keyfrom && !c_option(c_no_clone) + #error "i_keydrop defined but not i_keyfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'" #endif #if !defined i_keyfrom #define i_keyfrom c_default_clone @@ -167,8 +175,8 @@ #elif !defined i_equ #define i_equ c_default_equalto #endif - #ifndef i_keydel - #define i_keydel c_default_del + #ifndef i_keydrop + #define i_keydrop c_default_drop #endif #elif defined _i_isset #error "i_key define is missing." @@ -177,8 +185,8 @@ #ifndef i_tag #define i_tag i_val #endif -#if !defined _i_has_internal_clone && defined i_valdel && !defined i_valfrom && !c_option(c_no_clone) - #error "i_valdel/i_del defined but not i_valfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'" +#if !defined _i_has_internal_clone && defined i_valdrop && !defined i_valfrom && !c_option(c_no_clone) + #error "i_valdrop/i_drop defined but not i_valfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'" #endif #if !defined i_valfrom #define i_valfrom c_default_clone @@ -187,12 +195,11 @@ #define i_valraw i_val #define i_valto c_default_toraw #endif -#ifndef i_valdel - #define i_valdel c_default_del +#ifndef i_valdrop + #define i_valdrop c_default_drop #endif #ifndef i_cmp - #define _i_default_compare - #define i_cmp c_default_compare + #define i_cmp c_default_cmp #endif #ifndef i_hash #define i_hash c_default_hash @@ -205,29 +212,28 @@ #undef i_imp #undef i_opt #undef i_cmp -#undef i_del +#undef i_drop #undef i_equ #undef i_hash #undef i_from #undef i_val #undef i_val_str -#undef i_val_ref -#undef i_valdel +#undef i_val_bind +#undef i_valdrop #undef i_valraw #undef i_valfrom #undef i_valto #undef i_key #undef i_key_str -#undef i_key_ref -#undef i_keydel +#undef i_key_bind +#undef i_keydrop #undef i_keyraw #undef i_keyfrom #undef i_keyto #undef _i_prefix -#undef _i_default_compare #undef _i_has_internal_clone #undef _i_template #endif -- cgit v1.2.3