summaryrefslogtreecommitdiffhomepage
path: root/include/stc/cvec.h
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-04-21 13:47:54 +0200
committerTyge Løvset <[email protected]>2022-04-21 13:47:54 +0200
commitd122c77b5a755cd5b3211c26346a84bb2b27cdce (patch)
treea66aba11b04670c84cca9034e7266014e2052e8f /include/stc/cvec.h
parent91385d449d03145cfe8cc91f2704d4b24c63d37e (diff)
downloadSTC-modified-d122c77b5a755cd5b3211c26346a84bb2b27cdce.tar.gz
STC-modified-d122c77b5a755cd5b3211c26346a84bb2b27cdce.zip
Switched to use i_key as primary template type parameter for all containers. Only maps will actually use i_val.
Users can still specify i_val for non-maps, so there are no usability changes, other than the option to use i_key always, which makes the implementation and switching between container types simpler.
Diffstat (limited to 'include/stc/cvec.h')
-rw-r--r--include/stc/cvec.h86
1 files changed, 43 insertions, 43 deletions
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index b666e791..b98ecbd9 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -32,13 +32,13 @@ struct MyStruct {
cstr name;
} typedef MyStruct;
-#define i_val float
+#define i_key float
#include <stc/cvec.h>
-#define i_val_str // special for cstr
+#define i_key_str // special for cstr
#include <stc/cvec.h>
-#define i_val int
+#define i_key int
#define i_opt c_is_fwd // forward declared
#define i_tag i32
#include <stc/cvec.h>
@@ -74,31 +74,31 @@ struct cvec_rep { size_t size, cap; void* data[]; };
#include "template.h"
#if !c_option(c_is_fwd)
- _cx_deftypes(_c_cvec_types, _cx_self, i_val);
+ _cx_deftypes(_c_cvec_types, _cx_self, i_key);
#endif
-typedef i_valraw _cx_raw;
+typedef i_keyraw _cx_raw;
STC_API _cx_self _cx_memb(_init)(void);
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, size_t cap);
-STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_val null);
-STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_val value);
+STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_key null);
+STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value);
STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2);
STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2);
#if !c_option(c_no_cmp)
STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y);
-STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw raw);
-STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_valraw raw, _cx_iter* lower_bound);
+STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_keyraw raw);
+STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_keyraw raw, _cx_iter* lower_bound);
#endif
#if !defined _i_no_clone
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2);
-STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val)
- { return i_valclone(val); }
-STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); }
+STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val)
+ { return i_keyclone(val); }
+STC_INLINE i_key _cx_memb(_value_fromraw)(i_keyraw raw) { return i_keyfrom(raw); }
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
_cx_memb(_drop)(self);
@@ -107,16 +107,16 @@ STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
#if !defined _i_no_emplace
STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
const _cx_raw* p1, const _cx_raw* p2);
-STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_valraw raw)
- { return _cx_memb(_push)(self, i_valfrom(raw)); }
-STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw)
- { return _cx_memb(_push)(self, i_valfrom(raw)); }
+STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_keyraw raw)
+ { return _cx_memb(_push)(self, i_keyfrom(raw)); }
+STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_keyraw raw)
+ { return _cx_memb(_push)(self, i_keyfrom(raw)); }
STC_INLINE _cx_iter
_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) {
return _cx_memb(_emplace_range_p)(self, self->data + idx, arr, arr + n);
}
STC_INLINE _cx_iter
-_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) {
+_cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_keyraw raw) {
return _cx_memb(_emplace_range_p)(self, it.ref, &raw, &raw + 1);
}
STC_INLINE _cx_iter
@@ -129,14 +129,14 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2
STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cvec_rep_(&cx)->size; }
STC_INLINE size_t _cx_memb(_capacity)(_cx_self cx) { return cvec_rep_(&cx)->cap; }
STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return !cvec_rep_(&cx)->size; }
-STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* val) { return i_valto(val); }
+STC_INLINE i_keyraw _cx_memb(_value_toraw)(_cx_value* val) { return i_keyto(val); }
STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_self, *a, *b); }
STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; }
STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self)
{ return self->data + cvec_rep_(self)->size - 1; }
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
- { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); }
-STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value)
+ { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_keydrop(p); }
+STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value)
{ return _cx_memb(_push)(self, value); }
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
@@ -149,7 +149,7 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t offs)
STC_INLINE size_t _cx_memb(_index)(_cx_self cx, _cx_iter it) { return it.ref - cx.data; }
STC_INLINE _cx_self
-_cx_memb(_with_size)(const size_t size, i_val null) {
+_cx_memb(_with_size)(const size_t size, i_key null) {
_cx_self cx = _cx_memb(_init)();
_cx_memb(_resize)(&cx, size, null);
return cx;
@@ -168,7 +168,7 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) {
}
STC_INLINE _cx_iter
-_cx_memb(_insert)(_cx_self* self, const size_t idx, i_val value) {
+_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) {
return _cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1);
}
STC_INLINE _cx_iter
@@ -176,7 +176,7 @@ _cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], con
return _cx_memb(_insert_range_p)(self, self->data + idx, arr, arr + n);
}
STC_INLINE _cx_iter
-_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_val value) {
+_cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) {
return _cx_memb(_insert_range_p)(self, it.ref, &value, &value + 1);
}
@@ -205,29 +205,29 @@ _cx_memb(_at_mut)(_cx_self* self, const size_t idx) {
#if !c_option(c_no_cmp)
STC_INLINE _cx_iter
-_cx_memb(_find)(const _cx_self* self, i_valraw raw) {
+_cx_memb(_find)(const _cx_self* self, i_keyraw raw) {
return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw);
}
STC_INLINE const _cx_value*
-_cx_memb(_get)(const _cx_self* self, i_valraw raw) {
+_cx_memb(_get)(const _cx_self* self, i_keyraw raw) {
_cx_iter end = _cx_memb(_end)(self);
_cx_value* val = _cx_memb(_find)(self, raw).ref;
return val == end.ref ? NULL : val;
}
STC_INLINE _cx_value*
-_cx_memb(_get_mut)(const _cx_self* self, i_valraw raw)
+_cx_memb(_get_mut)(const _cx_self* self, i_keyraw raw)
{ return (_cx_value*) _cx_memb(_get)(self, raw); }
STC_INLINE _cx_iter
-_cx_memb(_bsearch)(const _cx_self* self, i_valraw raw) {
+_cx_memb(_bsearch)(const _cx_self* self, i_keyraw raw) {
_cx_iter lower;
return _cx_memb(_bsearch_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower);
}
STC_INLINE _cx_iter
-_cx_memb(_lower_bound)(const _cx_self* self, i_valraw raw) {
+_cx_memb(_lower_bound)(const _cx_self* self, i_keyraw raw) {
_cx_iter lower;
_cx_memb(_bsearch_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), raw, &lower);
return lower;
@@ -261,7 +261,7 @@ _cx_memb(_clear)(_cx_self* self) {
struct cvec_rep* rep = cvec_rep_(self);
if (rep->cap) {
for (_cx_value *p = self->data, *q = p + rep->size; p != q; ) {
- --q; i_valdrop(q);
+ --q; i_keydrop(q);
}
rep->size = 0;
}
@@ -282,7 +282,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) {
const size_t len = rep->size;
if (cap > rep->cap || (cap && cap == len)) {
rep = (struct cvec_rep*) c_realloc(rep->cap ? rep : NULL,
- offsetof(struct cvec_rep, data) + cap*sizeof(i_val));
+ offsetof(struct cvec_rep, data) + cap*sizeof(i_key));
if (!rep) return false;
self->data = (_cx_value*) rep->data;
rep->size = len;
@@ -292,18 +292,18 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) {
}
STC_DEF bool
-_cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) {
+_cx_memb(_resize)(_cx_self* self, const size_t len, i_key null) {
if (!_cx_memb(_reserve)(self, len)) return false;
struct cvec_rep *rep = cvec_rep_(self);
const size_t n = rep->size;
- for (size_t i = len; i < n; ++i) { i_valdrop((self->data + i)); }
+ for (size_t i = len; i < n; ++i) { i_keydrop((self->data + i)); }
for (size_t i = n; i < len; ++i) self->data[i] = null;
if (rep->cap) rep->size = len;
return true;
}
STC_DEF _cx_value*
-_cx_memb(_push)(_cx_self* self, i_val value) {
+_cx_memb(_push)(_cx_self* self, i_key value) {
struct cvec_rep *r = cvec_rep_(self);
if (r->size == r->cap) {
_cx_memb(_reserve)(self, (r->size*3 >> 1) + 4);
@@ -342,8 +342,8 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
intptr_t len = p2 - p1;
if (len > 0) {
_cx_value* p = p1, *end = self->data + cvec_rep_(self)->size;
- for (; p != p2; ++p) { i_valdrop(p); }
- memmove(p1, p2, (end - p2) * sizeof(i_val));
+ for (; p != p2; ++p) { i_keydrop(p); }
+ memmove(p1, p2, (end - p2) * sizeof(i_key));
cvec_rep_(self)->size -= len;
}
return c_make(_cx_iter){.ref = p1};
@@ -364,7 +364,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
pos = _cx_memb(_insert_space_)(self, pos, p2 - p1);
_cx_iter it = {pos};
for (; p1 != p2; ++p1)
- *pos++ = i_valclone((*p1));
+ *pos++ = i_keyclone((*p1));
return it;
}
@@ -374,7 +374,7 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
const _cx_raw* p1, const _cx_raw* p2) {
pos = _cx_memb(_insert_space_)(self, pos, p2 - p1);
_cx_iter it = {pos};
- for (; p1 != p2; ++p1) *pos++ = i_valfrom((*p1));
+ for (; p1 != p2; ++p1) *pos++ = i_keyfrom((*p1));
return it;
}
#endif // !_i_no_emplace
@@ -382,20 +382,20 @@ _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
#if !c_option(c_no_cmp)
STC_DEF _cx_iter
-_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) {
+_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_keyraw raw) {
for (; i1.ref != i2.ref; ++i1.ref) {
- i_valraw r = i_valto(i1.ref);
+ i_keyraw r = i_keyto(i1.ref);
if (i_eq((&raw), (&r))) return i1;
}
return i2;
}
STC_DEF _cx_iter
-_cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw, _cx_iter* lower_bound) {
+_cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_keyraw raw, _cx_iter* lower_bound) {
_cx_iter mid, last = i2;
while (i1.ref != i2.ref) {
mid.ref = i1.ref + ((i2.ref - i1.ref) >> 1);
- int c; i_valraw m = i_valto(mid.ref);
+ int c; i_keyraw m = i_keyto(mid.ref);
if (!(c = i_cmp((&raw), (&m)))) return *lower_bound = mid;
else if (c < 0) i2.ref = mid.ref;
else i1.ref = mid.ref + 1;
@@ -406,8 +406,8 @@ _cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw, _cx_iter* lower_bo
STC_DEF int
_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
- i_valraw rx = i_valto(x);
- i_valraw ry = i_valto(y);
+ i_keyraw rx = i_keyto(x);
+ i_keyraw ry = i_keyto(y);
return i_cmp((&rx), (&ry));
}
#endif // !c_no_cmp