summaryrefslogtreecommitdiffhomepage
path: root/include/stc/cmap.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-01 22:44:21 +0100
committerTyge Løvset <[email protected]>2023-02-02 21:24:43 +0100
commit473a13c1c15b8c1e1edc79cff074c0dcf490fc1b (patch)
tree65ee8bcf91cb794e6de11341dfdb03f6779f4d53 /include/stc/cmap.h
parentad9a74ebe39cd3371c5cf3a46b6c0286c0667914 (diff)
downloadSTC-modified-473a13c1c15b8c1e1edc79cff074c0dcf490fc1b.tar.gz
STC-modified-473a13c1c15b8c1e1edc79cff074c0dcf490fc1b.zip
Fixed to allow int64_t sized maps. (defaults to 32-bit).
Diffstat (limited to 'include/stc/cmap.h')
-rw-r--r--include/stc/cmap.h52
1 files changed, 29 insertions, 23 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);
}