diff options
| author | Tyge Løvset <[email protected]> | 2023-06-20 15:28:02 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-06-20 15:28:02 +0200 |
| commit | de4f8fa86f141dfeab15f5576029910474f56fa1 (patch) | |
| tree | 930f8ecb0a2765d4834aa34f6ac538096454816c /include/stc/cmap.h | |
| parent | b617eded8cee98dd3ff9ac896328b172d37fcf91 (diff) | |
| download | STC-modified-de4f8fa86f141dfeab15f5576029910474f56fa1.tar.gz STC-modified-de4f8fa86f141dfeab15f5576029910474f56fa1.zip | |
Internal only:
Renamed _cx_memb() macro to _cx_MEMB()
Renamed _cx_self macro to _cx_Self
Diffstat (limited to 'include/stc/cmap.h')
| -rw-r--r-- | include/stc/cmap.h | 194 |
1 files changed, 97 insertions, 97 deletions
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 2e234fb5..21e7b933 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -72,7 +72,7 @@ typedef struct { intptr_t idx; uint8_t hashx, found; } chash_bucket; #endif #include "priv/template.h" #ifndef i_is_forward - _cx_deftypes(_c_chash_types, _cx_self, i_key, i_val, _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 { @@ -81,60 +81,60 @@ _i_MAP_ONLY( struct _cx_value { }; ) typedef i_keyraw _cx_keyraw; -typedef i_valraw _cx_memb(_rmapped); +typedef i_valraw _cx_MEMB(_rmapped); typedef _i_SET_ONLY( i_keyraw ) _i_MAP_ONLY( struct { i_keyraw first; i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(intptr_t 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); +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, intptr_t capacity); -STC_API chash_bucket _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_API float _cx_memb(_max_load_factor)(const _cx_self* self); -STC_API intptr_t _cx_memb(_capacity)(const _cx_self* map); - -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, (intptr_t)self->size); } -STC_INLINE bool _cx_memb(_empty)(const _cx_self* map) { return !map->size; } -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 bool _cx_memb(_contains)(const _cx_self* self, _cx_keyraw rkey) - { return self->size && _cx_memb(_bucket_)(self, &rkey).found; } +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, intptr_t capacity); +STC_API chash_bucket _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_API float _cx_MEMB(_max_load_factor)(const _cx_Self* self); +STC_API intptr_t _cx_MEMB(_capacity)(const _cx_Self* map); + +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, (intptr_t)self->size); } +STC_INLINE bool _cx_MEMB(_empty)(const _cx_Self* map) { return !map->size; } +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 bool _cx_MEMB(_contains)(const _cx_Self* self, _cx_keyraw rkey) + { return self->size && _cx_MEMB(_bucket_)(self, &rkey).found; } #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_keyraw rkey, i_valraw rmapped); + STC_API _cx_result _cx_MEMB(_emplace_or_assign)(_cx_Self* self, _cx_keyraw rkey, i_valraw rmapped); #endif STC_INLINE const _cx_mapped* - _cx_memb(_at)(const _cx_self* self, _cx_keyraw rkey) { - chash_bucket b = _cx_memb(_bucket_)(self, &rkey); + _cx_MEMB(_at)(const _cx_Self* self, _cx_keyraw rkey) { + chash_bucket b = _cx_MEMB(_bucket_)(self, &rkey); assert(b.found); return &self->data[b.idx].second; } STC_INLINE _cx_mapped* - _cx_memb(_at_mut)(_cx_self* self, _cx_keyraw rkey) - { return (_cx_mapped*)_cx_memb(_at)(self, rkey); } + _cx_MEMB(_at_mut)(_cx_Self* self, _cx_keyraw rkey) + { return (_cx_mapped*)_cx_MEMB(_at)(self, rkey); } #endif // !_i_isset #if !defined i_no_clone -STC_INLINE void _cx_memb(_copy)(_cx_self *self, const _cx_self* 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); + _cx_MEMB(_drop)(self); + *self = _cx_MEMB(_clone)(*other); } STC_INLINE _cx_value -_cx_memb(_value_clone)(_cx_value _val) { +_cx_MEMB(_value_clone)(_cx_value _val) { *_i_keyref(&_val) = i_keyclone((*_i_keyref(&_val))); _i_MAP_ONLY( _val.second = i_valclone(_val.second); ) return _val; @@ -143,8 +143,8 @@ _cx_memb(_value_clone)(_cx_value _val) { #if !defined i_no_emplace STC_INLINE _cx_result -_cx_memb(_emplace)(_cx_self* self, _cx_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) { - _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); +_cx_MEMB(_emplace)(_cx_Self* self, _cx_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) { + _cx_result _res = _cx_MEMB(_insert_entry_)(self, rkey); if (_res.inserted) { *_i_keyref(_res.ref) = i_keyfrom(rkey); _i_MAP_ONLY( _res.ref->second = i_valfrom(rmapped); ) @@ -153,19 +153,19 @@ _cx_memb(_emplace)(_cx_self* self, _cx_keyraw rkey _i_MAP_ONLY(, i_valraw rmappe } #endif // !i_no_emplace -STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { +STC_INLINE _cx_raw _cx_MEMB(_value_toraw)(const _cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) _i_MAP_ONLY( c_LITERAL(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } -STC_INLINE void _cx_memb(_value_drop)(_cx_value* _val) { +STC_INLINE void _cx_MEMB(_value_drop)(_cx_value* _val) { i_keydrop(_i_keyref(_val)); _i_MAP_ONLY( i_valdrop((&_val->second)); ) } STC_INLINE _cx_result -_cx_memb(_insert)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { - _cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto((&_key))); +_cx_MEMB(_insert)(_cx_Self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { + _cx_result _res = _cx_MEMB(_insert_entry_)(self, i_keyto((&_key))); if (_res.inserted) { *_i_keyref(_res.ref) = _key; _i_MAP_ONLY( _res.ref->second = _mapped; )} else @@ -173,90 +173,90 @@ _cx_memb(_insert)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) { return _res; } -STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value _val) { - _cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto(_i_keyref(&_val))); +STC_INLINE _cx_value* _cx_MEMB(_push)(_cx_Self* self, _cx_value _val) { + _cx_result _res = _cx_MEMB(_insert_entry_)(self, i_keyto(_i_keyref(&_val))); if (_res.inserted) *_res.ref = _val; else - _cx_memb(_value_drop)(&_val); + _cx_MEMB(_value_drop)(&_val); return _res.ref; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t 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++); + _cx_MEMB(_insert)(self, *raw++); #elif defined _i_isset - _cx_memb(_emplace)(self, *raw++); + _cx_MEMB(_emplace)(self, *raw++); #elif defined i_no_emplace - _cx_memb(_insert_or_assign)(self, raw->first, raw->second), ++raw; + _cx_MEMB(_insert_or_assign)(self, raw->first, raw->second), ++raw; #else - _cx_memb(_emplace_or_assign)(self, raw->first, raw->second), ++raw; + _cx_MEMB(_emplace_or_assign)(self, raw->first, raw->second), ++raw; #endif } -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_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_API _cx_iter _cx_memb(_begin)(const _cx_self* self); +STC_API _cx_iter _cx_MEMB(_begin)(const _cx_Self* self); -STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) +STC_INLINE _cx_iter _cx_MEMB(_end)(const _cx_Self* self) { return c_LITERAL(_cx_iter){NULL}; } -STC_INLINE void _cx_memb(_next)(_cx_iter* it) { +STC_INLINE void _cx_MEMB(_next)(_cx_iter* it) { while ((++it->ref, (++it->sref)->hashx == 0)) ; if (it->ref == it->_end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) { - while (n-- && it.ref) _cx_memb(_next)(&it); +STC_INLINE _cx_iter _cx_MEMB(_advance)(_cx_iter it, size_t n) { + while (n-- && it.ref) _cx_MEMB(_next)(&it); return it; } STC_INLINE _cx_iter -_cx_memb(_find)(const _cx_self* self, _cx_keyraw rkey) { +_cx_MEMB(_find)(const _cx_Self* self, _cx_keyraw rkey) { chash_bucket b; - if (self->size && (b = _cx_memb(_bucket_)(self, &rkey)).found) + if (self->size && (b = _cx_MEMB(_bucket_)(self, &rkey)).found) return c_LITERAL(_cx_iter){self->data + b.idx, self->data + self->bucket_count, self->slot + b.idx}; - return _cx_memb(_end)(self); + return _cx_MEMB(_end)(self); } STC_INLINE const _cx_value* -_cx_memb(_get)(const _cx_self* self, _cx_keyraw rkey) { +_cx_MEMB(_get)(const _cx_Self* self, _cx_keyraw rkey) { chash_bucket b; - if (self->size && (b = _cx_memb(_bucket_)(self, &rkey)).found) + if (self->size && (b = _cx_MEMB(_bucket_)(self, &rkey)).found) return self->data + b.idx; return NULL; } STC_INLINE _cx_value* -_cx_memb(_get_mut)(_cx_self* self, _cx_keyraw rkey) - { return (_cx_value*)_cx_memb(_get)(self, rkey); } +_cx_MEMB(_get_mut)(_cx_Self* self, _cx_keyraw rkey) + { return (_cx_value*)_cx_MEMB(_get)(self, rkey); } STC_INLINE int -_cx_memb(_erase)(_cx_self* self, _cx_keyraw rkey) { +_cx_MEMB(_erase)(_cx_Self* self, _cx_keyraw rkey) { chash_bucket b = {0}; - if (self->size && (b = _cx_memb(_bucket_)(self, &rkey)).found) - _cx_memb(_erase_entry)(self, self->data + b.idx); + if (self->size && (b = _cx_MEMB(_bucket_)(self, &rkey)).found) + _cx_MEMB(_erase_entry)(self, self->data + b.idx); return b.found; } STC_INLINE _cx_iter -_cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { - _cx_memb(_erase_entry)(self, it.ref); +_cx_MEMB(_erase_at)(_cx_Self* self, _cx_iter it) { + _cx_MEMB(_erase_entry)(self, it.ref); if (it.sref->hashx == 0) - _cx_memb(_next)(&it); + _cx_MEMB(_next)(&it); return it; } STC_INLINE bool -_cx_memb(_eq)(const _cx_self* self, const _cx_self* other) { - if (_cx_memb(_size)(self) != _cx_memb(_size)(other)) return false; - for (_cx_iter i = _cx_memb(_begin)(self); i.ref; _cx_memb(_next)(&i)) { +_cx_MEMB(_eq)(const _cx_Self* self, const _cx_Self* other) { + if (_cx_MEMB(_size)(self) != _cx_MEMB(_size)(other)) return false; + for (_cx_iter i = _cx_MEMB(_begin)(self); i.ref; _cx_MEMB(_next)(&i)) { const _cx_keyraw _raw = i_keyto(_i_keyref(i.ref)); - if (!_cx_memb(_contains)(other, _raw)) return false; + if (!_cx_MEMB(_contains)(other, _raw)) return false; } return true; } @@ -268,7 +268,7 @@ _cx_memb(_eq)(const _cx_self* self, const _cx_self* other) { #endif #define fastrange_2(x, n) (intptr_t)((x) & (size_t)((n) - 1)) // n power of 2. -STC_DEF _cx_iter _cx_memb(_begin)(const _cx_self* self) { +STC_DEF _cx_iter _cx_MEMB(_begin)(const _cx_Self* self) { _cx_iter it = {self->data, self->data+self->bucket_count, self->slot}; if (it.sref) while (it.sref->hashx == 0) @@ -277,46 +277,46 @@ STC_DEF _cx_iter _cx_memb(_begin)(const _cx_self* self) { return it; } -STC_DEF float _cx_memb(_max_load_factor)(const _cx_self* self) { +STC_DEF float _cx_MEMB(_max_load_factor)(const _cx_Self* self) { return (float)(i_max_load_factor); } -STC_DEF intptr_t _cx_memb(_capacity)(const _cx_self* map) { +STC_DEF intptr_t _cx_MEMB(_capacity)(const _cx_Self* map) { return (intptr_t)((float)map->bucket_count * (i_max_load_factor)); } -STC_DEF _cx_self _cx_memb(_with_capacity)(const intptr_t cap) { - _cx_self map = {0}; - _cx_memb(_reserve)(&map, cap); +STC_DEF _cx_Self _cx_MEMB(_with_capacity)(const intptr_t cap) { + _cx_Self map = {0}; + _cx_MEMB(_reserve)(&map, cap); return map; } -STC_INLINE void _cx_memb(_wipe_)(_cx_self* self) { +STC_INLINE void _cx_MEMB(_wipe_)(_cx_Self* self) { if (self->size == 0) return; _cx_value* d = self->data, *_end = d + self->bucket_count; chash_slot* s = self->slot; for (; d != _end; ++d) if ((s++)->hashx) - _cx_memb(_value_drop)(d); + _cx_MEMB(_value_drop)(d); } -STC_DEF void _cx_memb(_drop)(_cx_self* self) { - _cx_memb(_wipe_)(self); +STC_DEF void _cx_MEMB(_drop)(_cx_Self* self) { + _cx_MEMB(_wipe_)(self); i_free(self->slot); i_free(self->data); } -STC_DEF void _cx_memb(_clear)(_cx_self* self) { - _cx_memb(_wipe_)(self); +STC_DEF void _cx_MEMB(_clear)(_cx_Self* self) { + _cx_MEMB(_wipe_)(self); self->size = 0; c_memset(self->slot, 0, c_sizeof(chash_slot)*self->bucket_count); } #ifndef _i_isset STC_DEF _cx_result - _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) { - _cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto((&_key))); + _cx_MEMB(_insert_or_assign)(_cx_Self* self, i_key _key, i_val _mapped) { + _cx_result _res = _cx_MEMB(_insert_entry_)(self, i_keyto((&_key))); _cx_mapped* _mp = _res.ref ? &_res.ref->second : &_mapped; if (_res.inserted) _res.ref->first = _key; @@ -328,8 +328,8 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { #if !defined i_no_emplace STC_DEF _cx_result - _cx_memb(_emplace_or_assign)(_cx_self* self, _cx_keyraw rkey, i_valraw rmapped) { - _cx_result _res = _cx_memb(_insert_entry_)(self, rkey); + _cx_MEMB(_emplace_or_assign)(_cx_Self* self, _cx_keyraw rkey, i_valraw rmapped) { + _cx_result _res = _cx_MEMB(_insert_entry_)(self, rkey); if (_res.inserted) _res.ref->first = i_keyfrom(rkey); else { @@ -343,7 +343,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { #endif // !_i_isset STC_DEF chash_bucket -_cx_memb(_bucket_)(const _cx_self* self, const _cx_keyraw* rkeyptr) { +_cx_MEMB(_bucket_)(const _cx_Self* self, const _cx_keyraw* rkeyptr) { const uint64_t _hash = i_hash(rkeyptr); intptr_t _cap = self->bucket_count; chash_bucket b = {fastrange_2(_hash, _cap), (uint8_t)(_hash | 0x80)}; @@ -362,13 +362,13 @@ _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_MEMB(_insert_entry_)(_cx_Self* self, _cx_keyraw rkey) { _cx_result res = {NULL}; if (self->size >= (intptr_t)((float)self->bucket_count * (i_max_load_factor))) - if (!_cx_memb(_reserve)(self, (intptr_t)(self->size*3/2 + 2))) + if (!_cx_MEMB(_reserve)(self, (intptr_t)(self->size*3/2 + 2))) return res; - chash_bucket b = _cx_memb(_bucket_)(self, &rkey); + chash_bucket b = _cx_MEMB(_bucket_)(self, &rkey); res.ref = &self->data[b.idx]; if (!b.found) { self->slot[b.idx].hashx = b.hashx; @@ -379,8 +379,8 @@ _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey) { } #if !defined i_no_clone -STC_DEF _cx_self -_cx_memb(_clone)(_cx_self m) { +STC_DEF _cx_Self +_cx_MEMB(_clone)(_cx_Self m) { if (m.data) { _cx_value *d = (_cx_value *)i_malloc(c_sizeof(_cx_value)*m.bucket_count), *_dst = d, *_end = m.data + m.bucket_count; @@ -391,7 +391,7 @@ _cx_memb(_clone)(_cx_self m) { else for (; m.data != _end; ++m.data, ++m.slot, ++_dst) if (m.slot->hashx) - *_dst = _cx_memb(_value_clone)(*m.data); + *_dst = _cx_MEMB(_value_clone)(*m.data); m.data = d, m.slot = s; } return m; @@ -399,13 +399,13 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { +_cx_MEMB(_reserve)(_cx_Self* self, const intptr_t _newcap) { const intptr_t _oldbucks = self->bucket_count; if (_newcap != self->size && _newcap <= _oldbucks) return true; intptr_t _newbucks = (intptr_t)((float)_newcap / (i_max_load_factor)) + 4; _newbucks = cnextpow2(_newbucks); - _cx_self m = { + _cx_Self m = { (_cx_value *)i_malloc(_newbucks*c_sizeof(_cx_value)), (chash_slot *)i_calloc(_newbucks + 1, sizeof(chash_slot)), self->size, _newbucks @@ -417,11 +417,11 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { const chash_slot* s = self->slot; for (intptr_t i = 0; i < _oldbucks; ++i, ++d) if ((s++)->hashx) { _cx_keyraw r = i_keyto(_i_keyref(d)); - chash_bucket b = _cx_memb(_bucket_)(&m, &r); + chash_bucket b = _cx_MEMB(_bucket_)(&m, &r); m.slot[b.idx].hashx = b.hashx; m.data[b.idx] = *d; // move } - c_swap(_cx_self, self, &m); + c_swap(_cx_Self, self, &m); } i_free(m.slot); i_free(m.data); @@ -429,12 +429,12 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { } STC_DEF void -_cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { +_cx_MEMB(_erase_entry)(_cx_Self* self, _cx_value* _val) { _cx_value* d = self->data; chash_slot* s = self->slot; intptr_t i = _val - d, j = i, k; const intptr_t _cap = self->bucket_count; - _cx_memb(_value_drop)(_val); + _cx_MEMB(_value_drop)(_val); for (;;) { // delete without leaving tombstone if (++j == _cap) j = 0; if (! s[j].hashx) |
