summaryrefslogtreecommitdiffhomepage
path: root/include/stc/csmap.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-04-18 17:48:47 +0200
committerTyge Løvset <[email protected]>2023-04-18 17:48:47 +0200
commita913490c248f6cfdca3481d81d6d344f4d066cb9 (patch)
treeb805826d496a8d5c8a449731b6c8bc3b1d056979 /include/stc/csmap.h
parent462adebf55c77697fbb379a62941c00b876c3f6a (diff)
downloadSTC-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/stc/csmap.h')
-rw-r--r--include/stc/csmap.h79
1 files changed, 36 insertions, 43 deletions
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