summaryrefslogtreecommitdiffhomepage
path: root/include
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-12-29 15:14:37 +0100
committerGitHub <[email protected]>2021-12-29 15:14:37 +0100
commita53b2f4269951e0b5340723670137b4fdf96534c (patch)
treee5d36c28d0fd548873216426bb00a6b59d2730dd /include
parent183a89859ba9914ee0546e4482b40be199e52292 (diff)
parent92f5421493932d00e63b33152331a36cc4fc9491 (diff)
downloadSTC-modified-a53b2f4269951e0b5340723670137b4fdf96534c.tar.gz
STC-modified-a53b2f4269951e0b5340723670137b4fdf96534c.zip
Merge pull request #17 from tylov/version3
Version3 Breaking changes are covered by the migration guide in the NEWS section in README.md
Diffstat (limited to 'include')
-rw-r--r--include/stc/alt/clist.h24
-rw-r--r--include/stc/alt/csmap.h34
-rw-r--r--include/stc/alt/cstr.h29
-rw-r--r--include/stc/alt/sstr.h406
-rw-r--r--include/stc/carc.h (renamed from include/stc/csptr.h)131
-rw-r--r--include/stc/carr2.h13
-rw-r--r--include/stc/carr3.h13
-rw-r--r--include/stc/cbits.h6
-rw-r--r--include/stc/cbox.h90
-rw-r--r--include/stc/ccommon.h71
-rw-r--r--include/stc/cdeq.h53
-rw-r--r--include/stc/clist.h51
-rw-r--r--include/stc/cmap.h50
-rw-r--r--include/stc/cpque.h20
-rw-r--r--include/stc/cset.h2
-rw-r--r--include/stc/csmap.h56
-rw-r--r--include/stc/csset.h2
-rw-r--r--include/stc/cstack.h18
-rw-r--r--include/stc/cstr.h11
-rw-r--r--include/stc/csview.h29
-rw-r--r--include/stc/cvec.h69
-rw-r--r--include/stc/forward.h4
-rw-r--r--include/stc/template.h190
23 files changed, 508 insertions, 864 deletions
diff --git a/include/stc/alt/clist.h b/include/stc/alt/clist.h
index 6695a26e..38b722a4 100644
--- a/include/stc/alt/clist.h
+++ b/include/stc/alt/clist.h
@@ -49,7 +49,7 @@
puts("sorted");
c_foreach (i, clist_ix, list)
if (++n % 10000 == 0) printf("%8d: %10zd\n", n, i.ref->value);
- clist_ix_del(&list);
+ clist_ix_drop(&list);
}
*/
#include <stc/ccommon.h>
@@ -61,16 +61,16 @@ _c_clist_types(clist_VOID, int);
STC_API size_t _clist_count(const clist_VOID* self);
#define _clist_node(_cx_self, vp) c_container_of(vp, _cx_node, value)
-#define _c_using_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw, defTypes) \
+#define _c_using_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw, defTypes) \
\
defTypes( _c_clist_types(_cx_self, i_val); ) \
- typedef i_valraw _cx_rawvalue; \
+ typedef i_valraw _cx_raw; \
\
STC_API _cx_self _cx_memb(_clone)(_cx_self lst); \
- STC_API void _cx_memb(_del)(_cx_self* self); \
+ STC_API void _cx_memb(_drop)(_cx_self* self); \
STC_API void _cx_memb(_push_back)(_cx_self* self, i_val value); \
STC_API void _cx_memb(_push_front)(_cx_self* self, i_val value); \
- STC_API void _cx_memb(_emplace_items)(_cx_self *self, const _cx_rawvalue arr[], size_t n); \
+ STC_API void _cx_memb(_emplace_items)(_cx_self *self, const _cx_raw arr[], size_t n); \
STC_API _cx_self _cx_memb(_split_after)(_cx_self* self, _cx_iter pos1, _cx_iter pos2); \
STC_API void _cx_memb(_splice_after)(_cx_self* self, _cx_iter pos, _cx_self* other); \
STC_DEF void _cx_memb(_splice_after_range)(_cx_self* self, _cx_iter pos, _cx_self* other, _cx_iter i1, _cx_iter i2); \
@@ -89,7 +89,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
STC_INLINE size_t _cx_memb(_count)(_cx_self lst) { return _clist_count((const clist_VOID*) &lst); } \
STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); } \
STC_INLINE i_val _cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } \
- STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); } \
+ STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } \
STC_INLINE void _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw) \
{ _cx_memb(_push_back)(self, i_valfrom(raw)); } \
STC_INLINE void _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw) \
@@ -143,13 +143,13 @@ STC_API size_t _clist_count(const clist_VOID* self);
while (n-- && it.ref) _cx_memb(_next)(&it); return it; \
} \
\
- _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw) \
+ _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw) \
struct stc_trailing_semicolon
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw) \
+#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw) \
\
STC_DEF _cx_self \
_cx_memb(_clone)(_cx_self lst) { \
@@ -160,7 +160,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
} \
\
STC_DEF void \
- _cx_memb(_del)(_cx_self* self) { \
+ _cx_memb(_drop)(_cx_self* self) { \
while (self->last) _cx_memb(_erase_after_)(self, self->last); \
} \
\
@@ -176,7 +176,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
} \
\
STC_DEF void \
- _cx_memb(_emplace_items)(_cx_self *self, const _cx_rawvalue arr[], size_t n) { \
+ _cx_memb(_emplace_items)(_cx_self *self, const _cx_raw arr[], size_t n) { \
for (size_t i=0; i<n; ++i) _cx_memb(_push_back)(self, i_valfrom(arr[i])); \
} \
\
@@ -232,7 +232,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
node->next = next; \
if (del == next) self->last = node = NULL; \
else if (self->last == del) self->last = node, node = NULL; \
- i_valdel(&del->value); c_free(del); \
+ i_valdrop(&del->value); c_free(del); \
return node; \
} \
\
@@ -361,7 +361,7 @@ _clist_mergesort(clist_VOID_node *list, int (*cmp)(const void*, const void*)) {
}
#else
-#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdel, i_valfrom, i_valto, i_valraw)
+#define _c_implement_clist(_cx_self, i_val, i_cmp, i_valdrop, i_valfrom, i_valto, i_valraw)
#endif
#endif
diff --git a/include/stc/alt/csmap.h b/include/stc/alt/csmap.h
index 5b331efb..9be41054 100644
--- a/include/stc/alt/csmap.h
+++ b/include/stc/alt/csmap.h
@@ -30,7 +30,7 @@
using_csmap(mx, int, char); // Sorted map<int, char>
int main(void) {
- c_autovar (csmap_mx m = csmap_mx_init(), csmap_mx_del(&m))
+ c_autovar (csmap_mx m = csmap_mx_init(), csmap_mx_drop(&m))
{
csmap_mx_insert(&m, 5, 'a');
csmap_mx_insert(&m, 8, 'b');
@@ -74,7 +74,7 @@ int main(void) {
typedef _i_SET_ONLY( _cx_rawkey ) \
_i_MAP_ONLY( struct { _cx_rawkey first; \
_cx_memb(_rawmapped) second; } ) \
- _cx_rawvalue; \
+ _cx_raw; \
\
STC_API _cx_self _cx_memb(_init)(void); \
STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, i_keyraw rkey, _cx_iter* out); \
@@ -91,8 +91,8 @@ int main(void) {
\
STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.size == 0; } \
STC_INLINE size_t _cx_memb(_size)(_cx_self cx) { return cx.size; } \
- STC_INLINE void _cx_memb(_del)(_cx_self* self) { _cx_memb(_del_r_)(self->root); } \
- STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); *self = _cx_memb(_init)(); } \
+ STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_del_r_)(self->root); } \
+ STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); *self = _cx_memb(_init)(); } \
STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) {c_swap(_cx_self, *a, *b); } \
STC_INLINE _cx_self _cx_memb(_clone)(_cx_self cx) { return c_make(_cx_self){ _cx_memb(_clone_r_)(cx.root), cx.size}; } \
STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) \
@@ -103,9 +103,9 @@ int main(void) {
{_cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); } \
\
STC_INLINE void \
- _cx_memb(_value_del)(_cx_value* val) { \
- i_keydel(_i_keyref(val)); \
- _i_MAP_ONLY( i_valdel(&val->second); ) \
+ _cx_memb(_value_drop)(_cx_value* val) { \
+ i_keydrop(_i_keyref(val)); \
+ _i_MAP_ONLY( i_valdrop(&val->second); ) \
} \
\
STC_INLINE void \
@@ -125,7 +125,7 @@ int main(void) {
} \
\
STC_INLINE void \
- _cx_memb(_emplace_items)(_cx_self* self, const _cx_rawvalue arr[], size_t n) { \
+ _cx_memb(_emplace_items)(_cx_self* self, const _cx_raw arr[], size_t n) { \
for (size_t i=0; i<n; ++i) _i_SET_ONLY( _cx_memb(_emplace)(self, arr[i]); ) \
_i_MAP_ONLY( _cx_memb(_emplace)(self, arr[i].first, arr[i].second); ) \
} \
@@ -134,7 +134,7 @@ int main(void) {
_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_keydel(&key); _i_MAP_ONLY( i_valdel(&mapped); )} \
+ else {i_keydrop(&key); _i_MAP_ONLY( i_valdrop(&mapped); )} \
return res; \
} \
\
@@ -143,7 +143,7 @@ int main(void) {
_cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped) { \
_cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key)); \
if (res.inserted) res.ref->first = key; \
- else {i_keydel(&key); i_valdel(&res.ref->second); } \
+ else {i_keydrop(&key); i_valdrop(&res.ref->second); } \
res.ref->second = mapped; return res; \
} \
\
@@ -156,7 +156,7 @@ int main(void) {
_cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) { \
_cx_result res = _cx_memb(_insert_entry_)(self, rkey); \
if (res.inserted) res.ref->first = i_keyfrom(rkey); \
- else i_valdel(&res.ref->second); \
+ else i_valdrop(&res.ref->second); \
res.ref->second = i_valfrom(rmapped); return res; \
} \
\
@@ -191,8 +191,8 @@ int main(void) {
} \
\
_c_implement_aatree(_cx_self, C, i_key, i_val, i_cmp, \
- i_valdel, i_valfrom, i_valto, i_valraw, \
- i_keydel, i_keyfrom, i_keyto, i_keyraw) \
+ i_valdrop, i_valfrom, i_valto, i_valraw, \
+ i_keydrop, i_keyfrom, i_keyto, i_keyraw) \
struct stc_trailing_semicolon
/* -------------------------- IMPLEMENTATION ------------------------- */
@@ -204,8 +204,8 @@ _c_aatree_complete_types(csmap_SENTINEL, csmap_);
static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_sentinel, 0};
#define _c_implement_aatree(_cx_self, C, i_key, i_val, i_cmp, \
- i_valdel, i_valfrom, i_valto, i_valraw, \
- i_keydel, i_keyfrom, i_keyto, i_keyraw) \
+ i_valdrop, i_valfrom, i_valto, i_valraw, \
+ i_keydrop, i_keyfrom, i_keyto, i_keyraw) \
STC_DEF _cx_self \
_cx_memb(_init)(void) { \
_cx_self cx = {(_cx_node *) &_aatree_sentinel, 0}; \
@@ -330,7 +330,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti
if (c != 0) \
tn->link[c < 0] = _cx_memb(_erase_r_)(tn->link[c < 0], rkey, erased); \
else { \
- if (!*erased) { _cx_memb(_value_del)(&tn->value); *erased = 1; } \
+ if (!*erased) { _cx_memb(_value_drop)(&tn->value); *erased = 1; } \
if (tn->link[0]->level && tn->link[1]->level) { \
tx = tn->link[0]; \
while (tx->link[1]->level) \
@@ -395,7 +395,7 @@ static csmap_SENTINEL_node _aatree_sentinel = {&_aatree_sentinel, &_aatree_senti
if (tn->level != 0) { \
_cx_memb(_del_r_)(tn->link[0]); \
_cx_memb(_del_r_)(tn->link[1]); \
- _cx_memb(_value_del)(&tn->value); \
+ _cx_memb(_value_drop)(&tn->value); \
c_free(tn); \
} \
}
diff --git a/include/stc/alt/cstr.h b/include/stc/alt/cstr.h
index 76b3a800..1a9280e8 100644
--- a/include/stc/alt/cstr.h
+++ b/include/stc/alt/cstr.h
@@ -41,7 +41,7 @@ typedef union {
/**************************** PRIVATE API **********************************/
-enum { SSO_CAP = offsetof(cstr, lon.ncap) + sizeof((cstr){{0}}.lon.ncap) - 1 };
+enum { SSO_CAP = sizeof(_cstr_rep_t) - 1 };
#define cstr_is_long(s) (bool)((s)->sso.cap_len & 128)
#define cstr_select_(s, memb) (cstr_is_long(s) ? cstr_l_##memb : cstr_s_##memb)
@@ -52,18 +52,18 @@ enum { SSO_CAP = offsetof(cstr, lon.ncap) + sizeof((cstr){{0}}.lon.ncap) - 1 };
#define cstr_s_end(s) ((s)->sso.data + cstr_s_size(s))
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-#define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8)
-#define cstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1))
-#define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1))
+ #define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8)
+ #define cstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1))
+ #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1))
#else
-#define cstr_l_cap(s) (~(s)->lon.ncap)
-#define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap))
+ #define cstr_l_cap(s) (~(s)->lon.ncap)
+ #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap))
#endif
#define cstr_l_size(s) ((s)->lon.size)
#define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0)
#define cstr_l_data(s) (s)->lon.data
#define cstr_l_end(s) ((s)->lon.data + cstr_l_size(s))
-#define cstr_l_del(s) free((s)->lon.data)
+#define cstr_l_drop(s) free((s)->lon.data)
STC_API char* cstr_init_(cstr* self, size_t len, size_t cap);
STC_API void cstr_internal_move_(cstr* self, size_t pos1, size_t pos2);
@@ -74,15 +74,16 @@ STC_INLINE void cstr_set_size_(cstr* self, size_t len) {
STC_INLINE _cstr_rep_t cstr_rep_(cstr* self) {
return cstr_is_long(self)
- ? c_make(_cstr_rep_t){self->lon.data, cstr_l_size(self), cstr_l_cap(self)}
- : c_make(_cstr_rep_t){self->sso.data, cstr_s_size(self), cstr_s_cap(self)};
+ ? c_make(_cstr_rep_t){self->lon.data, cstr_l_size(self), cstr_l_cap(self)}
+ : c_make(_cstr_rep_t){self->sso.data, cstr_s_size(self), cstr_s_cap(self)};
}
/**************************** PUBLIC API **********************************/
#define cstr_new(literal) cstr_from_n(literal, sizeof((c_strlit){literal}) - 1)
#define cstr_npos (SIZE_MAX >> 1)
-#define cstr_null (cstr){.sso = {.cap_len = SSO_CAP}}
+#define cstr_null (c_make(cstr){.sso = {.cap_len = SSO_CAP}})
+#define cstr_toraw(self) cstr_str(self)
STC_API char* cstr_reserve(cstr* self, size_t cap);
STC_API void cstr_shrink_to_fit(cstr* self);
@@ -126,8 +127,8 @@ STC_INLINE cstr cstr_clone(cstr s) {
return cstr_from_n(rep.data, rep.size);
}
-STC_INLINE void cstr_del(cstr* self) {
- if (cstr_is_long(self)) cstr_l_del(self);
+STC_INLINE void cstr_drop(cstr* self) {
+ if (cstr_is_long(self)) cstr_l_drop(self);
}
STC_INLINE void cstr_clear(cstr* self) {
@@ -166,11 +167,11 @@ STC_INLINE bool cstr_equals_s(cstr s1, cstr s2) {
return strcmp(cstr_str(&s1), cstr_str(&s2)) == 0;
}
-STC_INLINE bool cstr_equalto(const cstr* s1, const cstr* s2) {
+STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) {
return strcmp(cstr_str(s1), cstr_str(s2)) == 0;
}
-STC_INLINE int cstr_compare(const cstr* s1, const cstr* s2) {
+STC_INLINE int cstr_cmp(const cstr* s1, const cstr* s2) {
return strcmp(cstr_str(s1), cstr_str(s2));
}
diff --git a/include/stc/alt/sstr.h b/include/stc/alt/sstr.h
deleted file mode 100644
index 2ea1033d..00000000
--- a/include/stc/alt/sstr.h
+++ /dev/null
@@ -1,406 +0,0 @@
-/* MIT License
- *
- * Copyright (c) 2021 Tyge Løvset, NORCE, www.norceresearch.no
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
-/* A string type with short string optimization in C99 with optimal short string
- * utilization (23 characters with 24 bytes string representation).
- */
-#ifndef SSTR_INCLUDED
-#define SSTR_INCLUDED
-
-#include <stc/ccommon.h>
-#include <stdlib.h>
-#include <string.h>
-#include <stdio.h>
-
-#ifndef sstr_size_t
-typedef size_t sstr_size_t;
-#endif
-typedef struct { char* data; sstr_size_t size, cap; } sstr_rep_t;
-typedef const char sstr_literal_t[];
-
-typedef union {
- struct { char* data; sstr_size_t size, ncap; } lon;
- struct { char data[sizeof(sstr_rep_t)]; } sso;
-} sstr;
-
-/**************************** PRIVATE API **********************************/
-
-enum { SSO_CAP = offsetof(sstr, lon.ncap) + sizeof((sstr){{0}}.lon.ncap) - 1 };
-#define sstr_is_long(s) (bool)((s)->sso.data[SSO_CAP] & 128)
-#define sstr_select_(s, memb) (sstr_is_long(s) ? sstr_l_##memb : sstr_s_##memb)
-
-#define sstr_s_cap(s) SSO_CAP
-#define sstr_s_size(s) ((sstr_size_t)(SSO_CAP - (s)->sso.data[SSO_CAP]))
-#define sstr_s_set_size(s, len) ((s)->sso.data[SSO_CAP] = SSO_CAP - (len), (s)->sso.data[len] = 0)
-#define sstr_s_data(s) (s)->sso.data
-#define sstr_s_end(s) ((s)->sso.data + sstr_s_size(s))
-
-#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
-#define byte_rotl_(x, b) ((x) << (b)*8 | (x) >> (sizeof(x) - (b))*8)
-#define sstr_l_cap(s) (~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1))
-#define sstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_(cap, 1))
-#else
-#define sstr_l_cap(s) (~(s)->lon.ncap)
-#define sstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(cap))
-#endif
-#define sstr_l_size(s) ((s)->lon.size)
-#define sstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0)
-#define sstr_l_data(s) (s)->lon.data
-#define sstr_l_end(s) ((s)->lon.data + sstr_l_size(s))
-#define sstr_l_del(s) free((s)->lon.data)
-
-STC_API char* sstr_init_(sstr* self, sstr_size_t len, sstr_size_t cap);
-STC_API void sstr_internal_move_(sstr* self, size_t pos1, size_t pos2);
-
-STC_INLINE void sstr_set_size_(sstr* self, sstr_size_t len) {
- sstr_select_(self, set_size(self, len));
-}
-
-STC_INLINE sstr_rep_t sstr_rep_(sstr* self) {
- return sstr_is_long(self)
- ? c_make(sstr_rep_t){self->lon.data, sstr_l_size(self), sstr_l_cap(self)}
- : c_make(sstr_rep_t){self->sso.data, sstr_s_size(self), sstr_s_cap(self)};
-}
-
-/**************************** PUBLIC API **********************************/
-
-#define sstr_lit(lit) sstr_from_n(lit, sizeof((sstr_literal_t){lit}) - 1)
-#define sstr_npos (~(sstr_size_t)0 >> 1)
-
-STC_API char* sstr_reserve(sstr* self, sstr_size_t cap);
-STC_API void sstr_shrink_to_fit(sstr* self);
-STC_API void sstr_resize(sstr* self, sstr_size_t size, char value);
-STC_API char* strnstrn(const char *s, const char *needle, size_t slen, size_t nlen);
-STC_API sstr_size_t sstr_find_n(sstr s, const char* needle, sstr_size_t pos, sstr_size_t nmax);
-STC_API void sstr_assign_n(sstr* self, const char* str, sstr_size_t n);
-STC_API void sstr_append_n(sstr* self, const char* str, sstr_size_t n);
-STC_API bool sstr_getdelim(sstr *self, int delim, FILE *fp);
-STC_API void sstr_erase_n(sstr* self, size_t pos, size_t n);
-
-
-STC_INLINE sstr sstr_init(void) {
- sstr s;
- sstr_s_set_size(&s, 0);
- return s;
-}
-
-STC_INLINE sstr sstr_from_n(const char* str, sstr_size_t n) {
- sstr s;
- memcpy(sstr_init_(&s, n, n), str, n);
- return s;
-}
-
-STC_INLINE sstr sstr_from(const char* str) {
- return sstr_from_n(str, strlen(str));
-}
-
-STC_INLINE sstr sstr_with_size(sstr_size_t size, char value) {
- sstr s;
- memset(sstr_init_(&s, size, size), value, size);
- return s;
-}
-
-STC_INLINE sstr sstr_with_capacity(sstr_size_t cap) {
- sstr s;
- sstr_init_(&s, 0, cap);
- return s;
-}
-
-STC_INLINE sstr sstr_clone(sstr s) {
- sstr_rep_t rep = sstr_rep_(&s);
- return sstr_from_n(rep.data, rep.size);
-}
-
-STC_INLINE void sstr_del(sstr* self) {
- if (sstr_is_long(self)) sstr_l_del(self);
-}
-
-STC_INLINE void sstr_clear(sstr* self) {
- sstr_set_size_(self, 0);
-}
-
-STC_INLINE char* sstr_data(sstr* self) {
- return sstr_select_(self, data(self));
-}
-
-STC_INLINE const char* sstr_str(const sstr* self) {
- return sstr_select_(self, data(self));
-}
-
-STC_INLINE bool sstr_empty(sstr s) {
- return sstr_select_(&s, size(&s)) == 0;
-}
-
-STC_INLINE sstr_size_t sstr_size(sstr s) {
- return sstr_select_(&s, size(&s));
-}
-
-STC_INLINE sstr_size_t sstr_length(sstr s) {
- return sstr_select_(&s, size(&s));
-}
-
-STC_INLINE sstr_size_t sstr_capacity(sstr s) {
- return sstr_select_(&s, cap(&s));
-}
-
-STC_INLINE bool sstr_equals(sstr s1, const char* str) {
- return strcmp(sstr_str(&s1), str) == 0;
-}
-
-STC_INLINE bool sstr_equals_s(sstr s1, sstr s2) {
- return strcmp(sstr_str(&s1), sstr_str(&s2)) == 0;
-}
-
-STC_INLINE bool sstr_equalto(const sstr* s1, const sstr* s2) {
- return strcmp(sstr_str(s1), sstr_str(s2)) == 0;
-}
-
-STC_INLINE int sstr_compare(const sstr* s1, const sstr* s2) {
- return strcmp(sstr_str(s1), sstr_str(s2));
-}
-
-STC_INLINE sstr_size_t sstr_find(sstr s, const char* needle) {
- const char *str = sstr_str(&s), *res = strstr(str, needle);
- return res ? res - str : sstr_npos;
-}
-
-STC_INLINE bool sstr_find_s(sstr s, sstr needle) {
- return sstr_find(s, sstr_str(&needle));
-}
-
-STC_INLINE bool sstr_contains(sstr s, const char* needle) {
- return strstr(sstr_str(&s), needle) != NULL;
-}
-
-STC_INLINE bool sstr_contains_s(sstr s, sstr needle) {
- return strstr(sstr_str(&s), sstr_str(&needle)) != NULL;
-}
-
-STC_INLINE bool sstr_starts_with(sstr s, const char* sub) {
- const char* str = sstr_str(&s);
- while (*sub && *str == *sub) ++str, ++sub;
- return *sub == 0;
-}
-
-STC_INLINE bool sstr_starts_with_s(sstr s, sstr sub) {
- return sstr_starts_with(s, sstr_str(&sub));
-}
-
-STC_INLINE bool sstr_ends_with(sstr s, const char* sub) {
- sstr_rep_t rep = sstr_rep_(&s); sstr_size_t n = strlen(sub);
- return n <= rep.size && memcmp(rep.data + rep.size - n, sub, n) == 0;
-}
-
-STC_INLINE bool sstr_ends_with_s(sstr s, sstr sub) {
- return sstr_ends_with(s, sstr_str(&sub));
-}
-
-STC_INLINE void sstr_assign(sstr* self, const char* str) {
- sstr_assign_n(self, str, strlen(str));
-}
-
-STC_INLINE void sstr_copy(sstr* self, sstr s) {
- sstr_rep_t rep = sstr_rep_(&s);
- sstr_assign_n(self, rep.data, rep.size);
-}
-
-STC_INLINE void sstr_append(sstr* self, const char* str) {
- sstr_append_n(self, str, strlen(str));
-}
-
-STC_INLINE void sstr_append_s(sstr* self, sstr s) {
- sstr_rep_t rep = sstr_rep_(&s);
- sstr_append_n(self, rep.data, rep.size);
-}
-
-STC_INLINE void sstr_replace_n(sstr* self, size_t pos, size_t len, const char* str, size_t n) {
- sstr_internal_move_(self, pos + len, pos + n);
- memcpy(&sstr_data(self)[pos], str, n);
-}
-
-STC_INLINE void sstr_replace(sstr* self, size_t pos, size_t len, const char* str) {
- sstr_replace_n(self, pos, len, str, strlen(str));
-}
-
-STC_INLINE void sstr_replace_s(sstr* self, size_t pos, size_t len, sstr s) {
- sstr_rep_t rep = sstr_rep_(&s);
- sstr_replace_n(self, pos, len, rep.data, rep.size);
-}
-
-STC_INLINE void sstr_insert_n(sstr* self, size_t pos, const char* str, size_t n) {
- sstr_replace_n(self, pos, 0, str, n);
-}
-
-STC_INLINE void sstr_insert(sstr* self, size_t pos, const char* str) {
- sstr_replace_n(self, pos, 0, str, strlen(str));
-}
-
-STC_INLINE void sstr_insert_s(sstr* self, size_t pos, sstr s) {
- sstr_rep_t rep = sstr_rep_(&s);
- sstr_replace_n(self, pos, 0, rep.data, rep.size);
-}
-
-STC_INLINE bool sstr_getline(sstr *self, FILE *fp) {
- return sstr_getdelim(self, '\n', fp);
-}
-
-/* -------------------------- IMPLEMENTATION ------------------------- */
-
-#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-
-STC_DEF void sstr_internal_move_(sstr* self, size_t pos1, size_t pos2) {
- if (pos1 == pos2)
- return;
- sstr_rep_t rep = sstr_rep_(self);
- sstr_size_t newlen = rep.size + pos2 - pos1;
- if (newlen > rep.cap)
- rep.data = sstr_reserve(self, (rep.size*3 >> 1) + pos2 - pos1);
- memmove(&rep.data[pos2], &rep.data[pos1], rep.size - pos1);
- sstr_set_size_(self, newlen);
-}
-
-STC_DEF char* sstr_init_(sstr* self, sstr_size_t len, sstr_size_t cap) {
- if (cap > SSO_CAP) {
- self->lon.data = (char *)malloc(cap + 1);
- sstr_l_set_size(self, len);
- sstr_l_set_cap(self, cap);
- return self->lon.data;
- }
- sstr_s_set_size(self, len);
- return self->sso.data;
-}
-
-STC_DEF void sstr_shrink_to_fit(sstr* self) {
- sstr_rep_t rep = sstr_rep_(self);
- if (rep.size == rep.cap)
- return;
- if (rep.size > SSO_CAP) {
- self->lon.data = (char *)realloc(self->lon.data, sstr_l_size(self) + 1);
- sstr_l_set_cap(self, sstr_l_size(self));
- } else if (rep.cap > SSO_CAP) {
- memcpy(self->sso.data, rep.data, rep.size + 1);
- sstr_s_set_size(self, rep.size);
- free(rep.data);
- }
-}
-
-STC_DEF char* sstr_reserve(sstr* self, sstr_size_t cap) {
- if (sstr_is_long(self)) {
- if (cap > sstr_l_cap(self)) {
- self->lon.data = (char *)realloc(self->lon.data, cap + 1);
- sstr_l_set_cap(self, cap);
- }
- return self->lon.data;
- }
- /* from short to long: */
- if (cap > sstr_s_cap(self)) {
- char* data = (char *)malloc(cap + 1);
- sstr_size_t len = sstr_s_size(self);
- memcpy(data, self->sso.data, len);
- self->lon.data = data;
- sstr_l_set_size(self, len);
- sstr_l_set_cap(self, cap);
- return data;
- }
- return self->sso.data;
-}
-
-STC_DEF void sstr_resize(sstr* self, sstr_size_t size, char value) {
- sstr_rep_t rep = sstr_rep_(self);
- if (size > rep.size) {
- if (size > rep.cap) rep.data = sstr_reserve(self, size);
- memset(rep.data + rep.size, value, size - rep.size);
- }
- sstr_set_size_(self, size);
-}
-
-STC_DEF char* strnstrn(const char *s, const char *needle, size_t slen, size_t nlen) {
- if (!nlen) return (char *)s;
- if (nlen > slen) return NULL;
- slen -= nlen;
- do {
- if (*s == *needle && !memcmp(s, needle, nlen)) return (char *)s;
- ++s;
- } while (slen--);
- return NULL;
-}
-
-STC_DEF sstr_size_t
-sstr_find_n(sstr s, const char* needle, sstr_size_t pos, sstr_size_t nmax) {
- sstr_rep_t rep = sstr_rep_(&s);
- sstr_size_t nlen = (sstr_size_t) strlen(needle);
- if (pos > rep.size) return sstr_npos;
- char* res = strnstrn(rep.data + pos, needle, rep.size, nmax < nlen ? nmax : nlen);
- return res ? res - rep.data : sstr_npos;
-}
-
-STC_DEF void sstr_assign_n(sstr* self, const char* str, sstr_size_t n) {
- sstr_rep_t rep = sstr_rep_(self);
- if (n > rep.cap) {
- rep.data = (char *)realloc(sstr_is_long(self) ? rep.data : NULL, n + 1);
- sstr_l_set_cap(self, n);
- }
- memmove(rep.data, str, n);
- sstr_set_size_(self, n);
-}
-
-STC_DEF void sstr_append_n(sstr* self, const char* str, sstr_size_t n) {
- sstr_rep_t rep = sstr_rep_(self);
- if (rep.size + n > rep.cap) {
- sstr_size_t off = (sstr_size_t)(str - rep.data); /* handle self append */
- rep.data = sstr_reserve(self, (rep.size*3 >> 1) + n);
- if (off <= rep.size) str = rep.data + off;
- }
- memcpy(rep.data + rep.size, str, n);
- sstr_set_size_(self, rep.size + n);
-}
-
-STC_DEF bool sstr_getdelim(sstr *self, int delim, FILE *fp) {
- int c = fgetc(fp);
- if (c == EOF)
- return false;
- sstr_size_t pos = 0;
- sstr_rep_t rep = sstr_rep_(self);
- for (;;) {
- if (c == delim || c == EOF) {
- sstr_set_size_(self, pos);
- return true;
- }
- if (pos == rep.cap) {
- sstr_set_size_(self, pos);
- rep.data = sstr_reserve(self, (rep.cap = (rep.cap*3 >> 1) + 16));
- }
- rep.data[pos++] = (char) c;
- c = fgetc(fp);
- }
-}
-
-STC_DEF void sstr_erase_n(sstr* self, size_t pos, size_t n) {
- sstr_rep_t rep = sstr_rep_(self);
- if (n > rep.size - pos) n = rep.size - pos;
- memmove(&rep.data[pos], &rep.data[pos + n], rep.size - (pos + n));
- sstr_set_size_(self, rep.size - n);
-}
-
-#endif
-#endif \ No newline at end of file
diff --git a/include/stc/csptr.h b/include/stc/carc.h
index 4753ef12..b4e2e435 100644
--- a/include/stc/csptr.h
+++ b/include/stc/carc.h
@@ -21,35 +21,35 @@
* SOFTWARE.
*/
-/* csptr: shared_ptr type
+/* carc: atomic reference counted shared_ptr
#include <stc/cstr.h>
typedef struct { cstr name, last; } Person;
-Person Person_from(const char* name, const char* last) {
+Person Person_new(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
-void Person_del(Person* p) {
- printf("del: %s %s\n", p->name.str, p->last.str);
- c_del(cstr, &p->name, &p->last);
+void Person_drop(Person* p) {
+ printf("drop: %s %s\n", p->name.str, p->last.str);
+ c_drop(cstr, &p->name, &p->last);
}
#define i_tag person
#define i_val Person
-#define i_valdel Person_del
-#include <stc/csptr.h>
+#define i_valdrop Person_drop
+#include <stc/carc.h>
int main() {
- csptr_person p = csptr_person_new(Person_from("John", "Smiths"));
- csptr_person q = csptr_person_clone(p); // share the pointer
+ carc_person p = carc_person_from(Person_new("John", "Smiths"));
+ carc_person q = carc_person_clone(p); // share the pointer
printf("%s %s. uses: %zu\n", q.get->name.str, q.get->last.str, *q.use_count);
- c_del(csptr_person, &p, &q);
+ c_drop(carc_person, &p, &q);
}
*/
-#ifndef CSPTR_H_INCLUDED
-#define CSPTR_H_INCLUDED
+#ifndef CARC_H_INCLUDED
+#define CARC_H_INCLUDED
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>
@@ -67,15 +67,16 @@ int main() {
#define c_atomic_dec_and_test(v) (atomic_fetch_sub(v, 1) == 1)
#endif
-#define csptr_null {NULL, NULL}
-#define _cx_csptr_rep struct _cx_memb(_rep_)
-#endif // CSPTR_H_INCLUDED
+#define carc_null {NULL, NULL}
+#define _cx_carc_rep struct _cx_memb(_rep_)
+#endif // CARC_H_INCLUDED
#ifndef _i_prefix
-#define _i_prefix csptr_
+#define _i_prefix carc_
#endif
#define _i_has_internal_clone
#include "template.h"
+typedef i_valraw _cx_raw;
#if !c_option(c_no_atomic)
#define _i_atomic_inc(v) c_atomic_inc(v)
@@ -85,9 +86,9 @@ int main() {
#define _i_atomic_dec_and_test(v) !(--*(v))
#endif
#if !c_option(c_is_fwd)
-_cx_deftypes(_c_csptr_types, _cx_self, i_val);
+_cx_deftypes(_c_carc_types, _cx_self, i_val);
#endif
-_cx_csptr_rep { long counter; i_val value; };
+_cx_carc_rep { long counter; i_val value; };
STC_INLINE _cx_self
_cx_memb(_init)(void) { return c_make(_cx_self){NULL, NULL}; }
@@ -96,32 +97,22 @@ STC_INLINE long
_cx_memb(_use_count)(_cx_self ptr) { return ptr.use_count ? *ptr.use_count : 0; }
STC_INLINE _cx_self
-_cx_memb(_with)(_cx_value* p) {
+_cx_memb(_from_ptr)(_cx_value* p) {
_cx_self ptr = {p};
if (p) *(ptr.use_count = c_alloc(long)) = 1;
return ptr;
}
STC_INLINE _cx_self
-_cx_memb(_view)(const _cx_value* p) {
- _cx_self ptr = {(_cx_value*) p};
- return ptr;
-}
-
-STC_INLINE _cx_self
-_cx_memb(_new)(i_val val) {
- _cx_self ptr; _cx_csptr_rep *rep = c_alloc(_cx_csptr_rep);
+_cx_memb(_from)(i_val val) {
+ _cx_self ptr; _cx_carc_rep *rep = c_alloc(_cx_carc_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
}
-STC_INLINE _cx_self _cx_memb(_make)(i_val val) // [deprecated]
- { return _cx_memb(_new)(val); }
-STC_INLINE _cx_self // does not use i_valfrom, so we can bypass c_no_clone
-_cx_memb(_clone)(_cx_self ptr) {
- if (ptr.use_count) _i_atomic_inc(ptr.use_count);
- return ptr;
+STC_INLINE i_val _cx_memb(_toraw)(const _cx_self* self) {
+ return *self->get;
}
STC_INLINE _cx_self
@@ -132,10 +123,10 @@ _cx_memb(_move)(_cx_self* self) {
}
STC_INLINE void
-_cx_memb(_del)(_cx_self* self) {
+_cx_memb(_drop)(_cx_self* self) {
if (self->use_count && _i_atomic_dec_and_test(self->use_count)) {
- i_valdel(self->get);
- if (self->get != &((_cx_csptr_rep *)self->use_count)->value)
+ i_valdrop(self->get);
+ if (self->get != &((_cx_carc_rep *)self->use_count)->value)
c_free(self->get);
c_free(self->use_count);
}
@@ -143,68 +134,68 @@ _cx_memb(_del)(_cx_self* self) {
STC_INLINE void
_cx_memb(_reset)(_cx_self* self) {
- _cx_memb(_del)(self);
+ _cx_memb(_drop)(self);
self->use_count = NULL, self->get = NULL;
}
STC_INLINE void
-_cx_memb(_reset_with)(_cx_self* self, _cx_value* p) {
- _cx_memb(_del)(self);
- *self = _cx_memb(_with)(p);
+_cx_memb(_reset_from)(_cx_self* self, i_val val) {
+ _cx_memb(_drop)(self);
+ *self = _cx_memb(_from)(val);
}
-STC_INLINE void
-_cx_memb(_reset_new)(_cx_self* self, i_val val) {
- _cx_memb(_del)(self);
- *self = _cx_memb(_new)(val);
-}
-
-#if !c_option(c_no_clone)
- STC_INLINE _cx_self _cx_memb(_from)(i_valraw raw) {
- return _cx_memb(_new)(i_valfrom(raw));
- }
-
- STC_INLINE void
- _cx_memb(_reset_from)(_cx_self* self, i_valraw raw) {
- _cx_memb(_del)(self);
- *self = _cx_memb(_new)(i_valfrom(raw));
+#if !c_option(c_no_clone) && !defined _i_no_raw
+ STC_INLINE _cx_self _cx_memb(_new)(i_valraw raw) {
+ return _cx_memb(_from)(i_valfrom(raw));
}
#endif
+// does not use i_valfrom, so we can bypass c_no_clone
+STC_INLINE _cx_self
+_cx_memb(_clone)(_cx_self ptr) {
+ if (ptr.use_count) _i_atomic_inc(ptr.use_count);
+ return ptr;
+}
+
STC_INLINE void
_cx_memb(_copy)(_cx_self* self, _cx_self ptr) {
if (ptr.use_count) _i_atomic_inc(ptr.use_count);
- _cx_memb(_del)(self); *self = ptr;
+ _cx_memb(_drop)(self);
+ *self = ptr;
}
STC_INLINE void
_cx_memb(_take)(_cx_self* self, _cx_self ptr) {
- if (self->get != ptr.get) _cx_memb(_del)(self);
+ if (self->get != ptr.get) _cx_memb(_drop)(self);
*self = ptr;
}
STC_INLINE uint64_t
-_cx_memb(_hash)(const _cx_self* self, size_t n) {
- #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32
- return c_hash64(&self->get, 8);
- #elif (c_option(c_no_compare) || defined _i_default_compare)
- return c_hash32(&self->get, 4);
+_cx_memb(_value_hash)(const _cx_value* x, size_t n) {
+ #if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX
+ return c_hash64(&x, 8);
+ #elif c_option(c_no_cmp)
+ return c_hash32(&x, 4);
#else
- i_valraw raw = i_valto(self->get);
- return i_hash(&raw, sizeof raw);
+ i_valraw rx = i_valto(x);
+ return i_hash(&rx, sizeof rx);
#endif
}
STC_INLINE int
-_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) {
- #if (c_option(c_no_compare) || defined _i_default_compare)
- return (int)(x->get - y->get);
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
+ #if c_option(c_no_cmp)
+ return c_default_cmp(&x, &y);
#else
- i_valraw rx = i_valto(x->get);
- i_valraw ry = i_valto(y->get);
+ i_valraw rx = i_valto(x), ry = i_valto(x);
return i_cmp(&rx, &ry);
#endif
}
+
+STC_INLINE bool
+_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
+ return !_cx_memb(_value_cmp)(x, y);
+}
#undef _i_atomic_inc
#undef _i_atomic_dec_and_test
-#include "template.h" \ No newline at end of file
+#include "template.h"
diff --git a/include/stc/carr2.h b/include/stc/carr2.h
index d1feb90b..92964d4d 100644
--- a/include/stc/carr2.h
+++ b/include/stc/carr2.h
@@ -34,7 +34,7 @@
int main() {
int w = 7, h = 5;
- c_autovar (carr2_int image = carr2_int_init(w, h), carr2_int_del(&image))
+ c_autovar (carr2_int image = carr2_int_init(w, h), carr2_int_drop(&image))
{
int *dat = carr2_int_data(&image);
for (int i = 0; i < carr2_int_size(image); ++i)
@@ -63,7 +63,7 @@ _cx_deftypes(_c_carr2_types, _cx_self, i_val);
STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, i_val value);
STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, _cx_value* storage);
STC_API _cx_value* _cx_memb(_release)(_cx_self* self);
-STC_API void _cx_memb(_del)(_cx_self* self);
+STC_API void _cx_memb(_drop)(_cx_self* self);
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self src);
STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other);
@@ -124,7 +124,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) {
STC_DEF void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
#endif
@@ -135,10 +135,11 @@ STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) {
return values;
}
-STC_DEF void _cx_memb(_del)(_cx_self* self) {
+STC_DEF void _cx_memb(_drop)(_cx_self* self) {
if (!self->data) return;
- for (_cx_value* p = self->data[0], *e = p + _cx_memb(_size)(*self); p != e; ++p)
- i_valdel(p);
+ for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(*self); p != q; ) {
+ --q; i_valdrop(q);
+ }
c_free(self->data[0]); /* values */
c_free(self->data); /* pointers */
}
diff --git a/include/stc/carr3.h b/include/stc/carr3.h
index 4a4a6484..31df5bdf 100644
--- a/include/stc/carr3.h
+++ b/include/stc/carr3.h
@@ -34,7 +34,7 @@
int main() {
int w = 7, h = 5, d = 3;
- c_autovar (carr3_int image = carr3_int_init(w, h, d), carr3_int_del(&image))
+ c_autovar (carr3_int image = carr3_int_init(w, h, d), carr3_int_drop(&image))
{
int *dat = carr3_int_data(&image);
for (int i = 0; i < carr3_int_size(image); ++i)
@@ -65,7 +65,7 @@ _cx_deftypes(_c_carr3_types, _cx_self, i_val);
STC_API _cx_self _cx_memb(_with_values)(size_t xdim, size_t ydim, size_t zdim, i_val value);
STC_API _cx_self _cx_memb(_with_storage)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage);
STC_API _cx_value* _cx_memb(_release)(_cx_self* self);
-STC_API void _cx_memb(_del)(_cx_self* self);
+STC_API void _cx_memb(_drop)(_cx_self* self);
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self src);
STC_API void _cx_memb(_copy)(_cx_self *self, _cx_self other);
@@ -130,7 +130,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) {
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
#endif
@@ -141,10 +141,11 @@ STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) {
return values;
}
-STC_DEF void _cx_memb(_del)(_cx_self* self) {
+STC_DEF void _cx_memb(_drop)(_cx_self* self) {
if (!self->data) return;
- for (_cx_value* p = **self->data, *e = p + _cx_memb(_size)(*self); p != e; ++p)
- i_valdel(p);
+ for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(*self); p != q; ) {
+ --q; i_valdrop(q);
+ }
c_free(self->data[0][0]); /* data */
c_free(self->data); /* pointers */
}
diff --git a/include/stc/cbits.h b/include/stc/cbits.h
index b3a70edc..a6ee3948 100644
--- a/include/stc/cbits.h
+++ b/include/stc/cbits.h
@@ -30,7 +30,7 @@ Similar to boost::dynamic_bitset / std::bitset
#include "cbits.h"
int main() {
- c_autovar (cbits bset = cbits_with_size(23, true), cbits_del(&bset))
+ c_autovar (cbits bset = cbits_with_size(23, true), cbits_drop(&bset))
{
cbits_reset(&bset, 9);
cbits_resize(&bset, 43, false);
@@ -75,14 +75,14 @@ STC_API bool cbits_disjoint(cbits set, cbits other);
STC_INLINE cbits cbits_init() { return c_make(cbits){NULL, 0}; }
STC_INLINE cbits cbits_from(const char* s) { return cbits_from_n(s, strlen(s)); }
STC_INLINE void cbits_clear(cbits* self) { self->size = 0; }
-STC_INLINE void cbits_del(cbits* self) { c_free(self->data64); }
+STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); }
STC_INLINE size_t cbits_size(cbits set) { return set.size; }
#define cbits_new(literal) \
cbits_from_n(literal, sizeof c_make(c_strlit){literal} - 1)
STC_INLINE cbits* cbits_take(cbits* self, cbits other) {
- if (self->data64 != other.data64) {cbits_del(self); *self = other;}
+ if (self->data64 != other.data64) {cbits_drop(self); *self = other;}
return self;
}
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index 89bd272e..7ed0d641 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -26,7 +26,7 @@
typedef struct { cstr name, last; } Person;
-Person Person_from(const char* name, const char* last) {
+Person Person_new(const char* name, const char* last) {
return (Person){.name = cstr_from(name), .last = cstr_from(last)};
}
Person Person_clone(Person p) {
@@ -34,21 +34,21 @@ Person Person_clone(Person p) {
p.last = cstr_clone(p.last);
return p;
}
-void Person_del(Person* p) {
- printf("del: %s %s\n", p->name.str, p->last.str);
- c_del(cstr, &p->name, &p->last);
+void Person_drop(Person* p) {
+ printf("drop: %s %s\n", p->name.str, p->last.str);
+ c_drop(cstr, &p->name, &p->last);
}
#define i_val Person
-#define i_valdel Person_del
+#define i_valdrop Person_drop
#define i_valfrom Person_clone
-#define i_opt c_no_compare // compare by .get addresses only
+#define i_opt c_no_cmp // compare by .get addresses only
#define i_tag prs
#include <stc/cbox.h>
int main() {
- c_autovar (cbox_prs p = cbox_prs_new(Person_from("John", "Smiths")), cbox_prs_del(&p))
- c_autovar (cbox_prs q = cbox_prs_clone(p), cbox_prs_del(&q))
+ c_autovar (cbox_prs p = cbox_prs_new(Person_new("John", "Smiths")), cbox_prs_drop(&p))
+ c_autovar (cbox_prs q = cbox_prs_clone(p), cbox_prs_drop(&q))
{
cstr_assign(&q.get->name, "Joe");
@@ -63,6 +63,7 @@ int main() {
#include "ccommon.h"
#include "forward.h"
#include <stdlib.h>
+#include <string.h>
#define cbox_null {NULL}
#endif // CBOX_H_INCLUDED
@@ -71,6 +72,7 @@ int main() {
#define _i_prefix cbox_
#endif
#include "template.h"
+typedef i_valraw _cx_raw;
#if !c_option(c_is_fwd)
_cx_deftypes(_c_cbox_types, _cx_self, i_val);
@@ -81,17 +83,22 @@ STC_INLINE _cx_self
_cx_memb(_init)(void) { return c_make(_cx_self){NULL}; }
STC_INLINE _cx_self
-_cx_memb(_with)(i_val* p) { return c_make(_cx_self){p}; }
+_cx_memb(_from_ptr)(i_val* p) { return c_make(_cx_self){p}; }
STC_INLINE _cx_self
-_cx_memb(_new)(i_val val) {
+_cx_memb(_from)(i_val val) {
return c_make(_cx_self){c_new(i_val, val)};
}
+STC_INLINE i_val
+_cx_memb(_toraw)(const _cx_self* self) {
+ return *self->get;
+}
+
// destructor
STC_INLINE void
-_cx_memb(_del)(_cx_self* self) {
- if (self->get) { i_valdel(self->get); c_free(self->get); }
+_cx_memb(_drop)(_cx_self* self) {
+ if (self->get) { i_valdrop(self->get); c_free(self->get); }
}
STC_INLINE _cx_self
@@ -102,32 +109,23 @@ _cx_memb(_move)(_cx_self* self) {
STC_INLINE void
_cx_memb(_reset)(_cx_self* self) {
- _cx_memb(_del)(self); self->get = NULL;
-}
-
-// take ownership of *p
-STC_INLINE void
-_cx_memb(_reset_with)(_cx_self* self, _cx_value* p) {
- _cx_memb(_del)(self); self->get = p;
+ _cx_memb(_drop)(self); self->get = NULL;
}
// take ownership of val
STC_INLINE void
-_cx_memb(_reset_new)(_cx_self* self, i_val val) {
- if (self->get) { i_valdel(self->get); *self->get = val; }
+_cx_memb(_reset_from)(_cx_self* self, i_val val) {
+ if (self->get) { i_valdrop(self->get); *self->get = val; }
else self->get = c_new(i_val, val);
}
-#if !c_option(c_no_clone)
+#if !c_option(c_no_clone)
+#if !defined _i_no_raw
STC_INLINE _cx_self
- _cx_memb(_from)(i_valraw raw) {
+ _cx_memb(_new)(i_valraw raw) {
return c_make(_cx_self){c_new(i_val, i_valfrom(raw))};
}
-
- STC_INLINE void
- _cx_memb(_reset_from)(_cx_self* self, i_valraw raw) {
- _cx_memb(_reset_new)(self, i_valfrom(raw));
- }
+#endif
STC_INLINE _cx_self
_cx_memb(_clone)(_cx_self other) {
@@ -138,37 +136,41 @@ _cx_memb(_reset_new)(_cx_self* self, i_val val) {
STC_INLINE void
_cx_memb(_copy)(_cx_self* self, _cx_self other) {
if (self->get == other.get) return;
- if (other.get) _cx_memb(_reset_new)(self, *other.get);
- else _cx_memb(_reset)(self);
+ _cx_memb(_drop)(self);
+ *self = _cx_memb(_clone)(other);
}
#endif
STC_INLINE void
_cx_memb(_take)(_cx_self* self, _cx_self other) {
- if (other.get != self->get) _cx_memb(_del)(self);
+ if (other.get != self->get) _cx_memb(_drop)(self);
*self = other;
}
STC_INLINE uint64_t
-_cx_memb(_hash)(const _cx_self* self, size_t n) {
- #if (c_option(c_no_compare) || defined _i_default_compare) && SIZE_MAX >> 32
- return c_hash64(&self->get, 8);
- #elif (c_option(c_no_compare) || defined _i_default_compare)
- return c_hash32(&self->get, 4);
+_cx_memb(_value_hash)(const _cx_value* x, size_t n) {
+ #if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX
+ return c_hash64(&x, 8);
+ #elif c_option(c_no_cmp)
+ return c_hash32(&x, 4);
#else
- i_valraw raw = i_valto(self->get);
- return i_hash(&raw, sizeof raw);
+ i_valraw rx = i_valto(x);
+ return i_hash(&rx, sizeof rx);
#endif
}
STC_INLINE int
-_cx_memb(_compare)(const _cx_self* x, const _cx_self* y) {
- #if (c_option(c_no_compare) || defined _i_default_compare)
- return (int)(x->get - y->get);
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
+ #if c_option(c_no_cmp)
+ return c_default_cmp(&x, &y);
#else
- i_valraw rx = i_valto(x->get);
- i_valraw ry = i_valto(y->get);
+ i_valraw rx = i_valto(x), ry = i_valto(x);
return i_cmp(&rx, &ry);
#endif
}
-#include "template.h" \ No newline at end of file
+
+STC_INLINE bool
+_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) {
+ return !_cx_memb(_value_cmp)(x, y);
+}
+#include "template.h"
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 2942873f..b2ae7b97 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -93,33 +93,37 @@
#endif
typedef const char c_strlit[];
-#define c_delete(T, ptr) do { T *_c_p = ptr; T##_del(_c_p); c_free(_c_p); } while (0)
+#define c_delete(T, ptr) do { T *_c_p = ptr; T##_drop(_c_p); c_free(_c_p); } while (0)
#define c_swap(T, x, y) do { T _c_t = x; x = y; y = _c_t; } while (0)
#define c_arraylen(a) (sizeof (a)/sizeof (a)[0])
-#define c_less_compare(less, x, y) (less(y, x) - less(x, y))
+#define c_less_cmp(less, x, y) (less(y, x) - less(x, y))
#define c_default_less(x, y) (*(x) < *(y))
-#define c_default_compare(x, y) c_less_compare(c_default_less, x, y)
-#define c_default_equalto(x, y) (*(x) == *(y))
-#define c_memcmp_equalto(x, y) (memcmp(x, y, sizeof *(x)) == 0)
-
-#define c_rawstr_compare(x, y) strcmp(*(x), *(y))
-#define c_rawstr_equalto(x, y) (strcmp(*(x), *(y)) == 0)
-#define c_rawstr_hash(x, dummy) c_strhash(*(x))
+#define c_default_cmp(x, y) c_less_cmp(c_default_less, x, y)
+#define c_default_eq(x, y) (*(x) == *(y))
+#define c_memcmp_eq(x, y) (memcmp(x, y, sizeof *(x)) == 0)
#define c_default_clone(x) (x)
#define c_default_fromraw(x) (x) // [deprecated]
#define c_default_toraw(ptr) (*(ptr))
-#define c_default_del(ptr) ((void) (ptr))
+#define c_default_drop(ptr) ((void) (ptr))
#define c_option(flag) ((i_opt) & (flag))
#define c_is_fwd 1
#define c_no_atomic 2
#define c_no_clone 4
-#define c_no_compare 8
+#define c_no_cmp 8
/* Generic algorithms */
+typedef char* c_mutstr;
+typedef const char* c_rawstr;
+#define c_rawstr_cmp(xp, yp) strcmp(*(xp), *(yp))
+#define c_rawstr_eq(xp, yp) (!strcmp(*(xp), *(yp)))
+#define c_rawstr_hash(p, dummy) c_strhash(*(p))
+#define c_rawstr_clone(s) strcpy((char*)c_malloc(strlen(s) + 1), s)
+#define c_rawstr_drop(p) c_free((char *) &**(p))
+
#define _c_ROTL(x, k) (x << (k) | x >> (8*sizeof(x) - (k)))
STC_INLINE uint64_t c_strhash(const char *s) {
@@ -165,19 +169,20 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) {
#define c_autovar(declvar, ...) for (declvar, *_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_autoscope(init, ...) for (int _c_ii = (init, 0); !_c_ii; ++_c_ii, __VA_ARGS__)
#define c_autodefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__)
+#define c_breakauto continue
#define c_auto(...) c_MACRO_OVERLOAD(c_auto, __VA_ARGS__)
#define c_auto_2(C, a) \
- c_autovar(C a = C##_init(), C##_del(&a))
+ c_autovar(C a = C##_init(), C##_drop(&a))
#define c_auto_3(C, a, b) \
c_autovar(c_EXPAND(C a = C##_init(), b = C##_init()), \
- C##_del(&b), C##_del(&a))
+ C##_drop(&b), C##_drop(&a))
#define c_auto_4(C, a, b, c) \
c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \
- C##_del(&c), C##_del(&b), C##_del(&a))
+ C##_drop(&c), C##_drop(&b), C##_drop(&a))
#define c_auto_5(C, a, b, c, d) \
c_autovar(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \
- C##_del(&d), C##_del(&c), C##_del(&b), C##_del(&a))
+ C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a))
#define c_autobuf(b, type, n) c_autobuf_N(b, type, n, 256)
#define c_autobuf_N(b, type, n, BYTES) \
@@ -185,28 +190,42 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) {
*b = (n)*sizeof *b > (BYTES) ? c_alloc_n(type, n) : _c_b \
; b; b != _c_b ? c_free(b) : (void)0, b = NULL)
-#define c_apply(C, method, cx, ...) do { \
- const C##_rawvalue _c_arr[] = __VA_ARGS__; \
+#define c_apply(v, method, T, ...) do { \
+ const T _c_arr[] = __VA_ARGS__; \
+ for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \
+ { T v = _c_arr[index]; method; } \
+} while (0)
+#define c_apply_arr(v, method, T, arr, n) do { \
+ const T* _c_arr = arr; size_t _n = n; \
+ for (size_t index = 0; index < _n; ++index) \
+ { T v = _c_arr[index]; method; } \
+} while (0)
+#define c_apply_cnt(v, method, C, ...) do { \
+ size_t index = 0; \
+ c_foreach (_it, C, __VA_ARGS__) \
+ { const C##_value v = *_it.ref; method; ++index; } \
+} while (0)
+#define c_pair(v) (v).first, (v).second
+
+// [deprecated]
+#define c_apply_OLD(C, method, cx, ...) do { \
+ const C##_raw _c_arr[] = __VA_ARGS__; \
C* _c_cx = cx; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
C##_##method(_c_cx, _c_arr[_c_i]); \
} while (0)
-#define c_apply_pair(C, method, cx, ...) do { \
- const C##_rawvalue _c_arr[] = __VA_ARGS__; \
+// [deprecated]
+#define c_apply_pair_OLD(C, method, cx, ...) do { \
+ const C##_raw _c_arr[] = __VA_ARGS__; \
C* _c_cx = cx; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
C##_##method(_c_cx, _c_arr[_c_i].first, _c_arr[_c_i].second); \
} while (0)
-#define c_apply_n(C, method, cx, arr, n) do { \
- C* _c_cx = cx; \
- for (const C##_rawvalue *_c_i = arr, *_c_end = _c_i+(n); _c_i != _c_end; ++_c_i) \
- C##_##method(_c_cx, *_c_i); \
-} while (0)
-#define c_del(C, ...) do { \
+#define c_drop(C, ...) do { \
C* _c_arr[] = {__VA_ARGS__}; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
- C##_del(_c_arr[_c_i]); \
+ C##_drop(_c_arr[_c_i]); \
} while (0)
#if defined(__SIZEOF_INT128__)
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h
index f8cc1d52..97a19311 100644
--- a/include/stc/cdeq.h
+++ b/include/stc/cdeq.h
@@ -38,25 +38,27 @@ struct cdeq_rep { size_t size, cap; void* base[]; };
#if !c_option(c_is_fwd)
_cx_deftypes(_c_cdeq_types, _cx_self, i_val);
#endif
-typedef i_valraw _cx_rawvalue;
+typedef i_valraw _cx_raw;
STC_API _cx_self _cx_memb(_init)(void);
STC_API void _cx_memb(_clear)(_cx_self* self);
-STC_API void _cx_memb(_del)(_cx_self* self);
+STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value);
STC_API bool _cx_memb(_expand_right_half_)(_cx_self* self, size_t idx, size_t n);
+STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self);
#ifndef _i_queue
#if !c_option(c_no_clone)
-STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self);
STC_API _cx_iter _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2);
+#if !defined _i_no_raw
STC_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
- const _cx_rawvalue* p1, const _cx_rawvalue* p2);
+ const _cx_raw* p1, const _cx_raw* p2);
+#endif
#endif // !c_no_clone
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_API _cx_iter _cx_memb(_find_in)(_cx_iter p1, _cx_iter p2, i_valraw raw);
-STC_API int _cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y);
+STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y);
#endif
STC_API _cx_value* _cx_memb(_push_front)(_cx_self* self, i_val value);
STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2);
@@ -66,13 +68,15 @@ STC_API _cx_iter _cx_memb(_insert_range_p)(_cx_self* self, _cx_value* pos
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
+#if !defined _i_no_raw
STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw)
{ return _cx_memb(_push_back)(self, i_valfrom(raw)); }
+#endif
STC_INLINE i_val _cx_memb(_value_clone)(i_val val)
{ return i_valfrom(i_valto(&val)); }
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
#endif
STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return !cdeq_rep_(&cx)->size; }
@@ -83,7 +87,7 @@ STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfro
STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_valto(pval); }
STC_INLINE void _cx_memb(_pop_front)(_cx_self* self)
- { i_valdel(self->data); ++self->data; --cdeq_rep_(self)->size; }
+ { i_valdrop(self->data); ++self->data; --cdeq_rep_(self)->size; }
STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self)
{ return self->data + cdeq_rep_(self)->size - 1; }
STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; }
@@ -106,7 +110,7 @@ _cx_memb(_with_capacity)(const size_t n) {
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) {
_cx_value* p = &self->data[--cdeq_rep_(self)->size];
- i_valdel(p);
+ i_valdrop(p);
}
STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) {
@@ -150,7 +154,7 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx_iter it2) {
return _cx_memb(_erase_range_p)(self, it1.ref, it2.ref);
}
-#if !c_option(c_no_clone)
+#if !c_option(c_no_clone) && !defined _i_no_raw
STC_INLINE _cx_iter
_cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2) {
@@ -166,7 +170,7 @@ _cx_memb(_emplace)(_cx_self* self, const size_t idx, i_valraw raw) {
return _cx_memb(_emplace_range_p)(self, self->data + idx, &raw, &raw + 1);
}
STC_INLINE _cx_iter
-_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_rawvalue arr[], const size_t n) {
+_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
@@ -175,7 +179,7 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, i_valraw raw) {
}
#endif // !c_no_clone
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, i_valraw raw) {
@@ -201,9 +205,9 @@ _cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2,
STC_INLINE void
_cx_memb(_sort)(_cx_self* self) {
- _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_compare));
+ _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp));
}
-#endif // !c_no_compare
+#endif // !c_no_cmp
#endif // _i_queue
/* -------------------------- IMPLEMENTATION ------------------------- */
@@ -224,8 +228,9 @@ STC_DEF void
_cx_memb(_clear)(_cx_self* self) {
struct cdeq_rep* rep = cdeq_rep_(self);
if (rep->cap) {
- for (_cx_value *p = self->data, *q = p + rep->size; p != q; ++p)
- i_valdel(p);
+ for (_cx_value *p = self->data, *q = p + rep->size; p != q; ) {
+ --q; i_valdrop(q);
+ }
rep->size = 0;
}
}
@@ -242,7 +247,7 @@ _cx_memb(_shrink_to_fit)(_cx_self *self) {
}
STC_DEF void
-_cx_memb(_del)(_cx_self* self) {
+_cx_memb(_drop)(_cx_self* self) {
_cx_memb(_clear)(self);
if (cdeq_rep_(self)->cap)
c_free(cdeq_rep_(self));
@@ -357,7 +362,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
const size_t n = p2 - p1;
if (n > 0) {
_cx_value* p = p1, *end = self->data + cdeq_rep_(self)->size;
- for (; p != p2; ++p) i_valdel(p);
+ for (; p != p2; ++p) { i_valdrop(p); }
if (p1 == self->data) self->data += n;
else memmove(p1, p2, (end - p2) * sizeof(i_val));
cdeq_rep_(self)->size -= n;
@@ -366,15 +371,15 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) {
}
#if !c_option(c_no_clone)
-
+#if !defined _i_no_raw
STC_DEF _cx_iter
-_cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos, const _cx_rawvalue* p1, const _cx_rawvalue* p2) {
+_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);
return it;
}
-
+#endif
STC_DEF _cx_iter
_cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
const _cx_value* p1, const _cx_value* p2) {
@@ -385,7 +390,7 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
}
#endif // !c_option(c_no_clone)
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) {
@@ -397,12 +402,12 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) {
}
STC_DEF int
-_cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y) {
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
i_valraw rx = i_valto(x);
i_valraw ry = i_valto(y);
return i_cmp(&rx, &ry);
}
-#endif // !c_no_compare
+#endif // !c_no_cmp
#endif // !_i_queue
#endif // IMPLEMENTATION
#include "template.h"
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 7cd2df50..e2ff694e 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -87,19 +87,20 @@ _c_clist_complete_types(clist_VOID, dummy);
_cx_deftypes(_c_clist_types, _cx_self, i_val);
#endif
_cx_deftypes(_c_clist_complete_types, _cx_self, dummy);
-typedef i_valraw _cx_rawvalue;
+typedef i_valraw _cx_raw;
STC_API size_t _clist_count(const clist_VOID* self);
-STC_API void _cx_memb(_del)(_cx_self* self);
+STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val value);
STC_API _cx_value* _cx_memb(_push_front)(_cx_self* self, i_val value);
STC_API _cx_iter _cx_memb(_insert)(_cx_self* self, _cx_iter it, i_val value);
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);
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_API size_t _cx_memb(_remove)(_cx_self* self, i_valraw val);
STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw val);
+STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y);
#endif
STC_API _cx_iter _cx_memb(_splice)(_cx_self* self, _cx_iter it, _cx_self* other);
STC_API _cx_self _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2);
@@ -108,29 +109,28 @@ STC_API _cx_node* _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node);
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self cx);
-STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw)
- { return _cx_memb(_push_back)(self, i_valfrom(raw)); }
-STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw)
- { return _cx_memb(_push_front)(self, i_valfrom(raw)); }
-STC_INLINE _cx_iter _cx_memb(_emplace)(_cx_self* self, _cx_iter it, i_valraw raw)
- { return _cx_memb(_insert)(self, it, i_valfrom(raw)); }
-STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw)
- { return i_valfrom(raw); }
STC_INLINE i_val _cx_memb(_value_clone)(i_val val)
{ return i_valfrom(i_valto(&val)); }
STC_INLINE void
_cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->last == other.last) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
+#if !defined _i_no_raw
+STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw)
+ { return _cx_memb(_push_back)(self, i_valfrom(raw)); }
+STC_INLINE _cx_value* _cx_memb(_emplace_front)(_cx_self* self, i_valraw raw)
+ { return _cx_memb(_push_front)(self, i_valfrom(raw)); }
+STC_INLINE _cx_iter _cx_memb(_emplace)(_cx_self* self, _cx_iter it, i_valraw raw)
+ { return _cx_memb(_insert)(self, it, i_valfrom(raw)); }
+#endif
#endif
STC_INLINE _cx_self _cx_memb(_init)(void) { return c_make(_cx_self){NULL}; }
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; }
STC_INLINE bool _cx_memb(_empty)(_cx_self cx) { return cx.last == NULL; }
STC_INLINE size_t _cx_memb(_count)(_cx_self cx)
{ return _clist_count((const clist_VOID*) &cx); }
-STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); }
-STC_INLINE i_valraw _cx_memb(_value_toraw)(_cx_value* pval) { return i_valto(pval); }
+STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); }
STC_INLINE void _cx_memb(_pop_front)(_cx_self* self)
{ _cx_memb(_erase_after_)(self, self->last); }
STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return &self->last->next->value; }
@@ -166,7 +166,7 @@ _cx_memb(_splice_range)(_cx_self* self, _cx_iter it,
return _cx_memb(_splice)(self, it, &tmp);
}
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, i_valraw val) {
return _cx_memb(_find_in)(_cx_memb(_begin)(self), _cx_memb(_end)(self), val);
@@ -191,13 +191,13 @@ _cx_memb(_get_mut)(_cx_self* self, i_valraw val) {
STC_DEF _cx_self
_cx_memb(_clone)(_cx_self cx) {
_cx_self out = _cx_memb(_init)();
- c_foreach (it, _cx_self, cx) _cx_memb(_emplace_back)(&out, i_valto(it.ref));
+ c_foreach (it, _cx_self, cx) _cx_memb(_push_back)(&out, i_valfrom(i_valto(it.ref)));
return out;
}
#endif
STC_DEF void
-_cx_memb(_del)(_cx_self* self) {
+_cx_memb(_drop)(_cx_self* self) {
while (self->last) _cx_memb(_erase_after_)(self, self->last);
}
@@ -250,7 +250,7 @@ _cx_memb(_erase_after_)(_cx_self* self, _cx_node* node) {
node->next = next;
if (del == next) self->last = node = NULL;
else if (self->last == del) self->last = node, node = NULL;
- i_valdel(&del->value); c_free(del);
+ i_valdrop(&del->value); c_free(del);
return node;
}
@@ -280,7 +280,7 @@ _cx_memb(_split_off)(_cx_self* self, _cx_iter it1, _cx_iter it2) {
return cx;
}
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, i_valraw val) {
@@ -321,7 +321,14 @@ _cx_memb(_sort)(_cx_self* self) {
if (self->last)
self->last = (_cx_node *) _clist_mergesort((clist_VOID_node *) self->last->next, _cx_memb(_sort_cmp_));
}
-#endif // !c_no_compare
+
+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);
+ return i_cmp(&rx, &ry);
+}
+#endif // !c_no_cmp
#endif // TEMPLATE IMPLEMENTATION
@@ -336,7 +343,7 @@ _clist_count(const clist_VOID* self) {
return n;
}
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
// Singly linked list Mergesort implementation by Simon Tatham. O(n*log n).
// https://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
@@ -387,7 +394,7 @@ _clist_mergesort(clist_VOID_node *list, int (*cmp)(const clist_VOID_node*, const
insize *= 2;
}
}
-#endif // !c_no_compare
+#endif // !c_no_cmp
#endif // NON-TEMPLATE IMPLEMENTATION
#include "template.h"
#define CLIST_H_INCLUDED
diff --git a/include/stc/cmap.h b/include/stc/cmap.h
index 3d1915d3..2d0b11fc 100644
--- a/include/stc/cmap.h
+++ b/include/stc/cmap.h
@@ -31,7 +31,7 @@
#include <stc/cmap.h>
int main(void) {
- c_autovar (cmap_ichar m = cmap_ichar_init(), cmap_ichar_del(&m))
+ c_autovar (cmap_ichar m = cmap_ichar_init(), cmap_ichar_drop(&m))
{
cmap_ichar_emplace(&m, 5, 'a');
cmap_ichar_emplace(&m, 8, 'b');
@@ -85,13 +85,13 @@ typedef i_valraw _cx_memb(_rawmapped);
typedef _i_SET_ONLY( i_keyraw )
_i_MAP_ONLY( struct { i_keyraw first;
i_valraw second; } )
-_cx_rawvalue;
+_cx_raw;
STC_API _cx_self _cx_memb(_with_capacity)(size_t cap);
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self map);
#endif
-STC_API void _cx_memb(_del)(_cx_self* self);
+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 capacity);
STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr);
@@ -111,7 +111,7 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey)
{ return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; }
#ifndef _i_isset
- #if !c_option(c_no_clone)
+ #if !c_option(c_no_clone) && !defined _i_no_raw
STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped);
#endif
STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped);
@@ -132,15 +132,16 @@ STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey)
#if !c_option(c_no_clone)
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->table == other.table) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
-STC_INLINE void
-_cx_memb(_value_clone)(_cx_value* _dst, _cx_value* _val) {
- *_i_keyref(_dst) = i_keyfrom(i_keyto(_i_keyref(_val)));
- _i_MAP_ONLY( _dst->second = i_valfrom(i_valto(&_val->second)); )
+STC_INLINE _cx_value
+_cx_memb(_value_clone)(_cx_value _val) {
+ *_i_keyref(&_val) = i_keyfrom(i_keyto(_i_keyref(&_val)));
+ _i_MAP_ONLY( _val.second = i_valfrom(i_valto(&_val.second)); )
+ return _val;
}
-
+#if !defined _i_no_raw
STC_INLINE _cx_result
_cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) {
_cx_result _res = _cx_memb(_insert_entry_)(self, rkey);
@@ -150,25 +151,26 @@ _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)
}
return _res;
}
+#endif
#endif // !c_no_clone
-STC_INLINE _cx_rawvalue
+STC_INLINE _cx_raw
_cx_memb(_value_toraw)(_cx_value* val) {
return _i_SET_ONLY( i_keyto(val) )
- _i_MAP_ONLY( c_make(_cx_rawvalue){i_keyto(&val->first), i_valto(&val->second)} );
+ _i_MAP_ONLY( c_make(_cx_raw){i_keyto(&val->first), i_valto(&val->second)} );
}
STC_INLINE void
-_cx_memb(_value_del)(_cx_value* _val) {
- i_keydel(_i_keyref(_val));
- _i_MAP_ONLY( i_valdel(&_val->second); )
+_cx_memb(_value_drop)(_cx_value* _val) {
+ i_keydrop(_i_keyref(_val));
+ _i_MAP_ONLY( i_valdrop(&_val->second); )
}
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_keydel(&_key); _i_MAP_ONLY( i_valdel(&_mapped); )}
+ else { i_keydrop(&_key); _i_MAP_ONLY( i_valdrop(&_mapped); )}
return _res;
}
@@ -249,10 +251,10 @@ STC_INLINE void _cx_memb(_wipe_)(_cx_self* self) {
if (self->size == 0) return;
_cx_value* e = self->table, *end = e + self->bucket_count;
uint8_t *hx = self->_hashx;
- for (; e != end; ++e) if (*hx++) _cx_memb(_value_del)(e);
+ for (; e != end; ++e) if (*hx++) _cx_memb(_value_drop)(e);
}
-STC_DEF void _cx_memb(_del)(_cx_self* self) {
+STC_DEF void _cx_memb(_drop)(_cx_self* self) {
_cx_memb(_wipe_)(self);
c_free(self->_hashx);
c_free((void *) self->table);
@@ -269,15 +271,15 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) {
_cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) {
_cx_result _res = _cx_memb(_insert_entry_)(self, i_keyto(&_key));
if (_res.inserted) _res.ref->first = _key;
- else { i_keydel(&_key); i_valdel(&_res.ref->second); }
+ else { i_keydrop(&_key); i_valdrop(&_res.ref->second); }
_res.ref->second = _mapped; return _res;
}
- #if !c_option(c_no_clone)
+ #if !c_option(c_no_clone) && !defined _i_no_raw
STC_DEF _cx_result
_cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) {
_cx_result _res = _cx_memb(_insert_entry_)(self, rkey);
if (_res.inserted) _res.ref->first = i_keyfrom(rkey);
- else i_valdel(&_res.ref->second);
+ else i_valdrop(&_res.ref->second);
_res.ref->second = i_valfrom(rmapped); return _res;
}
#endif
@@ -292,7 +294,7 @@ _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) {
while ((_hx = _hashx[b.idx])) {
if (_hx == b.hx) {
_cx_rawkey _raw = i_keyto(_i_keyref(self->table + b.idx));
- if (i_equ(&_raw, rkeyptr)) break;
+ if (i_eq(&_raw, rkeyptr)) break;
}
if (++b.idx == _cap) b.idx = 0;
}
@@ -323,7 +325,7 @@ _cx_memb(_clone)(_cx_self m) {
};
_cx_value *e = m.table, *end = e + m.bucket_count, *dst = clone.table;
for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst)
- if (*hx) _cx_memb(_value_clone)(dst, e);
+ if (*hx) *dst = _cx_memb(_value_clone)(*e);
return clone;
}
#endif
@@ -363,7 +365,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) {
const _cx_size _cap = self->bucket_count;
_cx_value* _slot = self->table;
uint8_t* _hashx = self->_hashx;
- _cx_memb(_value_del)(&_slot[i]);
+ _cx_memb(_value_drop)(&_slot[i]);
for (;;) { /* delete without leaving tombstone */
if (++j == _cap) j = 0;
if (! _hashx[j])
diff --git a/include/stc/cpque.h b/include/stc/cpque.h
index 49aa2406..bf519125 100644
--- a/include/stc/cpque.h
+++ b/include/stc/cpque.h
@@ -36,7 +36,7 @@
#if !c_option(c_is_fwd)
_cx_deftypes(_c_cpque_types, _cx_self, i_val);
#endif
-typedef i_valraw _cx_rawvalue;
+typedef i_valraw _cx_raw;
STC_API void _cx_memb(_make_heap)(_cx_self* self);
STC_API void _cx_memb(_erase_at)(_cx_self* self, size_t idx);
@@ -55,7 +55,7 @@ STC_INLINE bool
_cx_memb(_resize)(_cx_self* self, const size_t len, i_val null) {
if (!_cx_memb(_reserve)(self, len)) return false;
const size_t n = self->size;
- for (size_t i = len; i < n; ++i) i_valdel(&self->data[i]);
+ for (size_t i = len; i < n; ++i) { i_valdrop(&self->data[i]); }
for (size_t i = n; i < len; ++i) self->data[i] = null;
self->size = len;
return true;
@@ -71,10 +71,10 @@ STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) {
STC_INLINE void _cx_memb(_clear)(_cx_self* self) {
size_t i = self->size; self->size = 0;
- while (i--) i_valdel(&self->data[i]);
+ while (i--) { i_valdrop(&self->data[i]); }
}
-STC_INLINE void _cx_memb(_del)(_cx_self* self)
+STC_INLINE void _cx_memb(_drop)(_cx_self* self)
{ _cx_memb(_clear)(self); c_free(self->data); }
STC_INLINE size_t _cx_memb(_size)(_cx_self q)
@@ -97,14 +97,14 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self q);
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
-
-STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_value val)
- { _cx_memb(_push)(self, i_valfrom(val)); }
-
STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val)
{ return i_valfrom(val); }
+#if !defined _i_no_raw
+STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_value val)
+ { _cx_memb(_push)(self, i_valfrom(val)); }
+#endif
#endif
/* -------------------------- IMPLEMENTATION ------------------------- */
@@ -139,7 +139,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) {
STC_DEF void
_cx_memb(_erase_at)(_cx_self* self, size_t idx) {
- i_valdel(&self->data[idx]);
+ i_valdrop(&self->data[idx]);
size_t n = --self->size;
self->data[idx] = self->data[n];
_cx_memb(_sift_down_)(self->data - 1, idx + 1, n);
diff --git a/include/stc/cset.h b/include/stc/cset.h
index d74a8ccc..91e703da 100644
--- a/include/stc/cset.h
+++ b/include/stc/cset.h
@@ -35,7 +35,7 @@ int main(void) {
c_foreach (i, cset_sx, s)
printf("set %d\n", *i.ref);
- cset_sx_del(&s);
+ cset_sx_drop(&s);
}
*/
diff --git a/include/stc/csmap.h b/include/stc/csmap.h
index 101e7399..fb8c868e 100644
--- a/include/stc/csmap.h
+++ b/include/stc/csmap.h
@@ -32,7 +32,7 @@
#include <stc/csmap.h>
int main(void) {
- c_autovar (csmap_sx m = csmap_sx_init(), csmap_sx_del(&m))
+ c_autovar (csmap_sx m = csmap_sx_init(), csmap_sx_drop(&m))
{
csmap_sx_emplace(&m, "Testing one", 1.234);
csmap_sx_emplace(&m, "Testing two", 12.34);
@@ -91,13 +91,13 @@ typedef i_keyraw _cx_rawkey;
typedef i_valraw _cx_memb(_rawmapped);
typedef _i_SET_ONLY( i_keyraw )
_i_MAP_ONLY( struct { i_keyraw first; i_valraw second; } )
- _cx_rawvalue;
+ _cx_raw;
STC_API _cx_self _cx_memb(_init)(void);
#if !c_option(c_no_clone)
STC_API _cx_self _cx_memb(_clone)(_cx_self tree);
#endif
-STC_API void _cx_memb(_del)(_cx_self* self);
+STC_API void _cx_memb(_drop)(_cx_self* self);
STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap);
STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, i_keyraw rkey, _cx_iter* out);
STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, i_keyraw rkey);
@@ -112,7 +112,7 @@ STC_API void _cx_memb(_next)(_cx_iter* it);
STC_INLINE bool _cx_memb(_empty)(_cx_self tree) { return _csmap_rep(&tree)->size == 0; }
STC_INLINE size_t _cx_memb(_size)(_cx_self tree) { return _csmap_rep(&tree)->size; }
STC_INLINE size_t _cx_memb(_capacity)(_cx_self tree) { return _csmap_rep(&tree)->cap; }
-STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_del)(self); *self = _cx_memb(_init)(); }
+STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); *self = _cx_memb(_init)(); }
STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_self, *a, *b); }
STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, i_keyraw rkey)
{ _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it) != NULL; }
@@ -128,20 +128,26 @@ _cx_memb(_with_capacity)(const size_t cap) {
return tree;
}
-STC_INLINE _cx_rawvalue
+STC_INLINE _cx_raw
_cx_memb(_value_toraw)(_cx_value* val) {
return _i_SET_ONLY( i_keyto(val) )
- _i_MAP_ONLY( c_make(_cx_rawvalue){i_keyto(&val->first), i_valto(&val->second)} );
+ _i_MAP_ONLY( c_make(_cx_raw){i_keyto(&val->first), i_valto(&val->second)} );
+}
+
+STC_INLINE int
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
+ _cx_rawkey rx = i_keyto(_i_keyref(x)), ry = i_keyto(_i_keyref(y));
+ return i_cmp(&rx, &ry);
}
STC_INLINE void
-_cx_memb(_value_del)(_cx_value* val) {
- i_keydel(_i_keyref(val));
- _i_MAP_ONLY( i_valdel(&val->second); )
+_cx_memb(_value_drop)(_cx_value* val) {
+ i_keydrop(_i_keyref(val));
+ _i_MAP_ONLY( i_valdrop(&val->second); )
}
#ifndef _i_isset
- #if !c_option(c_no_clone)
+ #if !c_option(c_no_clone) && !defined _i_no_raw
STC_API _cx_result _cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped);
#endif
STC_API _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped);
@@ -166,15 +172,16 @@ _cx_memb(_find)(const _cx_self* self, i_keyraw rkey) {
STC_INLINE void
_cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->nodes == other.nodes) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
-STC_INLINE void
-_cx_memb(_value_clone)(_cx_value* dst, _cx_value* val) {
- *_i_keyref(dst) = i_keyfrom(i_keyto(_i_keyref(val)));
- _i_MAP_ONLY( dst->second = i_valfrom(i_valto(&val->second)); )
+STC_INLINE _cx_value
+_cx_memb(_value_clone)(_cx_value _val) {
+ *_i_keyref(&_val) = i_keyfrom(i_keyto(_i_keyref(&_val)));
+ _i_MAP_ONLY( _val.second = i_valfrom(i_valto(&_val.second)); )
+ return _val;
}
-
+#if !defined _i_no_raw
STC_INLINE _cx_result
_cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)) {
_cx_result res = _cx_memb(_insert_entry_)(self, rkey);
@@ -184,13 +191,14 @@ _cx_memb(_emplace)(_cx_self* self, i_keyraw rkey _i_MAP_ONLY(, i_valraw rmapped)
}
return res;
}
+#endif
#endif // !c_no_clone
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_keydel(&key); _i_MAP_ONLY( i_valdel(&mapped); )}
+ else { i_keydrop(&key); _i_MAP_ONLY( i_valdrop(&mapped); )}
return res;
}
@@ -279,15 +287,15 @@ _cx_memb(_node_new_)(_cx_self* self, int level) {
_cx_memb(_insert_or_assign)(_cx_self* self, i_key key, i_val mapped) {
_cx_result res = _cx_memb(_insert_entry_)(self, i_keyto(&key));
if (res.inserted) res.ref->first = key;
- else { i_keydel(&key); i_valdel(&res.ref->second); }
+ else { i_keydrop(&key); i_valdrop(&res.ref->second); }
res.ref->second = mapped; return res;
}
- #if !c_option(c_no_clone)
+ #if !c_option(c_no_clone) && !defined _i_no_raw
STC_DEF _cx_result
_cx_memb(_emplace_or_assign)(_cx_self* self, i_keyraw rkey, i_valraw rmapped) {
_cx_result res = _cx_memb(_insert_entry_)(self, rkey);
if (res.inserted) res.ref->first = i_keyfrom(rkey);
- else i_valdel(&res.ref->second);
+ else i_valdrop(&res.ref->second);
res.ref->second = i_valfrom(rmapped); return res;
}
#endif
@@ -404,7 +412,7 @@ _cx_memb(_erase_r_)(_cx_node *d, _cx_size tn, const _cx_rawkey* rkey, int *erase
d[tn].link[c < 0] = _cx_memb(_erase_r_)(d, d[tn].link[c < 0], rkey, erased);
else {
if (!(*erased)++)
- _cx_memb(_value_del)(&d[tn].value);
+ _cx_memb(_value_drop)(&d[tn].value);
if (d[tn].link[0] && d[tn].link[1]) {
tx = d[tn].link[0];
while (d[tx].link[1])
@@ -470,7 +478,7 @@ STC_DEF _cx_size
_cx_memb(_clone_r_)(_cx_self* self, _cx_node* src, _cx_size sn) {
if (sn == 0) return 0;
_cx_size tx, tn = _cx_memb(_node_new_)(self, src[sn].level);
- _cx_memb(_value_clone)(&self->nodes[tn].value, &src[sn].value);
+ 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;
return tn;
@@ -491,12 +499,12 @@ _cx_memb(_del_r_)(_cx_node* d, _cx_size tn) {
if (tn) {
_cx_memb(_del_r_)(d, d[tn].link[0]);
_cx_memb(_del_r_)(d, d[tn].link[1]);
- _cx_memb(_value_del)(&d[tn].value);
+ _cx_memb(_value_drop)(&d[tn].value);
}
}
STC_DEF void
-_cx_memb(_del)(_cx_self* self) {
+_cx_memb(_drop)(_cx_self* self) {
if (_csmap_rep(self)->root) {
_cx_memb(_del_r_)(self->nodes, (_cx_size) _csmap_rep(self)->root);
c_free(_csmap_rep(self));
diff --git a/include/stc/csset.h b/include/stc/csset.h
index e4558192..07cf4b17 100644
--- a/include/stc/csset.h
+++ b/include/stc/csset.h
@@ -38,7 +38,7 @@ int main(void) {
c_foreach (k, csset_i, s)
printf("set %d\n", *k.ref);
- csset_i_del(&s);
+ csset_i_drop(&s);
}
*/
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index c1170f79..1ceec0cb 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -36,7 +36,7 @@
#if !c_option(c_is_fwd)
_cx_deftypes(_c_cstack_types, _cx_self, i_val);
#endif
-typedef i_valraw _cx_rawvalue;
+typedef i_valraw _cx_raw;
STC_INLINE _cx_self _cx_memb(_init)(void)
{ return c_make(_cx_self){0, 0, 0}; }
@@ -54,11 +54,11 @@ STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_val fill) {
STC_INLINE void _cx_memb(_clear)(_cx_self* self) {
_cx_value *p = self->data + self->size;
- while (p-- != self->data) i_valdel(p);
+ while (p-- != self->data) { i_valdrop(p); }
self->size = 0;
}
-STC_INLINE void _cx_memb(_del)(_cx_self* self)
+STC_INLINE void _cx_memb(_drop)(_cx_self* self)
{ _cx_memb(_clear)(self); c_free(self->data); }
STC_INLINE size_t _cx_memb(_size)(_cx_self v)
@@ -74,7 +74,7 @@ STC_INLINE _cx_value* _cx_memb(_top)(const _cx_self* self)
{ return &self->data[self->size - 1]; }
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
- { _cx_value* p = &self->data[--self->size]; i_valdel(p); }
+ { _cx_value* p = &self->data[--self->size]; i_valdrop(p); }
STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) {
if (n < self->size) return true;
@@ -91,14 +91,14 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) {
*vp = val; return vp;
}
-STC_INLINE _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx)
+STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx)
{ assert(idx < self->size); return self->data + idx; }
#if !c_option(c_no_clone)
-
-STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_rawvalue raw)
+#if !defined _i_no_raw
+STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw)
{ return _cx_memb(_push)(self, i_valfrom(raw)); }
-
+#endif
STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) {
_cx_self out = {(_cx_value *) c_malloc(v.size*sizeof(_cx_value)), v.size, v.size};
for (size_t i = 0; i < v.size; ++i, ++v.data) out.data[i] = i_valfrom(i_valto(v.data));
@@ -107,7 +107,7 @@ STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) {
STC_INLINE void _cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val)
diff --git a/include/stc/cstr.h b/include/stc/cstr.h
index a46de3c3..cfb2dfef 100644
--- a/include/stc/cstr.h
+++ b/include/stc/cstr.h
@@ -68,6 +68,7 @@ STC_API int c_strncasecmp(const char* s1, const char* s2, size_t nma
STC_INLINE cstr cstr_init() { return cstr_null; }
#define cstr_str(self) (self)->str
+#define cstr_toraw(self) (self)->str
#define cstr_new(literal) \
cstr_from_n(literal, sizeof c_make(c_strlit){literal} - 1)
STC_INLINE cstr cstr_from(const char* str)
@@ -77,7 +78,7 @@ STC_INLINE size_t cstr_size(cstr s) { return _cstr_rep(&s)->size; }
STC_INLINE size_t cstr_length(cstr s) { return _cstr_rep(&s)->size; }
STC_INLINE size_t cstr_capacity(cstr s) { return _cstr_rep(&s)->cap; }
STC_INLINE bool cstr_empty(cstr s) { return _cstr_rep(&s)->size == 0; }
-STC_INLINE void cstr_del(cstr* self)
+STC_INLINE void cstr_drop(cstr* self)
{ if (_cstr_rep(self)->cap) c_free(_cstr_rep(self)); }
STC_INLINE cstr cstr_clone(cstr s)
{ return cstr_from_n(s.str, _cstr_rep(&s)->size); }
@@ -166,9 +167,9 @@ cstr_ends_with(cstr s, const char* sub) {
}
/* container adaptor functions: */
-#define cstr_compare(xp, yp) strcmp((xp)->str, (yp)->str)
-#define cstr_equalto(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
-#define cstr_hash(xp, dummy) c_strhash((xp)->str)
+#define cstr_cmp(xp, yp) strcmp((xp)->str, (yp)->str)
+#define cstr_eq(xp, yp) (!cstr_cmp(xp, yp))
+#define cstr_hash(xp, dummy) c_strhash((xp)->str)
/* -------------------------- IMPLEMENTATION ------------------------- */
@@ -249,7 +250,7 @@ cstr_printf(cstr* self, const char* fmt, ...) {
va_start(args, fmt);
int n = cstr_vfmt(&ret, fmt, args);
va_end(args);
- cstr_del(self);
+ cstr_drop(self);
*self = ret;
return n;
}
diff --git a/include/stc/csview.h b/include/stc/csview.h
index 872b6e1e..c4844bfb 100644
--- a/include/stc/csview.h
+++ b/include/stc/csview.h
@@ -38,8 +38,7 @@ typedef char csview_value;
STC_API csview csview_substr(csview sv, intptr_t pos, size_t n);
STC_API csview csview_slice(csview sv, intptr_t p1, intptr_t p2);
-STC_API csview csview_first_token(csview sv, csview sep);
-STC_API csview csview_next_token(csview sv, csview sep, csview tok);
+STC_API csview csview_token(csview sv, csview sep, size_t* start);
#define csview_new(literal) \
c_make(csview){literal, sizeof c_make(c_strlit){literal} - 1}
@@ -114,9 +113,13 @@ STC_INLINE bool cstr_ends_with_v(cstr s, csview sub)
/* ---- Container helper functions ---- */
-#define csview_compare(xp, yp) strcmp((xp)->str, (yp)->str)
+STC_INLINE int csview_cmp(const csview* x, const csview* y) {
+ const size_t m = x->size < y->size ? x->size : y->size;
+ const int c = memcmp(x->str, y->str, m);
+ return c ? c : x->size - y->size;
+ }
#define csview_hash(xp, dummy) c_strhash((xp)->str)
-#define csview_equalto(xp, yp) (strcmp((xp)->str, (yp)->str) == 0)
+#define csview_eq(xp, yp) (!csview_cmp(xp, yp))
/* -------------------------- IMPLEMENTATION ------------------------- */
@@ -137,19 +140,11 @@ csview_slice(csview sv, intptr_t p1, intptr_t p2) {
}
STC_DEF csview
-csview_first_token(csview sv, csview sep) {
- const char* res = c_strnstrn(sv.str, sep.str, sv.size, sep.size);
- return c_make(csview){sv.str, (res ? res - sv.str : sv.size)};
-}
-
-STC_DEF csview
-csview_next_token(csview sv, csview sep, csview tok) {
- if (&tok.str[tok.size] == &sv.str[sv.size])
- return c_make(csview){&sv.str[sv.size], 0};
- tok.str += tok.size + sep.size;
- size_t n = sv.size - (tok.str - sv.str);
- const char* res = c_strnstrn(tok.str, sep.str, n, sep.size);
- tok.size = res ? res - tok.str : n;
+csview_token(csview sv, csview sep, size_t* start) {
+ csview slice = {sv.str + *start, sv.size - *start};
+ const char* res = c_strnstrn(slice.str, sep.str, slice.size, sep.size);
+ csview tok = {slice.str, res ? res - slice.str : (sep.size = 0, slice.size)};
+ *start += tok.size + sep.size;
return tok;
}
diff --git a/include/stc/cvec.h b/include/stc/cvec.h
index f64f7840..1faa16c3 100644
--- a/include/stc/cvec.h
+++ b/include/stc/cvec.h
@@ -46,15 +46,15 @@ struct MyStruct {
int main() {
cvec_i32 vec = cvec_i32_init();
cvec_i32_push_back(&vec, 123);
- cvec_i32_del(&vec);
+ cvec_i32_drop(&vec);
cvec_float fvec = cvec_float_init();
cvec_float_push_back(&fvec, 123.3);
- cvec_float_del(&fvec);
+ cvec_float_drop(&fvec);
cvec_str svec = cvec_str_init();
cvec_str_emplace_back(&svec, "Hello, friend");
- cvec_str_del(&svec);
+ cvec_str_drop(&svec);
}
*/
@@ -76,10 +76,10 @@ struct cvec_rep { size_t size, cap; void* data[]; };
#if !c_option(c_is_fwd)
_cx_deftypes(_c_cvec_types, _cx_self, i_val);
#endif
-typedef i_valraw _cx_rawvalue;
+typedef i_valraw _cx_raw;
STC_API _cx_self _cx_memb(_init)(void);
-STC_API void _cx_memb(_del)(_cx_self* self);
+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);
@@ -87,8 +87,8 @@ STC_API _cx_value* _cx_memb(_push_back)(_cx_self* self, i_val 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_compare)
-STC_API int _cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y);
+#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);
#endif
@@ -97,24 +97,23 @@ STC_API _cx_iter _cx_memb(_bsearch_in)(_cx_iter it1, _cx_iter it2, i_valr
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_API _cx_iter _cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
- const _cx_rawvalue* p1, const _cx_rawvalue* p2);
STC_INLINE i_val _cx_memb(_value_clone)(_cx_value val)
{ return i_valfrom(i_valto(&val)); }
STC_INLINE i_val _cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom(raw); }
-STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, i_valraw raw)
- { return _cx_memb(_push_back)(self, i_valfrom(raw)); }
STC_INLINE void
_cx_memb(_copy)(_cx_self *self, _cx_self other) {
if (self->data == other.data) return;
- _cx_memb(_del)(self); *self = _cx_memb(_clone)(other);
-}
-STC_INLINE _cx_iter
-_cx_memb(_emplace)(_cx_self* self, const size_t idx, i_valraw raw) {
- return _cx_memb(_emplace_range_p)(self, self->data + idx, &raw, &raw + 1);
+ _cx_memb(_drop)(self); *self = _cx_memb(_clone)(other);
}
+#if !defined _i_no_raw
+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_back)(_cx_self* self, i_valraw raw)
+ { return _cx_memb(_push_back)(self, i_valfrom(raw)); }
+STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, i_valraw raw)
+ { return _cx_memb(_push_back)(self, i_valfrom(raw)); }
STC_INLINE _cx_iter
-_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_rawvalue arr[], const size_t n) {
+_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
@@ -126,6 +125,7 @@ _cx_memb(_emplace_range)(_cx_self* self, _cx_iter it, _cx_iter it1, _cx_iter it2
return _cx_memb(_clone_range_p)(self, it.ref, it1.ref, it2.ref);
}
#endif
+#endif
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; }
@@ -135,8 +135,11 @@ STC_INLINE void _cx_memb(_swap)(_cx_self* a, _cx_self* b) { c_swap(_cx_s
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 _cx_value* _cx_memb(_push)(_cx_self* self, i_val value)
+ { return _cx_memb(_push_back)(self, value); }
STC_INLINE void _cx_memb(_pop_back)(_cx_self* self)
- { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdel(p); }
+ { _cx_value* p = &self->data[--cvec_rep_(self)->size]; i_valdrop(p); }
+STC_INLINE void _cx_memb(_pop)(_cx_self* self) { _cx_memb(_pop_back)(self); }
STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self)
{ return c_make(_cx_iter){self->data}; }
STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
@@ -197,7 +200,7 @@ _cx_memb(_at)(const _cx_self* self, const size_t idx) {
return self->data + idx;
}
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_INLINE _cx_iter
_cx_memb(_find)(const _cx_self* self, i_valraw raw) {
@@ -226,9 +229,9 @@ _cx_memb(_sort_range)(_cx_iter i1, _cx_iter i2,
}
STC_INLINE void
_cx_memb(_sort)(_cx_self* self) {
- _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_compare));
+ _cx_memb(_sort_range)(_cx_memb(_begin)(self), _cx_memb(_end)(self), _cx_memb(_value_cmp));
}
-#endif // !c_no_compare
+#endif // !c_no_cmp
/* -------------------------- IMPLEMENTATION ------------------------- */
#if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp)
@@ -246,14 +249,15 @@ STC_DEF void
_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; ++p)
- i_valdel(p);
+ for (_cx_value *p = self->data, *q = p + rep->size; p != q; ) {
+ --q; i_valdrop(q);
+ }
rep->size = 0;
}
}
STC_DEF void
-_cx_memb(_del)(_cx_self* self) {
+_cx_memb(_drop)(_cx_self* self) {
_cx_memb(_clear)(self);
if (cvec_rep_(self)->cap)
c_free(cvec_rep_(self));
@@ -263,7 +267,7 @@ STC_DEF bool
_cx_memb(_reserve)(_cx_self* self, const size_t cap) {
struct cvec_rep* rep = cvec_rep_(self);
const size_t len = rep->size;
- if (cap > rep->cap || cap && cap == len) {
+ 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));
if (!rep) return false;
@@ -279,7 +283,7 @@ _cx_memb(_resize)(_cx_self* self, const size_t len, i_val 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_valdel(&self->data[i]);
+ for (size_t i = len; i < n; ++i) { i_valdrop(&self->data[i]); }
for (size_t i = n; i < len; ++i) self->data[i] = null;
if (rep->cap) rep->size = len;
return true;
@@ -320,7 +324,7 @@ _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_valdel(p);
+ for (; p != p2; ++p) { i_valdrop(p); }
memmove(p1, p2, (end - p2) * sizeof(i_val));
cvec_rep_(self)->size -= len;
}
@@ -345,18 +349,19 @@ _cx_memb(_clone_range_p)(_cx_self* self, _cx_value* pos,
for (; p1 != p2; ++p1) *pos++ = i_valfrom(i_valto(p1));
return it;
}
-
+#if !defined _i_no_raw
STC_DEF _cx_iter
_cx_memb(_emplace_range_p)(_cx_self* self, _cx_value* pos,
- const _cx_rawvalue* p1, const _cx_rawvalue* p2) {
+ 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);
return it;
}
#endif
+#endif
-#if !c_option(c_no_compare)
+#if !c_option(c_no_cmp)
STC_DEF _cx_iter
_cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) {
@@ -381,12 +386,12 @@ _cx_memb(_bsearch_in)(_cx_iter i1, _cx_iter i2, i_valraw raw) {
}
STC_DEF int
-_cx_memb(_value_compare)(const _cx_value* x, const _cx_value* y) {
+_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) {
i_valraw rx = i_valto(x);
i_valraw ry = i_valto(y);
return i_cmp(&rx, &ry);
}
-#endif // !c_no_compare
+#endif // !c_no_cmp
#endif
#include "template.h"
#define CVEC_H_INCLUDED
diff --git a/include/stc/forward.h b/include/stc/forward.h
index bf2ba698..9a37297e 100644
--- a/include/stc/forward.h
+++ b/include/stc/forward.h
@@ -34,7 +34,7 @@
#define forward_cset(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, c_false, c_true)
#define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, c_false, c_true)
#define forward_cbox(CX, VAL) _c_cbox_types(CX, VAL)
-#define forward_csptr(CX, VAL) _c_csptr_types(CX, VAL)
+#define forward_carc(CX, VAL) _c_carc_types(CX, VAL)
#define forward_cpque(CX, VAL) _c_cpque_types(CX, VAL)
#define forward_cstack(CX, VAL) _c_cstack_types(CX, VAL)
#define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL)
@@ -132,7 +132,7 @@
SELF##_value* get; \
} SELF
-#define _c_csptr_types(SELF, VAL) \
+#define _c_carc_types(SELF, VAL) \
typedef VAL SELF##_value; \
\
typedef struct { \
diff --git a/include/stc/template.h b/include/stc/template.h
index 85c4a940..7d5c70b9 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -31,7 +31,7 @@
#define _cx_value _cx_memb(_value)
#define _cx_key _cx_memb(_key)
#define _cx_mapped _cx_memb(_mapped)
- #define _cx_rawvalue _cx_memb(_rawvalue)
+ #define _cx_raw _cx_memb(_raw)
#define _cx_rawkey _cx_memb(_rawkey)
#define _cx_rawmapped _cx_memb(_rawmapped)
#define _cx_iter _cx_memb(_iter)
@@ -40,23 +40,9 @@
#define _cx_size _cx_memb(_size_t)
#endif
-#if defined i_valraw && !(defined i_valto && defined i_valfrom)
- #error "if i_valraw or i_valto defined, i_valfrom must be defined"
-#endif
-#if defined i_keyraw && !(defined i_keyto && defined i_keyfrom)
- #error "if i_keyraw or i_keyto defined, i_keyfrom a must be defined"
-#endif
-#ifdef i_key_csptr // [deprecated]
- #define i_key_ref i_key_csptr
- #error "i_key_csptr no longer supported: use new name i_key_ref"
-#endif
-#ifdef i_val_csptr // [deprecated]
- #define i_val_ref i_val_csptr
- #error "i_val_csptr no longer supported: use new name i_val_ref"
-#endif
-#ifdef i_cnt // [deprecated]
+#if defined i_cnt || defined i_equ // [deprecated]
#define i_type i_cnt
- #error "i_cnt no longer supported: use new name i_type"
+ #error "i_cnt and i_equ no longer supported: use new name i_type / i_eq"
#endif
#ifdef i_type
@@ -69,46 +55,56 @@
#include "cstr.h"
#endif
-#if defined i_key_ref
- #define i_key i_key_ref
- #define i_keyfrom c_PASTE(i_key, _clone)
- #ifndef i_cmp
- #define i_cmp c_PASTE(i_key, _compare)
- #endif
- #ifndef i_hash
- #define i_hash c_PASTE(i_key, _hash)
- #endif
- #ifndef i_keydel
- #define i_keydel c_PASTE(i_key, _del)
- #endif
-
-#elif defined i_key_str
-
- #define i_key cstr
- #define i_keyfrom cstr_from
- #define i_keyto cstr_str
- #define i_keyraw const char*
+#ifdef i_key_str
+ #define i_key_bind cstr
+ #define i_keyraw c_rawstr
#ifndef i_tag
#define i_tag str
#endif
- #ifndef i_cmp
- #define i_cmp c_rawstr_compare
+#elif defined i_key_sptr
+ #define i_key_bind i_key_sptr
+ #define i_keyraw c_PASTE(i_key_sptr, _value)
+#endif
+
+#ifdef i_key_bind
+ #define i_key i_key_bind
+ #ifndef i_keyraw
+ #ifndef i_keyfrom
+ #define i_keyfrom c_PASTE(i_key, _clone)
+ #endif
+ #else
+ #ifndef i_keyfrom
+ #define i_keyfrom c_PASTE(i_key, _from)
+ #endif
+ #ifndef i_keyto
+ #define i_keyto c_PASTE(i_key, _toraw)
+ #endif
+ #endif
+ #ifndef i_cmp
+ #define i_cmp c_PASTE(i_keyraw, _cmp)
+ #endif
+ #ifndef i_eq
+ #define i_eq c_PASTE(i_keyraw, _eq)
#endif
#ifndef i_hash
- #define i_hash c_rawstr_hash
+ #define i_hash c_PASTE(i_keyraw, _hash)
#endif
- #if !defined i_keydel
- #define i_keydel cstr_del
+ #ifndef i_keydrop
+ #define i_keydrop c_PASTE(i_key, _drop)
#endif
#endif
-/* Resolve i_del and i_from here */
-#if defined i_del && defined i_isset
- #define i_keydel i_del
-#elif defined i_del && !defined i_key
- #define i_valdel i_del
-#elif defined i_del
- #error "i_del not supported for maps, define i_keydel / i_valdel instead."
+#if defined i_keyraw && !(defined i_keyto && defined i_keyfrom)
+ #error "if i_keyraw defined, i_keyfrom and i_keyto must be defined"
+#endif
+
+/* Resolve i_drop and i_from here */
+#if defined i_drop && defined i_isset
+ #define i_keydrop i_drop
+#elif defined i_drop && !defined i_key
+ #define i_valdrop i_drop
+#elif defined i_drop
+ #error "i_drop not supported for maps, define i_keydrop / i_valdrop instead."
#endif
#if defined i_from && defined i_isset
#define i_keyfrom i_from
@@ -118,33 +114,43 @@
#error "i_from not supported for maps, define i_keyfrom / i_valfrom instead."
#endif
-#if defined i_val_ref
- #define i_val i_val_ref
- #define i_valfrom c_PASTE(i_val, _clone)
- #if !defined i_cmp && !defined i_key
- #define i_cmp c_PASTE(i_val, _compare)
- #endif
- #if !defined i_valdel
- #define i_valdel c_PASTE(i_val, _del)
- #endif
-
-#elif defined i_val_str
-
- #define i_val cstr
- #define i_valfrom cstr_from
- #define i_valto cstr_str
- #define i_valraw const char*
+#ifdef i_val_str
+ #define i_val_bind cstr
+ #define i_valraw c_rawstr
#if !defined i_tag && !defined i_key
- #define i_tag str
+ #define i_tag str
+ #endif
+#elif defined i_val_sptr
+ #define i_val_bind i_val_sptr
+ #define i_valraw c_PASTE(i_val_sptr, _value)
+#endif
+
+#ifdef i_val_bind
+ #define i_val i_val_bind
+ #ifndef i_valraw
+ #ifndef i_valfrom
+ #define i_valfrom c_PASTE(i_val, _clone)
+ #endif
+ #else
+ #ifndef i_valfrom
+ #define i_valfrom c_PASTE(i_val, _from)
+ #endif
+ #ifndef i_valto
+ #define i_valto c_PASTE(i_val, _toraw)
+ #endif
#endif
#if !defined i_cmp && !defined i_key
- #define i_cmp c_rawstr_compare
+ #define i_cmp c_PASTE(i_valraw, _cmp)
#endif
- #if !defined i_valdel
- #define i_valdel cstr_del
+ #ifndef i_valdrop
+ #define i_valdrop c_PASTE(i_val, _drop)
#endif
#endif
+#if defined i_valraw && !(defined i_valto && defined i_valfrom)
+ #error "if i_valraw defined, i_valfrom and i_valto must be defined"
+#endif
+
#ifdef i_key
#ifdef _i_isset
#define i_val i_key
@@ -152,23 +158,24 @@
#ifndef i_tag
#define i_tag i_key
#endif
- #if !defined _i_has_internal_clone && defined i_keydel && !defined i_keyfrom && !c_option(c_no_clone)
- #error "i_keydel defined but not i_keyfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'"
+ #if !defined _i_has_internal_clone && defined i_keydrop && !defined i_keyfrom && !c_option(c_no_clone)
+ #error "i_keydrop defined but not i_keyfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'"
#endif
#if !defined i_keyfrom
#define i_keyfrom c_default_clone
#endif
#ifndef i_keyraw
+ #define _i_no_keyraw
#define i_keyraw i_key
#define i_keyto c_default_toraw
#endif
- #if !defined i_equ && defined i_cmp
- #define i_equ !i_cmp
- #elif !defined i_equ
- #define i_equ c_default_equalto
+ #if !defined i_eq && defined i_cmp
+ #define i_eq !i_cmp
+ #elif !defined i_eq
+ #define i_eq c_default_eq
#endif
- #ifndef i_keydel
- #define i_keydel c_default_del
+ #ifndef i_keydrop
+ #define i_keydrop c_default_drop
#endif
#elif defined _i_isset
#error "i_key define is missing."
@@ -177,22 +184,24 @@
#ifndef i_tag
#define i_tag i_val
#endif
-#if !defined _i_has_internal_clone && defined i_valdel && !defined i_valfrom && !c_option(c_no_clone)
- #error "i_valdel/i_del defined but not i_valfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'"
+#if !defined _i_has_internal_clone && defined i_valdrop && !defined i_valfrom && !c_option(c_no_clone)
+ #error "i_valdrop/i_drop defined but not i_valfrom (e.g. as c_default_clone), or no 'i_opt c_no_clone'"
#endif
#if !defined i_valfrom
#define i_valfrom c_default_clone
#endif
#ifndef i_valraw
+ #if !defined i_key || defined _i_no_keyraw
+ #define _i_no_raw
+ #endif
#define i_valraw i_val
#define i_valto c_default_toraw
#endif
-#ifndef i_valdel
- #define i_valdel c_default_del
+#ifndef i_valdrop
+ #define i_valdrop c_default_drop
#endif
#ifndef i_cmp
- #define _i_default_compare
- #define i_cmp c_default_compare
+ #define i_cmp c_default_cmp
#endif
#ifndef i_hash
#define i_hash c_default_hash
@@ -205,29 +214,32 @@
#undef i_imp
#undef i_opt
#undef i_cmp
-#undef i_del
-#undef i_equ
+#undef i_eq
#undef i_hash
#undef i_from
+#undef i_drop
#undef i_val
#undef i_val_str
-#undef i_val_ref
-#undef i_valdel
+#undef i_val_sptr
+#undef i_val_bind
#undef i_valraw
#undef i_valfrom
#undef i_valto
+#undef i_valdrop
#undef i_key
#undef i_key_str
-#undef i_key_ref
-#undef i_keydel
+#undef i_key_sptr
+#undef i_key_bind
#undef i_keyraw
#undef i_keyfrom
#undef i_keyto
+#undef i_keydrop
#undef _i_prefix
-#undef _i_default_compare
+#undef _i_no_raw
+#undef _i_no_keyraw
#undef _i_has_internal_clone
#undef _i_template
#endif