From d122c77b5a755cd5b3211c26346a84bb2b27cdce Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 21 Apr 2022 13:47:54 +0200 Subject: Switched to use i_key as primary template type parameter for all containers. Only maps will actually use i_val. Users can still specify i_val for non-maps, so there are no usability changes, other than the option to use i_key always, which makes the implementation and switching between container types simpler. --- include/stc/carc.h | 28 +++---- include/stc/carr2.h | 12 +-- include/stc/carr3.h | 12 +-- include/stc/cbox.h | 37 ++++----- include/stc/cdeq.h | 84 ++++++++++----------- include/stc/clist.h | 64 ++++++++-------- include/stc/cmap.h | 8 +- include/stc/cpque.h | 18 ++--- include/stc/cqueue.h | 2 +- include/stc/csmap.h | 2 + include/stc/cstack.h | 26 +++---- include/stc/cvec.h | 86 ++++++++++----------- include/stc/template.h | 201 ++++++++++++++++++++++++++----------------------- 13 files changed, 299 insertions(+), 281 deletions(-) (limited to 'include') diff --git a/include/stc/carc.h b/include/stc/carc.h index 7f9c4390..54ba3159 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -35,8 +35,8 @@ void Person_drop(Person* p) { } #define i_tag person -#define i_val Person -#define i_valdrop Person_drop +#define i_key Person +#define i_keydrop Person_drop #include int main() { @@ -75,7 +75,7 @@ int main() { #define _i_prefix carc_ #endif #include "template.h" -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; #if !c_option(c_no_atomic) #define _i_atomic_inc(v) c_atomic_inc(v) @@ -85,9 +85,9 @@ typedef i_valraw _cx_raw; #define _i_atomic_dec_and_test(v) !(--*(v)) #endif #if !c_option(c_is_fwd) -_cx_deftypes(_c_carc_types, _cx_self, i_val); +_cx_deftypes(_c_carc_types, _cx_self, i_key); #endif -_cx_carc_rep { long counter; i_val value; }; +_cx_carc_rep { long counter; i_key value; }; STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL, NULL}; } @@ -103,14 +103,14 @@ _cx_memb(_from_ptr)(_cx_value* p) { } STC_INLINE _cx_self -_cx_memb(_from)(i_val val) { // c++: std::make_shared(val) +_cx_memb(_from)(i_key val) { // c++: std::make_shared(val) _cx_self ptr; _cx_carc_rep *rep = c_alloc(_cx_carc_rep); *(ptr.use_count = &rep->counter) = 1; *(ptr.get = &rep->value) = val; return ptr; } -STC_INLINE i_val _cx_memb(_toraw)(const _cx_self* self) { +STC_INLINE i_key _cx_memb(_toraw)(const _cx_self* self) { return *self->get; } @@ -124,7 +124,7 @@ _cx_memb(_move)(_cx_self* self) { STC_INLINE void _cx_memb(_drop)(_cx_self* self) { if (self->use_count && _i_atomic_dec_and_test(self->use_count)) { - i_valdrop(self->get); + i_keydrop(self->get); if (self->get != &((_cx_carc_rep *)self->use_count)->value) c_free(self->get); c_free(self->use_count); @@ -138,17 +138,17 @@ _cx_memb(_reset)(_cx_self* self) { } STC_INLINE void -_cx_memb(_reset_from)(_cx_self* self, i_val val) { +_cx_memb(_reset_from)(_cx_self* self, i_key val) { _cx_memb(_drop)(self); *self = _cx_memb(_from)(val); } #if !defined _i_no_clone STC_INLINE _cx_self - _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } + _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); } #endif // !_i_no_clone -// does not use i_valfrom, so we can bypass c_no_clone +// does not use i_keyfrom, so we can bypass c_no_clone STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) { if (ptr.use_count) _i_atomic_inc(ptr.use_count); @@ -175,7 +175,7 @@ _cx_memb(_value_hash)(const _cx_value* x, size_t n) { #elif c_option(c_no_cmp) return c_hash32(&x, 4); #else - _cx_raw rx = i_valto(x); + _cx_raw rx = i_keyto(x); return i_hash((&rx), (sizeof rx)); #endif } @@ -185,7 +185,7 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return c_default_cmp(&x, &y); #else - _cx_raw rx = i_valto(x), ry = i_valto(y); + _cx_raw rx = i_keyto(x), ry = i_keyto(y); return i_cmp((&rx), (&ry)); #endif } @@ -195,7 +195,7 @@ _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return x == y; #else - _cx_raw rx = i_valto(x), ry = i_valto(y); + _cx_raw rx = i_keyto(x), ry = i_keyto(y); return i_eq((&rx), (&ry)); #endif } diff --git a/include/stc/carr2.h b/include/stc/carr2.h index 93ab26bc..a9e9d9f8 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -29,7 +29,7 @@ #endif /* // carr2- 2D dynamic array in one memory block with easy indexing. -#define i_val int +#define i_key int #include #include @@ -58,10 +58,10 @@ int main() { #endif #include "template.h" #if !c_option(c_is_fwd) -_cx_deftypes(_c_carr2_types, _cx_self, i_val); +_cx_deftypes(_c_carr2_types, _cx_self, i_key); #endif -STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value); +STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_key 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(_drop)(_cx_self* self); @@ -107,7 +107,7 @@ STC_DEF _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* bl return _arr; } -STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { +STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_key value) { _cx_self _arr = _cx_memb(_init)(xdim, ydim); for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p) *p = value; @@ -119,7 +119,7 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim); for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(src); p != e; ++p, ++q) - *p = i_valclone((*q)); + *p = i_keyclone((*q)); return _arr; } @@ -139,7 +139,7 @@ STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) { STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (!self->data) return; for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(*self); p != q; ) { - --q; i_valdrop(q); + --q; i_keydrop(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 a326105c..2b466b13 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -29,7 +29,7 @@ #endif /* // carr3 - 3D dynamic array in one memory block with easy indexing. -#define i_val int +#define i_key int #include #include @@ -60,10 +60,10 @@ int main() { #include "template.h" #if !c_option(c_is_fwd) -_cx_deftypes(_c_carr3_types, _cx_self, i_val); +_cx_deftypes(_c_carr3_types, _cx_self, i_key); #endif -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_values)(size_t xdim, size_t ydim, size_t zdim, i_key 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(_drop)(_cx_self* self); @@ -112,7 +112,7 @@ STC_DEF _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, return _arr; } -STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value) { +STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_key value) { _cx_self _arr = _cx_memb(_init)(xdim, ydim, zdim); for (_cx_value* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p) *p = value; @@ -124,7 +124,7 @@ STC_DEF _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_init)(src.xdim, src.ydim, src.zdim); for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(src); p != e; ++p, ++q) - *p = i_valclone((*q)); + *p = i_keyclone((*q)); return _arr; } @@ -144,7 +144,7 @@ STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) { STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (!self->data) return; for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(*self); p != q; ) { - --q; i_valdrop(q); + --q; i_keydrop(q); } c_free(self->data[0][0]); /* data */ c_free(self->data); /* pointers */ diff --git a/include/stc/cbox.h b/include/stc/cbox.h index c2ecbae4..27536c21 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -39,7 +39,7 @@ void Person_drop(Person* p) { c_drop(cstr, &p->name, &p->email); } -#define i_val_bind Person // bind Person clone+drop fn's +#define i_key_bind Person // bind Person clone+drop fn's #define i_opt c_no_cmp // compare by .get addresses only #define i_type PBox #include @@ -71,32 +71,35 @@ int main() { #define _i_prefix cbox_ #endif #include "template.h" -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; #if !c_option(c_is_fwd) -_cx_deftypes(_c_cbox_types, _cx_self, i_val); +_cx_deftypes(_c_cbox_types, _cx_self, i_key); #endif // constructors (takes ownsership) STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; } +STC_INLINE long +_cx_memb(_use_count)(_cx_self box) { return (long)(box.get != NULL); } + STC_INLINE _cx_self -_cx_memb(_from_ptr)(i_val* p) { return c_make(_cx_self){p}; } +_cx_memb(_from_ptr)(i_key* p) { return c_make(_cx_self){p}; } STC_INLINE _cx_self -_cx_memb(_from)(i_val val) { // c++: std::make_unique(val) - _cx_self ptr = {c_alloc(i_val)}; +_cx_memb(_from)(i_key val) { // c++: std::make_unique(val) + _cx_self ptr = {c_alloc(i_key)}; *ptr.get = val; return ptr; } -STC_INLINE i_val +STC_INLINE i_key _cx_memb(_toraw)(const _cx_self* self) { return *self->get; } // destructor STC_INLINE void _cx_memb(_drop)(_cx_self* self) { - if (self->get) { i_valdrop(self->get); c_free(self->get); } + if (self->get) { i_keydrop(self->get); c_free(self->get); } } STC_INLINE _cx_self @@ -112,20 +115,20 @@ _cx_memb(_reset)(_cx_self* self) { // take ownership of val STC_INLINE void -_cx_memb(_reset_from)(_cx_self* self, i_val val) { - if (self->get) { i_valdrop(self->get); *self->get = val; } - else self->get = c_new(i_val, val); +_cx_memb(_reset_from)(_cx_self* self, i_key val) { + if (self->get) { i_keydrop(self->get); *self->get = val; } + else self->get = c_new(i_key, val); } #if !defined _i_no_clone STC_INLINE _cx_self - _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_valfrom(raw)); } + _cx_memb(_make)(_cx_raw raw) { return _cx_memb(_from)(i_keyfrom(raw)); } STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { if (!other.get) return other; - i_valraw r = i_valto(other.get); - return c_make(_cx_self){c_new(i_val, i_valfrom(r))}; + i_keyraw r = i_keyto(other.get); + return c_make(_cx_self){c_new(i_key, i_keyfrom(r))}; } STC_INLINE void @@ -149,7 +152,7 @@ _cx_memb(_value_hash)(const _cx_value* x, size_t n) { #elif c_option(c_no_cmp) return c_hash32(&x, 4); #else - _cx_raw rx = i_valto(x); + _cx_raw rx = i_keyto(x); return i_hash((&rx), (sizeof rx)); #endif } @@ -159,7 +162,7 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return c_default_cmp(&x, &y); #else - _cx_raw rx = i_valto(x), ry = i_valto(y); + _cx_raw rx = i_keyto(x), ry = i_keyto(y); return i_cmp((&rx), (&ry)); #endif } @@ -169,7 +172,7 @@ _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) return x == y; #else - _cx_raw rx = i_valto(x), ry = i_valto(y); + _cx_raw rx = i_keyto(x), ry = i_keyto(y); return i_eq((&rx), (&ry)); #endif } diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 33747e02..7d6c0254 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -37,16 +37,16 @@ struct cdeq_rep { size_t size, cap; void* base[]; }; #include "template.h" #if !c_option(c_is_fwd) -_cx_deftypes(_c_cdeq_types, _cx_self, i_val); +_cx_deftypes(_c_cdeq_types, _cx_self, i_key); #endif -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); STC_API _cx_self _cx_memb(_with_capacity)(const size_t n); STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t n); STC_API void _cx_memb(_clear)(_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_back)(_cx_self* self, i_key value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); #if !defined _i_queue #if !defined _i_no_clone @@ -59,10 +59,10 @@ STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* po #endif // !_i_no_clone #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 _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, i_keyraw raw); 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_value* _cx_memb(_push_front)(_cx_self* self, i_key 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); @@ -71,11 +71,11 @@ STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); #if !defined _i_no_emplace -STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) - { return _cx_memb(_push_back)(self, i_valfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } #endif -STC_INLINE i_val _cx_memb(_value_clone)(i_val val) - { return i_valclone(val); } +STC_INLINE i_key _cx_memb(_value_clone)(i_key val) + { return i_keyclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); @@ -85,11 +85,11 @@ STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return !cdeq_rep_(&cx)-> STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cdeq_rep_(&cx)->size; } STC_INLINE size_t _cx_memb(_capacity)(_cx_self cx) { return cdeq_rep_(&cx)->cap; } STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) {c_swap(_cx_self, *a, *b); } -STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } -STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_valto(pval); } +STC_INLINE i_key _cx_memb(_value_fromraw)(i_keyraw raw) { return i_keyfrom(raw); } +STC_INLINE i_keyraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_keyto(pval); } STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _i_queue - { i_valdrop(self->data); ++self->data; --cdeq_rep_(self)->size; } + { i_keydrop(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 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { return it.ref - cx.data; } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) - { _cx_value* p = &self->data[--cdeq_rep_(self)->size]; i_valdrop(p); } + { _cx_value* p = &self->data[--cdeq_rep_(self)->size]; i_keydrop(p); } STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { assert(idx < cdeq_rep_(self)->size); return self->data + idx; @@ -116,7 +116,7 @@ STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { } STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_val value) { +_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { return _cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter @@ -124,7 +124,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con return _cx_memb(_insert_range_p)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter -_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_val value) { +_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1); } @@ -147,8 +147,8 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2 return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref); } -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_value* _cx_memb(_emplace_front)(_cx_self* self, i_keyraw raw) { + return _cx_memb(_push_front)(self, i_keyfrom(raw)); } STC_INLINE _cx_iter @@ -156,7 +156,7 @@ _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], cons return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter -_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) { +_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) { return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); } #endif // !_i_no_clone && !_i_no_emplace @@ -164,19 +164,19 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) { #if !c_option(c_no_cmp) STC_INLINE _cx_iter -_cx_memb(_find)(const _cx_self* self, i_valraw raw) { +_cx_memb(_find)(const _cx_self* self, i_keyraw raw) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw); } STC_INLINE const _cx_value* -_cx_memb(_get)(const _cx_self* self, i_valraw raw) { +_cx_memb(_get)(const _cx_self* self, i_keyraw raw) { _cx_iter end = _cx_memb(_end)(self); _cx_value* val = _cx_memb(_find_in)(_cx_memb(_begin)(self), end, raw).ref; return val == end.ref ? NULL : val; } STC_INLINE _cx_value* -_cx_memb(_get_mut)(_cx_self* self, i_valraw raw) +_cx_memb(_get_mut)(_cx_self* self, i_keyraw raw) { return (_cx_value *) _cx_memb(_get)(self, raw); } STC_INLINE void @@ -211,7 +211,7 @@ _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; ) { - --q; i_valdrop(q); + --q; i_keydrop(q); } rep->size = 0; } @@ -222,8 +222,8 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { if (_cx_memb(_size)(*self) != _cx_memb(_capacity)(*self)) { struct cdeq_rep* rep = cdeq_rep_(self); const size_t sz = rep->size; - memmove(self->_base, self->data, sz*sizeof(i_val)); - rep = (struct cdeq_rep*) c_realloc(rep, offsetof(struct cdeq_rep, base) + sz*sizeof(i_val)); + memmove(self->_base, self->data, sz*sizeof(i_key)); + rep = (struct cdeq_rep*) c_realloc(rep, offsetof(struct cdeq_rep, base) + sz*sizeof(i_key)); if (rep) { self->_base = self->data = (_cx_value*) rep->base; rep->cap = sz; } } } @@ -243,7 +243,7 @@ _cx_memb(_realloc_)(_cx_self* self, const size_t n) { const size_t sz = rep->size, cap = (size_t) (sz*1.7) + n + 7; const size_t nfront = _cdeq_nfront(self); rep = (struct cdeq_rep*) c_realloc(rep->cap ? rep : NULL, - offsetof(struct cdeq_rep, base) + cap*sizeof(i_val)); + offsetof(struct cdeq_rep, base) + cap*sizeof(i_key)); if (!rep) return 0; rep->size = sz, rep->cap = cap; self->_base = (_cx_value *) rep->base; @@ -258,7 +258,7 @@ _cx_memb(_expand_right_half_)(_cx_self* self, const size_t idx, const size_t n) const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; if (nback >= n || sz*1.3 + n > cap) { if (!_cx_memb(_realloc_)(self, n)) return false; - memmove(self->data + idx + n, self->data + idx, (sz - idx)*sizeof(i_val)); + memmove(self->data + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); } else { #if !defined _i_queue const size_t unused = cap - (sz + n); @@ -266,8 +266,8 @@ _cx_memb(_expand_right_half_)(_cx_self* self, const size_t idx, const size_t n) #else const size_t pos = 0; #endif - memmove(self->_base + pos, self->data, idx*sizeof(i_val)); - memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_val)); + memmove(self->_base + pos, self->data, idx*sizeof(i_key)); + memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); self->data = self->_base + pos; } return true; @@ -287,7 +287,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t n) { } STC_DEF _cx_value* -_cx_memb(_push_back)(_cx_self* self, i_val value) { +_cx_memb(_push_back)(_cx_self* self, i_key value) { struct cdeq_rep* r = cdeq_rep_(self); if (_cdeq_nfront(self) + r->size == r->cap) { _cx_memb(_expand_right_half_)(self, r->size, 1); @@ -304,7 +304,7 @@ _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_with_capacity)(sz); cdeq_rep_(&out)->size = sz; for (size_t i = 0; i < sz; ++i) - out.data[i] = i_valclone(cx.data[i]); + out.data[i] = i_keyclone(cx.data[i]); return out; } #endif @@ -318,13 +318,13 @@ _cx_memb(_expand_left_half_)(_cx_self* self, const size_t idx, const size_t n) { const size_t sz = rep->size; const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; if (nfront >= n) { - self->data = (_cx_value *) memmove(self->data - n, self->data, idx*sizeof(i_val)); + self->data = (_cx_value *) memmove(self->data - n, self->data, idx*sizeof(i_key)); } else { if (sz*1.3 + n > cap) cap = _cx_memb(_realloc_)(self, n); const size_t unused = cap - (sz + n); const size_t pos = (nback*2 < unused) ? unused - nback : unused/2; - memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_val)); - self->data = (_cx_value *) memmove(self->_base + pos, self->data, idx*sizeof(i_val)); + memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); + self->data = (_cx_value *) memmove(self->_base + pos, self->data, idx*sizeof(i_key)); } } @@ -338,7 +338,7 @@ _cx_memb(_insert_space_)(_cx_self* self, const _cx_value* pos, const size_t n) { } STC_DEF _cx_value* -_cx_memb(_push_front)(_cx_self* self, i_val value) { +_cx_memb(_push_front)(_cx_self* self, i_key value) { if (self->data == self->_base) _cx_memb(_expand_left_half_)(self, 0, 1); else @@ -362,9 +362,9 @@ _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_valdrop(p); } + for (; p != p2; ++p) { i_keydrop(p); } if (p1 == self->data) self->data += n; - else memmove(p1, p2, (end - p2) * sizeof(i_val)); + else memmove(p1, p2, (end - p2) * sizeof(i_key)); cdeq_rep_(self)->size -= n; } return c_make(_cx_iter){p1}; @@ -376,7 +376,7 @@ STC_DEF _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; - for (; p1 != p2; ++p1) *pos++ = i_valfrom((*p1)); + for (; p1 != p2; ++p1) *pos++ = i_keyfrom((*p1)); return it; } #endif // !_i_no_emplace @@ -387,7 +387,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; for (; p1 != p2; ++p1) - *pos++ = i_valclone((*p1)); + *pos++ = i_keyclone((*p1)); return it; } #endif // !_i_no_clone @@ -395,9 +395,9 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, #if !c_option(c_no_cmp) STC_DEF _cx_iter -_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { +_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_keyraw raw) { for (; i1.ref != i2.ref; ++i1.ref) { - i_valraw r = i_valto(i1.ref); + i_keyraw r = i_keyto(i1.ref); if (i_eq((&raw), (&r))) return i1; } return i2; @@ -405,8 +405,8 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { 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); + i_keyraw rx = i_keyto(x); + i_keyraw ry = i_keyto(y); return i_cmp((&rx), (&ry)); } #endif // !c_no_cmp diff --git a/include/stc/clist.h b/include/stc/clist.h index b871dd3c..8247b04a 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -28,7 +28,7 @@ #include #include - #define i_val int64_t + #define i_key int64_t #define i_tag ix #include @@ -83,22 +83,22 @@ _c_clist_complete_types(clist_VOID, dummy); #include "template.h" #if !c_option(c_is_fwd) - _cx_deftypes(_c_clist_types, _cx_self, i_val); + _cx_deftypes(_c_clist_types, _cx_self, i_key); #endif _cx_deftypes(_c_clist_complete_types, _cx_self, dummy); -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; STC_API size_t _clist_count(const clist_VOID* 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_at)(_cx_self* self, _cx_iter it, i_val value); +STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value); +STC_API _cx_value* _cx_memb(_push_front)(_cx_self* self, i_key value); +STC_API _cx_iter _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key 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_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 size_t _cx_memb(_remove)(_cx_self* self, i_keyraw val); +STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_keyraw val); STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API void _cx_memb(_sort)(_cx_self* self); #endif @@ -108,20 +108,20 @@ STC_API _cx_node* _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node); #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); -STC_INLINE i_val _cx_memb(_value_clone)(i_val val) - { return i_valclone(val); } +STC_INLINE i_key _cx_memb(_value_clone)(i_key val) + { return i_keyclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->last == other.last) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } #if !defined _i_no_emplace -STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) - { return _cx_memb(_push_back)(self, i_valfrom(raw)); } -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_at)(_cx_self* self, _cx_iter it, i_valraw raw) - { return _cx_memb(_insert_at)(self, it, i_valfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push_front)(self, i_keyfrom(raw)); } +STC_INLINE _cx_iter _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) + { return _cx_memb(_insert_at)(self, it, i_keyfrom(raw)); } #endif // !_i_no_emplace #endif // !_i_no_clone @@ -168,17 +168,17 @@ _cx_memb(_splice_range)(_cx_self* self, _cx_iter it, #if !c_option(c_no_cmp) STC_INLINE _cx_iter -_cx_memb(_find)(const _cx_self* self, i_valraw val) { +_cx_memb(_find)(const _cx_self* self, i_keyraw val) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val); } STC_INLINE const _cx_value* -_cx_memb(_get)(const _cx_self* self, i_valraw val) { +_cx_memb(_get)(const _cx_self* self, i_keyraw val) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val).ref; } STC_INLINE _cx_value* -_cx_memb(_get_mut)(_cx_self* self, i_valraw val) { +_cx_memb(_get_mut)(_cx_self* self, i_keyraw val) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val).ref; } #endif @@ -191,7 +191,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_init)(); c_foreach (it, _cx_self, cx) - _cx_memb(_push_back)(&out, i_valclone((*it.ref))); + _cx_memb(_push_back)(&out, i_keyclone((*it.ref))); return out; } #endif @@ -202,21 +202,21 @@ _cx_memb(_drop)(_cx_self* self) { } STC_DEF _cx_value* -_cx_memb(_push_back)(_cx_self* self, i_val value) { +_cx_memb(_push_back)(_cx_self* self, i_key value) { _c_clist_insert_after(self, _cx_self, self->last, value); self->last = entry; return &entry->value; } STC_DEF _cx_value* -_cx_memb(_push_front)(_cx_self* self, i_val value) { +_cx_memb(_push_front)(_cx_self* self, i_key value) { _c_clist_insert_after(self, _cx_self, self->last, value); if (!self->last) self->last = entry; return &entry->value; } STC_DEF _cx_iter -_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_val value) { +_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { _cx_node* node = it.ref ? it.prev : self->last; _c_clist_insert_after(self, _cx_self, node, value); if (!self->last || !it.ref) { @@ -250,7 +250,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_valdrop((&del->value)); c_free(del); + i_keydrop((&del->value)); c_free(del); return node; } @@ -283,21 +283,21 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) { #if !c_option(c_no_cmp) STC_DEF _cx_iter -_cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw val) { +_cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_keyraw val) { c_foreach (it, _cx_self, it1, it2) { - i_valraw r = i_valto(it.ref); + i_keyraw r = i_keyto(it.ref); if (i_eq((&r), (&val))) return it; } it2.ref = NULL; return it2; } STC_DEF size_t -_cx_memb(_remove)(_cx_self* self, i_valraw val) { +_cx_memb(_remove)(_cx_self* self, i_keyraw val) { size_t n = 0; _cx_node* prev = self->last, *node; while (prev) { node = prev->next; - i_valraw r = i_valto((&node->value)); + i_keyraw r = i_keyto((&node->value)); if (i_eq((&r), (&val))) prev = _cx_memb(_erase_after_)(self, prev), ++n; else @@ -308,8 +308,8 @@ _cx_memb(_remove)(_cx_self* self, i_valraw val) { static int _cx_memb(_sort_cmp_)(const clist_VOID_node* x, const clist_VOID_node* y) { - i_valraw a = i_valto((&((const _cx_node *) x)->value)); - i_valraw b = i_valto((&((const _cx_node *) y)->value)); + i_keyraw a = i_keyto((&((const _cx_node *) x)->value)); + i_keyraw b = i_keyto((&((const _cx_node *) y)->value)); return i_cmp((&a), (&b)); } @@ -324,8 +324,8 @@ _cx_memb(_sort)(_cx_self* self) { 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); + i_keyraw rx = i_keyto(x); + i_keyraw ry = i_keyto(y); return i_cmp((&rx), (&ry)); } #endif // !c_no_cmp diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 07ba9fe5..c5172f3b 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -65,15 +65,15 @@ typedef struct { size_t idx; uint8_t hx; } chash_bucket_t; #define _i_SET_ONLY c_true #define _i_keyref(vp) (vp) #else + #define _i_ismap #define _i_MAP_ONLY c_true #define _i_SET_ONLY c_false #define _i_keyref(vp) (&(vp)->first) #endif +#define _i_ishash #include "template.h" -#if _i_no_hash == 1 +#ifdef _i_no_hash #error "i_hash must be defined if i_cmp, i_eq or i_keyfrom is defined for cmap/cset. For basic types c_default_hash may be used." -#elif _i_no_hash == 2 - #error "i_cmp or i_eq must be defined if i_hash is defined. For basic types c_default_cmp may be used." #endif #if !c_option(c_is_fwd) _cx_deftypes(_c_chash_types, _cx_self, i_key, i_val, i_size, _i_MAP_ONLY, _i_SET_ONLY); @@ -392,6 +392,8 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { #endif // _i_implement #undef _i_isset +#undef _i_ismap +#undef _i_ishash #undef _i_keyref #undef _i_MAP_ONLY #undef _i_SET_ONLY diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 9844fc93..4b330707 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -34,9 +34,9 @@ #include "template.h" #if !c_option(c_is_fwd) - _cx_deftypes(_c_cpque_types, _cx_self, i_val); + _cx_deftypes(_c_cpque_types, _cx_self, i_key); #endif -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; STC_API void _cx_memb(_make_heap)(_cx_self* self); STC_API void _cx_memb(_erase_at)(_cx_self* self, size_t idx); @@ -59,7 +59,7 @@ STC_INLINE _cx_self _cx_memb(_with_capacity)(const size_t cap) { return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_val null) { +STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_key null) { _cx_self out = {NULL}; _cx_memb(_reserve)(&out, size); while (out.size < size) out.data[out.size++] = null; return out; @@ -67,7 +67,7 @@ STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_val null) { STC_INLINE void _cx_memb(_clear)(_cx_self* self) { size_t i = self->size; self->size = 0; - while (i--) { i_valdrop((self->data + i)); } + while (i--) { i_keydrop((self->data + i)); } } STC_INLINE void _cx_memb(_drop)(_cx_self* self) @@ -95,12 +95,12 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { return i_valclone(val); } +STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) + { return i_keyclone(val); } #if !defined _i_no_emplace STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) - { _cx_memb(_push)(self, i_valfrom(raw)); } + { _cx_memb(_push)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace #endif // !_i_no_clone @@ -128,14 +128,14 @@ _cx_memb(_make_heap)(_cx_self* self) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { _cx_self out = _cx_memb(_with_capacity)(q.size); for (; out.size < out.capacity; ++q.data) - out.data[out.size++] = i_valclone((*q.data)); + out.data[out.size++] = i_keyclone((*q.data)); return out; } #endif STC_DEF void _cx_memb(_erase_at)(_cx_self* self, const size_t idx) { - i_valdrop((self->data + idx)); + i_keydrop((self->data + idx)); const 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/cqueue.h b/include/stc/cqueue.h index a9a2fbfa..b07582e0 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -25,7 +25,7 @@ #include #include -#define i_val int +#define i_key int #include int main() { diff --git a/include/stc/csmap.h b/include/stc/csmap.h index d89a9fdc..eba9a591 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -67,6 +67,7 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; #define _i_SET_ONLY c_true #define _i_keyref(vp) (vp) #else + #define _i_ismap #define _i_MAP_ONLY c_true #define _i_SET_ONLY c_false #define _i_keyref(vp) (&(vp)->first) @@ -533,6 +534,7 @@ _cx_memb(_drop)(_cx_self* self) { #endif // _i_implement #undef _i_isset +#undef _i_ismap #undef _i_keyref #undef _i_MAP_ONLY #undef _i_SET_ONLY diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 7e20a632..f2412184 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -34,19 +34,19 @@ #include "template.h" #if !c_option(c_is_fwd) -_cx_deftypes(_c_cstack_types, _cx_self, i_val); +_cx_deftypes(_c_cstack_types, _cx_self, i_key); #endif -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){0, 0, 0}; } STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { - _cx_self out = {(_cx_value *) c_malloc(cap*sizeof(i_val)), 0, cap}; + _cx_self out = {(_cx_value *) c_malloc(cap*sizeof(i_key)), 0, cap}; return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val null) { +STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_key null) { _cx_self out = {(_cx_value *) c_malloc(size*sizeof null), size, size}; while (size) out.data[--size] = null; return out; @@ -54,7 +54,7 @@ STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val null) { STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_value *p = self->data + self->size; - while (p-- != self->data) { i_valdrop(p); } + while (p-- != self->data) { i_keydrop(p); } self->size = 0; } @@ -92,7 +92,7 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, _cx_value val) { return _cx_memb(_push)(self, val); } STC_INLINE void _cx_memb(_pop)(_cx_self* self) - { _cx_value* p = &self->data[--self->size]; i_valdrop(p); } + { _cx_value* p = &self->data[--self->size]; i_keydrop(p); } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } @@ -104,15 +104,15 @@ STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) #if !defined _i_no_clone #if !defined _i_no_emplace STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) - { return _cx_memb(_push)(self, i_valfrom(raw)); } + { return _cx_memb(_push)(self, i_keyfrom(raw)); } STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) - { return _cx_memb(_push)(self, i_valfrom(raw)); } + { return _cx_memb(_push)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { _cx_self out = {(_cx_value *) c_malloc(v.size*sizeof(_cx_value)), v.size, v.size}; for (size_t i = 0; i < v.size; ++v.data) - out.data[i++] = i_valclone((*v.data)); + out.data[i++] = i_keyclone((*v.data)); return out; } @@ -121,11 +121,11 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other); } -STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { return i_valclone(val); } +STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) + { return i_keyclone(val); } -STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* val) - { return i_valto(val); } +STC_INLINE i_keyraw _cx_memb(_value_toraw)(_cx_value* val) + { return i_keyto(val); } #endif // !_i_no_clone STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index b666e791..b98ecbd9 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -32,13 +32,13 @@ struct MyStruct { cstr name; } typedef MyStruct; -#define i_val float +#define i_key float #include -#define i_val_str // special for cstr +#define i_key_str // special for cstr #include -#define i_val int +#define i_key int #define i_opt c_is_fwd // forward declared #define i_tag i32 #include @@ -74,31 +74,31 @@ struct cvec_rep { size_t size, cap; void* data[]; }; #include "template.h" #if !c_option(c_is_fwd) - _cx_deftypes(_c_cvec_types, _cx_self, i_val); + _cx_deftypes(_c_cvec_types, _cx_self, i_key); #endif -typedef i_valraw _cx_raw; +typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); 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); -STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_val value); +STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_key null); +STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key 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_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, _cx_iter* lower_bound); +STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_keyraw raw); +STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_keyraw raw, _cx_iter* lower_bound); #endif #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val) - { return i_valclone(val); } -STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } +STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) + { return i_keyclone(val); } +STC_INLINE i_key _cx_memb(_value_fromraw)(i_keyraw raw) { return i_keyfrom(raw); } STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { if (self->data == other.data) return; _cx_memb(_drop)(self); @@ -107,16 +107,16 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { #if !defined _i_no_emplace STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); -STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_valraw raw) - { return _cx_memb(_push)(self, i_valfrom(raw)); } -STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) - { return _cx_memb(_push)(self, i_valfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push)(self, i_keyfrom(raw)); } +STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw) + { return _cx_memb(_push)(self, i_keyfrom(raw)); } STC_INLINE _cx_iter _cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter -_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) { +_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) { return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); } STC_INLINE _cx_iter @@ -129,14 +129,14 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2 STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cvec_rep_(&cx)->size; } STC_INLINE size_t _cx_memb(_capacity)(_cx_self cx) { return cvec_rep_(&cx)->cap; } STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return !cvec_rep_(&cx)->size; } -STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* val) { return i_valto(val); } +STC_INLINE i_keyraw _cx_memb(_value_toraw)(_cx_value* val) { return i_keyto(val); } STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_self, *a, *b); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } 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)(_cx_self* self) - { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); } -STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value) + { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_keydrop(p); } +STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) { return _cx_memb(_push)(self, value); } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) @@ -149,7 +149,7 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { return it.ref - cx.data; } STC_INLINE _cx_self -_cx_memb(_with_size)(const size_t size, i_val null) { +_cx_memb(_with_size)(const size_t size, i_key null) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_resize)(&cx, size, null); return cx; @@ -168,7 +168,7 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { } STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_val value) { +_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { return _cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter @@ -176,7 +176,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con return _cx_memb(_insert_range_p)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter -_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_val value) { +_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1); } @@ -205,29 +205,29 @@ _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { #if !c_option(c_no_cmp) STC_INLINE _cx_iter -_cx_memb(_find)(const _cx_self* self, i_valraw raw) { +_cx_memb(_find)(const _cx_self* self, i_keyraw raw) { return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw); } STC_INLINE const _cx_value* -_cx_memb(_get)(const _cx_self* self, i_valraw raw) { +_cx_memb(_get)(const _cx_self* self, i_keyraw raw) { _cx_iter end = _cx_memb(_end)(self); _cx_value* val = _cx_memb(_find)(self, raw).ref; return val == end.ref ? NULL : val; } STC_INLINE _cx_value* -_cx_memb(_get_mut)(const _cx_self* self, i_valraw raw) +_cx_memb(_get_mut)(const _cx_self* self, i_keyraw raw) { return (_cx_value*) _cx_memb(_get)(self, raw); } STC_INLINE _cx_iter -_cx_memb(_bsearch)(const _cx_self* self, i_valraw raw) { +_cx_memb(_bsearch)(const _cx_self* self, i_keyraw raw) { _cx_iter lower; return _cx_memb(_bsearch_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower); } STC_INLINE _cx_iter -_cx_memb(_lower_bound)(const _cx_self* self, i_valraw raw) { +_cx_memb(_lower_bound)(const _cx_self* self, i_keyraw raw) { _cx_iter lower; _cx_memb(_bsearch_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower); return lower; @@ -261,7 +261,7 @@ _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; ) { - --q; i_valdrop(q); + --q; i_keydrop(q); } rep->size = 0; } @@ -282,7 +282,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { const size_t len = rep->size; 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)); + offsetof(struct cvec_rep, data) + cap*sizeof(i_key)); if (!rep) return false; self->data = (_cx_value*) rep->data; rep->size = len; @@ -292,18 +292,18 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { } STC_DEF bool -_cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) { +_cx_memb(_resize)(_cx_self* self, const size_t len, i_key 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_valdrop((self->data + i)); } + for (size_t i = len; i < n; ++i) { i_keydrop((self->data + i)); } for (size_t i = n; i < len; ++i) self->data[i] = null; if (rep->cap) rep->size = len; return true; } STC_DEF _cx_value* -_cx_memb(_push)(_cx_self* self, i_val value) { +_cx_memb(_push)(_cx_self* self, i_key value) { struct cvec_rep *r = cvec_rep_(self); if (r->size == r->cap) { _cx_memb(_reserve)(self, (r->size*3 >> 1) + 4); @@ -342,8 +342,8 @@ _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_valdrop(p); } - memmove(p1, p2, (end - p2) * sizeof(i_val)); + for (; p != p2; ++p) { i_keydrop(p); } + memmove(p1, p2, (end - p2) * sizeof(i_key)); cvec_rep_(self)->size -= len; } return c_make(_cx_iter){.ref = p1}; @@ -364,7 +364,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; for (; p1 != p2; ++p1) - *pos++ = i_valclone((*p1)); + *pos++ = i_keyclone((*p1)); return it; } @@ -374,7 +374,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { pos = _cx_memb(_insert_space_)(self, pos, p2 - p1); _cx_iter it = {pos}; - for (; p1 != p2; ++p1) *pos++ = i_valfrom((*p1)); + for (; p1 != p2; ++p1) *pos++ = i_keyfrom((*p1)); return it; } #endif // !_i_no_emplace @@ -382,20 +382,20 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, #if !c_option(c_no_cmp) STC_DEF _cx_iter -_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) { +_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_keyraw raw) { for (; i1.ref != i2.ref; ++i1.ref) { - i_valraw r = i_valto(i1.ref); + i_keyraw r = i_keyto(i1.ref); if (i_eq((&raw), (&r))) return i1; } return i2; } STC_DEF _cx_iter -_cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw, _cx_iter* lower_bound) { +_cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_keyraw raw, _cx_iter* lower_bound) { _cx_iter mid, last = i2; while (i1.ref != i2.ref) { mid.ref = i1.ref + ((i2.ref - i1.ref) >> 1); - int c; i_valraw m = i_valto(mid.ref); + int c; i_keyraw m = i_keyto(mid.ref); if (!(c = i_cmp((&raw), (&m)))) return *lower_bound = mid; else if (c < 0) i2.ref = mid.ref; else i1.ref = mid.ref + 1; @@ -406,8 +406,8 @@ _cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw, _cx_iter* lower_bo 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); + i_keyraw rx = i_keyto(x); + i_keyraw ry = i_keyto(y); return i_cmp((&rx), (&ry)); } #endif // !c_no_cmp diff --git a/include/stc/template.h b/include/stc/template.h index 8d8bd6dc..6f784c35 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -54,6 +54,41 @@ #include "cstr.h" #endif +#if !(defined i_key || defined i_key_str || defined i_key_bind || defined i_key_arcbox) + #define _i_key_from_val + #if defined _i_ismap + #error "i_key* must be defined for maps." + #endif + + #if defined i_val_str + #define i_key_str i_val_str + #endif + #if defined i_val_arcbox + #define i_key_arcbox i_val_arcbox + #endif + #if defined i_val_bind + #define i_key_bind i_val_bind + #endif + #if defined i_val + #define i_key i_val + #endif + #if defined i_valraw + #define i_keyraw i_valraw + #endif + #if defined i_valclone + #define i_keyclone i_valclone + #endif + #if defined i_valfrom + #define i_keyfrom i_valfrom + #endif + #if defined i_valto + #define i_keyto i_valto + #endif + #if defined i_valdrop + #define i_keydrop i_valdrop + #endif +#endif + #ifdef i_key_str #define i_key_bind cstr #define i_keyraw crawstr @@ -63,6 +98,7 @@ #elif defined i_key_arcbox #define i_key_bind i_key_arcbox #define i_keyraw c_paste(i_key_arcbox, _value) + // smart pointers have special clone, so override: #define i_keyclone c_paste(i_key_arcbox, _clone) #define _i_no_emplace #endif @@ -81,21 +117,25 @@ #define i_keyto c_paste(i_key, _toraw) #endif #endif + #ifndef i_keydrop + #define i_keydrop c_paste(i_key, _drop) + #endif #ifndef i_cmp #define i_cmp c_paste(i_keyraw, _cmp) #endif - #ifndef i_eq - #define i_eq c_paste(i_keyraw, _eq) - #endif - #ifndef i_hash - #define i_hash c_paste(i_keyraw, _hash) - #endif - #ifndef i_keydrop - #define i_keydrop c_paste(i_key, _drop) + #if defined _i_ishash + #ifndef i_eq + #define i_eq c_paste(i_keyraw, _eq) + #endif + #if !defined i_hash + #define i_hash c_paste(i_keyraw, _hash) + #endif #endif #endif -#if defined i_keyraw && !defined i_keyfrom +#if !defined i_key + #error "no i_key or i_val provided" +#elif defined i_keyraw && !defined i_keyfrom #error "if i_keyraw is defined, i_keyfrom (and normally i_keyto) must be defined" #elif defined i_drop #error "i_drop not supported. Define i_keydrop/i_valdrop instead." @@ -103,12 +143,49 @@ #error "i_from not supported. Define i_keyfrom/i_valfrom instead." #endif +#ifndef i_tag + #define i_tag i_key +#endif +#if (!defined i_keyfrom && defined i_keydrop) || c_option(c_no_clone) + #define _i_no_clone +#endif +#if !defined i_hash && (defined i_keyfrom || defined i_keyclone || defined i_cmp || defined i_eq) + #define _i_no_hash +#endif +#ifndef i_keyfrom + #define i_keyfrom c_default_from +#endif +#ifndef i_keyraw + #define i_keyraw i_key +#else + #define _i_has_raw +#endif +#ifndef i_keyto + #define i_keyto c_default_toraw +#endif +#ifndef i_keyclone + #define i_keyclone(key) i_keyfrom((i_keyto((&(key))))) +#endif +#ifndef i_keydrop + #define i_keydrop c_default_drop +#endif +#if !defined i_eq && defined i_cmp + #define i_eq(x, y) !(i_cmp(x, y)) +#elif !defined i_eq + #define i_eq c_default_eq +#endif +#ifndef i_cmp + #define i_cmp c_default_cmp +#endif +#ifndef i_hash + #define i_hash c_default_hash +#endif + +#if defined _i_ismap // ---- process cmap/csmap value i_val, ... ---- + #ifdef i_val_str #define i_val_bind cstr #define i_valraw crawstr - #if !defined i_tag && !defined i_key - #define i_tag str - #endif #elif defined i_val_arcbox #define i_val_bind i_val_arcbox #define i_valraw c_paste(i_val_arcbox, _value) @@ -130,15 +207,6 @@ #define i_valto c_paste(i_val, _toraw) #endif #endif - #if !defined i_cmp && !defined i_key - #define i_cmp c_paste(i_valraw, _cmp) - #endif - #if !defined i_hash && c_option(c_hash) - #define i_hash c_paste(i_val, _hash) - #endif - #if !defined i_eq && c_option(c_eq) - #define i_eq c_paste(i_val, _eq) - #endif #ifndef i_valdrop #define i_valdrop c_paste(i_val, _drop) #endif @@ -148,69 +216,7 @@ #error "if i_valraw is defined, i_valfrom (and normally i_valto) must be defined" #endif -#if !defined i_keyraw && !defined i_valraw - #define _i_no_emplace -#endif - -/* Copy i_val* macros to i_key* if _i_isset */ -#if defined _i_isset && defined i_val - #if !defined i_key - #define i_key i_val - #endif - #if defined i_valraw && !defined i_keyraw - #define i_keyraw i_valraw - #endif - #if defined i_valclone && !defined i_keyclone - #define i_keyclone i_valclone - #endif - #if defined i_valfrom && !defined i_keyfrom - #define i_keyfrom i_valfrom - #endif - #if defined i_valto && !defined i_keyto - #define i_keyto i_valto - #endif - #if defined i_valdrop && !defined i_keydrop - #define i_keydrop i_valdrop - #endif -#endif - -#ifdef i_key - #if defined _i_isset && !defined i_val - #define i_val i_key - #endif - #ifndef i_tag - #define i_tag i_key - #endif - #if !defined i_keyfrom && defined i_keydrop - #define _i_no_clone - #endif - #if !defined i_hash && (defined i_keyfrom || defined i_cmp || defined i_eq) - #define _i_no_hash 1 - #endif - #if !defined i_cmp && !defined i_eq && defined i_hash - #define _i_no_hash 2 - #endif - #ifndef i_keyfrom - #define i_keyfrom c_default_from - #endif - #ifndef i_keyraw - #define i_keyraw i_key - #endif - #ifndef i_keyto - #define i_keyto c_default_toraw - #endif - #ifndef i_keyclone - #define i_keyclone(key) i_keyfrom((i_keyto((&(key))))) - #endif - #ifndef i_keydrop - #define i_keydrop c_default_drop - #endif -#endif - -#ifndef i_tag - #define i_tag i_val -#endif -#if (!defined i_valfrom && defined i_valdrop) || c_option(c_no_clone) +#if !defined i_valfrom && defined i_valdrop #define _i_no_clone #endif #ifndef i_valfrom @@ -218,6 +224,8 @@ #endif #ifndef i_valraw #define i_valraw i_val +#else + #define _i_has_raw #endif #ifndef i_valto #define i_valto c_default_toraw @@ -228,19 +236,20 @@ #ifndef i_valdrop #define i_valdrop c_default_drop #endif -#if !defined i_eq && defined i_cmp - #define i_eq(x, y) !(i_cmp(x, y)) -#elif !defined i_eq - #define i_eq c_default_eq + +#endif // !_i_ismap + +#ifndef i_val + #define i_val i_key #endif -#ifndef i_cmp - #define i_cmp c_default_cmp +#ifndef i_valraw + #define i_valraw i_keyraw #endif -#ifndef i_hash - #define i_hash c_default_hash +#ifndef _i_has_raw + #define _i_no_emplace #endif -#else // ------------------------------------------------------- +#else // ============================================================ #undef i_type #undef i_tag @@ -256,22 +265,24 @@ #undef i_val_arcbox #undef i_val_bind #undef i_valraw +#undef i_valclone #undef i_valfrom #undef i_valto #undef i_valdrop -#undef i_valclone #undef i_key #undef i_key_str #undef i_key_arcbox #undef i_key_bind #undef i_keyraw +#undef i_keyclone #undef i_keyfrom #undef i_keyto #undef i_keydrop -#undef i_keyclone #undef _i_prefix +#undef _i_has_raw +#undef _i_key_from_val #undef _i_no_clone #undef _i_no_emplace #undef _i_no_hash -- cgit v1.2.3