diff options
| author | Tyge Løvset <[email protected]> | 2023-04-18 17:48:47 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-04-18 17:48:47 +0200 |
| commit | a913490c248f6cfdca3481d81d6d344f4d066cb9 (patch) | |
| tree | b805826d496a8d5c8a449731b6c8bc3b1d056979 /include | |
| parent | 462adebf55c77697fbb379a62941c00b876c3f6a (diff) | |
| download | STC-modified-a913490c248f6cfdca3481d81d6d344f4d066cb9.tar.gz STC-modified-a913490c248f6cfdca3481d81d6d344f4d066cb9.zip | |
Removed unneeded custom size type in maps and bits.h. Prepared for possible robin-hood impl.
Improved sso_bench.c testing string hash - twice as fast as m.ankeln robin impl !?.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cbits.h | 70 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 6 | ||||
| -rw-r--r-- | include/stc/cmap.h | 77 | ||||
| -rw-r--r-- | include/stc/csmap.h | 79 | ||||
| -rw-r--r-- | include/stc/forward.h | 23 | ||||
| -rw-r--r-- | include/stc/priv/template.h | 4 | ||||
| -rw-r--r-- | include/stc/priv/template2.h | 1 |
7 files changed, 118 insertions, 142 deletions
diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 826df847..19e281a8 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -54,11 +54,8 @@ int main() { #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) (i_ssize)(((n) + 63)>>6) +#define _cbits_words(n) (int64_t)(((n) + 63)>>6) #define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t)) #if defined(__GNUC__) @@ -80,23 +77,23 @@ int main() { } #endif -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) +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) 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 i_ssize sz, - char* out, i_ssize start, i_ssize stop) { +STC_INLINE char* _cbits_to_str(const uint64_t* set, const int64_t sz, + char* out, int64_t start, int64_t stop) { if (stop > sz) stop = sz; assert(start <= stop); c_memset(out, '0', stop - start); - for (i_ssize i = start; i < stop; ++i) + for (int64_t i = start; i < stop; ++i) if ((set[i>>6] & _cbits_bit(i)) != 0) out[i - start] = '1'; out[stop - start] = '\0'; @@ -104,8 +101,8 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const i_ssize sz, } #define _cbits_OPR(OPR, VAL) \ - const i_ssize n = sz>>6; \ - for (i_ssize i = 0; i < n; ++i) \ + const int64_t n = sz>>6; \ + for (int64_t i = 0; i < n; ++i) \ if ((set[i] OPR other[i]) != VAL) \ return false; \ if (!(sz & 63)) \ @@ -113,10 +110,10 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const i_ssize 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 i_ssize sz) +STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const int64_t sz) { _cbits_OPR(|, set[i]); } -STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const i_ssize sz) +STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const int64_t sz) { _cbits_OPR(&, 0); } #endif // CBITS_H_INCLUDED @@ -128,12 +125,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; i_ssize _size; } typedef i_type; +struct { uint64_t *data64; int64_t _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 i_ssize cbits_size(const cbits* self) { return self->_size; } +STC_INLINE int64_t cbits_size(const cbits* self) { return self->_size; } STC_INLINE cbits* cbits_take(cbits* self, cbits other) { if (self->data64 != other.data64) { @@ -144,7 +141,7 @@ STC_INLINE cbits* cbits_take(cbits* self, cbits other) { } STC_INLINE cbits cbits_clone(cbits other) { - const i_ssize bytes = _cbits_bytes(other._size); + const int64_t bytes = _cbits_bytes(other._size); cbits set = {(uint64_t *)c_memcpy(c_malloc(bytes), other.data64, bytes), other._size}; return set; } @@ -158,8 +155,8 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) { return self; } -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); +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); self->data64 = (uint64_t *)c_realloc(self->data64, new_n*8); self->_size = size; if (new_n >= old_n) { @@ -181,13 +178,13 @@ STC_INLINE cbits cbits_move(cbits* self) { return tmp; } -STC_INLINE cbits cbits_with_size(const i_ssize size, const bool value) { +STC_INLINE cbits cbits_with_size(const int64_t 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 i_ssize size, const uint64_t pattern) { +STC_INLINE cbits cbits_with_pattern(const int64_t size, const uint64_t pattern) { cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_pattern(&set, pattern); return set; @@ -205,7 +202,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 i_ssize _i_memb(_size)(const i_type* self) { return i_capacity; } +STC_INLINE int64_t _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) @@ -220,13 +217,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 i_ssize size, const bool value) { +STC_INLINE i_type _i_memb(_with_size)(const int64_t 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 i_ssize size, const uint64_t pattern) { +STC_INLINE i_type _i_memb(_with_pattern)(const int64_t size, const uint64_t pattern) { assert(size <= i_capacity); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; @@ -239,31 +236,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) { - i_ssize n = _cbits_words(_i_memb(_size)(self)); + int64_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] = pattern; } -STC_INLINE bool _i_memb(_test)(const i_type* self, const i_ssize i) +STC_INLINE bool _i_memb(_test)(const i_type* self, const int64_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE bool _i_memb(_at)(const i_type* self, const i_ssize i) +STC_INLINE bool _i_memb(_at)(const i_type* self, const int64_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE void _i_memb(_set)(i_type *self, const i_ssize i) +STC_INLINE void _i_memb(_set)(i_type *self, const int64_t i) { self->data64[i>>6] |= _cbits_bit(i); } -STC_INLINE void _i_memb(_reset)(i_type *self, const i_ssize i) +STC_INLINE void _i_memb(_reset)(i_type *self, const int64_t i) { self->data64[i>>6] &= ~_cbits_bit(i); } -STC_INLINE void _i_memb(_set_value)(i_type *self, const i_ssize i, const bool b) { +STC_INLINE void _i_memb(_set_value)(i_type *self, const int64_t 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 i_ssize i) +STC_INLINE void _i_memb(_flip)(i_type *self, const int64_t i) { self->data64[i>>6] ^= _cbits_bit(i); } STC_INLINE void _i_memb(_flip_all)(i_type *self) { - i_ssize n = _cbits_words(_i_memb(_size)(self)); + int64_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= ~(uint64_t)0; } @@ -277,19 +274,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); - i_ssize n = _cbits_words(_i_memb(_size)(self)); + int64_t 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); - i_ssize n = _cbits_words(_i_memb(_size)(self)); + int64_t 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); - i_ssize n = _cbits_words(_i_memb(_size)(self)); + int64_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= other->data64[n]; } @@ -312,7 +309,6 @@ STC_INLINE bool _i_memb(_disjoint)(const i_type* self, const i_type* other) { #pragma GCC diagnostic pop #endif #define CBITS_H_INCLUDED -#undef _i_size #undef _i_memb #undef _i_assert #undef i_capacity diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index bc12e642..0735e855 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -147,13 +147,13 @@ STC_INLINE uint64_t cfasthash(const void* key, intptr_t len) { case 0: return 1; } const uint8_t *x = (const uint8_t*)key; - uint64_t h = 0xcbf29ce484222325 + *x*0x811c9dc5UL, n = (uint64_t)len >> 3; + uint64_t h = *x*0x811c9dc5ULL, n = (uint64_t)len >> 3; len &= 7; while (n--) { memcpy(&u8, x, 8), x += 8; - h = (h ^ u8)*0x01000193UL; + h = (h ^ u8)*0x01000193ULL; } - while (len--) h = (h ^ *x++)*0x01000193UL; + while (len--) h = (h ^ *x++)*0x01000193ULL; return h; } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 14782b71..cfed5540 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -73,17 +73,12 @@ typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t; #ifndef i_max_load_factor #define i_max_load_factor 0.85f #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 +#ifndef i_expandby + #define i_expandby 1 #endif #include "priv/template.h" #ifndef i_is_forward - _cx_deftypes(_c_chash_types, _cx_self, i_key, i_val, i_ssize, _i_MAP_ONLY, _i_SET_ONLY); + _cx_deftypes(_c_chash_types, _cx_self, i_key, i_val, _i_MAP_ONLY, _i_SET_ONLY); #endif _i_MAP_ONLY( struct _cx_value { @@ -98,25 +93,25 @@ typedef _i_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(_i_size cap); +STC_API _cx_self _cx_memb(_with_capacity)(intptr_t 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, _i_size capacity); +STC_API bool _cx_memb(_reserve)(_cx_self* self, intptr_t capacity); STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_keyraw* rkeyptr); STC_API _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey); STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val); STC_INLINE _cx_self _cx_memb(_init)(void) { _cx_self map = {0}; return map; } -STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } +STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, (intptr_t)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 _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 intptr_t _cx_memb(_size)(const _cx_self* map) { return (intptr_t)map->size; } +STC_INLINE intptr_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket.count; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* map) + { return (intptr_t)((float)map->bucket.count * (i_max_load_factor)); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_keyraw rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } @@ -197,7 +192,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, _i_size n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -210,11 +205,11 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t 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) { - _cx_iter it = {self->table, self->table+self->bucket_count, self->_hashx}; + _cx_iter it = {self->table, self->table+self->bucket.count, self->_hashx}; if (it._hx) while (*it._hx == 0) ++it.ref, ++it._hx; @@ -243,7 +238,7 @@ _cx_memb(_find)(const _cx_self* self, _cx_keyraw rkey) { int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return c_LITERAL(_cx_iter){self->table + idx, - self->table + self->bucket_count, + self->table + self->bucket.count, self->_hashx + idx}; return _cx_memb(_end)(self); } @@ -306,7 +301,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 _i_size cap) { +_cx_memb(_with_capacity)(const intptr_t cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -315,7 +310,7 @@ _cx_memb(_with_capacity)(const _i_size cap) { STC_INLINE void _cx_memb(_wipe_)(_cx_self* self) { if (self->size == 0) return; - _cx_value* e = self->table, *end = e + self->bucket_count; + _cx_value* e = self->table, *end = e + self->bucket.count; uint8_t *hx = self->_hashx; for (; e != end; ++e) if (*hx++) @@ -331,7 +326,7 @@ STC_DEF void _cx_memb(_drop)(_cx_self* self) { STC_DEF void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_wipe_)(self); self->size = 0; - c_memset(self->_hashx, 0, self->bucket_count); + c_memset(self->_hashx, 0, self->bucket.count); } #ifndef _i_isset @@ -366,8 +361,8 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { STC_DEF chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_keyraw* rkeyptr) { const uint64_t _hash = i_hash(rkeyptr); - int64_t _cap = self->bucket_count; - chash_bucket_t b = {c_PASTE(fastrange_,_i_expandby)(_hash, (uint64_t)_cap), (uint8_t)(_hash | 0x80)}; + int64_t _cap = self->bucket.count; + chash_bucket_t b = {c_PASTE(fastrange_,i_expandby)(_hash, (uint64_t)_cap), (uint8_t)(_hash | 0x80)}; const uint8_t* _hx = self->_hashx; while (_hx[b.idx]) { if (_hx[b.idx] == b.hx) { @@ -384,8 +379,8 @@ _cx_memb(_bucket_)(const _cx_self* self, const _cx_keyraw* rkeyptr) { STC_DEF _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey) { _cx_result res = {NULL}; - if (self->size + 2 > (i_ssize)((float)self->bucket_count * (i_max_load_factor))) - if (!_cx_memb(_reserve)(self, self->size*3/2)) + if (self->size + 2 > (intptr_t)((float)self->bucket.count * (i_max_load_factor))) + if (!_cx_memb(_reserve)(self, (intptr_t)(self->size*3/2))) return res; chash_bucket_t b = _cx_memb(_bucket_)(self, &rkey); @@ -401,11 +396,11 @@ _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self m) { if (m.table) { - _cx_value *t = (_cx_value *)i_malloc(c_sizeof(_cx_value)*m.bucket_count), - *dst = t, *m_end = m.table + m.bucket_count; - uint8_t *h = (uint8_t *)c_memcpy(i_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1); + _cx_value *t = (_cx_value *)i_malloc(c_sizeof(_cx_value)*m.bucket.count), + *dst = t, *m_end = m.table + m.bucket.count; + uint8_t *h = (uint8_t *)c_memcpy(i_malloc(m.bucket.count + 1), m._hashx, m.bucket.count + 1); if (!(t && h)) - { i_free(t), i_free(h), t = 0, h = 0, m.bucket_count = 0; } + { i_free(t), i_free(h), t = 0, h = 0, m.bucket.count = 0; } else for (; m.table != m_end; ++m.table, ++m._hashx, ++dst) if (*m._hashx) @@ -417,27 +412,27 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const _i_size newcap) { - const i_ssize _oldbuckets = self->bucket_count, _newcap = (i_ssize)newcap; +_cx_memb(_reserve)(_cx_self* self, const intptr_t newcap) { + const intptr_t _oldbuckets = self->bucket.count, _newcap = newcap; if (_newcap != self->size && _newcap <= _oldbuckets) return true; - i_ssize _nbuckets = (i_ssize)((float)_newcap / (i_max_load_factor)) + 4; - #if _i_expandby == 2 - _nbuckets = (i_ssize)next_power_of_2(_nbuckets); + uintptr_t _nbuckets = (uintptr_t)((float)_newcap / (i_max_load_factor)) + 4; + #if i_expandby == 2 + _nbuckets = (intptr_t)next_power_of_2(_nbuckets); #else _nbuckets |= 1; #endif _cx_self m = { (_cx_value *)i_malloc(c_sizeof(_cx_value)*_nbuckets), (uint8_t *)i_calloc(_nbuckets + 1, 1), - self->size, _nbuckets, + self->size, {_nbuckets & ((1ULL << 48) - 1)} }; bool ok = m.table && m._hashx; if (ok) { /* Rehash: */ m._hashx[_nbuckets] = 0xff; const _cx_value* e = self->table; const uint8_t* h = self->_hashx; - for (i_ssize i = 0; i < _oldbuckets; ++i, ++e) if (*h++) { + for (intptr_t i = 0; i < _oldbuckets; ++i, ++e) if (*h++) { _cx_keyraw r = i_keyto(_i_keyref(e)); chash_bucket_t b = _cx_memb(_bucket_)(&m, &r); m.table[b.idx] = *e; @@ -452,8 +447,8 @@ _cx_memb(_reserve)(_cx_self* self, const _i_size newcap) { STC_DEF void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { - i_ssize i = (i_ssize)(_val - self->table), j = i, k; - const i_ssize _cap = self->bucket_count; + intptr_t i = (intptr_t)(_val - self->table), j = i, k; + const intptr_t _cap = self->bucket.count; _cx_value* _slot = self->table; uint8_t* _hashx = self->_hashx; _cx_memb(_value_drop)(_val); @@ -463,7 +458,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { if (! _hashx[j]) break; const _cx_keyraw _raw = i_keyto(_i_keyref(_slot + j)); - k = (i_ssize)c_PASTE(fastrange_,_i_expandby)(i_hash((&_raw)), (uint64_t)_cap); + k = (intptr_t)c_PASTE(fastrange_,i_expandby)(i_hash((&_raw)), (uint64_t)_cap); if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */ _slot[i] = _slot[j], _hashx[i] = _hashx[j], i = j; } @@ -473,7 +468,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { #endif // i_implement #undef i_max_load_factor -#undef _i_size +#undef i_expandby #undef _i_isset #undef _i_ismap #undef _i_ishash diff --git a/include/stc/csmap.h b/include/stc/csmap.h index ebfe8d44..dc0387f7 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -70,15 +70,9 @@ int main(void) { #define _i_SET_ONLY c_false #define _i_keyref(vp) (&(vp)->first) #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_is_forward - _cx_deftypes(_c_aatree_types, _cx_self, i_key, i_val, i_ssize, _i_MAP_ONLY, _i_SET_ONLY); + _cx_deftypes(_c_aatree_types, _cx_self, i_key, i_val, _i_MAP_ONLY, _i_SET_ONLY); #endif _i_MAP_ONLY( struct _cx_value { @@ -86,7 +80,7 @@ _i_MAP_ONLY( struct _cx_value { _cx_mapped second; }; ) struct _cx_node { - i_ssize link[2]; + int32_t link[2]; int8_t level; _cx_value value; }; @@ -106,7 +100,7 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self tree); 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, _i_size cap); +STC_API bool _cx_memb(_reserve)(_cx_self* self, intptr_t cap); STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_keyraw rkey, _cx_iter* out); STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, _cx_keyraw rkey); STC_API _cx_value* _cx_memb(_front)(const _cx_self* self); @@ -118,8 +112,8 @@ STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE _cx_self _cx_memb(_init)(void) { _cx_self tree = {0}; return tree; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return cx->size == 0; } -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 intptr_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_keyraw rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_keyraw rkey) @@ -130,7 +124,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_keyraw rkey) { _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); } STC_INLINE _cx_self -_cx_memb(_with_capacity)(const _i_size cap) { +_cx_memb(_with_capacity)(const intptr_t cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -226,7 +220,7 @@ _cx_memb(_eq)(const _cx_self* self, const _cx_self* other) { return true; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -239,14 +233,14 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, _i_size n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, _i_size n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const _i_size cap) { +_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { if (cap <= self->cap) return false; _cx_node* nodes = (_cx_node*)i_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node)); @@ -254,14 +248,14 @@ _cx_memb(_reserve)(_cx_self* self, const _i_size cap) { return false; nodes[0] = c_LITERAL(_cx_node){{0, 0}, 0}; self->nodes = nodes; - self->cap = (i_ssize)cap; + self->cap = (int32_t)cap; return true; } STC_DEF _cx_value* _cx_memb(_front)(const _cx_self* self) { _cx_node *d = self->nodes; - i_ssize tn = self->root; + int32_t tn = self->root; while (d[tn].link[0]) tn = d[tn].link[0]; return &d[tn].value; @@ -270,15 +264,15 @@ _cx_memb(_front)(const _cx_self* self) { STC_DEF _cx_value* _cx_memb(_back)(const _cx_self* self) { _cx_node *d = self->nodes; - i_ssize tn = self->root; + int32_t tn = self->root; while (d[tn].link[1]) tn = d[tn].link[1]; return &d[tn].value; } -static i_ssize +static int32_t _cx_memb(_new_node_)(_cx_self* self, int level) { - i_ssize tn; + int32_t tn; if (self->disp) { tn = self->disp; self->disp = self->nodes[tn].link[1]; @@ -346,7 +340,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { STC_DEF _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_keyraw rkey, _cx_iter* out) { - i_ssize tn = self->root; + int32_t tn = self->root; _cx_node *d = out->_d = self->nodes; out->_top = 0; while (tn) { @@ -366,7 +360,7 @@ _cx_memb(_lower_bound)(const _cx_self* self, _cx_keyraw rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); if (!it.ref && it._top) { - i_ssize tn = it._st[--it._top]; + int32_t tn = it._st[--it._top]; it._tn = it._d[tn].link[1]; it.ref = &it._d[tn].value; } @@ -375,7 +369,7 @@ _cx_memb(_lower_bound)(const _cx_self* self, _cx_keyraw rkey) { STC_DEF void _cx_memb(_next)(_cx_iter *it) { - i_ssize tn = it->_tn; + int32_t tn = it->_tn; if (it->_top || tn) { while (tn) { it->_st[it->_top++] = tn; @@ -388,10 +382,10 @@ _cx_memb(_next)(_cx_iter *it) { it->ref = NULL; } -STC_DEF i_ssize -_cx_memb(_skew_)(_cx_node *d, i_ssize tn) { +STC_DEF int32_t +_cx_memb(_skew_)(_cx_node *d, int32_t tn) { if (tn && d[d[tn].link[0]].level == d[tn].level) { - i_ssize tmp = d[tn].link[0]; + int32_t tmp = d[tn].link[0]; d[tn].link[0] = d[tmp].link[1]; d[tmp].link[1] = tn; tn = tmp; @@ -399,10 +393,10 @@ _cx_memb(_skew_)(_cx_node *d, i_ssize tn) { return tn; } -STC_DEF i_ssize -_cx_memb(_split_)(_cx_node *d, i_ssize tn) { +STC_DEF int32_t +_cx_memb(_split_)(_cx_node *d, int32_t tn) { if (d[d[d[tn].link[1]].link[1]].level == d[tn].level) { - i_ssize tmp = d[tn].link[1]; + int32_t tmp = d[tn].link[1]; d[tn].link[1] = d[tmp].link[0]; d[tmp].link[0] = tn; tn = tmp; @@ -411,9 +405,9 @@ _cx_memb(_split_)(_cx_node *d, i_ssize tn) { return tn; } -static i_ssize -_cx_memb(_insert_entry_i_)(_cx_self* self, i_ssize tn, const _cx_keyraw* rkey, _cx_result* _res) { - i_ssize up[64], tx = tn; +static int32_t +_cx_memb(_insert_entry_i_)(_cx_self* self, int32_t tn, const _cx_keyraw* rkey, _cx_result* _res) { + int32_t up[64], tx = tn; _cx_node* d = self->nodes; int c, top = 0, dir = 0; while (tx) { @@ -446,19 +440,19 @@ _cx_memb(_insert_entry_i_)(_cx_self* self, i_ssize tn, const _cx_keyraw* rkey, _ static _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey) { _cx_result res = {NULL}; - i_ssize tn = _cx_memb(_insert_entry_i_)(self, self->root, &rkey, &res); + int32_t tn = _cx_memb(_insert_entry_i_)(self, self->root, &rkey, &res); self->root = tn; self->size += res.inserted; return res; } -static i_ssize -_cx_memb(_erase_r_)(_cx_self *self, i_ssize tn, const _cx_keyraw* rkey, int *erased) { +static int32_t +_cx_memb(_erase_r_)(_cx_self *self, int32_t tn, const _cx_keyraw* rkey, int *erased) { _cx_node *d = self->nodes; if (tn == 0) return 0; _cx_keyraw raw = i_keyto(_i_keyref(&d[tn].value)); - i_ssize tx; int c = i_cmp((&raw), rkey); + int32_t tx; int c = i_cmp((&raw), rkey); if (c != 0) d[tn].link[c < 0] = _cx_memb(_erase_r_)(self, d[tn].link[c < 0], rkey, erased); else { @@ -495,7 +489,7 @@ _cx_memb(_erase_r_)(_cx_self *self, i_ssize tn, const _cx_keyraw* rkey, int *era STC_DEF int _cx_memb(_erase)(_cx_self* self, _cx_keyraw rkey) { int erased = 0; - i_ssize root = _cx_memb(_erase_r_)(self, self->root, &rkey, &erased); + int32_t root = _cx_memb(_erase_r_)(self, self->root, &rkey, &erased); if (!erased) return 0; self->root = root; @@ -537,11 +531,11 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) { } #if !defined i_no_clone -static i_ssize -_cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, i_ssize sn) { +static int32_t +_cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, int32_t sn) { if (sn == 0) return 0; - i_ssize tx, tn = _cx_memb(_new_node_)(self, src[sn].level); + int32_t tx, tn = _cx_memb(_new_node_)(self, src[sn].level); self->nodes[tn].value = _cx_memb(_value_clone)(src[sn].value); tx = _cx_memb(_clone_r_)(self, src, src[sn].link[0]); self->nodes[tn].link[0] = tx; tx = _cx_memb(_clone_r_)(self, src, src[sn].link[1]); self->nodes[tn].link[1] = tx; @@ -551,7 +545,7 @@ _cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, i_ssize sn) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self tree) { _cx_self clone = _cx_memb(_with_capacity)(tree.size); - i_ssize root = _cx_memb(_clone_r_)(&clone, tree.nodes, tree.root); + int32_t root = _cx_memb(_clone_r_)(&clone, tree.nodes, tree.root); clone.root = root; clone.size = tree.size; return clone; @@ -571,7 +565,7 @@ _cx_memb(_emplace)(_cx_self* self, _cx_keyraw rkey _i_MAP_ONLY(, i_valraw rmappe #endif // i_no_emplace static void -_cx_memb(_drop_r_)(_cx_node* d, i_ssize tn) { +_cx_memb(_drop_r_)(_cx_node* d, int32_t tn) { if (tn) { _cx_memb(_drop_r_)(d, d[tn].link[0]); _cx_memb(_drop_r_)(d, d[tn].link[1]); @@ -588,7 +582,6 @@ _cx_memb(_drop)(_cx_self* self) { } #endif // i_implement -#undef _i_size #undef _i_isset #undef _i_ismap #undef _i_keyref diff --git a/include/stc/forward.h b/include/stc/forward.h index 31e67e7d..e543b94a 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -29,12 +29,10 @@ #define forward_cbox(CX, VAL) _c_cbox_types(CX, VAL) #define forward_cdeq(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_clist(CX, VAL) _c_clist_types(CX, VAL) -#define forward_cmap(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, int32_t, c_true, c_false) -#define forward_cmap64(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, int64_t, c_true, c_false) -#define forward_cset(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, int32_t, c_false, c_true) -#define forward_cset64(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, int64_t, c_false, c_true) -#define forward_csmap(CX, KEY, VAL) _c_aatree_types(CX, KEY, VAL, int32_t, c_true, c_false) -#define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, int32_t, c_false, c_true) +#define forward_cmap(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, c_true, c_false) +#define forward_cset(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, c_false, c_true) +#define forward_csmap(CX, KEY, VAL) _c_aatree_types(CX, KEY, VAL, c_true, c_false) +#define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, c_false, c_true) #define forward_cstack(CX, VAL) _c_cstack_types(CX, VAL) #define forward_cpque(CX, VAL) _c_cpque_types(CX, VAL) #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) @@ -103,10 +101,9 @@ typedef union { SELF##_node *last; \ } SELF -#define _c_chash_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ +#define _c_chash_types(SELF, KEY, VAL, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_ssize; \ \ typedef SET_ONLY( SELF##_key ) \ MAP_ONLY( struct SELF##_value ) \ @@ -125,13 +122,13 @@ typedef union { typedef struct SELF { \ SELF##_value* table; \ uint8_t* _hashx; \ - SELF##_ssize size, bucket_count; \ + intptr_t size; \ + struct { uint64_t count: 48, maxdist: 16; } bucket; \ } SELF -#define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ +#define _c_aatree_types(SELF, KEY, VAL, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_ssize; \ typedef struct SELF##_node SELF##_node; \ \ typedef SET_ONLY( SELF##_key ) \ @@ -147,12 +144,12 @@ typedef union { SELF##_value *ref; \ SELF##_node *_d; \ int _top; \ - SELF##_ssize _tn, _st[36]; \ + int32_t _tn, _st[36]; \ } SELF##_iter; \ \ typedef struct SELF { \ SELF##_node *nodes; \ - SELF##_ssize root, disp, head, size, cap; \ + int32_t root, disp, head, size, cap; \ } SELF #define _c_cstack_fixed(SELF, VAL, CAP) \ diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 16ef51af..2605a434 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -44,10 +44,6 @@ #define i_type c_PASTE(_i_prefix, i_tag) #endif -#ifndef i_ssize - #define i_ssize intptr_t -#endif - #ifndef i_allocator #define i_allocator c #endif diff --git a/include/stc/priv/template2.h b/include/stc/priv/template2.h index 27c6a890..2e8a6c8d 100644 --- a/include/stc/priv/template2.h +++ b/include/stc/priv/template2.h @@ -30,7 +30,6 @@ #undef i_hash #undef i_rawclass #undef i_capacity -#undef i_ssize #undef i_val #undef i_val_str |
