diff options
| -rw-r--r-- | benchmarks/others/clist_v1.h | 2 | ||||
| -rw-r--r-- | benchmarks/others/csmap_v1.h | 54 | ||||
| -rw-r--r-- | docs/ccommon_api.md | 2 | ||||
| -rw-r--r-- | examples/bits.c | 2 | ||||
| -rw-r--r-- | stc/cbits.h | 98 | ||||
| -rw-r--r-- | stc/cdeq.h | 2 | ||||
| -rw-r--r-- | stc/clist.h | 2 | ||||
| -rw-r--r-- | stc/cmap.h | 6 | ||||
| -rw-r--r-- | stc/cset.h | 2 | ||||
| -rw-r--r-- | stc/csmap.h | 6 | ||||
| -rw-r--r-- | stc/csset.h | 2 | ||||
| -rw-r--r-- | stc/cstr.h | 160 | ||||
| -rw-r--r-- | stc/cvec.h | 2 |
13 files changed, 187 insertions, 153 deletions
diff --git a/benchmarks/others/clist_v1.h b/benchmarks/others/clist_v1.h index c2fd7164..0887d5b3 100644 --- a/benchmarks/others/clist_v1.h +++ b/benchmarks/others/clist_v1.h @@ -69,7 +69,7 @@ _c_using_clist(clist_##X, Value, valueCompare, valueDel, valueFromRaw, valueToRaw, RawValue)
#define using_clist_str() \
- _c_using_clist(clist_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
+ _c_using_clist(clist_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
#define _c_using_clist_types(CX, Value) \
diff --git a/benchmarks/others/csmap_v1.h b/benchmarks/others/csmap_v1.h index 1164d509..1d65b516 100644 --- a/benchmarks/others/csmap_v1.h +++ b/benchmarks/others/csmap_v1.h @@ -75,9 +75,9 @@ int main(void) { /* csset_str, csmap_str, csmap_strkey, csmap_strval: */
#define using_csset_str() \
- _c_using_aatree_strkey(str, csset_, cstr_t, @@, @@)
+ _c_using_aatree_strkey(str, csset_, cstr, @@, @@)
#define using_csmap_str() \
- _c_using_aatree(csmap_str, csmap_, cstr_t, cstr_t, cstr_compare_raw, cstr_del, cstr_del, \
+ _c_using_aatree(csmap_str, csmap_, cstr, cstr, cstr_compare_raw, cstr_del, cstr_del, \
cstr_from, cstr_c_str, const char*, cstr_from, cstr_c_str, const char*)
@@ -88,7 +88,7 @@ int main(void) { #define using_csmap_strkey_4(X, Mapped, mappedDel, mappedClone) \
_c_using_aatree_strkey(X, csmap_, Mapped, mappedDel, mappedClone)
#define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedClone) \
- _c_using_aatree(C##X, C, cstr_t, Mapped, cstr_compare_raw, mappedDel, cstr_del, \
+ _c_using_aatree(C##X, C, cstr, Mapped, cstr_compare_raw, mappedDel, cstr_del, \
cstr_from, cstr_c_str, const char*, mappedClone, c_trivial_toraw, Mapped)
@@ -101,7 +101,7 @@ int main(void) { #define using_csmap_strval_5(X, Key, keyCompare, keyDel, keyClone) \
using_csmap_strval_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key)
#define using_csmap_strval_7(X, Key, keyCompare, keyDel, keyFromRaw, keyToRaw, RawKey) \
- _c_using_aatree(csmap_##X, csmap_, Key, cstr_t, keyCompare, cstr_del, keyDel, \
+ _c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompare, cstr_del, keyDel, \
keyFromRaw, keyToRaw, RawKey, cstr_from, cstr_c_str, const char*)
#define SET_ONLY_csset_(...) __VA_ARGS__
@@ -156,6 +156,9 @@ int main(void) { \
STC_API CX CX##_init(void); \
STC_API CX##_value_t* CX##_find_it(const CX* self, RawKey rkey, CX##_iter_t* out); \
+ STC_API CX##_iter_t CX##_lower_bound(const CX* self, RawKey rkey); \
+ STC_API CX##_iter_t CX##_erase_at(CX* self, CX##_iter_t it); \
+ STC_API CX##_iter_t CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2); \
STC_API CX##_node_t* CX##_erase_r_(CX##_node_t *tn, const CX##_rawkey_t* rkey, int *erased); \
STC_API void CX##_del_r_(CX##_node_t* tn); \
STC_API CX##_node_t* CX##_clone_r_(CX##_node_t *tn); \
@@ -167,7 +170,7 @@ int main(void) { STC_INLINE void CX##_del(CX* self) {CX##_del_r_(self->root);} \
STC_INLINE void CX##_clear(CX* self) {CX##_del(self); *self = CX##_init();} \
STC_INLINE void CX##_swap(CX* a, CX* b) {c_swap(CX, *a, *b);} \
- STC_INLINE CX CX##_clone(CX t) {CX c = {CX##_clone_r_(t.root), t.size}; return c;} \
+ STC_INLINE CX CX##_clone(CX m) {CX c = {CX##_clone_r_(m.root), m.size}; return c;} \
STC_INLINE CX##_iter_t CX##_find(const CX* self, RawKey rkey) \
{CX##_iter_t it; CX##_find_it(self, rkey, &it); return it;} \
STC_INLINE bool CX##_contains(const CX* self, RawKey rkey) \
@@ -271,11 +274,6 @@ int main(void) { self->size -= erased; return erased; \
} \
\
- STC_INLINE size_t \
- CX##_erase_at(CX* self, CX##_iter_t it) { \
- return CX##_erase(self, keyToRaw(KEY_REF_##C(it.ref))); \
- } \
-\
_c_implement_aatree(CX, C, Key, Mapped, keyCompareRaw, mappedDel, keyDel, \
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \
typedef CX CX##_t
@@ -304,6 +302,18 @@ int main(void) { return (out->ref = NULL); \
} \
\
+ STC_DEF CX##_iter_t \
+ CX##_lower_bound(const CX* self, RawKey rkey) { \
+ CX##_iter_t it; \
+ CX##_find_it(self, rkey, &it); \
+ if (!it.ref && it._top) { \
+ CX##_node_t *tn = it._st[--it._top]; \
+ it._tn = tn->link[1]; \
+ it.ref = &tn->value; \
+ } \
+ return it; \
+ } \
+\
STC_DEF void \
CX##_next(CX##_iter_t *it) { \
CX##_node_t *tn = it->_tn; \
@@ -374,6 +384,30 @@ int main(void) { return res; \
} \
\
+ STC_DEF CX##_iter_t \
+ CX##_erase_at(CX* self, CX##_iter_t it) { \
+ CX##_rawkey_t raw = keyToRaw(KEY_REF_##C(it.ref)), nxt; \
+ CX##_next(&it); \
+ if (it.ref) nxt = keyToRaw(KEY_REF_##C(it.ref)); \
+ CX##_erase(self, raw); \
+ if (it.ref) CX##_find_it(self, nxt, &it); \
+ return it; \
+ } \
+\
+ STC_DEF CX##_iter_t \
+ CX##_erase_range(CX* self, CX##_iter_t it1, CX##_iter_t it2) { \
+ if (!it2.ref) { while (it1.ref) it1 = CX##_erase_at(self, it1); \
+ return it1; } \
+ CX##_key_t k1 = *KEY_REF_##C(it1.ref), k2 = *KEY_REF_##C(it2.ref); \
+ CX##_rawkey_t r1 = keyToRaw(&k1); \
+ for (;;) { \
+ if (memcmp(&k1, &k2, sizeof k1) == 0) return it1; \
+ CX##_next(&it1); k1 = *KEY_REF_##C(it1.ref); \
+ CX##_erase(self, r1); \
+ CX##_find_it(self, (r1 = keyToRaw(&k1)), &it1); \
+ } \
+ } \
+\
STC_DEF CX##_node_t* \
CX##_erase_r_(CX##_node_t *tn, const CX##_rawkey_t* rkey, int *erased) { \
if (tn->level == 0) \
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index cabd2711..ec546dd6 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -71,7 +71,7 @@ cvec_str readFile(const char* name) { // Next line declares, opens, and closes the FILE* c_withfile (fp, fopen(name, "r")) { - cstr_t line = cstr_init(); + cstr line = cstr_init(); while (cstr_getline(&line, fp)) cvec_str_emplace_back(&vec, line.str); cstr_del(&line); diff --git a/examples/bits.c b/examples/bits.c index c8b037a2..e7b843fb 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -34,7 +34,7 @@ int main() { printf("%d", cbits_test(set, i));
puts("");
- cbits_t s2 = cbits_clone(set);
+ cbits s2 = cbits_clone(set);
cbits_flip_all(&s2);
cbits_set(&s2, 16);
cbits_set(&s2, 17);
diff --git a/stc/cbits.h b/stc/cbits.h index 0da46f11..4c708a7d 100644 --- a/stc/cbits.h +++ b/stc/cbits.h @@ -30,7 +30,7 @@ Similar to boost::dynamic_bitset / std::bitset #include "cbits.h"
int main() {
- cbits_t bset = cbits_with_size(23, true);
+ cbits bset = cbits_with_size(23, true);
cbits_reset(&bset, 9);
cbits_resize(&bset, 43, false);
@@ -55,89 +55,89 @@ int main() { #include <string.h>
#include "ccommon.h"
-typedef struct cbits {
+typedef struct {
uint64_t *at64;
size_t size;
-} cbits, cbits_t;
-
-STC_API cbits_t cbits_with_size(size_t size, bool value);
-STC_API cbits_t cbits_with_values(size_t size, uint64_t pattern);
-STC_API cbits_t cbits_from_str(const char* str);
-STC_API char* cbits_to_str(cbits_t set, char* str, size_t start, intptr_t stop);
-STC_API cbits_t cbits_clone(cbits_t other);
-STC_API void cbits_resize(cbits_t* self, size_t size, bool value);
-STC_API cbits_t* cbits_assign(cbits_t* self, cbits_t other);
-STC_API size_t cbits_count(cbits_t set);
-STC_API bool cbits_subset_of(cbits_t set, cbits_t other);
-STC_API bool cbits_disjoint(cbits_t set, cbits_t other);
-
-STC_INLINE cbits_t cbits_init() { cbits_t set = {NULL, 0}; return set; }
-STC_INLINE void cbits_clear(cbits_t* self) { self->size = 0; }
-STC_INLINE void cbits_del(cbits_t* self) { c_free(self->at64); }
-STC_INLINE size_t cbits_size(cbits_t set) { return set.size; }
-
-STC_INLINE cbits_t* cbits_take(cbits_t* self, cbits_t other) {
+} cbits;
+
+STC_API cbits cbits_with_size(size_t size, bool value);
+STC_API cbits cbits_with_values(size_t size, uint64_t pattern);
+STC_API cbits cbits_from_str(const char* str);
+STC_API char* cbits_to_str(cbits set, char* str, size_t start, intptr_t stop);
+STC_API cbits cbits_clone(cbits other);
+STC_API void cbits_resize(cbits* self, size_t size, bool value);
+STC_API cbits* cbits_assign(cbits* self, cbits other);
+STC_API size_t cbits_count(cbits set);
+STC_API bool cbits_subset_of(cbits set, cbits other);
+STC_API bool cbits_disjoint(cbits set, cbits other);
+
+STC_INLINE cbits cbits_init() { cbits set = {NULL, 0}; return set; }
+STC_INLINE void cbits_clear(cbits* self) { self->size = 0; }
+STC_INLINE void cbits_del(cbits* self) { c_free(self->at64); }
+STC_INLINE size_t cbits_size(cbits set) { return set.size; }
+
+STC_INLINE cbits* cbits_take(cbits* self, cbits other) {
if (self->at64 != other.at64) {cbits_del(self); *self = other;}
return self;
}
-STC_INLINE cbits_t cbits_move(cbits_t* self) {
- cbits_t tmp = *self; self->at64 = NULL, self->size = 0;
+STC_INLINE cbits cbits_move(cbits* self) {
+ cbits tmp = *self; self->at64 = NULL, self->size = 0;
return tmp;
}
-STC_INLINE bool cbits_test(cbits_t set, size_t i) {
+STC_INLINE bool cbits_test(cbits set, size_t i) {
return (set.at64[i >> 6] & (1ull << (i & 63))) != 0;
}
-STC_INLINE bool cbits_at(cbits_t set, size_t i) {
+STC_INLINE bool cbits_at(cbits set, size_t i) {
return (set.at64[i >> 6] & (1ull << (i & 63))) != 0;
}
-STC_INLINE void cbits_set(cbits_t *self, size_t i) {
+STC_INLINE void cbits_set(cbits *self, size_t i) {
self->at64[i >> 6] |= 1ull << (i & 63);
}
-STC_INLINE void cbits_reset(cbits_t *self, size_t i) {
+STC_INLINE void cbits_reset(cbits *self, size_t i) {
self->at64[i >> 6] &= ~(1ull << (i & 63));
}
-STC_INLINE void cbits_set_value(cbits_t *self, size_t i, bool value) {
+STC_INLINE void cbits_set_value(cbits *self, size_t i, bool value) {
self->at64[i >> 6] ^= (-(uint64_t)value ^ self->at64[i >> 6]) & 1ull << (i & 63);
}
-STC_INLINE void cbits_flip(cbits_t *self, size_t i) {
+STC_INLINE void cbits_flip(cbits *self, size_t i) {
self->at64[i >> 6] ^= 1ull << (i & 63);
}
-STC_INLINE void cbits_set_all(cbits_t *self, bool value) {
+STC_INLINE void cbits_set_all(cbits *self, bool value) {
memset(self->at64, -(int)value, ((self->size + 63) >> 6) * 8);
}
-STC_INLINE void cbits_set_values(cbits_t *self, uint64_t pattern) {
+STC_INLINE void cbits_set_values(cbits *self, uint64_t pattern) {
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->at64[i] = pattern;
}
-STC_INLINE void cbits_flip_all(cbits_t *self) {
+STC_INLINE void cbits_flip_all(cbits *self) {
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->at64[i] ^= ~0ull;
}
/* Intersection */
-STC_INLINE void cbits_intersect(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_intersect(cbits *self, cbits other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->at64[i] &= other.at64[i];
}
/* Union */
-STC_INLINE void cbits_union(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_union(cbits *self, cbits other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->at64[i] |= other.at64[i];
}
/* Exclusive disjunction */
-STC_INLINE void cbits_xor(cbits_t *self, cbits_t other) {
+STC_INLINE void cbits_xor(cbits *self, cbits other) {
assert(self->size == other.size);
size_t n = (self->size + 63) >> 6;
for (size_t i=0; i<n; ++i) self->at64[i] ^= other.at64[i];
@@ -159,14 +159,14 @@ STC_INLINE void cbits_xor(cbits_t *self, cbits_t other) { #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
-STC_DEF cbits_t* cbits_assign(cbits_t* self, cbits_t other) {
+STC_DEF cbits* cbits_assign(cbits* self, cbits other) {
if (self->at64 == other.at64) return self;
if (self->size != other.size) return cbits_take(self, cbits_clone(other));
memcpy(self->at64, other.at64, ((other.size + 63) >> 6)*8);
return self;
}
-STC_DEF void cbits_resize(cbits_t* self, size_t size, bool value) {
+STC_DEF void cbits_resize(cbits* self, size_t size, bool value) {
size_t new_n = (size + 63) >> 6, osize = self->size, old_n = (osize + 63) >> 6;
self->at64 = (uint64_t *) c_realloc(self->at64, new_n * 8);
self->size = size;
@@ -179,33 +179,33 @@ STC_DEF void cbits_resize(cbits_t* self, size_t size, bool value) { }
}
-STC_DEF cbits_t cbits_with_size(size_t size, bool value) {
- cbits_t set = {(uint64_t *) c_malloc(((size + 63) >> 6) * 8), size};
+STC_DEF cbits cbits_with_size(size_t size, bool value) {
+ cbits set = {(uint64_t *) c_malloc(((size + 63) >> 6) * 8), size};
cbits_set_all(&set, value);
return set;
}
-STC_DEF cbits_t cbits_with_values(size_t size, uint64_t pattern) {
- cbits_t set = {(uint64_t *) c_malloc(((size + 63) >> 6) * 8), size};
+STC_DEF cbits cbits_with_values(size_t size, uint64_t pattern) {
+ cbits set = {(uint64_t *) c_malloc(((size + 63) >> 6) * 8), size};
cbits_set_values(&set, pattern);
return set;
}
-STC_DEF cbits_t cbits_from_str(const char* str) {
+STC_DEF cbits cbits_from_str(const char* str) {
const char* p = str; while (*p) ++p;
- cbits_t set = cbits_with_size(p - str, false);
+ cbits set = cbits_with_size(p - str, false);
for (size_t i=0; i<set.size; ++i) if (str[i] == '1') cbits_set(&set, i);
return set;
}
-STC_DEF char* cbits_to_str(cbits_t set, char* out, size_t start, intptr_t stop) {
+STC_DEF char* cbits_to_str(cbits set, char* out, size_t start, intptr_t stop) {
size_t end = stop < 0 ? set.size : stop;
for (size_t i=start; i<end; ++i) out[i] = cbits_test(set, i) ? '1' : '0';
out[end] = '\0'; return out;
}
-STC_DEF cbits_t cbits_clone(cbits_t other) {
+STC_DEF cbits cbits_clone(cbits other) {
size_t bytes = ((other.size + 63) >> 6) * 8;
- cbits_t set = {(uint64_t *) memcpy(c_malloc(bytes), other.at64, bytes), other.size};
+ cbits set = {(uint64_t *) memcpy(c_malloc(bytes), other.at64, bytes), other.size};
return set;
}
-STC_DEF size_t cbits_count(cbits_t s) {
+STC_DEF size_t cbits_count(cbits s) {
size_t count = 0, n = s.size >> 6;
for (size_t i = 0; i < n; ++i) count += cpopcount64(s.at64[i]);
if (s.size & 63) count += cpopcount64(s.at64[n] & ((1ull << (s.size & 63)) - 1));
@@ -222,8 +222,8 @@ STC_DEF size_t cbits_count(cbits_t s) { uint64_t i = n, m = (1ull << (s.size & 63)) - 1; \
return ((s.at64[i] OPR other.at64[i]) & m) == (x & m)
-STC_DEF bool cbits_subset_of(cbits_t s, cbits_t other) { _cbits_SETOP(|, s.at64[i]); }
-STC_DEF bool cbits_disjoint(cbits_t s, cbits_t other) { _cbits_SETOP(&, 0); }
+STC_DEF bool cbits_subset_of(cbits s, cbits other) { _cbits_SETOP(|, s.at64[i]); }
+STC_DEF bool cbits_disjoint(cbits s, cbits other) { _cbits_SETOP(&, 0); }
#endif
#endif
@@ -41,7 +41,7 @@ _c_using_cdeq(cdeq_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue)
#define using_cdeq_str() \
- _c_using_cdeq(cdeq_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
+ _c_using_cdeq(cdeq_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
struct cdeq_rep { size_t size, cap; void* base[]; };
diff --git a/stc/clist.h b/stc/clist.h index dbdac503..d44636a8 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -69,7 +69,7 @@ _c_using_clist(clist_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue)
#define using_clist_str() \
- _c_using_clist(clist_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
+ _c_using_clist(clist_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
#define _c_using_clist_types(CX, Value) \
@@ -90,7 +90,7 @@ int main(void) { keyDel, keyFromRaw, keyToRaw, RawKey)
#define using_cmap_str() \
- _c_using_chash(cmap_str, cmap_, cstr_t, cstr_t, cstr_equals_raw, cstr_hash_raw, \
+ _c_using_chash(cmap_str, cmap_, cstr, cstr, cstr_equals_raw, cstr_hash_raw, \
cstr_del, cstr_from, cstr_c_str, const char*, \
cstr_del, cstr_from, cstr_c_str, const char*)
@@ -107,7 +107,7 @@ int main(void) { _c_using_chash_strkey(X, cmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped)
#define _c_using_chash_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \
- _c_using_chash(C##X, C, cstr_t, Mapped, cstr_equals_raw, cstr_hash_raw, \
+ _c_using_chash(C##X, C, cstr, Mapped, cstr_equals_raw, cstr_hash_raw, \
mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
cstr_del, cstr_from, cstr_c_str, const char*)
@@ -124,7 +124,7 @@ int main(void) { using_cmap_strval_8(X, Key, keyEquals, keyHash, keyDel, keyClone, c_trivial_toraw, Key)
#define using_cmap_strval_8(X, Key, keyEqualsRaw, keyHashRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \
- _c_using_chash(cmap_##X, cmap_, Key, cstr_t, keyEqualsRaw, keyHashRaw, \
+ _c_using_chash(cmap_##X, cmap_, Key, cstr, keyEqualsRaw, keyHashRaw, \
cstr_del, cstr_from, cstr_c_str, const char*, \
keyDel, keyFromRaw, keyToRaw, RawKey)
@@ -62,7 +62,7 @@ int main(void) { /* cset_str: */
#define using_cset_str() \
- _c_using_chash_strkey(str, cset_, cstr_t, @@, @@, @@, void)
+ _c_using_chash_strkey(str, cset_, cstr, @@, @@, @@, void)
#define SET_ONLY_cset_(...) __VA_ARGS__
#define MAP_ONLY_cset_(...)
diff --git a/stc/csmap.h b/stc/csmap.h index fc1e37a5..6fe35118 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -85,7 +85,7 @@ int main(void) { keyDel, keyFromRaw, keyToRaw, RawKey)
#define using_csmap_str() \
- _c_using_aatree(csmap_str, csmap_, cstr_t, cstr_t, cstr_compare_raw, \
+ _c_using_aatree(csmap_str, csmap_, cstr, cstr, cstr_compare_raw, \
cstr_del, cstr_from, cstr_c_str, const char*, \
cstr_del, cstr_from, cstr_c_str, const char*)
@@ -102,7 +102,7 @@ int main(void) { _c_using_aatree_strkey(X, csmap_, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped)
#define _c_using_aatree_strkey(X, C, Mapped, mappedDel, mappedFromRaw, mappedToRaw, RawMapped) \
- _c_using_aatree(C##X, C, cstr_t, Mapped, cstr_compare_raw, \
+ _c_using_aatree(C##X, C, cstr, Mapped, cstr_compare_raw, \
mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \
cstr_del, cstr_from, cstr_c_str, const char*)
@@ -119,7 +119,7 @@ int main(void) { using_csmap_strval_7(X, Key, keyCompare, keyDel, keyClone, c_trivial_toraw, Key)
#define using_csmap_strval_7(X, Key, keyCompareRaw, keyDel, keyFromRaw, keyToRaw, RawKey) \
- _c_using_aatree(csmap_##X, csmap_, Key, cstr_t, keyCompareRaw, \
+ _c_using_aatree(csmap_##X, csmap_, Key, cstr, keyCompareRaw, \
cstr_del, cstr_from, cstr_c_str, const char*, \
keyDel, keyFromRaw, keyToRaw, RawKey)
diff --git a/stc/csset.h b/stc/csset.h index 7d02b055..48ad6656 100644 --- a/stc/csset.h +++ b/stc/csset.h @@ -62,7 +62,7 @@ int main(void) { @@, @@, @@, void, keyDel, keyFromRaw, keyToRaw, RawKey)
#define using_csset_str() \
- _c_using_aatree_strkey(str, csset_, cstr_t, @@, @@, @@, void)
+ _c_using_aatree_strkey(str, csset_, cstr, @@, @@, @@, void)
#define SET_ONLY_csset_(...) __VA_ARGS__
#define MAP_ONLY_csset_(...)
@@ -30,138 +30,138 @@ #include <stdio.h> /* vsnprintf */
#include <ctype.h>
-typedef struct { char* str; } cstr, cstr_t;
+typedef struct { char* str; } cstr;
typedef struct { char *ref; } cstr_iter_t;
typedef char cstr_value_t;
#define cstr_npos ((size_t) (-1))
struct cstr_rep { size_t size, cap; char str[sizeof(size_t)]; };
#define _cstr_rep(self) c_container_of((self)->str, struct cstr_rep, str)
-STC_LIBRARY_ONLY( extern const cstr_t cstr_inits; )
+STC_LIBRARY_ONLY( extern const cstr cstr_inits; )
STC_STATIC_ONLY( static struct cstr_rep _cstr_nullrep = {0, 0, {0}};
- static const cstr_t cstr_inits = {_cstr_nullrep.str}; )
+ static const cstr cstr_inits = {_cstr_nullrep.str}; )
/* optimal memory: based on malloc_usable_size() sequence: 24, 40, 56, ... */
#define _cstr_opt_mem(cap) ((((offsetof(struct cstr_rep, str) + (cap) + 8)>>4)<<4) + 8)
/* optimal string capacity: 7, 23, 39, ... */
#define _cstr_opt_cap(cap) (_cstr_opt_mem(cap) - offsetof(struct cstr_rep, str) - 1)
-STC_API cstr_t cstr_from_n(const char* str, size_t n);
-STC_API cstr_t cstr_from_fmt(const char* fmt, ...);
-STC_API void cstr_fmt(cstr_t* self, const char* fmt, ...);
-STC_API size_t cstr_reserve(cstr_t* self, size_t cap);
-STC_API void cstr_resize(cstr_t* self, size_t len, char fill);
-STC_API cstr_t* cstr_assign_n(cstr_t* self, const char* str, size_t n);
-STC_API cstr_t* cstr_append_n(cstr_t* self, const char* str, size_t n);
-STC_API void cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n);
-STC_API void cstr_erase_n(cstr_t* self, size_t pos, size_t n);
-STC_API size_t cstr_find(cstr_t s, const char* needle);
-STC_API size_t cstr_find_n(cstr_t s, const char* needle, size_t pos, size_t n);
-STC_API size_t cstr_ifind_n(cstr_t s, const char* needle, size_t pos, size_t n);
-STC_API bool cstr_getdelim(cstr_t *self, int delim, FILE *stream);
+STC_API cstr cstr_from_n(const char* str, size_t n);
+STC_API cstr cstr_from_fmt(const char* fmt, ...);
+STC_API void cstr_fmt(cstr* self, const char* fmt, ...);
+STC_API size_t cstr_reserve(cstr* self, size_t cap);
+STC_API void cstr_resize(cstr* self, size_t len, char fill);
+STC_API cstr* cstr_assign_n(cstr* self, const char* str, size_t n);
+STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n);
+STC_API void cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n);
+STC_API void cstr_erase_n(cstr* self, size_t pos, size_t n);
+STC_API size_t cstr_find(cstr s, const char* needle);
+STC_API size_t cstr_find_n(cstr s, const char* needle, size_t pos, size_t n);
+STC_API size_t cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t n);
+STC_API bool cstr_getdelim(cstr *self, int delim, FILE *stream);
STC_API int c_strncasecmp(const char* s1, const char* s2, size_t n);
STC_API char* c_strnstr(const char* s, const char* needle, size_t nmax);
STC_API char* c_strncasestr(const char* s, const char* needle, size_t nmax);
-STC_INLINE cstr_t cstr_init() { return cstr_inits; }
-STC_INLINE size_t cstr_size(cstr_t s) { return _cstr_rep(&s)->size; }
-STC_INLINE size_t cstr_capacity(cstr_t s) { return _cstr_rep(&s)->cap; }
-STC_INLINE size_t cstr_empty(cstr_t s) { return _cstr_rep(&s)->size == 0; }
-STC_INLINE size_t cstr_length(cstr_t s) { return _cstr_rep(&s)->size; }
-STC_INLINE void cstr_del(cstr_t* self)
+STC_INLINE cstr cstr_init() { return cstr_inits; }
+STC_INLINE size_t cstr_size(cstr s) { return _cstr_rep(&s)->size; }
+STC_INLINE size_t cstr_capacity(cstr s) { return _cstr_rep(&s)->cap; }
+STC_INLINE size_t cstr_empty(cstr s) { return _cstr_rep(&s)->size == 0; }
+STC_INLINE size_t cstr_length(cstr s) { return _cstr_rep(&s)->size; }
+STC_INLINE void cstr_del(cstr* self)
{ if (_cstr_rep(self)->cap) c_free(_cstr_rep(self)); }
-STC_INLINE cstr_t cstr_from(const char* str)
+STC_INLINE cstr cstr_from(const char* str)
{ return cstr_from_n(str, strlen(str)); }
-STC_INLINE cstr_t cstr_clone(cstr_t s)
+STC_INLINE cstr cstr_clone(cstr s)
{ return cstr_from_n(s.str, _cstr_rep(&s)->size); }
-STC_INLINE void cstr_clear(cstr_t* self)
+STC_INLINE void cstr_clear(cstr* self)
{ self->str[_cstr_rep(self)->size = 0] = '\0'; }
-STC_INLINE cstr_t* cstr_assign(cstr_t* self, const char* str)
+STC_INLINE cstr* cstr_assign(cstr* self, const char* str)
{ return cstr_assign_n(self, str, strlen(str)); }
-STC_INLINE cstr_t* cstr_assign_s(cstr_t* self, cstr s)
+STC_INLINE cstr* cstr_assign_s(cstr* self, cstr s)
{ return cstr_assign_n(self, s.str, _cstr_rep(&s)->size); }
-STC_INLINE cstr_t* cstr_append(cstr_t* self, const char* str)
+STC_INLINE cstr* cstr_append(cstr* self, const char* str)
{ return cstr_append_n(self, str, strlen(str)); }
-STC_INLINE cstr_t* cstr_append_s(cstr_t* self, cstr s)
+STC_INLINE cstr* cstr_append_s(cstr* self, cstr s)
{ return cstr_append_n(self, s.str, _cstr_rep(&s)->size); }
-STC_INLINE void cstr_push_back(cstr_t* self, char value)
+STC_INLINE void cstr_push_back(cstr* self, char value)
{ cstr_append_n(self, &value, 1); }
-STC_INLINE void cstr_pop_back(cstr_t* self)
+STC_INLINE void cstr_pop_back(cstr* self)
{ self->str[ --_cstr_rep(self)->size ] = '\0'; }
-STC_INLINE void cstr_insert_n(cstr_t* self, size_t pos, const char* str, size_t n)
+STC_INLINE void cstr_insert_n(cstr* self, size_t pos, const char* str, size_t n)
{ cstr_replace_n(self, pos, 0, str, n); }
-STC_INLINE void cstr_insert(cstr_t* self, size_t pos, const char* str)
+STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str)
{ cstr_replace_n(self, pos, 0, str, strlen(str)); }
-STC_INLINE void cstr_insert_s(cstr_t* self, size_t pos, cstr s)
+STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s)
{ cstr_replace_n(self, pos, 0, s.str, _cstr_rep(&s)->size); }
-STC_INLINE void cstr_replace(cstr_t* self, size_t pos, size_t len, const char* str)
+STC_INLINE void cstr_replace(cstr* self, size_t pos, size_t len, const char* str)
{ cstr_replace_n(self, pos, len, str, strlen(str)); }
-STC_INLINE void cstr_replace_s(cstr_t* self, size_t pos, size_t len, cstr s)
+STC_INLINE void cstr_replace_s(cstr* self, size_t pos, size_t len, cstr s)
{ cstr_replace_n(self, pos, len, s.str, _cstr_rep(&s)->size); }
-STC_INLINE void cstr_erase(cstr_t* self, size_t pos)
+STC_INLINE void cstr_erase(cstr* self, size_t pos)
{ cstr_erase_n(self, pos, 1); }
-STC_INLINE char* cstr_front(cstr_t* self) { return self->str; }
-STC_INLINE char* cstr_back(cstr_t* self)
+STC_INLINE char* cstr_front(cstr* self) { return self->str; }
+STC_INLINE char* cstr_back(cstr* self)
{ return self->str + _cstr_rep(self)->size - 1; }
-STC_INLINE cstr_iter_t cstr_begin(cstr_t* self)
+STC_INLINE cstr_iter_t cstr_begin(cstr* self)
{ cstr_iter_t it = {self->str}; return it; }
-STC_INLINE cstr_iter_t cstr_end(cstr_t* self)
+STC_INLINE cstr_iter_t cstr_end(cstr* self)
{ cstr_iter_t it = {self->str + _cstr_rep(self)->size}; return it; }
STC_INLINE void cstr_next(cstr_iter_t* it) {++it->ref; }
-STC_INLINE bool cstr_equals(cstr_t s1, const char* str)
+STC_INLINE bool cstr_equals(cstr s1, const char* str)
{ return strcmp(s1.str, str) == 0; }
-STC_INLINE bool cstr_equals_s(cstr_t s1, cstr_t s2)
+STC_INLINE bool cstr_equals_s(cstr s1, cstr s2)
{ return strcmp(s1.str, s2.str) == 0; }
-STC_INLINE bool cstr_iequals(cstr_t s1, const char* str)
+STC_INLINE bool cstr_iequals(cstr s1, const char* str)
{ return c_strncasecmp(s1.str, str, cstr_npos) == 0; }
-STC_INLINE bool cstr_contains(cstr_t s, const char* needle)
+STC_INLINE bool cstr_contains(cstr s, const char* needle)
{ return strstr(s.str, needle) != NULL; }
-STC_INLINE bool cstr_icontains(cstr_t s, const char* needle)
+STC_INLINE bool cstr_icontains(cstr s, const char* needle)
{ return c_strncasestr(s.str, needle, cstr_npos) != NULL; }
-STC_INLINE bool cstr_begins_with(cstr_t s, const char* needle)
+STC_INLINE bool cstr_begins_with(cstr s, const char* needle)
{ return strncmp(s.str, needle, strlen(needle)) == 0; }
-STC_INLINE bool cstr_ibegins_with(cstr_t s, const char* needle)
+STC_INLINE bool cstr_ibegins_with(cstr s, const char* needle)
{ return c_strncasecmp(s.str, needle, strlen(needle)) == 0; }
-STC_INLINE bool cstr_getline(cstr_t *self, FILE *stream)
+STC_INLINE bool cstr_getline(cstr *self, FILE *stream)
{ return cstr_getdelim(self, '\n', stream); }
-STC_INLINE cstr_t
+STC_INLINE cstr
cstr_with_capacity(size_t cap) {
- cstr_t s = cstr_inits;
+ cstr s = cstr_inits;
cstr_reserve(&s, cap);
return s;
}
-STC_INLINE cstr_t
+STC_INLINE cstr
cstr_with_size(size_t len, char fill) {
- cstr_t s = cstr_inits;
+ cstr s = cstr_inits;
cstr_resize(&s, len, fill);
return s;
}
-STC_INLINE cstr_t*
-cstr_take(cstr_t* self, cstr_t s) {
+STC_INLINE cstr*
+cstr_take(cstr* self, cstr s) {
if (self->str != s.str && _cstr_rep(self)->cap)
c_free(_cstr_rep(self));
self->str = s.str;
return self;
}
-STC_INLINE cstr_t
-cstr_move(cstr_t* self) {
- cstr_t tmp = *self;
+STC_INLINE cstr
+cstr_move(cstr* self) {
+ cstr tmp = *self;
*self = cstr_inits;
return tmp;
}
STC_INLINE bool
-cstr_ends_with(cstr_t s, const char* needle) {
+cstr_ends_with(cstr s, const char* needle) {
size_t n = strlen(needle), sz = _cstr_rep(&s)->size;
return n <= sz ? memcmp(s.str + sz - n, needle, n) == 0 : false;
}
STC_INLINE bool
-cstr_iends_with(cstr_t s, const char* needle) {
+cstr_iends_with(cstr s, const char* needle) {
size_t n = strlen(needle), sz = _cstr_rep(&s)->size;
return n <= sz ? c_strncasecmp(s.str + sz - n, needle, n) == 0 : false;
}
@@ -183,10 +183,10 @@ cstr_iends_with(cstr_t s, const char* needle) { #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
STC_LIBRARY_ONLY( static struct cstr_rep _cstr_nullrep = {0, 0, {0}};
- const cstr_t cstr_inits = {_cstr_nullrep.str}; )
+ const cstr cstr_inits = {_cstr_nullrep.str}; )
STC_DEF size_t
-cstr_reserve(cstr_t* self, size_t cap) {
+cstr_reserve(cstr* self, size_t cap) {
struct cstr_rep* rep = _cstr_rep(self);
size_t oldcap = rep->cap;
if (cap > oldcap) {
@@ -199,18 +199,18 @@ cstr_reserve(cstr_t* self, size_t cap) { }
STC_DEF void
-cstr_resize(cstr_t* self, size_t len, char fill) {
+cstr_resize(cstr* self, size_t len, char fill) {
size_t n = _cstr_rep(self)->size;
cstr_reserve(self, len);
if (len > n) memset(self->str + n, fill, len - n);
if (len | n) self->str[_cstr_rep(self)->size = len] = '\0';
}
-STC_DEF cstr_t
+STC_DEF cstr
cstr_from_n(const char* str, size_t n) {
if (n == 0) return cstr_inits;
struct cstr_rep* rep = (struct cstr_rep*) c_malloc(_cstr_opt_mem(n));
- cstr_t s = {(char *) memcpy(rep->str, str, n)};
+ cstr s = {(char *) memcpy(rep->str, str, n)};
s.str[rep->size = n] = '\0';
rep->cap = _cstr_opt_cap(n);
return s;
@@ -225,7 +225,7 @@ cstr_from_n(const char* str, size_t n) { #endif
///
STC_DEF void
-cstr_vfmt(cstr_t* self, const char* fmt, va_list args) {
+cstr_vfmt(cstr* self, const char* fmt, va_list args) {
va_list args2;
va_copy(args2, args);
int len = vsnprintf(NULL, (size_t)0, fmt, args);
@@ -242,23 +242,23 @@ cstr_vfmt(cstr_t* self, const char* fmt, va_list args) { #endif
STC_DEF void
-cstr_fmt(cstr_t* self, const char* fmt, ...) {
+cstr_fmt(cstr* self, const char* fmt, ...) {
va_list args; va_start(args, fmt);
cstr_vfmt(self, fmt, args);
va_end(args);
}
-STC_DEF cstr_t
+STC_DEF cstr
cstr_from_fmt(const char* fmt, ...) {
- cstr_t ret = cstr_inits;
+ cstr ret = cstr_inits;
va_list args; va_start(args, fmt);
cstr_vfmt(&ret, fmt, args);
va_end(args);
return ret;
}
-STC_DEF cstr_t*
-cstr_assign_n(cstr_t* self, const char* str, size_t n) {
+STC_DEF cstr*
+cstr_assign_n(cstr* self, const char* str, size_t n) {
if (n || _cstr_rep(self)->cap) {
cstr_reserve(self, n);
memmove(self->str, str, n);
@@ -267,8 +267,8 @@ cstr_assign_n(cstr_t* self, const char* str, size_t n) { return self;
}
-STC_DEF cstr_t*
-cstr_append_n(cstr_t* self, const char* str, size_t n) {
+STC_DEF cstr*
+cstr_append_n(cstr* self, const char* str, size_t n) {
if (n) {
size_t oldlen = _cstr_rep(self)->size, newlen = oldlen + n;
if (newlen > _cstr_rep(self)->cap) {
@@ -282,7 +282,7 @@ cstr_append_n(cstr_t* self, const char* str, size_t n) { return self;
}
-STC_INLINE void _cstr_internal_move(cstr_t* self, size_t pos1, size_t pos2) {
+STC_INLINE void _cstr_internal_move(cstr* self, size_t pos1, size_t pos2) {
if (pos1 == pos2)
return;
size_t len = _cstr_rep(self)->size, newlen = len + pos2 - pos1;
@@ -293,7 +293,7 @@ STC_INLINE void _cstr_internal_move(cstr_t* self, size_t pos1, size_t pos2) { }
STC_DEF void
-cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n) {
+cstr_replace_n(cstr* self, size_t pos, size_t len, const char* str, size_t n) {
c_withbuffer (xstr, char, n) {
memcpy(xstr, str, n);
_cstr_internal_move(self, pos + len, pos + n);
@@ -302,7 +302,7 @@ cstr_replace_n(cstr_t* self, size_t pos, size_t len, const char* str, size_t n) }
STC_DEF void
-cstr_erase_n(cstr_t* self, size_t pos, size_t n) {
+cstr_erase_n(cstr* self, size_t pos, size_t n) {
size_t len = _cstr_rep(self)->size;
if (len) {
memmove(&self->str[pos], &self->str[pos + n], len - (pos + n));
@@ -311,7 +311,7 @@ cstr_erase_n(cstr_t* self, size_t pos, size_t n) { }
STC_DEF bool
-cstr_getdelim(cstr_t *self, int delim, FILE *fp) {
+cstr_getdelim(cstr *self, int delim, FILE *fp) {
size_t pos = 0, cap = _cstr_rep(self)->cap;
int c = fgetc(fp);
if (c == EOF)
@@ -329,20 +329,20 @@ cstr_getdelim(cstr_t *self, int delim, FILE *fp) { }
STC_DEF size_t
-cstr_find(cstr_t s, const char* needle) {
+cstr_find(cstr s, const char* needle) {
char* res = strstr(s.str, needle);
return res ? res - s.str : cstr_npos;
}
STC_DEF size_t
-cstr_find_n(cstr_t s, const char* needle, size_t pos, size_t n) {
+cstr_find_n(cstr s, const char* needle, size_t pos, size_t n) {
if (pos > _cstr_rep(&s)->size) return cstr_npos;
char* res = c_strnstr(s.str + pos, needle, n);
return res ? res - s.str : cstr_npos;
}
STC_DEF size_t
-cstr_ifind_n(cstr_t s, const char* needle, size_t pos, size_t n) {
+cstr_ifind_n(cstr s, const char* needle, size_t pos, size_t n) {
if (pos > _cstr_rep(&s)->size) return cstr_npos;
char* res = c_strncasestr(s.str + pos, needle, n);
return res ? res - s.str : cstr_npos;
@@ -41,7 +41,7 @@ _c_using_cvec(cvec_##X, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue)
#define using_cvec_str() \
- _c_using_cvec(cvec_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
+ _c_using_cvec(cvec_str, cstr, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*)
struct cvec_rep { size_t size, cap; void* data[]; };
|
