diff options
| author | Tyge Løvset <[email protected]> | 2022-07-06 22:26:53 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-07-06 22:26:53 +0200 |
| commit | b94170eefce899d0b236804681d77fe026956fd9 (patch) | |
| tree | 9d1bad3bdfce085cc8ff8c567943ffd8750aa644 /include | |
| parent | 6e91820d7cf632ff30b936c554a0bdf83c9e64b2 (diff) | |
| download | STC-modified-b94170eefce899d0b236804681d77fe026956fd9.tar.gz STC-modified-b94170eefce899d0b236804681d77fe026956fd9.zip | |
Version 3.7. Make sure to check NEWS/Changes in README.md for a few code-breaking API changes.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/alt/csmap.h | 6 | ||||
| -rw-r--r-- | include/stc/carc.h | 2 | ||||
| -rw-r--r-- | include/stc/carr2.h | 18 | ||||
| -rw-r--r-- | include/stc/carr3.h | 20 | ||||
| -rw-r--r-- | include/stc/cbits.h | 26 | ||||
| -rw-r--r-- | include/stc/cbox.h | 6 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 32 | ||||
| -rw-r--r-- | include/stc/clist.h | 11 | ||||
| -rw-r--r-- | include/stc/cmap.h | 26 | ||||
| -rw-r--r-- | include/stc/cpque.h | 23 | ||||
| -rw-r--r-- | include/stc/cqueue.h | 4 | ||||
| -rw-r--r-- | include/stc/csmap.h | 26 | ||||
| -rw-r--r-- | include/stc/cstack.h | 75 | ||||
| -rw-r--r-- | include/stc/cstr.h | 3 | ||||
| -rw-r--r-- | include/stc/cvec.h | 43 | ||||
| -rw-r--r-- | include/stc/forward.h | 8 | ||||
| -rw-r--r-- | include/stc/template.h | 1 |
17 files changed, 188 insertions, 142 deletions
diff --git a/include/stc/alt/csmap.h b/include/stc/alt/csmap.h index 168cf480..60a1b72a 100644 --- a/include/stc/alt/csmap.h +++ b/include/stc/alt/csmap.h @@ -155,11 +155,11 @@ _cx_memb(_value_clone)(_cx_value _val) { } STC_INLINE void -_cx_memb(_copy)(_cx_self *self, _cx_self other) { - if (self->root == other.root) +_cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->root == other->root) return; _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(other); + *self = _cx_memb(_clone)(*other); } #endif // !_i_no_clone diff --git a/include/stc/carc.h b/include/stc/carc.h index 73ccc5bf..57f00899 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -155,7 +155,7 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self ptr) { return ptr; } -STC_INLINE void _cx_memb(_copy)(_cx_self* self, _cx_self ptr) { +STC_INLINE void _cx_memb(_assign)(_cx_self* self, _cx_self ptr) { if (ptr.use_count) _i_atomic_inc(ptr.use_count); _cx_memb(_drop)(self); diff --git a/include/stc/carr2.h b/include/stc/carr2.h index dc35893a..66a98f58 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -38,7 +38,7 @@ int main() { c_autovar (carr2_int image = carr2_int_new_uninit(w, h), carr2_int_drop(&image)) { int *dat = carr2_int_data(&image); - for (int i = 0; i < carr2_int_size(image); ++i) + for (int i = 0; i < carr2_int_size(&image); ++i) dat[i] = i; for (int x = 0; x < image.xdim; ++x) @@ -67,14 +67,14 @@ STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self src); -STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); #endif STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim) { return _cx_memb(_with_data)(xdim, ydim, c_alloc_n(_cx_value, xdim*ydim)); } -STC_INLINE size_t _cx_memb(_size)(_cx_self arr) - { return arr.xdim*arr.ydim; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim; } STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self) { return *self->data; } @@ -118,14 +118,14 @@ STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_new_uninit)(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) + for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) *p = i_keyclone((*q)); return _arr; } -STC_DEF 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_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); } #endif @@ -138,7 +138,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; ) { + for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(self); p != q; ) { --q; i_keydrop(q); } c_free(self->data[0]); /* values */ diff --git a/include/stc/carr3.h b/include/stc/carr3.h index bbd3a065..8ff1670e 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -38,7 +38,7 @@ int main() { c_autovar (carr3_int image = carr3_int_new_uninit(w, h, d), carr3_int_drop(&image)) { int *dat = carr3_int_data(&image); - for (int i = 0; i < carr3_int_size(image); ++i) + for (int i = 0; i < carr3_int_size(&image); ++i) dat[i] = i; for (int x = 0; x < image.xdim; ++x) @@ -69,15 +69,15 @@ STC_API _cx_value* _cx_memb(_release)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); #if !defined _i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self src); -STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other); +STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other); #endif STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim, size_t zdim) { return _cx_memb(_with_data)(xdim, ydim, zdim, c_alloc_n(_cx_value, xdim*ydim*zdim)); } -STC_INLINE size_t _cx_memb(_size)(_cx_self arr) - { return arr.xdim*arr.ydim*arr.zdim; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->xdim*self->ydim*self->zdim; } STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self) { return **self->data; } @@ -95,7 +95,7 @@ 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) - { return c_make(_cx_iter){**self->data + _cx_memb(_size)(*self)}; } + { return c_make(_cx_iter){**self->data + _cx_memb(_size)(self)}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; } @@ -123,14 +123,14 @@ STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_k STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) { _cx_self _arr = _cx_memb(_new_uninit)(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) + for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q) *p = i_keyclone((*q)); return _arr; } -STC_DEF 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_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); } #endif @@ -143,7 +143,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; ) { + for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(self); p != q; ) { --q; i_keydrop(q); } c_free(self->data[0][0]); /* data */ diff --git a/include/stc/cbits.h b/include/stc/cbits.h index f7ce2368..dccd5fb3 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -82,7 +82,7 @@ STC_API char* _cbits_to_str(const uint64_t* set, const size_t sz, #define _i_memb(name) c_paste(i_type, name) -#if !defined i_len +#if !defined i_cap #define _i_assert(x) assert(x) #define i_type cbits @@ -90,6 +90,7 @@ STC_API char* _cbits_to_str(const uint64_t* set, const size_t sz, struct { uint64_t *data64; size_t _size; } typedef i_type; STC_INLINE cbits cbits_init(void) { return c_make(cbits){NULL}; } +STC_INLINE void cbits_inits(cbits* self) { self->data64 = NULL; self->_size = 0; } STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } STC_INLINE size_t cbits_size(const cbits* self) { return self->_size; } STC_API void cbits_resize(cbits* self, size_t size, bool value); @@ -131,18 +132,19 @@ STC_INLINE cbits cbits_with_pattern(const size_t size, const uint64_t pattern) { return set; } -#else // i_len +#else // i_cap #define _i_assert(x) (void)0 #if !defined i_type - #define i_type c_paste(cbits, i_len) + #define i_type c_paste(cbits, i_cap) #endif -struct { uint64_t data64[(i_len - 1)/64 + 1]; } typedef i_type; +struct { uint64_t data64[(i_cap - 1)/64 + 1]; } typedef i_type; STC_INLINE i_type _i_memb(_init)(void) { return c_make(i_type){0}; } +STC_INLINE void _i_memb(_inits)(i_type* self) {} STC_INLINE void _i_memb(_drop)(i_type* self) {} -STC_INLINE size_t _i_memb(_size)(const i_type* self) { return i_len; } +STC_INLINE size_t _i_memb(_size)(const i_type* self) { return i_cap; } STC_INLINE i_type _i_memb(_move)(i_type* self) { return *self; } STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other) @@ -151,24 +153,24 @@ STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other) STC_INLINE i_type _i_memb(_clone)(i_type other) { return other; } -STC_INLINE i_type* _i_memb(_copy)(i_type* self, i_type other) - { *self = other; return self; } +STC_INLINE i_type* _i_memb(_copy)(i_type* self, const i_type* other) + { *self = *other; return self; } STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value); STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern); STC_INLINE i_type _i_memb(_with_size)(const size_t size, const bool value) { - assert(size <= i_len); + assert(size <= i_cap); i_type set; _i_memb(_set_all)(&set, value); return set; } STC_INLINE i_type _i_memb(_with_pattern)(const size_t size, const uint64_t pattern) { - assert(size <= i_len); + assert(size <= i_cap); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; } -#endif // i_len +#endif // i_cap STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value) @@ -248,7 +250,7 @@ STC_INLINE bool _i_memb(_disjoint)(const i_type* self, const i_type* other) { /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) || defined(i_extern) -#if !defined i_len +#if !defined i_cap STC_DEF cbits* cbits_copy(cbits* self, const cbits* other) { if (self->data64 == other->data64) return self; @@ -318,7 +320,7 @@ STC_DEF bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const s #define CBITS_H_INCLUDED #undef _i_memb #undef _i_assert -#undef i_len +#undef i_cap #undef i_type #undef i_opt #undef i_header diff --git a/include/stc/cbox.h b/include/stc/cbox.h index f1e410b1..c3a9dd02 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -139,11 +139,11 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) { return out; } - STC_INLINE void _cx_memb(_copy)(_cx_self* self, _cx_self other) { - if (self->get == other.get) + STC_INLINE void _cx_memb(_assign)(_cx_self* self, const _cx_self ptr) { + if (self->get == ptr.get) return; _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(other); + *self = _cx_memb(_clone)(ptr); } #endif // !_i_no_clone diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 9c706e52..c5e08393 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -49,13 +49,13 @@ STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push)(_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 -STC_API _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2); #if !defined _i_no_emplace STC_API _cx_value* _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); #endif // _i_no_emplace +#if !defined _i_no_clone +STC_API _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2); #endif // !_i_no_clone #if !c_option(c_no_cmp) @@ -76,15 +76,16 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #endif 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); +STC_INLINE void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); + *self = _cx_memb(_clone)(*other); } #endif // !_i_no_clone -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 bool _cx_memb(_empty)(_cx_self cx) { return !cdeq_rep_(&cx)->size; } -STC_INLINE _cx_raw _cx_memb(_value_toraw)(_cx_value* pval) { return i_keyto(pval); } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cdeq_rep_(cx)->size; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return cdeq_rep_(cx)->cap; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return !cdeq_rep_(cx)->size; } +STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* pval) { return i_keyto(pval); } 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) @@ -142,12 +143,13 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref); } -#if !defined _i_no_clone && !defined _i_no_emplace +#if !defined _i_no_clone STC_INLINE _cx_value* _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); } - +#endif // !_i_no_clone +#if !defined _i_no_emplace STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push_front)(self, i_keyfrom(raw)); @@ -165,7 +167,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); } -#endif // !_i_no_clone && !_i_no_emplace +#endif // !_i_no_emplace #if !c_option(c_no_cmp) @@ -225,7 +227,7 @@ _cx_memb(_clear)(_cx_self* self) { STC_DEF void _cx_memb(_shrink_to_fit)(_cx_self *self) { - if (_cx_memb(_size)(*self) != _cx_memb(_capacity)(*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_key)); @@ -389,7 +391,6 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { return c_make(_cx_iter){p1}; } -#if !defined _i_no_clone #if !defined _i_no_emplace STC_DEF _cx_value* _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { @@ -401,6 +402,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, co } #endif // !_i_no_emplace +#if !defined _i_no_clone STC_DEF _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { diff --git a/include/stc/clist.h b/include/stc/clist.h index 29bf56f3..0fbf01ae 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -116,10 +116,12 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self cx); 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); +_cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->last == other->last) return; + _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other); } +#endif // !_i_no_clone + #if !defined _i_no_emplace STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } @@ -130,11 +132,10 @@ STC_INLINE _cx_iter _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_r STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace -#endif // !_i_no_clone STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; } STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; } -STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.last == NULL; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return self->last == NULL; } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, i_key value) { return _cx_memb(_push_back)(self, value); } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 3cc08cde..cfff214b 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -102,18 +102,18 @@ STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val); STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self)_cmap_inits; } STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } STC_INLINE void _cx_memb(_max_load_factor)(_cx_self* self, float ml) {self->max_load_factor = ml; } -STC_INLINE bool _cx_memb(_empty)(_cx_self m) { return m.size == 0; } -STC_INLINE size_t _cx_memb(_size)(_cx_self m) { return m.size; } -STC_INLINE size_t _cx_memb(_bucket_count)(_cx_self map) { return map.bucket_count; } -STC_INLINE size_t _cx_memb(_capacity)(_cx_self map) - { return (size_t)(map.bucket_count*map.max_load_factor); } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* map) { return !map->size; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* map) { return map->size; } +STC_INLINE size_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* map) + { return (size_t)(map->bucket_count*map->max_load_factor); } STC_INLINE void _cx_memb(_swap)(_cx_self *map1, _cx_self *map2) {c_swap(_cx_self, *map1, *map2); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } #ifndef _i_isset STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped); - #if !defined _i_no_clone && !defined _i_no_emplace + #if !defined _i_no_emplace STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); #endif @@ -129,11 +129,11 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rke #endif // !_i_isset #if !defined _i_no_clone -STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) { - if (self->table == other.table) +STC_INLINE void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->table == other->table) return; _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(other); + *self = _cx_memb(_clone)(*other); } STC_INLINE _cx_value @@ -142,6 +142,7 @@ _cx_memb(_value_clone)(_cx_value _val) { _i_MAP_ONLY( _val.second = i_valclone(_val.second); ) return _val; } +#endif // !_i_no_clone #if !defined _i_no_emplace STC_INLINE _cx_result @@ -154,10 +155,9 @@ _cx_memb(_emplace)(_cx_self* self, _cx_rawkey rkey _i_MAP_ONLY(, i_valraw rmappe return _res; } #endif // !_i_no_emplace -#endif // !_i_no_clone STC_INLINE _cx_raw -_cx_memb(_value_toraw)(_cx_value* val) { +_cx_memb(_value_toraw)(const _cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) _i_MAP_ONLY( c_make(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } @@ -299,7 +299,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { return _res; } - #if !defined _i_no_clone && !defined _i_no_emplace + #if !defined _i_no_emplace STC_DEF _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped) { _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); @@ -310,7 +310,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { _res.ref->second = i_valfrom(rmapped); return _res; } - #endif // !_i_no_clone && !_i_no_emplace + #endif // !_i_no_emplace #endif // !_i_isset STC_DEF chash_bucket_t diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 3a7cc0a1..bb7c207e 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -73,14 +73,14 @@ STC_INLINE void _cx_memb(_clear)(_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) - { return q.size; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* q) + { return q->size; } -STC_INLINE bool _cx_memb(_empty)(_cx_self q) - { return !q.size; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* q) + { return !q->size; } -STC_INLINE size_t _cx_memb(_capacity)(_cx_self q) - { return q.capacity; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* q) + { return q->capacity; } STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) { return &self->data[0]; } @@ -91,18 +91,19 @@ STC_INLINE void _cx_memb(_pop)(_cx_self* self) #if !defined _i_no_clone 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(_drop)(self); *self = _cx_memb(_clone)(other); +STC_INLINE void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); + *self = _cx_memb(_clone)(*other); } STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } +#endif // !_i_no_clone #if !defined _i_no_emplace STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { _cx_memb(_push)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace -#endif // !_i_no_clone /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) @@ -118,7 +119,7 @@ _cx_memb(_sift_down_)(_cx_value* arr, const size_t idx, const size_t n) { STC_DEF void _cx_memb(_make_heap)(_cx_self* self) { - size_t n = _cx_memb(_size)(*self); + size_t n = self->size; _cx_value *arr = self->data - 1; for (size_t k = n >> 1; k != 0; --k) _cx_memb(_sift_down_)(arr, k, n); diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index 00874f35..0c0df063 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -40,7 +40,7 @@ int main() { cqueue_int_push(&Q, stc64_uniform(&rng, &dist)); // Push or pop on the queue ten million times - printf("before: size, capacity: %d, %d\n", n, cqueue_int_size(Q), cqueue_int_capacity(Q)); + printf("before: size, capacity: %d, %d\n", n, cqueue_int_size(&Q), cqueue_int_capacity(&Q)); for (int i=n; i>0; --i) { int r = stc64_uniform(&rng, &dist); if (r & 1) @@ -48,7 +48,7 @@ int main() { else --n, cqueue_int_pop(&Q); } - printf("after: size, capacity: %d, %d\n", n, cqueue_int_size(Q), cqueue_int_capacity(Q)); + printf("after: size, capacity: %d, %d\n", n, cqueue_int_size(&Q), cqueue_int_capacity(&Q)); } } */ diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 5af28830..97f1fce1 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -97,11 +97,11 @@ typedef _i_SET_ONLY( i_keyraw ) _i_MAP_ONLY( struct { i_keyraw first; i_valraw second; } ) _cx_raw; -#if !defined _i_no_clone -STC_API _cx_self _cx_memb(_clone)(_cx_self tree); #if !defined _i_no_emplace STC_API _cx_result _cx_memb(_emplace)(_cx_self* self, _cx_rawkey rkey _i_MAP_ONLY(, i_valraw rmapped)); #endif // !_i_no_emplace +#if !defined _i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self tree); #endif // !_i_no_clone STC_API _cx_self _cx_memb(_init)(void); STC_API _cx_result _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped)); @@ -117,9 +117,9 @@ 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); STC_API void _cx_memb(_next)(_cx_iter* it); -STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return _csmap_rep(&cx)->size == 0; } -STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return _csmap_rep(&cx)->size; } -STC_INLINE size_t _cx_memb(_capacity)(_cx_self cx) { return _csmap_rep(&cx)->cap; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return _csmap_rep(cx)->size == 0; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return _csmap_rep(cx)->size; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return _csmap_rep(cx)->cap; } STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_self, *a, *b); } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; } @@ -142,7 +142,7 @@ _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); *self = _cx_memb(_init)(); } STC_INLINE _cx_raw -_cx_memb(_value_toraw)(_cx_value* val) { +_cx_memb(_value_toraw)(const _cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) _i_MAP_ONLY( c_make(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); @@ -169,11 +169,11 @@ _cx_memb(_value_clone)(_cx_value _val) { } STC_INLINE void -_cx_memb(_copy)(_cx_self *self, _cx_self other) { - if (self->nodes == other.nodes) +_cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->nodes == other->nodes) return; _cx_memb(_drop)(self); - *self = _cx_memb(_clone)(other); + *self = _cx_memb(_clone)(*other); } STC_INLINE void @@ -184,7 +184,7 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { #endif // !_i_no_clone #ifndef _i_isset - #if !defined _i_no_clone && !defined _i_no_emplace + #if !defined _i_no_emplace STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); #endif STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped); @@ -275,7 +275,7 @@ static i_size _cx_memb(_new_node_)(_cx_self* self, int level) { i_size tn; struct csmap_rep *rep = _csmap_rep(self); if (rep->disp) { - tn = rep->disp; + tn = (i_size)rep->disp; rep->disp = self->nodes[tn].link[1]; } else { if (rep->head == rep->cap) @@ -324,7 +324,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { return res; } - #if !defined _i_no_clone && !defined _i_no_emplace + #if !defined _i_no_emplace STC_DEF _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped) { _cx_result res = _cx_memb(_insert_entry_)(self, rkey); @@ -337,7 +337,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { } return res; } - #endif // !_i_no_clone && !_i_no_emplace + #endif // !_i_no_emplace #endif // !_i_isset STC_DEF _cx_value* diff --git a/include/stc/cstack.h b/include/stc/cstack.h index b9cff196..46d209ca 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -34,12 +34,29 @@ #include "template.h" #if !c_option(c_is_fwd) -_cx_deftypes(_c_cstack_types, _cx_self, i_key); +#ifdef i_cap + #define _i_no_clone + _cx_deftypes(_c_cstack_fixed, _cx_self, i_key, i_cap); +#else + _cx_deftypes(_c_cstack_types, _cx_self, i_key); +#endif #endif 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(_init)(void) { + _cx_self s; s.size = 0; +#ifndef i_cap + s.capacity = 0; s.data = NULL; +#endif + return s; +} + +#ifdef i_cap +STC_INLINE void _cx_memb(_inits)(_cx_self* self) + { self->size = 0; } +#else +STC_INLINE void _cx_memb(_inits)(_cx_self* self) + { self->size = 0; self->capacity = 0; self->data = NULL; } STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { _cx_self out = {(_cx_value *) c_malloc(cap*sizeof(i_key)), 0, cap}; @@ -51,6 +68,7 @@ STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_key null) { while (size) out.data[--size] = null; return out; } +#endif STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_value *p = self->data + self->size; @@ -58,22 +76,33 @@ STC_INLINE void _cx_memb(_clear)(_cx_self* self) { self->size = 0; } -STC_INLINE void _cx_memb(_drop)(_cx_self* self) - { _cx_memb(_clear)(self); c_free(self->data); } +STC_INLINE void _cx_memb(_drop)(_cx_self* self) { + _cx_memb(_clear)(self); +#ifndef i_cap + c_free(self->data); +#endif +} -STC_INLINE size_t _cx_memb(_size)(_cx_self v) - { return v.size; } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) + { return self->size; } -STC_INLINE bool _cx_memb(_empty)(_cx_self v) - { return !v.size; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) + { return !self->size; } -STC_INLINE size_t _cx_memb(_capacity)(_cx_self v) - { return v.capacity; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { +#ifndef i_cap + return self->capacity; +#else + return i_cap; +#endif +} STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { if (n < self->size) return true; +#ifndef i_cap _cx_value *t = (_cx_value *)c_realloc(self->data, n*sizeof *t); if (t) { self->capacity = n, self->data = t; return true; } +#endif return false; } @@ -88,11 +117,11 @@ _cx_memb(_append_uninit)(_cx_self *self, size_t n) { STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } -STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self) +STC_INLINE const _cx_value* _cx_memb(_top)(const _cx_self* self) { return &self->data[self->size - 1]; } STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { - if (self->size == self->capacity) + if (self->size == _cx_memb(_capacity)(self)) if (!_cx_memb(_reserve)(self, self->size*3/2 + 4)) return NULL; _cx_value* vp = self->data + self->size++; @@ -111,7 +140,6 @@ STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) { assert(idx < self->size); return self->data + 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_keyfrom(raw)); } @@ -119,31 +147,36 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_keyfrom(raw)); } #endif // !_i_no_emplace +#if !defined _i_no_clone 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}; + _cx_self out = {(_cx_value *)c_malloc(v.size*sizeof(_cx_value)), v.size, v.size}; if (!out.data) out.capacity = 0; else for (size_t i = 0; i < v.size; ++v.data) out.data[i++] = i_keyclone((*v.data)); return out; } -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 void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); + *self = _cx_memb(_clone)(*other); } STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } -STC_INLINE i_keyraw _cx_memb(_value_toraw)(_cx_value* val) +STC_INLINE i_keyraw _cx_memb(_value_toraw)(const _cx_value* val) { return i_keyto(val); } #endif // !_i_no_clone STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) - { return c_make(_cx_iter){self->data}; } + { return c_make(_cx_iter){(_cx_value*)self->data}; } + STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_make(_cx_iter){self->data + self->size}; } + { return c_make(_cx_iter){(_cx_value*)self->data + self->size}; } + STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; } + STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) { it.ref += offs; return it; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index ed533064..441fe94a 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -300,9 +300,6 @@ STC_INLINE char* cstr_assign_s(cstr* self, cstr s) { return cstr_assign_n(self, sv.str, sv.size); } -STC_INLINE void cstr_copy(cstr* self, cstr s) - { cstr_assign_s(self, s); } - STC_INLINE char* cstr_append(cstr* self, const char* str) { return cstr_append_n(self, str, strlen(str)); } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 77462a30..19303125 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -93,17 +93,6 @@ STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw r STC_API _cx_iter _cx_memb(_binary_search_in)(_cx_iter it1, _cx_iter it2, _cx_raw raw, _cx_iter* lower_bound); #endif -#if !defined _i_no_clone -STC_API _cx_self _cx_memb(_clone)(_cx_self cx); -STC_API _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, - const _cx_value* p1, const _cx_value* p2); -STC_INLINE i_key _cx_memb(_value_clone)(_cx_value 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); - } #if !defined _i_no_emplace STC_API _cx_value* _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2); @@ -119,17 +108,29 @@ STC_INLINE _cx_value* _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1); } +#endif // !_i_no_emplace + +#if !defined _i_no_clone +STC_API _cx_self _cx_memb(_clone)(_cx_self cx); +STC_API _cx_value* _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos, + const _cx_value* p1, const _cx_value* p2); +STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) + { return i_keyclone(val); } +STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { + if (self->data == other->data) return; + _cx_memb(_drop)(self); + *self = _cx_memb(_clone)(*other); + } STC_INLINE _cx_value* _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); } -#endif // !_i_no_emplace #endif // !_i_no_clone -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 _cx_raw _cx_memb(_value_toraw)(_cx_value* val) { return i_keyto(val); } +STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cvec_rep_(cx)->size; } +STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return cvec_rep_(cx)->cap; } +STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return !cvec_rep_(cx)->size; } +STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _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) @@ -146,7 +147,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) STC_INLINE void _cx_memb(_next)(_cx_iter* it) { ++it->ref; } STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs) { it.ref += offs; return it; } -STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { return it.ref - cx.data; } +STC_INLINE size_t _cx_memb(_index)(const _cx_self* cx, _cx_iter it) { return it.ref - cx->data; } STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_key null) { @@ -163,13 +164,13 @@ _cx_memb(_with_capacity)(const size_t cap) { } STC_INLINE void -_cx_memb(_shrink_to_fit)(_cx_self *self) { - _cx_memb(_reserve)(self, _cx_memb(_size)(*self)); +_cx_memb(_shrink_to_fit)(_cx_self* self) { + _cx_memb(_reserve)(self, _cx_memb(_size)(self)); } STC_INLINE _cx_value* -_cx_memb(_append_uninit)(_cx_self *self, const size_t n) { - return _cx_memb(_insert_uninit_p)(self, self->data + _cx_memb(_size)(*self), n); +_cx_memb(_append_uninit)(_cx_self* self, const size_t n) { + return _cx_memb(_insert_uninit_p)(self, self->data + _cx_memb(_size)(self), n); } STC_INLINE _cx_value* diff --git a/include/stc/forward.h b/include/stc/forward.h index 18c3d7b0..9c462b8e 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -192,6 +192,14 @@ typedef union { size_t size, capacity; \ } SELF +#define _c_cstack_fixed(SELF, VAL, CAP) \ + typedef VAL SELF##_value; \ + typedef struct { SELF##_value *ref; } SELF##_iter; \ + typedef struct SELF { \ + SELF##_value data[CAP]; \ + size_t size; \ + } SELF + #define _c_cpque_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct SELF { \ diff --git a/include/stc/template.h b/include/stc/template.h index 217b29d3..93f5e4c2 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -259,6 +259,7 @@ #undef i_cmp #undef i_eq #undef i_hash +#undef i_cap #undef i_size #undef i_val |
