diff options
| author | Tyge Løvset <[email protected]> | 2023-02-01 22:44:21 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2023-02-02 21:24:43 +0100 |
| commit | 473a13c1c15b8c1e1edc79cff074c0dcf490fc1b (patch) | |
| tree | 65ee8bcf91cb794e6de11341dfdb03f6779f4d53 /include | |
| parent | ad9a74ebe39cd3371c5cf3a46b6c0286c0667914 (diff) | |
| download | STC-modified-473a13c1c15b8c1e1edc79cff074c0dcf490fc1b.tar.gz STC-modified-473a13c1c15b8c1e1edc79cff074c0dcf490fc1b.zip | |
Fixed to allow int64_t sized maps. (defaults to 32-bit).
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cmap.h | 52 | ||||
| -rw-r--r-- | include/stc/csmap.h | 21 | ||||
| -rw-r--r-- | include/stc/forward.h | 18 | ||||
| -rw-r--r-- | include/stc/priv/template.h | 7 |
4 files changed, 52 insertions, 46 deletions
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index f90d9a4a..393df53f 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -53,7 +53,7 @@ int main(void) { #include "forward.h" #include <stdlib.h> #include <string.h> -typedef struct { intptr_t idx; uint8_t hx; } chash_bucket_t; +typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t; #endif // CMAP_H_INCLUDED #ifndef _i_prefix @@ -73,6 +73,12 @@ typedef struct { intptr_t idx; uint8_t hx; } chash_bucket_t; #ifndef i_max_load_factor #define i_max_load_factor 0.85f #endif +#ifndef i_size + #define i_size int32_t + #define _i_expandby 1 +#else + #define _i_expandby 2 +#endif #include "priv/template.h" #ifndef i_hash_functor #define i_hash_functor(self, x) i_hash(x) @@ -96,13 +102,13 @@ typedef _i_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(intptr_t cap); +STC_API _cx_self _cx_memb(_with_capacity)(int64_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, intptr_t capacity); +STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t 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); @@ -111,10 +117,10 @@ 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 intptr_t _cx_memb(_size)(const _cx_self* map) { return 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 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 bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } @@ -195,7 +201,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, intptr_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -208,7 +214,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_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) { @@ -231,14 +237,14 @@ _cx_memb(_next)(_cx_iter* it) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, size_t n) { +_cx_memb(_advance)(_cx_iter it, uint64_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { - intptr_t idx; + 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, @@ -248,7 +254,7 @@ _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { STC_INLINE const _cx_value* _cx_memb(_get)(const _cx_self* self, _cx_rawkey rkey) { - intptr_t idx; + int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return self->table + idx; return NULL; @@ -258,7 +264,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey) { return (_cx_value*)_cx_memb(_get)(self, rkey); } -STC_INLINE intptr_t +STC_INLINE int _cx_memb(_erase)(_cx_self* self, _cx_rawkey rkey) { if (self->size == 0) return 0; @@ -278,11 +284,11 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { #if defined(i_implement) #ifndef CMAP_H_INCLUDED -STC_INLINE intptr_t fastrange_1(uint64_t x, uint64_t n) - { return (intptr_t)((uint32_t)x*n >> 32); } // n < 2^32 +STC_INLINE int64_t fastrange_1(uint64_t x, uint64_t n) + { return (int64_t)((uint32_t)x*n >> 32); } // n < 2^32 -STC_INLINE intptr_t fastrange_2(uint64_t x, uint64_t n) - { return (intptr_t)(x & (n - 1)); } // n power of 2. +STC_INLINE int64_t fastrange_2(uint64_t x, uint64_t n) + { return (int64_t)(x & (n - 1)); } // n power of 2. STC_INLINE uint64_t next_power_of_2(uint64_t n) { n--; @@ -294,7 +300,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 intptr_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -354,7 +360,7 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { STC_DEF chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) { const uint64_t _hash = i_hash_functor(self, rkeyptr); - i_size _cap = self->bucket_count; + 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]) { @@ -404,8 +410,8 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { - const i_size _oldbuckets = self->bucket_count; +_cx_memb(_reserve)(_cx_self* self, const int64_t newcap) { + const i_size _oldbuckets = self->bucket_count, _newcap = (i_size)newcap; if (_newcap != self->size && _newcap <= _oldbuckets) return true; i_size _nbuckets = (i_size)((float)_newcap / (i_max_load_factor)) + 4; @@ -417,7 +423,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { _cx_self m = { c_ALLOC_N(_cx_value, _nbuckets), (uint8_t *) c_calloc(_nbuckets + 1, 1), - self->size, (i_size)_nbuckets, + self->size, _nbuckets, }; bool ok = m.table && m._hashx; if (ok) { /* Rehash: */ @@ -428,7 +434,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { _cx_rawkey r = i_keyto(_i_keyref(e)); chash_bucket_t b = _cx_memb(_bucket_)(&m, &r); m.table[b.idx] = *e; - m._hashx[b.idx] = (uint8_t)b.hx; + m._hashx[b.idx] = b.hx; } c_swap(_cx_self, self, &m); } diff --git a/include/stc/csmap.h b/include/stc/csmap.h index de948618..0f21f954 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -73,6 +73,9 @@ int main(void) { #define _i_SET_ONLY c_false #define _i_keyref(vp) (&(vp)->first) #endif +#ifndef i_size + #define i_size int32_t +#endif #include "priv/template.h" #ifndef i_cmp_functor #define i_cmp_functor(self, x, y) i_cmp(x, y) @@ -107,7 +110,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, intptr_t cap); +STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t 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); @@ -118,8 +121,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 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 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 _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) @@ -130,7 +133,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 intptr_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -209,7 +212,7 @@ _cx_memb(_end)(const _cx_self* self) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, size_t n) { +_cx_memb(_advance)(_cx_iter it, uint64_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; @@ -225,8 +228,8 @@ _cx_memb(_init)(void) { } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { - if (cap <= self->cap) +_cx_memb(_reserve)(_cx_self* self, const int64_t cap) { + if ((i_size)cap <= self->cap) return false; _cx_node* nodes = (_cx_node*)c_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node)); if (!nodes) @@ -294,7 +297,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, intptr_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -307,7 +310,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #ifndef _i_isset diff --git a/include/stc/forward.h b/include/stc/forward.h index 594c76fa..6103ba39 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -30,9 +30,9 @@ #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_cmap_huge(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, intptr_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_cset_huge(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, intptr_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_cstack(CX, VAL) _c_cstack_types(CX, VAL) @@ -98,7 +98,7 @@ typedef union { #define _c_chash_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ + typedef SZ SELF##_sizet; \ \ typedef SET_ONLY( SELF##_key ) \ MAP_ONLY( struct SELF##_value ) \ @@ -117,14 +117,14 @@ typedef union { typedef struct { \ SELF##_value* table; \ uint8_t* _hashx; \ - SELF##_size_t size, bucket_count; \ + SELF##_sizet size, bucket_count; \ } SELF #if defined STC_CSMAP_V1 #define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ + typedef SZ SELF##_sizet; \ typedef struct SELF##_node SELF##_node; \ \ typedef SET_ONLY( SELF##_key ) \ @@ -144,13 +144,13 @@ typedef union { \ typedef struct { \ SELF##_node *root; \ - SELF##_size_t size; \ + SELF##_sizet size; \ } SELF #else #define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ + typedef SZ SELF##_sizet; \ typedef struct SELF##_node SELF##_node; \ \ typedef SET_ONLY( SELF##_key ) \ @@ -166,12 +166,12 @@ typedef union { SELF##_value *ref; \ SELF##_node *_d; \ int _top; \ - SELF##_size_t _tn, _st[36]; \ + SELF##_sizet _tn, _st[36]; \ } SELF##_iter; \ \ typedef struct { \ SELF##_node *nodes; \ - SELF##_size_t root, disp, head, size, cap; \ + SELF##_sizet root, disp, head, size, cap; \ } SELF #endif diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 4aa54bcc..6eb82bcb 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -44,11 +44,8 @@ #define _i_prefix #endif -#ifdef i_size - #define _i_expandby 2 -#else - #define i_size int32_t - #define _i_expandby 1 +#ifndef i_size + #define i_size intptr_t #endif #if !(defined i_key || defined i_key_str || defined i_key_ssv || \ |
