diff options
| author | Tyge Løvset <[email protected]> | 2023-02-15 15:40:08 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-15 15:40:08 +0100 |
| commit | 290055ad96c22fd9dfb539d0217e45cd2f1cdb91 (patch) | |
| tree | ce6d1398ca4a08ddcb42653ad46c0ab76655ee47 /include | |
| parent | e456085a392d063df1a2495422523a2474ea6a18 (diff) | |
| download | STC-modified-290055ad96c22fd9dfb539d0217e45cd2f1cdb91.tar.gz STC-modified-290055ad96c22fd9dfb539d0217e45cd2f1cdb91.zip | |
Cleaned up in size-types. API always uses intptr_t as default for all containers.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cbits.h | 77 | ||||
| -rw-r--r-- | include/stc/cmap.h | 34 | ||||
| -rw-r--r-- | include/stc/csmap.h | 31 |
3 files changed, 81 insertions, 61 deletions
diff --git a/include/stc/cbits.h b/include/stc/cbits.h index c3167dae..e2d97f8c 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -49,14 +49,16 @@ int main() { } } */ - #ifndef CBITS_H_INCLUDED #include "ccommon.h" #include <stdlib.h> #include <string.h> +#ifndef i_ssize +#define i_ssize intptr_t +#endif #define _cbits_bit(i) ((uint64_t)1 << ((i) & 63)) -#define _cbits_words(n) (int64_t)(((n) + 63)>>6) +#define _cbits_words(n) (i_ssize)(((n) + 63)>>6) #define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t)) #if defined(__GNUC__) || defined(__clang__) @@ -73,23 +75,23 @@ int main() { } #endif -STC_INLINE int64_t _cbits_count(const uint64_t* set, const int64_t sz) { - const int64_t n = sz>>6; - int64_t count = 0; - for (int64_t i = 0; i < n; ++i) +STC_INLINE i_ssize _cbits_count(const uint64_t* set, const i_ssize sz) { + const i_ssize n = sz>>6; + i_ssize count = 0; + for (i_ssize i = 0; i < n; ++i) count += cpopcount64(set[i]); if (sz & 63) count += cpopcount64(set[n] & (_cbits_bit(sz) - 1)); return count; } -STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz, - char* out, int64_t start, int64_t stop) { +STC_INLINE char* _cbits_to_str(const uint64_t* set, const i_ssize sz, + char* out, i_ssize start, i_ssize stop) { if (stop > sz) stop = sz; assert(start <= stop); c_memset(out, '0', stop - start); - for (int64_t i = start; i < stop; ++i) + for (i_ssize i = start; i < stop; ++i) if ((set[i>>6] & _cbits_bit(i)) != 0) out[i - start] = '1'; out[stop - start] = '\0'; @@ -97,8 +99,8 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz, } #define _cbits_OPR(OPR, VAL) \ - const int64_t n = sz>>6; \ - for (int64_t i = 0; i < n; ++i) \ + const i_ssize n = sz>>6; \ + for (i_ssize i = 0; i < n; ++i) \ if ((set[i] OPR other[i]) != VAL) \ return false; \ if (!(sz & 63)) \ @@ -106,10 +108,10 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz, const uint64_t i = (uint64_t)n, m = _cbits_bit(sz) - 1; \ return ((set[i] OPR other[i]) & m) == (VAL & m) -STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const int64_t sz) +STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const i_ssize sz) { _cbits_OPR(|, set[i]); } -STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const int64_t sz) +STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const i_ssize sz) { _cbits_OPR(&, 0); } #endif // CBITS_H_INCLUDED @@ -121,12 +123,12 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons #define _i_assert(x) assert(x) #define i_type cbits -struct { uint64_t *data64; int64_t _size; } typedef i_type; +struct { uint64_t *data64; i_ssize _size; } typedef i_type; -STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; } -STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; } -STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } -STC_INLINE int64_t cbits_size(const cbits* self) { return self->_size; } +STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; } +STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; } +STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } +STC_INLINE i_ssize cbits_size(const cbits* self) { return self->_size; } STC_INLINE cbits* cbits_take(cbits* self, cbits other) { if (self->data64 != other.data64) { @@ -137,7 +139,7 @@ STC_INLINE cbits* cbits_take(cbits* self, cbits other) { } STC_INLINE cbits cbits_clone(cbits other) { - const int64_t bytes = _cbits_bytes(other._size); + const i_ssize bytes = _cbits_bytes(other._size); cbits set = {(uint64_t *)c_memcpy(c_malloc(bytes), other.data64, bytes), other._size}; return set; } @@ -151,8 +153,8 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) { return self; } -STC_INLINE void cbits_resize(cbits* self, const int64_t size, const bool value) { - const int64_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); +STC_INLINE void cbits_resize(cbits* self, const i_ssize size, const bool value) { + const i_ssize new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); self->data64 = (uint64_t *)c_realloc(self->data64, new_n*8); self->_size = size; if (new_n >= old_n) { @@ -174,13 +176,13 @@ STC_INLINE cbits cbits_move(cbits* self) { return tmp; } -STC_INLINE cbits cbits_with_size(const int64_t size, const bool value) { +STC_INLINE cbits cbits_with_size(const i_ssize size, const bool value) { cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_all(&set, value); return set; } -STC_INLINE cbits cbits_with_pattern(const int64_t size, const uint64_t pattern) { +STC_INLINE cbits cbits_with_pattern(const i_ssize size, const uint64_t pattern) { cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_pattern(&set, pattern); return set; @@ -198,7 +200,7 @@ struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type; STC_INLINE i_type _i_memb(_init)(void) { return c_LITERAL(i_type){0}; } STC_INLINE void _i_memb(_create)(i_type* self) {} STC_INLINE void _i_memb(_drop)(i_type* self) {} -STC_INLINE int64_t _i_memb(_size)(const i_type* self) { return i_capacity; } +STC_INLINE i_ssize _i_memb(_size)(const i_type* self) { return i_capacity; } 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) @@ -213,13 +215,13 @@ STC_INLINE i_type* _i_memb(_copy)(i_type* self, const i_type* other) 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 int64_t size, const bool value) { +STC_INLINE i_type _i_memb(_with_size)(const i_ssize size, const bool value) { assert(size <= i_capacity); i_type set; _i_memb(_set_all)(&set, value); return set; } -STC_INLINE i_type _i_memb(_with_pattern)(const int64_t size, const uint64_t pattern) { +STC_INLINE i_type _i_memb(_with_pattern)(const i_ssize size, const uint64_t pattern) { assert(size <= i_capacity); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; @@ -232,31 +234,31 @@ STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value) { c_memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); } STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern) { - int64_t n = _cbits_words(_i_memb(_size)(self)); + i_ssize n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] = pattern; } -STC_INLINE bool _i_memb(_test)(const i_type* self, const int64_t i) +STC_INLINE bool _i_memb(_test)(const i_type* self, const i_ssize i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE bool _i_memb(_at)(const i_type* self, const int64_t i) +STC_INLINE bool _i_memb(_at)(const i_type* self, const i_ssize i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE void _i_memb(_set)(i_type *self, const int64_t i) +STC_INLINE void _i_memb(_set)(i_type *self, const i_ssize i) { self->data64[i>>6] |= _cbits_bit(i); } -STC_INLINE void _i_memb(_reset)(i_type *self, const int64_t i) +STC_INLINE void _i_memb(_reset)(i_type *self, const i_ssize i) { self->data64[i>>6] &= ~_cbits_bit(i); } -STC_INLINE void _i_memb(_set_value)(i_type *self, const int64_t i, const bool b) { +STC_INLINE void _i_memb(_set_value)(i_type *self, const i_ssize i, const bool b) { self->data64[i>>6] ^= ((uint64_t)-(int)b ^ self->data64[i>>6]) & _cbits_bit(i); } -STC_INLINE void _i_memb(_flip)(i_type *self, const int64_t i) +STC_INLINE void _i_memb(_flip)(i_type *self, const i_ssize i) { self->data64[i>>6] ^= _cbits_bit(i); } STC_INLINE void _i_memb(_flip_all)(i_type *self) { - int64_t n = _cbits_words(_i_memb(_size)(self)); + i_ssize n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= ~(uint64_t)0; } @@ -270,19 +272,19 @@ STC_INLINE i_type _i_memb(_from)(const char* str) { /* Intersection */ STC_INLINE void _i_memb(_intersect)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - int64_t n = _cbits_words(_i_memb(_size)(self)); + i_ssize n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] &= other->data64[n]; } /* Union */ STC_INLINE void _i_memb(_union)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - int64_t n = _cbits_words(_i_memb(_size)(self)); + i_ssize n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] |= other->data64[n]; } /* Exclusive disjunction */ STC_INLINE void _i_memb(_xor)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - int64_t n = _cbits_words(_i_memb(_size)(self)); + i_ssize n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= other->data64[n]; } @@ -303,6 +305,7 @@ STC_INLINE bool _i_memb(_disjoint)(const i_type* self, const i_type* other) { } #define CBITS_H_INCLUDED +#undef _i_size #undef _i_memb #undef _i_assert #undef i_capacity diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 799df1ec..5e370167 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -75,9 +75,11 @@ typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t; #endif #ifndef i_ssize #define i_ssize int32_t + #define _i_size intptr_t #define _i_expandby 1 #else #define _i_expandby 2 + #define _i_size i_ssize #endif #include "priv/template.h" #ifndef i_hash_functor @@ -102,13 +104,13 @@ typedef _i_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(int64_t cap); +STC_API _cx_self _cx_memb(_with_capacity)(_i_size cap); #if !defined i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self map); #endif 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, int64_t capacity); +STC_API bool _cx_memb(_reserve)(_cx_self* self, _i_size capacity); STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr); STC_API _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_rawkey rkey); STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val); @@ -117,17 +119,22 @@ STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){0}; } STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } STC_INLINE float _cx_memb(_max_load_factor)(const _cx_self* self) { return (float)(i_max_load_factor); } STC_INLINE bool _cx_memb(_empty)(const _cx_self* map) { return !map->size; } -STC_INLINE int64_t _cx_memb(_size)(const _cx_self* map) { return map->size; } -STC_INLINE int64_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } -STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* map) - { return (int64_t)((float)map->bucket_count * (i_max_load_factor)); } +STC_INLINE _i_size _cx_memb(_size)(const _cx_self* map) { return map->size; } +STC_INLINE _i_size _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } +STC_INLINE _i_size _cx_memb(_capacity)(const _cx_self* map) + { return (_i_size)((float)map->bucket_count * (i_max_load_factor)); } 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); + STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped); #if !defined i_no_emplace - STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); + STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); + STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped) + { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } + #else + STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped) + { return _cx_memb(_insert_or_assign)(self, key, mapped); } #endif STC_INLINE const _cx_mapped* @@ -201,7 +208,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { return _res; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -214,7 +221,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { @@ -237,7 +244,7 @@ _cx_memb(_next)(_cx_iter* it) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, uint64_t n) { +_cx_memb(_advance)(_cx_iter it, size_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; } @@ -300,7 +307,7 @@ STC_INLINE uint64_t next_power_of_2(uint64_t n) { #endif // CMAP_H_INCLUDED STC_DEF _cx_self -_cx_memb(_with_capacity)(const int64_t cap) { +_cx_memb(_with_capacity)(const _i_size cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -411,7 +418,7 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const int64_t newcap) { +_cx_memb(_reserve)(_cx_self* self, const _i_size newcap) { const i_ssize _oldbuckets = self->bucket_count, _newcap = (i_ssize)newcap; if (_newcap != self->size && _newcap <= _oldbuckets) return true; @@ -469,6 +476,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { #undef i_max_load_factor #undef i_hash_functor #undef i_eq_functor +#undef _i_size #undef _i_isset #undef _i_ismap #undef _i_ishash diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 4344ceb9..48e1eb62 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -75,6 +75,9 @@ int main(void) { #endif #ifndef i_ssize #define i_ssize int32_t + #define _i_size intptr_t +#else + #define _i_size i_ssize #endif #include "priv/template.h" #ifndef i_cmp_functor @@ -110,7 +113,7 @@ 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)); STC_API _cx_result _cx_memb(_push)(_cx_self* self, _cx_value _val); STC_API void _cx_memb(_drop)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t cap); +STC_API bool _cx_memb(_reserve)(_cx_self* self, _i_size cap); STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_rawkey rkey, _cx_iter* out); STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, _cx_rawkey rkey); STC_API _cx_value* _cx_memb(_front)(const _cx_self* self); @@ -121,8 +124,8 @@ STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return cx->size == 0; } -STC_INLINE int64_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } -STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } +STC_INLINE _i_size _cx_memb(_size)(const _cx_self* cx) { return cx->size; } +STC_INLINE _i_size _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } 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; } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) @@ -133,7 +136,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey) { _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); } STC_INLINE _cx_self -_cx_memb(_with_capacity)(const int64_t cap) { +_cx_memb(_with_capacity)(const _i_size cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -180,10 +183,15 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) { #endif // !i_no_clone #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_emplace - STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); + STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped); + STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, _cx_rawkey rkey, i_valraw rmapped) + { return _cx_memb(_emplace_or_assign)(self, rkey, rmapped); } + #else + STC_INLINE _cx_result _cx_memb(_put)(_cx_self* self, i_key key, i_val mapped) + { return _cx_memb(_insert_or_assign)(self, key, mapped); } #endif - STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped); STC_INLINE const _cx_mapped* _cx_memb(_at)(const _cx_self* self, _cx_rawkey rkey) @@ -212,7 +220,7 @@ _cx_memb(_end)(const _cx_self* self) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, uint64_t n) { +_cx_memb(_advance)(_cx_iter it, size_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; @@ -228,8 +236,8 @@ _cx_memb(_init)(void) { } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const int64_t cap) { - if ((i_ssize)cap <= self->cap) +_cx_memb(_reserve)(_cx_self* self, const _i_size cap) { + if (cap <= self->cap) return false; _cx_node* nodes = (_cx_node*)i_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node)); if (!nodes) @@ -297,7 +305,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { return _res; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -310,7 +318,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #ifndef _i_isset @@ -587,6 +595,7 @@ _cx_memb(_drop)(_cx_self* self) { #endif // i_implement #undef i_cmp_functor +#undef _i_size #undef _i_isset #undef _i_ismap #undef _i_keyref |
