summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-04-27 17:55:31 +0200
committerTyge Løvset <[email protected]>2023-04-27 17:55:31 +0200
commita922157394a9a3e0cffe26a5fa4d29c9d78ecc06 (patch)
tree219814dc5c1e87b109bb30ae27589a93dcad80a9 /include
parentba3f2284731e50100d249cf1d825b8d24efad658 (diff)
downloadSTC-modified-a922157394a9a3e0cffe26a5fa4d29c9d78ecc06.tar.gz
STC-modified-a922157394a9a3e0cffe26a5fa4d29c9d78ecc06.zip
Reshuffled code in csmap.h and cmap.h
Diffstat (limited to 'include')
-rw-r--r--include/stc/cmap.h81
-rw-r--r--include/stc/csmap.h105
2 files changed, 94 insertions, 92 deletions
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 9f21b811..cd8878ea 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -70,13 +70,6 @@ typedef struct { intptr_t idx; uint8_t hashx, found; } chash_bucket;
#define _i_SET_ONLY c_false
#define _i_keyref(vp) (&(vp)->first)
#endif
-#define _i_ishash
-#ifndef i_max_load_factor
- #define i_max_load_factor 0.80f
-#endif
-#ifndef i_expandby
- #define i_expandby 2
-#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);
@@ -104,15 +97,14 @@ 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 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 (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 && _cx_memb(_bucket_)(self, &rkey).found; }
@@ -161,14 +153,12 @@ _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)); )
}
@@ -183,14 +173,13 @@ _cx_memb(_insert)(_cx_self* self, i_key _key _i_MAP_ONLY(, i_val _mapped)) {
return _res;
}
-STC_INLINE _cx_result
-_cx_memb(_push)(_cx_self* self, _cx_value _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);
- return _res;
+ return _res.ref;
}
STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) {
@@ -209,27 +198,17 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t 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->data, self->data+self->bucket_count, self->slot};
- if (it.sref)
- while (it.sref->hashx == 0)
- ++it.ref, ++it.sref;
- if (it.ref == it._end) it.ref = NULL;
- return it;
-}
+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) {
+STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) {
while (n-- && it.ref) _cx_memb(_next)(&it);
return it;
}
@@ -284,13 +263,19 @@ _cx_memb(_eq)(const _cx_self* self, const _cx_self* other) {
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(i_implement)
+#ifndef i_max_load_factor
+ #define i_max_load_factor 0.80f
+#endif
+#ifndef i_expandby
+ #define i_expandby 2
+#endif
#ifndef CMAP_H_INCLUDED
-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_1(uint64_t x, uint64_t n)
+ { return (intptr_t)((uint32_t)x*n >> 32); } // n < 2^32
-STC_INLINE int64_t fastrange_2(uint64_t x, uint64_t n)
- { return (int64_t)(x & (n - 1)); } // n power of 2.
+STC_INLINE intptr_t fastrange_2(uint64_t x, uint64_t n)
+ { return (intptr_t)(x & (n - 1)); } // n power of 2.
STC_INLINE uint64_t next_power_of_2(uint64_t n) {
n--;
@@ -301,8 +286,24 @@ 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) {
+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)
+ ++it.ref, ++it.sref;
+ if (it.ref == it._end) it.ref = NULL;
+ return it;
+}
+
+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) {
+ 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);
return map;
@@ -471,13 +472,13 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
s[i].hashx = 0;
--self->size;
}
-
-#endif // i_implement
#undef i_max_load_factor
#undef i_expandby
+#elif defined i_max_load_factor || defined i_expandby
+#error i_max_load_factor and i_expandby may only be defined for the implementation.
+#endif // i_implement
#undef _i_isset
#undef _i_ismap
-#undef _i_ishash
#undef _i_keyref
#undef _i_MAP_ONLY
#undef _i_SET_ONLY
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index dc0387f7..cd7c1d95 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -97,8 +97,6 @@ STC_API _cx_result _cx_memb(_emplace)(_cx_self* self, _cx_keyraw rkey _i_MA
#if !defined i_no_clone
STC_API _cx_self _cx_memb(_clone)(_cx_self tree);
#endif // !i_no_clone
-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 _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_keyraw rkey, _cx_iter* out);
@@ -108,6 +106,7 @@ STC_API _cx_value* _cx_memb(_back)(const _cx_self* self);
STC_API int _cx_memb(_erase)(_cx_self* self, _cx_keyraw rkey);
STC_API _cx_iter _cx_memb(_erase_at)(_cx_self* self, _cx_iter it);
STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2);
+STC_API _cx_iter _cx_memb(_begin)(const _cx_self* self);
STC_API void _cx_memb(_next)(_cx_iter* it);
STC_INLINE _cx_self _cx_memb(_init)(void) { _cx_self tree = {0}; return tree; }
@@ -185,17 +184,6 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) {
#endif // !_i_isset
STC_INLINE _cx_iter
-_cx_memb(_begin)(const _cx_self* self) {
- _cx_iter it;
- it.ref = NULL;
- it._d = self->nodes, it._top = 0;
- it._tn = self->root;
- if (it._tn)
- _cx_memb(_next)(&it);
- return it;
-}
-
-STC_INLINE _cx_iter
_cx_memb(_end)(const _cx_self* self) {
(void)self;
_cx_iter it; it.ref = NULL, it._top = 0, it._tn = 0;
@@ -220,7 +208,30 @@ _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, intptr_t n) {
+static _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey);
+
+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)));
+ if (_res.inserted)
+ { *_i_keyref(_res.ref) = _key; _i_MAP_ONLY( _res.ref->second = _mapped; )}
+ else
+ { i_keydrop((&_key)); _i_MAP_ONLY( i_valdrop((&_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)));
+ if (_res.inserted)
+ *_res.ref = _val;
+ else
+ _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) {
while (n--)
#if defined _i_isset && defined i_no_emplace
_cx_memb(_insert)(self, *raw++);
@@ -233,12 +244,39 @@ 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, intptr_t n)
{ _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; }
/* -------------------------- IMPLEMENTATION ------------------------- */
#if defined(i_implement)
+STC_DEF void
+_cx_memb(_next)(_cx_iter *it) {
+ int32_t tn = it->_tn;
+ if (it->_top || tn) {
+ while (tn) {
+ it->_st[it->_top++] = tn;
+ tn = it->_d[tn].link[0];
+ }
+ tn = it->_st[--it->_top];
+ it->_tn = it->_d[tn].link[1];
+ it->ref = &it->_d[tn].value;
+ } else
+ it->ref = NULL;
+}
+
+STC_DEF _cx_iter
+_cx_memb(_begin)(const _cx_self* self) {
+ _cx_iter it;
+ it.ref = NULL;
+ it._d = self->nodes, it._top = 0;
+ it._tn = self->root;
+ if (it._tn)
+ _cx_memb(_next)(&it);
+ return it;
+}
+
STC_DEF bool
_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) {
if (cap <= self->cap)
@@ -287,28 +325,6 @@ _cx_memb(_new_node_)(_cx_self* self, int level) {
return tn;
}
-static _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_keyraw rkey);
-
-STC_DEF _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)));
- if (_res.inserted)
- { *_i_keyref(_res.ref) = _key; _i_MAP_ONLY( _res.ref->second = _mapped; )}
- else
- { i_keydrop((&_key)); _i_MAP_ONLY( i_valdrop((&_mapped)); )}
- return _res;
-}
-
-STC_DEF _cx_result
-_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);
- return _res;
-}
-
#ifndef _i_isset
STC_DEF _cx_result
_cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) {
@@ -367,21 +383,6 @@ _cx_memb(_lower_bound)(const _cx_self* self, _cx_keyraw rkey) {
return it;
}
-STC_DEF void
-_cx_memb(_next)(_cx_iter *it) {
- int32_t tn = it->_tn;
- if (it->_top || tn) {
- while (tn) {
- it->_st[it->_top++] = tn;
- tn = it->_d[tn].link[0];
- }
- tn = it->_st[--it->_top];
- it->_tn = it->_d[tn].link[1];
- it->ref = &it->_d[tn].value;
- } else
- it->ref = NULL;
-}
-
STC_DEF int32_t
_cx_memb(_skew_)(_cx_node *d, int32_t tn) {
if (tn && d[d[tn].link[0]].level == d[tn].level) {