diff options
| author | Tyge Løvset <[email protected]> | 2023-02-04 23:35:56 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2023-02-04 23:35:56 +0100 |
| commit | adc47cefc2976768c3f0b773bd26bfd1062e8a53 (patch) | |
| tree | 4923f88afb0d091d5d39ae03d65a4998a0517652 /include/stc | |
| parent | 0c4c4f8bba17562735b67b2923cd23c773aa53a7 (diff) | |
| parent | d2ff84c53aa9bd3857fdf22dcf7cd9398a4780be (diff) | |
| download | STC-modified-adc47cefc2976768c3f0b773bd26bfd1062e8a53.tar.gz STC-modified-adc47cefc2976768c3f0b773bd26bfd1062e8a53.zip | |
Merge pull request #46 from tylov/newinit
Version 4.1 RC2: signed sizes and indices, cspan with numpy slicing.
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/algo/crange.h | 8 | ||||
| -rw-r--r-- | include/stc/algo/csort.h | 10 | ||||
| -rw-r--r-- | include/stc/algo/filter.h | 16 | ||||
| -rw-r--r-- | include/stc/carc.h | 8 | ||||
| -rw-r--r-- | include/stc/cbits.h | 104 | ||||
| -rw-r--r-- | include/stc/cbox.h | 8 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 140 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 123 | ||||
| -rw-r--r-- | include/stc/clist.h | 26 | ||||
| -rw-r--r-- | include/stc/cmap.h | 101 | ||||
| -rw-r--r-- | include/stc/cpque.h | 40 | ||||
| -rw-r--r-- | include/stc/crandom.h | 6 | ||||
| -rw-r--r-- | include/stc/cregex.h | 87 | ||||
| -rw-r--r-- | include/stc/csmap.h | 41 | ||||
| -rw-r--r-- | include/stc/cspan.h | 247 | ||||
| -rw-r--r-- | include/stc/cstack.h | 49 | ||||
| -rw-r--r-- | include/stc/cstr.h | 244 | ||||
| -rw-r--r-- | include/stc/csview.h | 88 | ||||
| -rw-r--r-- | include/stc/cvec.h | 95 | ||||
| -rw-r--r-- | include/stc/forward.h | 80 | ||||
| -rw-r--r-- | include/stc/priv/altnames.h (renamed from include/stc/priv/lowcase.h) | 18 | ||||
| -rw-r--r-- | include/stc/priv/template.h | 7 | ||||
| -rw-r--r-- | include/stc/utf8.h | 30 |
23 files changed, 849 insertions, 727 deletions
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 63242a54..39236ae0 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -36,7 +36,7 @@ int main() int a = 100, b = INT32_MAX; c_FORFILTER (i, crange, crange_literal(a, b, 8) , i.index > 10 - , c_FLT_TAKE(i, 3)) + , c_flt_take(i, 3)) printf(" %lld", *i.ref); puts(""); } @@ -54,10 +54,10 @@ typedef struct { crange_value start, end, step, value; } crange; typedef struct { crange_value *ref, end, step; } crange_iter; #define crange_make(...) c_MACRO_OVERLOAD(crange_make, __VA_ARGS__) -#define crange_make1(stop) crange_make3(0, stop, 1) -#define crange_make2(start, stop) crange_make3(start, stop, 1) +#define crange_make_1(stop) crange_make_3(0, stop, 1) +#define crange_make_2(start, stop) crange_make_3(start, stop, 1) -STC_INLINE crange crange_make3(crange_value start, crange_value stop, crange_value step) +STC_INLINE crange crange_make_3(crange_value start, crange_value stop, crange_value step) { crange r = {start, stop - (step > 0), step}; return r; } STC_INLINE crange_iter crange_begin(crange* self) diff --git a/include/stc/algo/csort.h b/include/stc/algo/csort.h index 2cd7b548..c8c41257 100644 --- a/include/stc/algo/csort.h +++ b/include/stc/algo/csort.h @@ -84,13 +84,13 @@ static inline void c_PASTE(cqsort_, i_tag)(i_val arr[], intptr_t lo, intptr_t hi while (i_less((&arr[i]), (&pivot))) ++i; while (i_less((&pivot), (&arr[j]))) --j; if (i <= j) { - c_SWAP(i_val, arr+i, arr+j); + c_swap(i_val, arr+i, arr+j); ++i; --j; } } if (j - lo > hi - i) { - c_SWAP(intptr_t, &lo, &i); - c_SWAP(intptr_t, &hi, &j); + c_swap(intptr_t, &lo, &i); + c_swap(intptr_t, &hi, &j); } if (j - lo > 64) c_PASTE(cqsort_, i_tag)(arr, lo, j); @@ -99,7 +99,7 @@ static inline void c_PASTE(cqsort_, i_tag)(i_val arr[], intptr_t lo, intptr_t hi } } -static inline void c_PASTE(csort_, i_tag)(i_val arr[], size_t n) - { c_PASTE(cqsort_, i_tag)(arr, 0, (intptr_t)n - 1); } +static inline void c_PASTE(csort_, i_tag)(i_val arr[], intptr_t n) + { c_PASTE(cqsort_, i_tag)(arr, 0, n - 1); } #include <stc/priv/template.h> diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h index e941be2e..9037c984 100644 --- a/include/stc/algo/filter.h +++ b/include/stc/algo/filter.h @@ -37,9 +37,9 @@ int main() puts(""); c_FORFILTER (i, cstack_int, stk - , c_FLT_SKIPWHILE(i, *i.ref < 3) + , c_flt_skipwhile(i, *i.ref < 3) && (*i.ref & 1) == 0 // even only - , c_FLT_TAKE(i, 2)) // break after 2 + , c_flt_take(i, 2)) // break after 2 printf(" %d", *i.ref); puts(""); } @@ -54,17 +54,17 @@ int main() #define c_NFILTERS 14 /* 22, 30, .. */ #endif -#define c_FLT_TAKE(i, n) (++(i).s1[(i).s1top++] <= (n)) -#define c_FLT_SKIP(i, n) (++(i).s1[(i).s1top++] > (n)) -#define c_FLT_SKIPWHILE(i, pred) ((i).s2[(i).s2top++] |= !(pred)) -#define c_FLT_TAKEWHILE(i, pred) !c_FLT_SKIPWHILE(i, pred) +#define c_flt_take(i, n) (++(i).s1[(i).s1top++] <= (n)) +#define c_flt_skip(i, n) (++(i).s1[(i).s1top++] > (n)) +#define c_flt_skipwhile(i, pred) ((i).s2[(i).s2top++] |= !(pred)) +#define c_flt_takewhile(i, pred) !c_flt_skipwhile(i, pred) #define c_FORFILTER(...) c_MACRO_OVERLOAD(c_FORFILTER, __VA_ARGS__) -#define c_FORFILTER4(i, C, cnt, filter) \ +#define c_FORFILTER_4(i, C, cnt, filter) \ c_FORFILTER_B(i, C, C##_begin(&cnt), filter) -#define c_FORFILTER5(i, C, cnt, filter, cond) \ +#define c_FORFILTER_5(i, C, cnt, filter, cond) \ c_FORFILTER_B(i, C, C##_begin(&cnt), filter) if (!(cond)) break; else #define c_FORFILTER_B(i, C, start, filter) \ diff --git a/include/stc/carc.h b/include/stc/carc.h index 614af18e..5bf4ebc2 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -45,7 +45,7 @@ int main() { ArcPers q = ArcPers_clone(p); // share the pointer printf("%s %s. uses: %ld\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count); - c_DROP(ArcPers, &p, &q); + c_drop(ArcPers, &p, &q); } */ #include "ccommon.h" @@ -93,7 +93,7 @@ _cx_deftypes(_c_carc_types, _cx_self, i_key); struct _cx_memb(_rep_) { catomic_long counter; i_key value; }; STC_INLINE _cx_self _cx_memb(_init)(void) - { return c_INIT(_cx_self){NULL, NULL}; } + { return c_LITERAL(_cx_self){NULL, NULL}; } STC_INLINE long _cx_memb(_use_count)(const _cx_self* self) { return self->use_count ? *self->use_count : 0; } @@ -127,8 +127,8 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) { if (self->use_count && _i_atomic_dec_and_test(self->use_count)) { i_keydrop(self->get); if ((char *)self->get != (char *)self->use_count + offsetof(struct _cx_memb(_rep_), value)) - c_FREE(self->get); - c_FREE((long*)self->use_count); + c_free(self->get); + c_free((long*)self->use_count); } } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 6a71487f..577ecb13 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -56,40 +56,40 @@ int main() { #include <string.h> #define _cbits_bit(i) ((uint64_t)1 << ((i) & 63)) -#define _cbits_words(n) (((n) + 63)>>6) -#define _cbits_bytes(n) (_cbits_words(n) * sizeof(uint64_t)) +#define _cbits_words(n) (intptr_t)(((n) + 63)>>6) +#define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t)) #if defined(__GNUC__) || defined(__clang__) - STC_INLINE uint64_t cpopcount64(uint64_t x) {return (uint64_t)__builtin_popcountll(x);} + STC_INLINE int cpopcount64(uint64_t x) {return __builtin_popcountll(x);} #elif defined(_MSC_VER) && defined(_WIN64) #include <intrin.h> - STC_INLINE uint64_t cpopcount64(uint64_t x) {return __popcnt64(x);} + STC_INLINE int cpopcount64(uint64_t x) {return (int)__popcnt64(x);} #else - STC_INLINE uint64_t cpopcount64(uint64_t x) { /* http://en.wikipedia.org/wiki/Hamming_weight */ + STC_INLINE int cpopcount64(uint64_t x) { /* http://en.wikipedia.org/wiki/Hamming_weight */ x -= (x >> 1) & 0x5555555555555555; x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333); x = (x + (x >> 4)) & 0x0f0f0f0f0f0f0f0f; - return (x * 0x0101010101010101) >> 56; + return (int)((x * 0x0101010101010101) >> 56); } #endif -STC_INLINE size_t _cbits_count(const uint64_t* set, const size_t sz) { - const size_t n = sz>>6; - size_t count = 0; - for (size_t i = 0; i < n; ++i) +STC_INLINE intptr_t _cbits_count(const uint64_t* set, const intptr_t sz) { + const intptr_t n = sz>>6; + intptr_t count = 0; + for (intptr_t i = 0; i < n; ++i) count += cpopcount64(set[i]); if (sz & 63) count += cpopcount64(set[n] & (_cbits_bit(sz) - 1)); return count; } -STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, - char* out, size_t start, size_t stop) { +STC_INLINE char* _cbits_to_str(const uint64_t* set, const intptr_t sz, + char* out, intptr_t start, intptr_t stop) { if (stop > sz) stop = sz; assert(start <= stop); - memset(out, '0', stop - start); - for (size_t i = start; i < stop; ++i) + c_memset(out, '0', stop - start); + for (intptr_t i = start; i < stop; ++i) if ((set[i>>6] & _cbits_bit(i)) != 0) out[i - start] = '1'; out[stop - start] = '\0'; @@ -97,8 +97,8 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, } #define _cbits_OPR(OPR, VAL) \ - const size_t n = sz>>6; \ - for (size_t i = 0; i < n; ++i) \ + const uint64_t n = (uint64_t)sz>>6; \ + for (uint64_t i = 0; i < n; ++i) \ if ((set[i] OPR other[i]) != VAL) \ return false; \ if (!(sz & 63)) \ @@ -106,10 +106,10 @@ STC_INLINE char* _cbits_to_str(const uint64_t* set, const size_t sz, const uint64_t i = n, m = _cbits_bit(sz) - 1; \ return ((set[i] OPR other[i]) & m) == (VAL & m) -STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const size_t sz) +STC_INLINE bool _cbits_subset_of(const uint64_t* set, const uint64_t* other, const intptr_t sz) { _cbits_OPR(|, set[i]); } -STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const size_t sz) +STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, const intptr_t sz) { _cbits_OPR(&, 0); } #endif // CBITS_H_INCLUDED @@ -121,12 +121,12 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons #define _i_assert(x) assert(x) #define i_type cbits -struct { uint64_t *data64; size_t _size; } typedef i_type; +struct { uint64_t *data64; intptr_t _size; } typedef i_type; -STC_INLINE cbits cbits_init(void) { return c_INIT(cbits){NULL}; } +STC_INLINE cbits cbits_init(void) { return c_LITERAL(cbits){NULL}; } STC_INLINE void cbits_create(cbits* self) { self->data64 = NULL; self->_size = 0; } -STC_INLINE void cbits_drop(cbits* self) { c_FREE(self->data64); } -STC_INLINE size_t cbits_size(const cbits* self) { return self->_size; } +STC_INLINE void cbits_drop(cbits* self) { c_free(self->data64); } +STC_INLINE intptr_t cbits_size(const cbits* self) { return self->_size; } STC_INLINE cbits* cbits_take(cbits* self, cbits other) { if (self->data64 != other.data64) { @@ -137,8 +137,8 @@ STC_INLINE cbits* cbits_take(cbits* self, cbits other) { } STC_INLINE cbits cbits_clone(cbits other) { - const size_t bytes = _cbits_bytes(other._size); - cbits set = {(uint64_t *)memcpy(c_MALLOC(bytes), other.data64, bytes), other._size}; + const intptr_t bytes = _cbits_bytes(other._size); + cbits set = {(uint64_t *)c_memcpy(c_malloc(bytes), other.data64, bytes), other._size}; return set; } @@ -147,16 +147,16 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) { return self; if (self->_size != other->_size) return cbits_take(self, cbits_clone(*other)); - memcpy(self->data64, other->data64, _cbits_bytes(other->_size)); + c_memcpy(self->data64, other->data64, _cbits_bytes(other->_size)); return self; } -STC_INLINE void cbits_resize(cbits* self, const size_t size, const bool value) { - const size_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); - self->data64 = (uint64_t *)c_REALLOC(self->data64, new_n*8); +STC_INLINE void cbits_resize(cbits* self, const intptr_t size, const bool value) { + const intptr_t new_n = _cbits_words(size), osize = self->_size, old_n = _cbits_words(osize); + self->data64 = (uint64_t *)c_realloc(self->data64, new_n*8); self->_size = size; if (new_n >= old_n) { - memset(self->data64 + old_n, -(int)value, (new_n - old_n)*8); + c_memset(self->data64 + old_n, -(int)value, (new_n - old_n)*8); if (old_n > 0) { uint64_t m = _cbits_bit(osize) - 1; /* mask */ value ? (self->data64[old_n - 1] |= ~m) @@ -174,14 +174,14 @@ STC_INLINE cbits cbits_move(cbits* self) { return tmp; } -STC_INLINE cbits cbits_with_size(const size_t size, const bool value) { - cbits set = {(uint64_t *)c_MALLOC(_cbits_bytes(size)), size}; +STC_INLINE cbits cbits_with_size(const intptr_t size, const bool value) { + cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_all(&set, value); return set; } -STC_INLINE cbits cbits_with_pattern(const size_t size, const uint64_t pattern) { - cbits set = {(uint64_t *)c_MALLOC(_cbits_bytes(size)), size}; +STC_INLINE cbits cbits_with_pattern(const intptr_t size, const uint64_t pattern) { + cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_pattern(&set, pattern); return set; } @@ -195,10 +195,10 @@ STC_INLINE cbits cbits_with_pattern(const size_t size, const uint64_t pattern) { struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type; -STC_INLINE i_type _i_memb(_init)(void) { return c_INIT(i_type){0}; } +STC_INLINE i_type _i_memb(_init)(void) { return c_LITERAL(i_type){0}; } STC_INLINE void _i_memb(_create)(i_type* self) {} STC_INLINE void _i_memb(_drop)(i_type* self) {} -STC_INLINE size_t _i_memb(_size)(const i_type* self) { return i_capacity; } +STC_INLINE intptr_t _i_memb(_size)(const i_type* self) { return i_capacity; } STC_INLINE i_type _i_memb(_move)(i_type* self) { return *self; } STC_INLINE i_type* _i_memb(_take)(i_type* self, i_type other) @@ -213,13 +213,13 @@ STC_INLINE i_type* _i_memb(_copy)(i_type* self, const i_type* other) STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value); STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern); -STC_INLINE i_type _i_memb(_with_size)(const size_t size, const bool value) { +STC_INLINE i_type _i_memb(_with_size)(const intptr_t size, const bool value) { assert(size <= i_capacity); i_type set; _i_memb(_set_all)(&set, value); return set; } -STC_INLINE i_type _i_memb(_with_pattern)(const size_t size, const uint64_t pattern) { +STC_INLINE i_type _i_memb(_with_pattern)(const intptr_t size, const uint64_t pattern) { assert(size <= i_capacity); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; @@ -229,39 +229,39 @@ STC_INLINE i_type _i_memb(_with_pattern)(const size_t size, const uint64_t patte // COMMON: STC_INLINE void _i_memb(_set_all)(i_type *self, const bool value) - { memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); } + { c_memset(self->data64, value? ~0 : 0, _cbits_bytes(_i_memb(_size)(self))); } STC_INLINE void _i_memb(_set_pattern)(i_type *self, const uint64_t pattern) { - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] = pattern; } -STC_INLINE bool _i_memb(_test)(const i_type* self, const size_t i) +STC_INLINE bool _i_memb(_test)(const i_type* self, const intptr_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE bool _i_memb(_at)(const i_type* self, const size_t i) +STC_INLINE bool _i_memb(_at)(const i_type* self, const intptr_t i) { return (self->data64[i>>6] & _cbits_bit(i)) != 0; } -STC_INLINE void _i_memb(_set)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_set)(i_type *self, const intptr_t i) { self->data64[i>>6] |= _cbits_bit(i); } -STC_INLINE void _i_memb(_reset)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_reset)(i_type *self, const intptr_t i) { self->data64[i>>6] &= ~_cbits_bit(i); } -STC_INLINE void _i_memb(_set_value)(i_type *self, const size_t i, const bool b) { +STC_INLINE void _i_memb(_set_value)(i_type *self, const intptr_t i, const bool b) { self->data64[i>>6] ^= ((uint64_t)-(int)b ^ self->data64[i>>6]) & _cbits_bit(i); } -STC_INLINE void _i_memb(_flip)(i_type *self, const size_t i) +STC_INLINE void _i_memb(_flip)(i_type *self, const intptr_t i) { self->data64[i>>6] ^= _cbits_bit(i); } STC_INLINE void _i_memb(_flip_all)(i_type *self) { - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= ~(uint64_t)0; } STC_INLINE i_type _i_memb(_from)(const char* str) { - size_t n = strlen(str); + intptr_t n = c_strlen(str); i_type set = _i_memb(_with_size)(n, false); while (n--) if (str[n] == '1') _i_memb(_set)(&set, n); return set; @@ -270,26 +270,26 @@ STC_INLINE i_type _i_memb(_from)(const char* str) { /* Intersection */ STC_INLINE void _i_memb(_intersect)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] &= other->data64[n]; } /* Union */ STC_INLINE void _i_memb(_union)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] |= other->data64[n]; } /* Exclusive disjunction */ STC_INLINE void _i_memb(_xor)(i_type *self, const i_type* other) { _i_assert(self->_size == other->_size); - size_t n = _cbits_words(_i_memb(_size)(self)); + intptr_t n = _cbits_words(_i_memb(_size)(self)); while (n--) self->data64[n] ^= other->data64[n]; } -STC_INLINE size_t _i_memb(_count)(const i_type* self) +STC_INLINE intptr_t _i_memb(_count)(const i_type* self) { return _cbits_count(self->data64, _i_memb(_size)(self)); } -STC_INLINE char* _i_memb(_to_str)(const i_type* self, char* out, size_t start, size_t stop) +STC_INLINE char* _i_memb(_to_str)(const i_type* self, char* out, intptr_t start, intptr_t stop) { return _cbits_to_str(self->data64, _i_memb(_size)(self), out, start, stop); } STC_INLINE bool _i_memb(_subset_of)(const i_type* self, const i_type* other) { diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 95dfdf8f..4f0a2976 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -37,7 +37,7 @@ Person Person_clone(Person p) { } void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->email)); - c_DROP(cstr, &p->name, &p->email); + c_drop(cstr, &p->name, &p->email); } #define i_keyclass Person // bind Person clone+drop fn's @@ -79,13 +79,13 @@ _cx_deftypes(_c_cbox_types, _cx_self, i_key); // constructors (take ownership) STC_INLINE _cx_self _cx_memb(_init)(void) - { return c_INIT(_cx_self){NULL}; } + { return c_LITERAL(_cx_self){NULL}; } STC_INLINE long _cx_memb(_use_count)(const _cx_self* self) { return (long)(self->get != NULL); } STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p) - { return c_INIT(_cx_self){p}; } + { return c_LITERAL(_cx_self){p}; } // c++: std::make_unique<i_key>(val) STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) { @@ -100,7 +100,7 @@ STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self) STC_INLINE void _cx_memb(_drop)(_cx_self* self) { if (self->get) { i_keydrop(self->get); - c_FREE(self->get); + c_free(self->get); } } diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index edf4b2cb..b8ea6316 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -29,17 +29,13 @@ #include <stdbool.h> #include <string.h> #include <assert.h> -#include "priv/lowcase.h" +#include "priv/altnames.h" -#if SIZE_MAX == UINT32_MAX - #define c_ZU PRIu32 - #define c_NPOS INT32_MAX -#elif SIZE_MAX == UINT64_MAX - #define c_ZU PRIu64 - #define c_NPOS INT64_MAX -#endif +#define c_NPOS INTPTR_MAX +#define c_ZI PRIiPTR +#define c_ZU PRIuPTR #if defined STC_NDEBUG || defined NDEBUG - #define c_ASSERT(expr) (void)(expr) + #define c_ASSERT(expr) (void)(0) #else #define c_ASSERT(expr) assert(expr) #endif @@ -56,7 +52,7 @@ /* Macro overloading feature support based on: https://rextester.com/ONP80107 */ #define c_MACRO_OVERLOAD(name, ...) \ - c_PASTE(name, c_NUMARGS(__VA_ARGS__))(__VA_ARGS__) + c_PASTE(c_CONCAT(name,_), c_NUMARGS(__VA_ARGS__))(__VA_ARGS__) #define c_CONCAT(a, b) a ## b #define c_PASTE(a, b) c_CONCAT(a, b) #define c_EXPAND(...) __VA_ARGS__ @@ -68,36 +64,46 @@ #ifdef __cplusplus #include <new> - #define c_ALLOC(T) static_cast<T*>(c_MALLOC(sizeof(T))) - #define c_ALLOC_N(T, n) static_cast<T*>(c_MALLOC(sizeof(T)*(n))) + #define c_ALLOC(T) static_cast<T*>(c_malloc(c_sizeof(T))) + #define c_ALLOC_N(T, n) static_cast<T*>(c_malloc(c_sizeof(T)*(n))) #define c_NEW(T, ...) new (c_ALLOC(T)) T(__VA_ARGS__) - #define c_INIT(T) T + #define c_LITERAL(T) T #else - #define c_ALLOC(T) ((T*)c_MALLOC(sizeof(T))) - #define c_ALLOC_N(T, n) ((T*)c_MALLOC(sizeof(T)*(n))) + #define c_ALLOC(T) ((T*)c_malloc(c_sizeof(T))) + #define c_ALLOC_N(T, n) ((T*)c_malloc(c_sizeof(T)*(n))) #define c_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T))) - #define c_INIT(T) (T) -#endif -#ifndef c_MALLOC - #define c_MALLOC(sz) malloc(sz) - #define c_CALLOC(n, sz) calloc(n, sz) - #define c_REALLOC(p, sz) realloc(p, sz) - #define c_FREE(p) free(p) + #define c_LITERAL(T) (T) #endif -#define c_STATIC_ASSERT(b) ((int)(0*sizeof(int[(b) ? 1 : -1]))) -#define c_CONTAINER_OF(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m))) -#define c_DELETE(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_FREE(_tp); } while (0) -#define c_SWAP(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ +#ifndef c_malloc + #define c_malloc(sz) malloc(c_i2u(sz)) + #define c_calloc(n, sz) calloc(c_i2u(n), c_i2u(sz)) + #define c_realloc(p, sz) realloc(p, c_i2u(sz)) + #define c_free(p) free(p) +#endif +#define c_static_assert(b) ((int)(0*sizeof(int[(b) ? 1 : -1]))) +#define c_container_of(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m))) +#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); c_free(_tp); } while (0) +#define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ _tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0) -#define c_ARRAYLEN(a) (sizeof(a)/sizeof 0[a]) +#define c_sizeof (intptr_t)sizeof +#define c_strlen(s) (intptr_t)strlen(s) +#define c_strncmp(a, b, ilen) strncmp(a, b, c_i2u(ilen)) +#define c_memcpy(d, s, ilen) memcpy(d, s, c_i2u(ilen)) +#define c_memmove(d, s, ilen) memmove(d, s, c_i2u(ilen)) +#define c_memset(d, val, ilen) memset(d, val, c_i2u(ilen)) +#define c_memcmp(a, b, ilen) memcmp(a, b, c_i2u(ilen)) + +#define c_u2i(u) ((intptr_t)((u) + 0*sizeof((u) == 1U))) +#define c_i2u(i) ((size_t)(i) + 0*sizeof((i) == 1)) +#define c_LTu(a, b) ((size_t)(a) < (size_t)(b)) // x and y are i_keyraw* type, defaults to i_key*: #define c_default_cmp(x, y) (c_default_less(y, x) - c_default_less(x, y)) #define c_default_less(x, y) (*(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_hash(x) cfasthash(x, sizeof *(x)) +#define c_default_hash(x) cfasthash(x, c_sizeof(*(x))) #define c_default_clone(v) (v) #define c_default_toraw(vp) (*(vp)) @@ -113,24 +119,28 @@ #define c_no_hash (1<<6) #define c_no_lookup (c_no_cmp|c_no_eq|c_no_hash) -/* Generic algorithms */ +/* Function macros and others */ + +#define c_make(C, ...) \ + C##_from_n((C##_raw[])__VA_ARGS__, c_sizeof((C##_raw[])__VA_ARGS__)/c_sizeof(C##_raw)) typedef const char* crawstr; #define crawstr_cmp(xp, yp) strcmp(*(xp), *(yp)) #define crawstr_hash(p) cstrhash(*(p)) -#define crawstr_len(literal) (sizeof("" literal) - 1U) +#define crawstr_len(literal) (c_sizeof("" literal) - 1) +#define c_ARRAYLEN(a) (intptr_t)(sizeof(a)/sizeof 0[a]) #define c_SV(...) c_MACRO_OVERLOAD(c_SV, __VA_ARGS__) -#define c_SV1(lit) c_SV2(lit, crawstr_len(lit)) -#define c_SV2(str, n) (c_INIT(csview){str, n}) +#define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit)) +#define c_SV_2(str, n) (c_LITERAL(csview){str, n}) #define c_ARGSV(sv) (int)(sv).size, (sv).str /* use with "%.*s" */ #define c_PAIR(ref) (ref)->first, (ref)->second #define _c_ROTL(x, k) (x << (k) | x >> (8*sizeof(x) - (k))) -STC_INLINE uint64_t cfasthash(const void* key, size_t len) { +STC_INLINE uint64_t cfasthash(const void* key, intptr_t len) { const uint8_t *x = (const uint8_t*) key; - uint64_t u8, h = 1; size_t n = len >> 3; + uint64_t u8, h = 1; intptr_t n = len >> 3; uint32_t u4; while (n--) { memcpy(&u8, x, 8), x += 8; @@ -147,30 +157,32 @@ STC_INLINE uint64_t cfasthash(const void* key, size_t len) { } STC_INLINE uint64_t cstrhash(const char *str) - { return cfasthash(str, strlen(str)); } + { return cfasthash(str, c_strlen(str)); } STC_INLINE char* cstrnstrn(const char *str, const char *needle, - size_t slen, const size_t nlen) { + intptr_t slen, const intptr_t nlen) { if (!nlen) return (char *)str; if (nlen > slen) return NULL; slen -= nlen; do { - if (*str == *needle && !memcmp(str, needle, nlen)) + if (*str == *needle && !c_memcmp(str, needle, nlen)) return (char *)str; ++str; } while (slen--); return NULL; } +/* Control block macros */ + #define c_FOREACH(...) c_MACRO_OVERLOAD(c_FOREACH, __VA_ARGS__) -#define c_FOREACH3(it, C, cnt) \ +#define c_FOREACH_3(it, C, cnt) \ for (C##_iter it = C##_begin(&cnt); it.ref; C##_next(&it)) -#define c_FOREACH4(it, C, start, finish) \ +#define c_FOREACH_4(it, C, start, finish) \ for (C##_iter it = start, *_endref = (C##_iter*)(finish).ref \ ; it.ref != (C##_value*)_endref; C##_next(&it)) #define c_FORWHILE(i, C, start, cond) \ - for (struct {C##_iter it; C##_value *ref; size_t index;} \ + for (struct {C##_iter it; C##_value *ref; intptr_t index;} \ i = {.it=start, .ref=i.it.ref}; i.it.ref && (cond) \ ; C##_next(&i.it), i.ref = i.it.ref, ++i.index) @@ -180,17 +192,17 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, ; C##_next(&_.it)) #define c_FORRANGE(...) c_MACRO_OVERLOAD(c_FORRANGE, __VA_ARGS__) -#define c_FORRANGE1(stop) c_FORRANGE3(_c_i, 0, stop) -#define c_FORRANGE2(i, stop) c_FORRANGE3(i, 0, stop) -#define c_FORRANGE3(i, start, stop) \ +#define c_FORRANGE_1(stop) c_FORRANGE_3(_c_i, 0, stop) +#define c_FORRANGE_2(i, stop) c_FORRANGE_3(i, 0, stop) +#define c_FORRANGE_3(i, start, stop) \ for (long long i=start, _end=(long long)(stop); i < _end; ++i) -#define c_FORRANGE4(i, start, stop, step) \ +#define c_FORRANGE_4(i, start, stop, step) \ for (long long i=start, _inc=step, _end=(long long)(stop) - (_inc > 0) \ ; (_inc > 0) ^ (i > _end); i += _inc) #ifndef __cplusplus #define c_FORLIST(it, T, ...) \ for (struct {T* data; T* ref; int size, index;} \ - it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=sizeof((T[])__VA_ARGS__)/sizeof(T)} \ + it = {.data=(T[])__VA_ARGS__, .ref=it.data, .size=(int)(sizeof((T[])__VA_ARGS__)/sizeof(T))} \ ; it.index < it.size; ++it.ref, ++it.index) #else #include <initializer_list> @@ -200,42 +212,44 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, ; it.index < it.size; ++it.ref, ++it.index) #endif #define c_WITH(...) c_MACRO_OVERLOAD(c_WITH, __VA_ARGS__) -#define c_WITH2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop) -#define c_WITH3(declvar, pred, drop) for (declvar, **_c_i = NULL; !_c_i && (pred); ++_c_i, drop) +#define c_WITH_2(declvar, drop) for (declvar, **_c_i = NULL; !_c_i; ++_c_i, drop) +#define c_WITH_3(declvar, pred, drop) for (declvar, **_c_i = NULL; !_c_i && (pred); ++_c_i, drop) #define c_SCOPE(init, drop) for (int _c_i = (init, 1); _c_i; --_c_i, drop) #define c_DEFER(...) for (int _c_i = 1; _c_i; --_c_i, __VA_ARGS__) -#define c_DROP(C, ...) do { c_FORLIST (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0) #define c_AUTO(...) c_MACRO_OVERLOAD(c_AUTO, __VA_ARGS__) -#define c_AUTO2(C, a) \ - c_WITH2(C a = C##_init(), C##_drop(&a)) -#define c_AUTO3(C, a, b) \ - c_WITH2(c_EXPAND(C a = C##_init(), b = C##_init()), \ +#define c_AUTO_2(C, a) \ + c_WITH_2(C a = C##_init(), C##_drop(&a)) +#define c_AUTO_3(C, a, b) \ + c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init()), \ (C##_drop(&b), C##_drop(&a))) -#define c_AUTO4(C, a, b, c) \ - c_WITH2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \ +#define c_AUTO_4(C, a, b, c) \ + c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init()), \ (C##_drop(&c), C##_drop(&b), C##_drop(&a))) -#define c_AUTO5(C, a, b, c, d) \ - c_WITH2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \ +#define c_AUTO_5(C, a, b, c, d) \ + c_WITH_2(c_EXPAND(C a = C##_init(), b = C##_init(), c = C##_init(), d = C##_init()), \ (C##_drop(&d), C##_drop(&c), C##_drop(&b), C##_drop(&a))) -#define c_FIND_IF(...) c_MACRO_OVERLOAD(c_FIND_IF, __VA_ARGS__) -#define c_FIND_IF4(it, C, cnt, pred) do { \ - size_t index = 0; \ +/* Generic functions */ + +#define c_drop(C, ...) do { c_FORLIST (_i, C*, {__VA_ARGS__}) C##_drop(*_i.ref); } while(0) +#define c_find_if(...) c_MACRO_OVERLOAD(c_find_if, __VA_ARGS__) +#define c_find_if_4(it, C, cnt, pred) do { \ + intptr_t index = 0; \ for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \ ++index; \ } while (0) -#define c_FIND_IF5(it, C, start, end, pred) do { \ - size_t index = 0; \ +#define c_find_if_5(it, C, start, end, pred) do { \ + intptr_t index = 0; \ const C##_value* _endref = (end).ref; \ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ ++index; \ if (it.ref == _endref) it.ref = NULL; \ } while (0) -#define c_ERASE_IF(it, C, cnt, pred) do { \ +#define c_erase_if(it, C, cnt, pred) do { \ C##_iter it = C##_begin(&cnt); \ - for (size_t index = 0; it.ref; ++index) { \ + for (intptr_t index = 0; it.ref; ++index) { \ if (pred) it = C##_erase_at(&cnt, it); \ else C##_next(&it); \ } \ diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 1fe52548..56c0e867 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -42,13 +42,16 @@ _cx_deftypes(_c_cdeq_types, _cx_self, i_key); typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); -STC_API _cx_self _cx_memb(_with_capacity)(const size_t n); -STC_API bool _cx_memb(_reserve)(_cx_self* self, const size_t n); +STC_API _cx_self _cx_memb(_with_capacity)(const intptr_t n); +STC_API bool _cx_memb(_reserve)(_cx_self* self, const intptr_t n); STC_API void _cx_memb(_clear)(_cx_self* self); STC_API void _cx_memb(_drop)(_cx_self* self); STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API void _cx_memb(_shrink_to_fit)(_cx_self *self); - +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #if !defined _i_queue #if !defined i_no_emplace STC_API _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, @@ -86,8 +89,8 @@ STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_INLINE i_key _cx_memb(_value_clone)(i_key val) { return i_keyclone(val); } #endif // !i_no_clone -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* pval) { return i_keyto(pval); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } @@ -97,30 +100,30 @@ STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _ { i_keydrop(self->data); ++self->data; --self->_len; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = self->_len; - return c_INIT(_cx_iter){n ? self->data : NULL, self->data + n}; + intptr_t n = self->_len; + return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL, self->data + self->_len}; } + { return c_LITERAL(_cx_iter){NULL, self->data + self->_len}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) { if ((it.ref += n) >= it.end) it.ref = NULL; return it; } #if !defined _i_queue -STC_INLINE size_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) - { return (size_t)(it.ref - self->data); } +STC_INLINE intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) + { return (it.ref - self->data); } STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const size_t idx) { +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } -STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } @@ -128,11 +131,11 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) { return _cx_memb(_push)(self, value); } STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { +_cx_memb(_insert)(_cx_self* self, const intptr_t idx, i_key value) { return _cx_memb(_insert_range)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter -_cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], const size_t n) { +_cx_memb(_insert_n)(_cx_self* self, const intptr_t idx, const _cx_value arr[], const intptr_t n) { return _cx_memb(_insert_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -141,7 +144,7 @@ _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { } STC_INLINE _cx_iter -_cx_memb(_erase_n)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_erase_n)(_cx_self* self, const intptr_t idx, const intptr_t n) { return _cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n); } STC_INLINE _cx_iter @@ -164,7 +167,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { } STC_INLINE _cx_iter -_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { +_cx_memb(_emplace_n)(_cx_self* self, const intptr_t idx, const _cx_raw arr[], const intptr_t n) { return _cx_memb(_emplace_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -204,7 +207,7 @@ _cx_memb(_sort)(_cx_self* self) { /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) -#define _cdeq_nfront(self) (size_t)((self)->data - (self)->_base) +#define _cdeq_nfront(self) ((self)->data - (self)->_base) STC_DEF _cx_self _cx_memb(_init)(void) { @@ -225,8 +228,8 @@ _cx_memb(_clear)(_cx_self* self) { STC_DEF void _cx_memb(_shrink_to_fit)(_cx_self *self) { if (self->_len != self->_cap) { - memmove(self->_base, self->data, self->_len*sizeof(i_key)); - _cx_value* d = (_cx_value*)c_REALLOC(self->_base, self->_len*sizeof(i_key)); + c_memmove(self->_base, self->data, self->_len*c_sizeof(i_key)); + _cx_value* d = (_cx_value*)c_realloc(self->_base, self->_len*c_sizeof(i_key)); if (d) { self->_base = d; self->_cap = self->_len; @@ -239,15 +242,15 @@ STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (self->_base) { _cx_memb(_clear)(self); - c_FREE(self->_base); + c_free(self->_base); } } -static size_t -_cx_memb(_realloc_)(_cx_self* self, const size_t n) { - const size_t cap = (size_t)((float)self->_len*1.7f) + n + 7U; - const size_t nfront = _cdeq_nfront(self); - _cx_value* d = (_cx_value*)c_REALLOC(self->_base, cap*sizeof(i_key)); +static intptr_t +_cx_memb(_realloc_)(_cx_self* self, const intptr_t n) { + const intptr_t cap = (intptr_t)((float)self->_len*1.7f) + n + 7; + const intptr_t nfront = _cdeq_nfront(self); + _cx_value* d = (_cx_value*)c_realloc(self->_base, cap*c_sizeof(i_key)); if (!d) return 0; self->_cap = cap; @@ -257,37 +260,37 @@ _cx_memb(_realloc_)(_cx_self* self, const size_t n) { } static bool -_cx_memb(_expand_right_half_)(_cx_self* self, const size_t idx, const size_t n) { - const size_t sz = self->_len, cap = self->_cap; - const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; - if (nback >= n || (size_t)((float)sz*1.3f) + n > cap) { +_cx_memb(_expand_right_half_)(_cx_self* self, const intptr_t idx, const intptr_t n) { + const intptr_t sz = self->_len, cap = self->_cap; + const intptr_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; + if (nback >= n || (intptr_t)((float)sz*1.3f) + n > cap) { if (!_cx_memb(_realloc_)(self, n)) return false; - memmove(self->data + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); + c_memmove(self->data + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); } else { #if !defined _i_queue - const size_t unused = cap - (sz + n); - const size_t pos = (nfront*2 < unused) ? nfront : unused/2; + const intptr_t unused = cap - (sz + n); + const intptr_t pos = (nfront*2 < unused) ? nfront : unused/2; #else - const size_t pos = 0; + const intptr_t pos = 0; #endif - memmove(self->_base + pos, self->data, idx*sizeof(i_key)); - memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); + c_memmove(self->_base + pos, self->data, idx*c_sizeof(i_key)); + c_memmove(self->data + pos + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); self->data = self->_base + pos; } return true; } STC_DEF _cx_self -_cx_memb(_with_capacity)(const size_t n) { +_cx_memb(_with_capacity)(const intptr_t n) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_expand_right_half_)(&cx, 0, n); return cx; } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t n) { - const size_t sz = self->_len; +_cx_memb(_reserve)(_cx_self* self, const intptr_t n) { + const intptr_t sz = self->_len; return n <= sz || _cx_memb(_expand_right_half_)(self, sz, n - sz); } @@ -305,7 +308,7 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { _cx_self out = _cx_memb(_with_capacity)(cx._len); if (out._base) - for (size_t i = 0; i < cx._len; ++i) + for (intptr_t i = 0; i < cx._len; ++i) out.data[i] = i_keyclone(cx.data[i]); return out; } @@ -314,27 +317,27 @@ _cx_memb(_clone)(_cx_self cx) { #if !defined _i_queue static void -_cx_memb(_expand_left_half_)(_cx_self* self, const size_t idx, const size_t n) { - size_t cap = self->_cap; - const size_t sz = self->_len; - const size_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; +_cx_memb(_expand_left_half_)(_cx_self* self, const intptr_t idx, const intptr_t n) { + intptr_t cap = self->_cap; + const intptr_t sz = self->_len; + const intptr_t nfront = _cdeq_nfront(self), nback = cap - sz - nfront; if (nfront >= n) { - self->data = (_cx_value *)memmove(self->data - n, self->data, idx*sizeof(i_key)); + self->data = (_cx_value *)c_memmove(self->data - n, self->data, idx*c_sizeof(i_key)); } else { - if ((size_t)((float)sz*1.3f) + n > cap) + if ((intptr_t)((float)sz*1.3f) + n > cap) cap = _cx_memb(_realloc_)(self, n); - const size_t unused = cap - (sz + n); - const size_t pos = (nback*2 < unused) ? unused - nback : unused/2; - memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*sizeof(i_key)); - self->data = (_cx_value *)memmove(self->_base + pos, self->data, idx*sizeof(i_key)); + const intptr_t unused = cap - (sz + n); + const intptr_t pos = (nback*2 < unused) ? unused - nback : unused/2; + c_memmove(self->_base + pos + idx + n, self->data + idx, (sz - idx)*c_sizeof(i_key)); + self->data = (_cx_value *)c_memmove(self->_base + pos, self->data, idx*c_sizeof(i_key)); } } static _cx_iter -_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { +_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n) { if (n) { if (!pos) pos = self->data + self->_len; - const size_t idx = (size_t)(pos - self->data); + const intptr_t idx = (pos - self->data); if (idx*2 < self->_len) _cx_memb(_expand_left_half_)(self, idx, n); else @@ -342,7 +345,7 @@ _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { self->_len += n; pos = self->data + idx; } - return c_INIT(_cx_iter){pos, self->data + self->_len}; + return c_LITERAL(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_value* @@ -359,9 +362,9 @@ _cx_memb(_push_front)(_cx_self* self, i_key value) { STC_DEF _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) - memcpy(it.ref, p1, (size_t)(p2 - p1)*sizeof *p1); + c_memcpy(it.ref, p1, (p2 - p1)*c_sizeof *p1); return it; } @@ -372,16 +375,16 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { _cx_value* p = p1, *end = self->data + self->_len; for (; p != p2; ++p) { i_keydrop(p); } - memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); - self->_len -= (size_t)len; - return c_INIT(_cx_iter){p2 == end ? NULL : p1, end - len}; + c_memmove(p1, p2, (end - p2)*c_sizeof *p1); + self->_len -= len; + return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone STC_DEF _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyclone((*p1)); @@ -393,7 +396,7 @@ _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, STC_DEF _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyfrom((*p1)); diff --git a/include/stc/clist.h b/include/stc/clist.h index 37fa447a..283c8774 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -64,7 +64,7 @@ SELF##_value value; \ } -#define _clist_tonode(vp) c_CONTAINER_OF(vp, _cx_node, value) +#define _clist_tonode(vp) c_container_of(vp, _cx_node, value) _c_clist_types(clist_VOID, int); _c_clist_complete_types(clist_VOID, dummy); @@ -98,7 +98,7 @@ STC_API _cx_iter _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key 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 !defined i_no_cmp -STC_API size_t _cx_memb(_remove)(_cx_self* self, _cx_raw val); +STC_API intptr_t _cx_memb(_remove)(_cx_self* self, _cx_raw val); STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val); STC_API int _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y); STC_API int _cx_memb(_sort_cmp_)(const clist_VOID_node* x, const clist_VOID_node* y); @@ -133,8 +133,12 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push_back)(self, i_keyfrom(raw)); } #endif // !i_no_emplace -STC_INLINE _cx_self _cx_memb(_init)(void) { return c_INIT(_cx_self){NULL}; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; } +STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){NULL}; } +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) + { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) { return true; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return self->last == NULL; } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_drop)(self); } STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, i_key value) @@ -146,9 +150,9 @@ STC_INLINE _cx_node* _cx_memb(_unlink_node_front)(_cx_self* self) STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return &self->last->next->value; } STC_INLINE _cx_value* _cx_memb(_back)(const _cx_self* self) { return &self->last->value; } -STC_INLINE size_t +STC_INLINE intptr_t _cx_memb(_count)(const _cx_self* self) { - size_t n = 1; const _cx_node *node = self->last; + intptr_t n = 1; const _cx_node *node = self->last; if (!node) return 0; while ((node = node->next) != self->last) ++n; return n; @@ -157,12 +161,12 @@ _cx_memb(_count)(const _cx_self* self) { STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { _cx_value* head = self->last ? &self->last->next->value : NULL; - return c_INIT(_cx_iter){head, &self->last, self->last}; + return c_LITERAL(_cx_iter){head, &self->last, self->last}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL}; } + { return c_LITERAL(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { @@ -344,7 +348,7 @@ STC_DEF void _cx_memb(_erase_node_after)(_cx_self* self, _cx_node* ref) { _cx_node* node = _cx_memb(_unlink_node_after)(self, ref); i_keydrop((&node->value)); - c_FREE(node); + c_free(node); } STC_DEF _cx_node* @@ -410,9 +414,9 @@ _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw val) { it2.ref = NULL; return it2; } -STC_DEF size_t +STC_DEF intptr_t _cx_memb(_remove)(_cx_self* self, _cx_raw val) { - size_t n = 0; + intptr_t n = 0; _cx_node *prev = self->last, *node; if (prev) do { node = prev->next; diff --git a/include/stc/cmap.h b/include/stc/cmap.h index df3ce6fc..393df53f 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -53,7 +53,7 @@ int main(void) { #include "forward.h" #include <stdlib.h> #include <string.h> -typedef struct { size_t idx; uint8_t hx; } chash_bucket_t; +typedef struct { int64_t idx; uint8_t hx; } chash_bucket_t; #endif // CMAP_H_INCLUDED #ifndef _i_prefix @@ -73,6 +73,12 @@ typedef struct { size_t idx; uint8_t hx; } chash_bucket_t; #ifndef i_max_load_factor #define i_max_load_factor 0.85f #endif +#ifndef i_size + #define i_size int32_t + #define _i_expandby 1 +#else + #define _i_expandby 2 +#endif #include "priv/template.h" #ifndef i_hash_functor #define i_hash_functor(self, x) i_hash(x) @@ -96,25 +102,25 @@ typedef _i_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(size_t cap); +STC_API _cx_self _cx_memb(_with_capacity)(int64_t cap); #if !defined i_no_clone STC_API _cx_self _cx_memb(_clone)(_cx_self map); #endif 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 bool _cx_memb(_reserve)(_cx_self* self, int64_t capacity); STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr); STC_API _cx_result _cx_memb(_insert_entry_)(_cx_self* self, _cx_rawkey rkey); STC_API void _cx_memb(_erase_entry)(_cx_self* self, _cx_value* val); -STC_INLINE _cx_self _cx_memb(_init)(void) { return c_INIT(_cx_self){0}; } +STC_INLINE _cx_self _cx_memb(_init)(void) { return c_LITERAL(_cx_self){0}; } STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->size); } STC_INLINE float _cx_memb(_max_load_factor)(const _cx_self* self) { return (float)(i_max_load_factor); } STC_INLINE bool _cx_memb(_empty)(const _cx_self* map) { return !map->size; } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* map) { return map->size; } -STC_INLINE size_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* map) - { return (size_t)((float)map->bucket_count * (i_max_load_factor)); } +STC_INLINE int64_t _cx_memb(_size)(const _cx_self* map) { return map->size; } +STC_INLINE int64_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } +STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* map) + { return (int64_t)((float)map->bucket_count * (i_max_load_factor)); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } @@ -166,7 +172,7 @@ _cx_memb(_emplace)(_cx_self* self, _cx_rawkey rkey _i_MAP_ONLY(, i_valraw rmappe STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) - _i_MAP_ONLY( c_INIT(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); + _i_MAP_ONLY( c_LITERAL(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } STC_INLINE void @@ -195,8 +201,23 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { return _res; } -STC_INLINE _cx_iter -_cx_memb(_begin)(const _cx_self* self) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { + while (n--) +#if defined _i_isset && defined i_no_emplace + _cx_memb(_insert)(self, *raw++); +#elif defined _i_isset + _cx_memb(_emplace)(self, *raw++); +#elif defined i_no_emplace + _cx_memb(_insert_or_assign)(self, raw->first, raw->second), ++raw; +#else + _cx_memb(_emplace_or_assign)(self, raw->first, raw->second), ++raw; +#endif +} + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } + +STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { _cx_iter it = {self->table, self->table+self->bucket_count, self->_hashx}; if (it._hx) while (*it._hx == 0) @@ -207,7 +228,7 @@ _cx_memb(_begin)(const _cx_self* self) { STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL}; } + { return c_LITERAL(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { @@ -216,16 +237,16 @@ _cx_memb(_next)(_cx_iter* it) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, size_t n) { +_cx_memb(_advance)(_cx_iter it, uint64_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { - size_t idx; + int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) - return c_INIT(_cx_iter){self->table + idx, + return c_LITERAL(_cx_iter){self->table + idx, self->table + self->bucket_count, self->_hashx + idx}; return _cx_memb(_end)(self); @@ -233,7 +254,7 @@ _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { STC_INLINE const _cx_value* _cx_memb(_get)(const _cx_self* self, _cx_rawkey rkey) { - size_t idx; + int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return self->table + idx; return NULL; @@ -243,12 +264,12 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey) { return (_cx_value*)_cx_memb(_get)(self, rkey); } -STC_INLINE size_t +STC_INLINE int _cx_memb(_erase)(_cx_self* self, _cx_rawkey rkey) { if (self->size == 0) return 0; chash_bucket_t b = _cx_memb(_bucket_)(self, &rkey); - return self->_hashx[b.idx] ? _cx_memb(_erase_entry)(self, self->table + b.idx), 1U : 0U; + return self->_hashx[b.idx] ? _cx_memb(_erase_entry)(self, self->table + b.idx), 1 : 0; } STC_INLINE _cx_iter @@ -263,11 +284,11 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { #if defined(i_implement) #ifndef CMAP_H_INCLUDED -STC_INLINE size_t fastrange_1(uint64_t x, uint64_t n) - { return (size_t)((uint32_t)x*n >> 32); } // n < 2^32 +STC_INLINE int64_t fastrange_1(uint64_t x, uint64_t n) + { return (int64_t)((uint32_t)x*n >> 32); } // n < 2^32 -STC_INLINE size_t fastrange_2(uint64_t x, uint64_t n) - { return x & (n - 1); } // n power of 2. +STC_INLINE int64_t fastrange_2(uint64_t x, uint64_t n) + { return (int64_t)(x & (n - 1)); } // n power of 2. STC_INLINE uint64_t next_power_of_2(uint64_t n) { n--; @@ -279,7 +300,7 @@ STC_INLINE uint64_t next_power_of_2(uint64_t n) { #endif // CMAP_H_INCLUDED STC_DEF _cx_self -_cx_memb(_with_capacity)(const size_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -297,14 +318,14 @@ STC_INLINE void _cx_memb(_wipe_)(_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); + c_free(self->_hashx); + c_free((void *) self->table); } STC_DEF void _cx_memb(_clear)(_cx_self* self) { _cx_memb(_wipe_)(self); self->size = 0; - memset(self->_hashx, 0, self->bucket_count); + c_memset(self->_hashx, 0, self->bucket_count); } #ifndef _i_isset @@ -339,8 +360,8 @@ STC_DEF void _cx_memb(_clear)(_cx_self* self) { STC_DEF chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawkey* rkeyptr) { const uint64_t _hash = i_hash_functor(self, rkeyptr); - i_size _cap = self->bucket_count; - chash_bucket_t b = {c_PASTE(fastrange_,_i_expandby)(_hash, _cap), (uint8_t)(_hash | 0x80)}; + int64_t _cap = self->bucket_count; + chash_bucket_t b = {c_PASTE(fastrange_,_i_expandby)(_hash, (uint64_t)_cap), (uint8_t)(_hash | 0x80)}; const uint8_t* _hx = self->_hashx; while (_hx[b.idx]) { if (_hx[b.idx] == b.hx) { @@ -375,9 +396,9 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self m) { if (m.table) { _cx_value *t = c_ALLOC_N(_cx_value, m.bucket_count), *dst = t, *m_end = m.table + m.bucket_count; - uint8_t *h = (uint8_t *)memcpy(c_MALLOC(m.bucket_count + 1), m._hashx, m.bucket_count + 1); + uint8_t *h = (uint8_t *)c_memcpy(c_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1); if (!(t && h)) - { c_FREE(t), c_FREE(h), t = 0, h = 0, m.bucket_count = 0; } + { c_free(t), c_free(h), t = 0, h = 0, m.bucket_count = 0; } else for (; m.table != m_end; ++m.table, ++m._hashx, ++dst) if (*m._hashx) @@ -389,8 +410,8 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { - const i_size _oldbuckets = self->bucket_count; +_cx_memb(_reserve)(_cx_self* self, const int64_t newcap) { + const i_size _oldbuckets = self->bucket_count, _newcap = (i_size)newcap; if (_newcap != self->size && _newcap <= _oldbuckets) return true; i_size _nbuckets = (i_size)((float)_newcap / (i_max_load_factor)) + 4; @@ -401,24 +422,24 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { #endif _cx_self m = { c_ALLOC_N(_cx_value, _nbuckets), - (uint8_t *) c_CALLOC(_nbuckets + 1, 1), - self->size, (i_size)_nbuckets, + (uint8_t *) c_calloc(_nbuckets + 1, 1), + self->size, _nbuckets, }; bool ok = m.table && m._hashx; if (ok) { /* Rehash: */ m._hashx[_nbuckets] = 0xff; const _cx_value* e = self->table; const uint8_t* h = self->_hashx; - for (size_t i = 0; i < _oldbuckets; ++i, ++e) if (*h++) { + for (i_size i = 0; i < _oldbuckets; ++i, ++e) if (*h++) { _cx_rawkey r = i_keyto(_i_keyref(e)); chash_bucket_t b = _cx_memb(_bucket_)(&m, &r); m.table[b.idx] = *e; - m._hashx[b.idx] = (uint8_t)b.hx; + m._hashx[b.idx] = b.hx; } - c_SWAP(_cx_self, self, &m); + c_swap(_cx_self, self, &m); } - c_FREE(m._hashx); - c_FREE(m.table); + c_free(m._hashx); + c_free(m.table); return ok; } @@ -435,7 +456,7 @@ _cx_memb(_erase_entry)(_cx_self* self, _cx_value* _val) { if (! _hashx[j]) break; const _cx_rawkey _raw = i_keyto(_i_keyref(_slot + j)); - k = (i_size)c_PASTE(fastrange_,_i_expandby)(i_hash_functor(self, (&_raw)), _cap); + k = (i_size)c_PASTE(fastrange_,_i_expandby)(i_hash_functor(self, (&_raw)), (uint64_t)_cap); if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */ _slot[i] = _slot[j], _hashx[i] = _hashx[j], i = j; } diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 28b5eb86..55df70ef 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -41,47 +41,53 @@ typedef i_keyraw _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); +STC_API void _cx_memb(_erase_at)(_cx_self* self, intptr_t idx); STC_API void _cx_memb(_push)(_cx_self* self, _cx_value value); STC_INLINE _cx_self _cx_memb(_init)(void) - { return c_INIT(_cx_self){NULL}; } + { return c_LITERAL(_cx_self){NULL}; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } + +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { if (cap != self->_len && cap <= self->_cap) return true; - _cx_value *d = (_cx_value *)c_REALLOC(self->data, cap*sizeof *d); + _cx_value *d = (_cx_value *)c_realloc(self->data, cap*c_sizeof *d); return d ? (self->data = d, self->_cap = cap, true) : false; } STC_INLINE void _cx_memb(_shrink_to_fit)(_cx_self* self) { _cx_memb(_reserve)(self, self->_len); } -STC_INLINE _cx_self _cx_memb(_with_capacity)(const size_t cap) { +STC_INLINE _cx_self _cx_memb(_with_capacity)(const intptr_t cap) { _cx_self out = {NULL}; _cx_memb(_reserve)(&out, cap); return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(const size_t size, i_key null) { +STC_INLINE _cx_self _cx_memb(_with_size)(const intptr_t size, i_key null) { _cx_self out = {NULL}; _cx_memb(_reserve)(&out, size); while (out._len < size) out.data[out._len++] = null; return out; } STC_INLINE void _cx_memb(_clear)(_cx_self* self) { - size_t i = self->_len; self->_len = 0; + intptr_t i = self->_len; self->_len = 0; while (i--) { i_keydrop((self->data + i)); } } STC_INLINE void _cx_memb(_drop)(_cx_self* self) - { _cx_memb(_clear)(self); c_FREE(self->data); } + { _cx_memb(_clear)(self); c_free(self->data); } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* q) +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* q) { return q->_len; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* q) { return !q->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* q) +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* q) { return q->_cap; } STC_INLINE const _cx_value* _cx_memb(_top)(const _cx_self* self) @@ -111,9 +117,9 @@ STC_INLINE void _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #if defined(i_implement) STC_DEF void -_cx_memb(_sift_down_)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_sift_down_)(_cx_self* self, const intptr_t idx, const intptr_t n) { _cx_value t, *arr = self->data - 1; - for (size_t r = idx, c = idx*2; c <= n; c *= 2) { + for (intptr_t r = idx, c = idx*2; c <= n; c *= 2) { c += i_less_functor(self, (&arr[c]), (&arr[c + (c < n)])); if (!(i_less_functor(self, (&arr[r]), (&arr[c])))) return; t = arr[r], arr[r] = arr[c], arr[r = c] = t; @@ -122,8 +128,8 @@ _cx_memb(_sift_down_)(_cx_self* self, const size_t idx, const size_t n) { STC_DEF void _cx_memb(_make_heap)(_cx_self* self) { - size_t n = self->_len; - for (size_t k = n/2; k != 0; --k) + intptr_t n = self->_len; + for (intptr_t k = n/2; k != 0; --k) _cx_memb(_sift_down_)(self, k, n); } @@ -137,9 +143,9 @@ STC_DEF _cx_self _cx_memb(_clone)(_cx_self q) { #endif STC_DEF void -_cx_memb(_erase_at)(_cx_self* self, const size_t idx) { +_cx_memb(_erase_at)(_cx_self* self, const intptr_t idx) { i_keydrop((self->data + idx)); - const size_t n = --self->_len; + const intptr_t n = --self->_len; self->data[idx] = self->data[n]; _cx_memb(_sift_down_)(self, idx + 1, n); } @@ -149,7 +155,7 @@ _cx_memb(_push)(_cx_self* self, _cx_value value) { if (self->_len == self->_cap) _cx_memb(_reserve)(self, self->_len*3/2 + 4); _cx_value *arr = self->data - 1; /* base 1 */ - size_t c = ++self->_len; + intptr_t c = ++self->_len; for (; c > 1 && (i_less_functor(self, (&arr[c/2]), (&value))); c /= 2) arr[c] = arr[c/2]; arr[c] = value; diff --git a/include/stc/crandom.h b/include/stc/crandom.h index 733c3ec5..32e541ff 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -46,7 +46,7 @@ int main() { typedef struct stc64 { uint64_t state[5]; } stc64_t; typedef struct stc64_uniform { int64_t lower; uint64_t range, threshold; } stc64_uniform_t; typedef struct stc64_uniformf { double lower, range; } stc64_uniformf_t; -typedef struct stc64_normalf { double mean, stddev, next; unsigned has_next; } stc64_normalf_t; +typedef struct stc64_normalf { double mean, stddev, next; int has_next; } stc64_normalf_t; /* PRNG stc64. * Very fast PRNG suited for parallel usage with Weyl-sequence parameter. @@ -100,12 +100,12 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) { /* Init uniform distributed float64 RNG, range [low, high). */ STC_INLINE stc64_uniformf_t stc64_uniformf_new(double low, double high) { - return c_INIT(stc64_uniformf_t){low, high - low}; + return c_LITERAL(stc64_uniformf_t){low, high - low}; } /* Marsaglia polar method for gaussian/normal distribution, float64. */ STC_INLINE stc64_normalf_t stc64_normalf_new(double mean, double stddev) { - return c_INIT(stc64_normalf_t){mean, stddev, 0.0, 0}; + return c_LITERAL(stc64_normalf_t){mean, stddev, 0.0, 0}; } /* -------------------------- IMPLEMENTATION ------------------------- */ diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 4bb17938..564fe157 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -33,6 +33,7 @@ THE SOFTWARE. #include <stdbool.h> #include <string.h> #include "forward.h" // csview +#include "ccommon.h" enum { CREG_DEFAULT = 0, @@ -81,69 +82,87 @@ typedef struct { #define c_FORMATCH(it, Re, Input) \ for (cregex_iter it = {Re, Input}; \ - cregex_find(it.re, it.input, it.match, CREG_M_NEXT) == CREG_OK; ) + cregex_find_4(it.re, it.input, it.match, CREG_M_NEXT) == CREG_OK; ) -static inline -cregex cregex_init(void) { +STC_INLINE cregex cregex_init(void) { cregex re = {0}; return re; } /* return CREG_OK, or negative error code on failure. */ -int cregex_compile(cregex *self, const char* pattern, int cflags); +#define cregex_compile(...) c_MACRO_OVERLOAD(cregex_compile, __VA_ARGS__) +#define cregex_compile_2(re, pattern) cregex_compile_3(re, pattern, CREG_DEFAULT) +int cregex_compile_3(cregex *re, const char* pattern, int cflags); -static inline -cregex cregex_from(const char* pattern, int cflags) { +/* construct and return a regex from a pattern. */ +#define cregex_from(...) c_MACRO_OVERLOAD(cregex_from, __VA_ARGS__) +#define cregex_from_1(pattern) cregex_from_2(pattern, CREG_DEFAULT) + +STC_INLINE cregex cregex_from_2(const char* pattern, int cflags) { cregex re = {0}; - cregex_compile(&re, pattern, cflags); + cregex_compile_3(&re, pattern, cflags); return re; } -/* number of capture groups in a regex pattern, 0 if regex is invalid */ -unsigned cregex_captures(const cregex* self); +/* number of capture groups in a regex pattern including full the match capture, 0 if regex is invalid */ +int cregex_captures(const cregex* re); /* return CREG_OK, CREG_NOMATCH or CREG_MATCHERROR. */ -int cregex_find(const cregex* re, const char* input, - csview match[], int mflags); -static inline -int cregex_find_sv(const cregex* re, csview input, csview match[]) { +#define cregex_find(...) c_MACRO_OVERLOAD(cregex_find, __VA_ARGS__) +#define cregex_find_3(re, input, match) cregex_find_4(re, input, match, CREG_DEFAULT) +int cregex_find_4(const cregex* re, const char* input, csview match[], int mflags); + +/* find with csview as input. */ +STC_INLINE int cregex_find_sv(const cregex* re, csview input, csview match[]) { csview *mp = NULL; if (match) { match[0] = input; mp = match; } return cregex_find(re, input.str, mp, CREG_M_STARTEND); } /* match + compile RE pattern */ -int cregex_find_pattern(const char* pattern, const char* input, - csview match[], int cmflags); - -static inline -bool cregex_is_match(const cregex* re, const char* input) - { return cregex_find(re, input, NULL, CREG_DEFAULT) == CREG_OK; } - -/* replace regular expression */ -cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, +#define cregex_find_pattern(...) c_MACRO_OVERLOAD(cregex_find_pattern, __VA_ARGS__) +#define cregex_find_pattern_3(pattern, input, match) \ + cregex_find_pattern_4(pattern, input, match, CREG_DEFAULT) +int cregex_find_pattern_4(const char* pattern, const char* input, + csview match[], int cmflags); + +STC_INLINE bool cregex_is_match(const cregex* re, const char* input) + { return cregex_find_4(re, input, NULL, CREG_DEFAULT) == CREG_OK; } + +/* replace csview input with replace using regular expression pattern */ +#define cregex_replace_sv(...) c_MACRO_OVERLOAD(cregex_replace_sv, __VA_ARGS__) +#define cregex_replace_sv_3(pattern, input, replace) \ + cregex_replace_sv_4(pattern, input, replace, INT32_MAX) +#define cregex_replace_sv_4(pattern, input, replace, count) \ + cregex_replace_sv_6(pattern, input, replace, count, NULL, CREG_DEFAULT) +cstr cregex_replace_sv_6(const cregex* re, csview input, const char* replace, int count, bool (*mfun)(int i, csview match, cstr* mstr), int rflags); -static inline -cstr cregex_replace(const cregex* re, const char* input, const char* replace) { - csview sv = {input, strlen(input)}; - return cregex_replace_sv(re, sv, replace, ~0U, NULL, CREG_DEFAULT); +/* replace input with replace using regular expression */ +#define cregex_replace(...) c_MACRO_OVERLOAD(cregex_replace, __VA_ARGS__) +#define cregex_replace_3(re, input, replace) cregex_replace_4(re, input, replace, INT32_MAX) + +STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char* replace, int count) { + csview sv = {input, c_strlen(input)}; + return cregex_replace_sv_4(re, sv, replace, count); } /* replace + compile RE pattern, and extra arguments */ -cstr cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count, - bool (*mfun)(int i, csview match, cstr* mstr), int crflags); -static inline -cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace) - { return cregex_replace_pattern_ex(pattern, input, replace, ~0U, NULL, CREG_DEFAULT); } +#define cregex_replace_pattern(...) c_MACRO_OVERLOAD(cregex_replace_pattern, __VA_ARGS__) +#define cregex_replace_pattern_3(pattern, input, replace) \ + cregex_replace_pattern_4(pattern, input, replace, INT32_MAX) +#define cregex_replace_pattern_4(pattern, input, replace, count) \ + cregex_replace_pattern_6(pattern, input, replace, count, NULL, CREG_DEFAULT) +cstr cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, int count, + bool (*mfun)(int i, csview match, cstr* mstr), int crflags); /* destroy regex */ -void cregex_drop(cregex* self); +void cregex_drop(cregex* re); #endif // CREGEX_H_INCLUDED #if defined i_extern || defined STC_EXTERN -# include "../src/cregex.c" -# include "../src/utf8code.c" +# include "../../src/cregex.c" +# include "../../src/utf8code.c" #endif #undef i_opt #undef i_header diff --git a/include/stc/csmap.h b/include/stc/csmap.h index e5df0fe2..0f21f954 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -73,6 +73,9 @@ int main(void) { #define _i_SET_ONLY c_false #define _i_keyref(vp) (&(vp)->first) #endif +#ifndef i_size + #define i_size int32_t +#endif #include "priv/template.h" #ifndef i_cmp_functor #define i_cmp_functor(self, x, y) i_cmp(x, y) @@ -107,7 +110,7 @@ STC_API _cx_self _cx_memb(_init)(void); STC_API _cx_result _cx_memb(_insert)(_cx_self* self, i_key key _i_MAP_ONLY(, i_val mapped)); STC_API _cx_result _cx_memb(_push)(_cx_self* self, _cx_value _val); STC_API void _cx_memb(_drop)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); +STC_API bool _cx_memb(_reserve)(_cx_self* self, int64_t cap); STC_API _cx_value* _cx_memb(_find_it)(const _cx_self* self, _cx_rawkey rkey, _cx_iter* out); STC_API _cx_iter _cx_memb(_lower_bound)(const _cx_self* self, _cx_rawkey rkey); STC_API _cx_value* _cx_memb(_front)(const _cx_self* self); @@ -118,8 +121,8 @@ STC_API _cx_iter _cx_memb(_erase_range)(_cx_self* self, _cx_iter it1, _cx STC_API void _cx_memb(_next)(_cx_iter* it); STC_INLINE bool _cx_memb(_empty)(const _cx_self* cx) { return cx->size == 0; } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } +STC_INLINE int64_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } +STC_INLINE int64_t _cx_memb(_capacity)(const _cx_self* cx) { return cx->cap; } STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { _cx_iter it; _cx_memb(_find_it)(self, rkey, &it); return it; } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) @@ -130,7 +133,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey) { _cx_iter it; return _cx_memb(_find_it)(self, rkey, &it); } STC_INLINE _cx_self -_cx_memb(_with_capacity)(const size_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -143,7 +146,7 @@ _cx_memb(_clear)(_cx_self* self) STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) - _i_MAP_ONLY( c_INIT(_cx_raw){i_keyto((&val->first)), + _i_MAP_ONLY( c_LITERAL(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } @@ -209,7 +212,7 @@ _cx_memb(_end)(const _cx_self* self) { } STC_INLINE _cx_iter -_cx_memb(_advance)(_cx_iter it, size_t n) { +_cx_memb(_advance)(_cx_iter it, uint64_t n) { while (n-- && it.ref) _cx_memb(_next)(&it); return it; @@ -225,13 +228,13 @@ _cx_memb(_init)(void) { } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t cap) { - if (cap <= self->cap) +_cx_memb(_reserve)(_cx_self* self, const int64_t cap) { + if ((i_size)cap <= self->cap) return false; - _cx_node* nodes = (_cx_node*)c_REALLOC(self->nodes, (cap + 1)*sizeof(_cx_node)); + _cx_node* nodes = (_cx_node*)c_realloc(self->nodes, (cap + 1)*c_sizeof(_cx_node)); if (!nodes) return false; - nodes[0] = c_INIT(_cx_node){{0, 0}, 0}; + nodes[0] = c_LITERAL(_cx_node){{0, 0}, 0}; self->nodes = nodes; self->cap = (i_size)cap; return true; @@ -294,6 +297,22 @@ _cx_memb(_push)(_cx_self* self, _cx_value _val) { return _res; } +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, int64_t n) { + while (n--) +#if defined _i_isset && defined i_no_emplace + _cx_memb(_insert)(self, *raw++); +#elif defined _i_isset + _cx_memb(_emplace)(self, *raw++); +#elif defined i_no_emplace + _cx_memb(_insert_or_assign)(self, raw->first, raw->second), ++raw; +#else + _cx_memb(_emplace_or_assign)(self, raw->first, raw->second), ++raw; +#endif +} + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } + #ifndef _i_isset STC_DEF _cx_result _cx_memb(_insert_or_assign)(_cx_self* self, i_key _key, i_val _mapped) { @@ -562,7 +581,7 @@ STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (self->cap) { _cx_memb(_drop_r_)(self->nodes, self->root); - c_FREE(self->nodes); + c_free(self->nodes); } } diff --git a/include/stc/cspan.h b/include/stc/cspan.h index a4c8f5cb..68a13522 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -30,28 +30,27 @@ using_cspan(Intspan, int, 1); int demo1() { float raw[4*5]; - Span2f ms = cspan_make(raw, 4, 5); + Span2f ms = cspan_md(raw, 4, 5); - for (size_t i=0; i<ms.dim[0]; i++) - for (size_t j=0; j<ms.dim[1]; j++) + for (int i=0; i<ms.shape[0]; i++) + for (int j=0; j<ms.shape[1]; j++) *cspan_at(&ms, i, j) = i*1000 + j; printf("%f\n", *cspan_at(&ms, 3, 4)); } int demo2() { - int array[] = {1, 2, 3, 4, 5}; + int array[] = {10, 20, 30, 23, 22, 21}; Intspan span = cspan_from_array(array); c_FOREACH (i, Intspan, span) printf(" %d", *i.ref); puts(""); - // use a temporary Intspan object. - c_FORFILTER (i, Intspan, cspan_literal(Intspan, {10, 20, 30, 23, 22, 21}) - , c_FLT_SKIPWHILE(i, *i.ref < 25) + c_FORFILTER (i, Intspan, span, + , c_flt_skipwhile(i, *i.ref < 25) && (*i.ref & 1) == 0 // even only - , c_FLT_TAKE(i, 2)) // break after 2 + , c_flt_take(i, 2)) // break after 2 printf(" %d", *i.ref); puts(""); } @@ -61,102 +60,190 @@ int demo2() { #include "ccommon.h" -#define using_cspan(Self, T, RANK) \ - typedef T Self##_value, Self##_raw; \ - typedef struct { Self##_value *ref, *end; } Self##_iter; \ - typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ +#define using_cspan(...) c_MACRO_OVERLOAD(using_cspan, __VA_ARGS__) +#define using_cspan_2(Self, T) using_cspan_3(Self, T, 1) +#define using_cspan_3(Self, T, RANK) \ + typedef T Self##_value; typedef T Self##_raw; \ + typedef struct { \ + Self##_value *data; \ + int32_t shape[RANK]; \ + cspan_idx##RANK stride; \ + } Self; \ + typedef struct { Self##_value *ref; int32_t pos[RANK]; const Self *_s; } Self##_iter; \ \ + STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ + return (Self){.data=raw, .shape={(int32_t)n}}; \ + } \ + STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t shape[], const int32_t stri[], \ + const int rank, const int32_t a[][2]) { \ + Self s = {.data=v}; int outrank; \ + s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ + c_ASSERT(outrank == RANK); \ + return s; \ + } \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ - size_t n = cspan_size(self); \ - Self##_iter it = {n ? self->data : NULL, self->data + n}; \ + Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \ return it; \ } \ STC_INLINE Self##_iter Self##_end(const Self* self) { \ - Self##_iter it = {NULL, self->data + cspan_size(self)}; \ + Self##_iter it = {.ref=NULL}; \ return it; \ } \ - STC_INLINE void Self##_next(Self##_iter* it) \ - { if (++it->ref == it->end) it->ref = NULL; } \ + STC_INLINE void Self##_next(Self##_iter* it) { \ + it->ref += _cspan_next##RANK(RANK, it->pos, it->_s->shape, it->_s->stride.d); \ + if (it->pos[0] == it->_s->shape[0]) it->ref = NULL; \ + } \ struct stc_nostruct -#define using_cspan2(Self, T) using_cspan(Self, T, 1); using_cspan(Self##2, T, 2) -#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan(Self##3, T, 3) -#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan(Self##4, T, 4) - -#define cspan_rank_ok(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank) - -#define cspan_make(array, ...) \ - {.data=array, .dim={__VA_ARGS__}} - +#define using_cspan2(Self, T) using_cspan_3(Self, T, 1); using_cspan_3(Self##2, T, 2) +#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan_3(Self##3, T, 3) +#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan_3(Self##4, T, 4) +typedef struct { int32_t d[1]; } cspan_idx1; +typedef struct { int32_t d[2]; } cspan_idx2; +typedef struct { int32_t d[3]; } cspan_idx3; +typedef struct { int32_t d[4]; } cspan_idx4; +typedef struct { int32_t d[5]; } cspan_idx5; +typedef struct { int32_t d[6]; } cspan_idx6; +#define c_END -1 +#define c_ALL 0,-1 + +#define cspan_md(array, ...) \ + {.data=array, .shape={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} + +/* For static initialization, use cspan_make(). c_make() for non-static only. */ +#define cspan_make(SpanType, ...) \ + {.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} + +#define cspan_slice(OutSpan, parent, ...) \ + OutSpan##_slice_((parent)->data, (parent)->shape, (parent)->stride.d, cspan_rank(parent) + \ + c_static_assert(cspan_rank(parent) == sizeof((int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2])), \ + (const int32_t[][2]){__VA_ARGS__}) + /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ - {.data=(container)->data, .dim={(container)->_len}} + {.data=(container)->data, .shape={(int32_t)(container)->_len}} #define cspan_from_array(array) \ - {.data=(array) + c_STATIC_ASSERT(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}} - -#define cspan_from_list(ValueType, ...) \ - {.data=(ValueType[])__VA_ARGS__, .dim={sizeof((ValueType[])__VA_ARGS__)/sizeof(ValueType)}} - -#define cspan_literal(SpanType, ...) \ - (c_INIT(SpanType)cspan_from_list(SpanType##_value, __VA_ARGS__)) - -#define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) -#define cspan_rank(self) c_ARRAYLEN((self)->dim) -#define cspan_index(self, ...) \ - c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) + \ - cspan_rank_ok(self, c_NUMARGS(__VA_ARGS__)) - -#define cspan_resize(self, ...) \ - (void)memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \ - sizeof((self)->dim) + cspan_rank_ok(self, c_NUMARGS(__VA_ARGS__))) + {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .shape={c_ARRAYLEN(array)}} + +#define cspan_size(self) _cspan_size((self)->shape, cspan_rank(self)) +#define cspan_rank(self) c_ARRAYLEN((self)->shape) + +#define cspan_index(self, ...) c_PASTE(cspan_idx_, c_NUMARGS(__VA_ARGS__))(self, __VA_ARGS__) +#define cspan_idx_1 cspan_idx_4 +#define cspan_idx_2 cspan_idx_4 +#define cspan_idx_3 cspan_idx_4 +#define cspan_idx_4(self, ...) \ + c_PASTE(_cspan_idx, c_NUMARGS(__VA_ARGS__))((self)->shape, (self)->stride, __VA_ARGS__) // small/fast +#define cspan_idx_5(self, ...) \ + (_cspan_idxN(c_NUMARGS(__VA_ARGS__), (self)->shape, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ + c_static_assert(cspan_rank(self) == c_NUMARGS(__VA_ARGS__))) // general +#define cspan_idx_6 cspan_idx_5 #define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__)) #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) +// cspan_subspanN. (N<4) Optimized, same as e.g. cspan_slice(Span3, &ms3, {off,off+count}, {c_ALL}, {c_ALL}); #define cspan_subspan(self, offset, count) \ - {.data=cspan_at(self, offset), .dim={count}} + {.data=cspan_at(self, offset), .shape={count}} #define cspan_subspan2(self, offset, count) \ - {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[1]}} + {.data=cspan_at(self, offset, 0), .shape={count, (self)->shape[1]}, .stride={(self)->stride}} #define cspan_subspan3(self, offset, count) \ - {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}} -#define cspan_subspan4(self, offset, count) \ - {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}} - -#define cspan_at4(...) c_MACRO_OVERLOAD(cspan_at4, __VA_ARGS__) -#define cspan_at3(...) c_MACRO_OVERLOAD(cspan_at3, __VA_ARGS__) -#define cspan_at2(self, x) \ - {.data=cspan_at(self, x, 0), .dim={(self)->dim[1]}} -#define cspan_at32(self, x) \ - {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}} -#define cspan_at33(self, x, y) \ - {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} -#define cspan_at42(self, x) \ - {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}} -#define cspan_at43(self, x, y) \ - {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}} -#define cspan_at44(self, x, y, z) \ - {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} - -STC_INLINE size_t _cspan_i1(const uint32_t dim[1], uint32_t x) - { c_ASSERT(x < dim[0]); return x; } - -STC_INLINE size_t _cspan_i2(const uint32_t dim[2], uint32_t x, uint32_t y) - { c_ASSERT(x < dim[0] && y < dim[1]); return dim[1]*x + y; } - -STC_INLINE size_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) { - c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]); - return dim[2]*(dim[1]*x + y) + z; + {.data=cspan_at(self, offset, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride={(self)->stride}} + +// cspan_submdN: reduce rank (N<5) Optimized, same as e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL}); +#define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__) +#define cspan_submd3(...) c_MACRO_OVERLOAD(cspan_submd3, __VA_ARGS__) +#define cspan_submd2(self, x) \ + {.data=cspan_at(self, x, 0), .shape={(self)->shape[1]}} +#define cspan_submd3_2(self, x) \ + {.data=cspan_at(self, x, 0, 0), .shape={(self)->shape[1], (self)->shape[2]}, \ + .stride={.d={0, (self)->stride.d[2]}}} +#define cspan_submd3_3(self, x, y) \ + {.data=cspan_at(self, x, y, 0), .shape={(self)->shape[2]}} +#define cspan_submd4_2(self, x) \ + {.data=cspan_at(self, x, 0, 0, 0), .shape={(self)->shape[1], (self)->shape[2], (self)->shape[3]}, \ + .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3]}}} +#define cspan_submd4_3(self, x, y) \ + {.data=cspan_at(self, x, y, 0, 0), .shape={(self)->shape[2], (self)->shape[3]}, .stride={.d={0, (self)->stride.d[3]}}} +#define cspan_submd4_4(self, x, y, z) \ + {.data=cspan_at(self, x, y, z, 0), .shape={(self)->shape[3]}} + +// FUNCTIONS + +STC_INLINE intptr_t _cspan_idx1(const int32_t shape[1], const cspan_idx1 stri, int32_t x) + { c_ASSERT(c_LTu(x, shape[0])); return x; } + +STC_INLINE intptr_t _cspan_idx2(const int32_t shape[2], const cspan_idx2 stri, int32_t x, int32_t y) + { c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1])); return (intptr_t)stri.d[1]*x + y; } + +STC_INLINE intptr_t _cspan_idx3(const int32_t shape[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { + c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1]) && c_LTu(z, shape[2])); + return (intptr_t)stri.d[2]*(stri.d[1]*x + y) + z; } -STC_INLINE size_t _cspan_i4(const uint32_t dim[4], uint32_t x, uint32_t y, uint32_t z, uint32_t w) { - c_ASSERT(x < dim[0] && y < dim[1] && z < dim[3] && w < dim[3]); - return dim[3]*(dim[2]*(dim[1]*x + y) + z) + w; +STC_INLINE intptr_t _cspan_idx4(const int32_t shape[4], const cspan_idx4 stri, int32_t x, int32_t y, + int32_t z, int32_t w) { + c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1]) && c_LTu(z, shape[2]) && c_LTu(w, shape[3])); + return (intptr_t)stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w; } -STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { - size_t sz = dim[0]; - while (rank-- > 1) sz *= dim[rank]; +STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t shape[], const int32_t stri[], const int32_t a[]) { + intptr_t off = a[0]; + bool ok = c_LTu(a[0], shape[0]); + for (int i = 1; i < rank; ++i) { + off *= stri[i]; + off += a[i]; + ok &= c_LTu(a[i], shape[i]); + } + c_ASSERT(ok); + return off; +} + +STC_INLINE intptr_t _cspan_size(const int32_t shape[], int rank) { + intptr_t sz = shape[0]; + while (rank-- > 1) sz *= shape[rank]; return sz; } +#define _cspan_next1(r, pos, d, s) (++pos[0], 1) +#define _cspan_next3 _cspan_next2 +#define _cspan_next4 _cspan_next2 +#define _cspan_next5 _cspan_next2 +#define _cspan_next6 _cspan_next2 + +STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t shape[], const int32_t stride[]) { + intptr_t off = 1, rs = 1; + ++pos[rank - 1]; + while (--rank && pos[rank] == shape[rank]) { + pos[rank] = 0, ++pos[rank - 1]; + const intptr_t ds = rs*shape[rank]; + rs *= stride[rank]; + off += rs - ds; + } + return off; +} + +STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, + const int32_t shape[], const int32_t stri[], + int rank, const int32_t a[][2]) { + intptr_t off = 0; + int i = 0, j = 0, ok = true; + int32_t t, s = 1; + for (; i < rank; ++i) { + off *= stri[i]; + off += a[i][0]; + switch (a[i][1]) { + case 0: s *= stri[i]; ok &= c_LTu(a[i][0], shape[i]); continue; + case -1: t = shape[i]; break; + default: t = a[i][1]; break; + } + odim[j] = t - a[i][0]; + ostri[j] = s*stri[i]; + ok &= c_LTu(0, odim[j]) & !c_LTu(shape[i], t); + s = 1; ++j; + } + *orank = j; + c_ASSERT(ok); + return off; +} #endif diff --git a/include/stc/cstack.h b/include/stc/cstack.h index f4a86156..dd4686e9 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -44,11 +44,11 @@ typedef i_keyraw _cx_raw; STC_INLINE _cx_self _cx_memb(_init)(void) { - _cx_self s; s._len = 0; + _cx_self cx; cx._len = 0; #ifndef i_capacity - s._cap = 0; s.data = NULL; + cx._cap = 0; cx.data = NULL; #endif - return s; + return cx; } #ifdef i_capacity @@ -58,13 +58,13 @@ STC_INLINE void _cx_memb(_create)(_cx_self* self) STC_INLINE void _cx_memb(_create)(_cx_self* self) { self->_len = 0; self->_cap = 0; self->data = NULL; } -STC_INLINE _cx_self _cx_memb(_with_capacity)(size_t cap) { - _cx_self out = {(_cx_value *) c_MALLOC(cap*sizeof(i_key)), 0, cap}; +STC_INLINE _cx_self _cx_memb(_with_capacity)(intptr_t cap) { + _cx_self out = {(_cx_value *) c_malloc(cap*c_sizeof(i_key)), 0, cap}; return out; } -STC_INLINE _cx_self _cx_memb(_with_size)(size_t size, i_key null) { - _cx_self out = {(_cx_value *) c_MALLOC(size*sizeof null), size, size}; +STC_INLINE _cx_self _cx_memb(_with_size)(intptr_t size, i_key null) { + _cx_self out = {(_cx_value *) c_malloc(size*c_sizeof null), size, size}; while (size) out.data[--size] = null; return out; } @@ -79,17 +79,17 @@ STC_INLINE void _cx_memb(_clear)(_cx_self* self) { STC_INLINE void _cx_memb(_drop)(_cx_self* self) { _cx_memb(_clear)(self); #ifndef i_capacity - c_FREE(self->data); + c_free(self->data); #endif } -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { #ifndef i_capacity return self->_cap; #else @@ -97,18 +97,17 @@ STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { #endif } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { +STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, intptr_t n) { if (n < self->_len) return true; #ifndef i_capacity - _cx_value *t = (_cx_value *)c_REALLOC(self->data, n*sizeof *t); + _cx_value *t = (_cx_value *)c_realloc(self->data, n*c_sizeof *t); if (t) { self->_cap = n, self->data = t; return true; } #endif return false; } -STC_INLINE _cx_value* -_cx_memb(_append_uninit)(_cx_self *self, size_t n) { - size_t len = self->_len; +STC_INLINE _cx_value* _cx_memb(_append_uninit)(_cx_self *self, intptr_t n) { + intptr_t len = self->_len; if (!_cx_memb(_reserve)(self, len + n)) return NULL; self->_len += n; return self->data + len; @@ -137,9 +136,15 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) { STC_INLINE void _cx_memb(_pop)(_cx_self* self) { assert(!_cx_memb(_empty)(self)); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } + +STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, intptr_t idx) { assert(idx < self->_len); return self->data + idx; } -STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) +STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, intptr_t idx) { assert(idx < self->_len); return self->data + idx; } #if !defined i_no_emplace @@ -149,9 +154,9 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #if !defined i_no_clone STC_INLINE _cx_self _cx_memb(_clone)(_cx_self v) { - _cx_self out = {(_cx_value *)c_MALLOC(v._len*sizeof(_cx_value)), v._len, v._len}; + _cx_self out = {(_cx_value *)c_malloc(v._len*c_sizeof(_cx_value)), v._len, v._len}; if (!out.data) out._cap = 0; - else for (size_t i = 0; i < v._len; ++v.data) + else for (intptr_t i = 0; i < v._len; ++v.data) out.data[i++] = i_keyclone((*v.data)); return out; } @@ -170,17 +175,17 @@ STC_INLINE i_keyraw _cx_memb(_value_toraw)(const _cx_value* val) #endif // !i_no_clone STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - return c_INIT(_cx_iter){self->_len ? (_cx_value*)self->data : NULL, + return c_LITERAL(_cx_iter){self->_len ? (_cx_value*)self->data : NULL, (_cx_value*)self->data + self->_len}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL, (_cx_value*)self->data + self->_len}; } + { return c_LITERAL(_cx_iter){NULL, (_cx_value*)self->data + self->_len}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) { if ((it.ref += n) >= it.end) it.ref = NULL ; return it; } #include "priv/template.h" diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 46ad2fcd..2966db81 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -47,79 +47,79 @@ #endif enum { cstr_s_cap = sizeof(cstr_buf) - 2 }; -#define cstr_s_size(s) ((size_t)(s)->sml.size) +#define cstr_s_size(s) ((intptr_t)(s)->sml.size) #define cstr_s_set_size(s, len) ((s)->sml.size = (uint8_t)(len), (s)->sml.data[len] = 0) #define cstr_s_data(s) (s)->sml.data #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 cstr_l_cap(s) (intptr_t)(~byte_rotl_((s)->lon.ncap, sizeof((s)->lon.ncap) - 1)) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~byte_rotl_((size_t)(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) (intptr_t)(~(s)->lon.ncap) + #define cstr_l_set_cap(s, cap) ((s)->lon.ncap = ~(size_t)(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_size(s) (intptr_t)((s)->lon.size) +#define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (size_t)(len)] = 0) #define cstr_l_data(s) (s)->lon.data -#define cstr_l_drop(s) c_FREE((s)->lon.data) +#define cstr_l_drop(s) c_free((s)->lon.data) #define cstr_is_long(s) ((s)->sml.size > 127) -STC_API char* _cstr_init(cstr* self, size_t len, size_t cap); -STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); +STC_API char* _cstr_init(cstr* self, intptr_t len, intptr_t cap); +STC_API char* _cstr_internal_move(cstr* self, intptr_t pos1, intptr_t pos2); /**************************** PUBLIC API **********************************/ #define cstr_lit(literal) cstr_from_n(literal, crawstr_len(literal)) -#define cstr_NULL (c_INIT(cstr){{{0}, 0}}) +#define cstr_NULL (c_LITERAL(cstr){{{0}, 0}}) #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); -STC_API void cstr_resize(cstr* self, size_t size, char value); -STC_API size_t cstr_find_at(const cstr* self, size_t pos, const char* search); -STC_API char* cstr_assign_n(cstr* self, const char* str, size_t len); -STC_API char* cstr_append_n(cstr* self, const char* str, size_t len); -STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); -STC_API void cstr_erase(cstr* self, size_t pos, size_t len); -STC_API void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); -STC_API cstr cstr_from_fmt(const char* fmt, ...); -STC_API size_t cstr_append_fmt(cstr* self, const char* fmt, ...); -STC_API size_t cstr_printf(cstr* self, const char* fmt, ...); -STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, unsigned count); +STC_API char* cstr_reserve(cstr* self, intptr_t cap); +STC_API void cstr_shrink_to_fit(cstr* self); +STC_API void cstr_resize(cstr* self, intptr_t size, char value); +STC_API intptr_t cstr_find_at(const cstr* self, intptr_t pos, const char* search); +STC_API char* cstr_assign_n(cstr* self, const char* str, intptr_t len); +STC_API char* cstr_append_n(cstr* self, const char* str, intptr_t len); +STC_API bool cstr_getdelim(cstr *self, int delim, FILE *fp); +STC_API void cstr_erase(cstr* self, intptr_t pos, intptr_t len); +STC_API void cstr_u8_erase(cstr* self, intptr_t bytepos, intptr_t u8len); +STC_API cstr cstr_from_fmt(const char* fmt, ...); +STC_API intptr_t cstr_append_fmt(cstr* self, const char* fmt, ...); +STC_API intptr_t cstr_printf(cstr* self, const char* fmt, ...); +STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, int32_t count); STC_INLINE cstr_buf cstr_buffer(cstr* s) { return cstr_is_long(s) - ? c_INIT(cstr_buf){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} - : c_INIT(cstr_buf){s->sml.data, cstr_s_size(s), cstr_s_cap}; + ? c_LITERAL(cstr_buf){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} + : c_LITERAL(cstr_buf){s->sml.data, cstr_s_size(s), cstr_s_cap}; } STC_INLINE csview cstr_sv(const cstr* s) { - return cstr_is_long(s) ? c_INIT(csview){s->lon.data, cstr_l_size(s)} - : c_INIT(csview){s->sml.data, cstr_s_size(s)}; + return cstr_is_long(s) ? c_LITERAL(csview){s->lon.data, cstr_l_size(s)} + : c_LITERAL(csview){s->sml.data, cstr_s_size(s)}; } STC_INLINE cstr cstr_init(void) { return cstr_NULL; } -STC_INLINE cstr cstr_from_n(const char* str, const size_t len) { +STC_INLINE cstr cstr_from_n(const char* str, const intptr_t len) { cstr s; - memcpy(_cstr_init(&s, len, len), str, len); + c_memcpy(_cstr_init(&s, len, len), str, len); return s; } STC_INLINE cstr cstr_from(const char* str) - { return cstr_from_n(str, strlen(str)); } + { return cstr_from_n(str, c_strlen(str)); } STC_INLINE cstr cstr_from_sv(csview sv) { return cstr_from_n(sv.str, sv.size); } -STC_INLINE cstr cstr_with_size(const size_t size, const char value) { +STC_INLINE cstr cstr_with_size(const intptr_t size, const char value) { cstr s; - memset(_cstr_init(&s, size, size), value, size); + c_memset(_cstr_init(&s, size, size), value, size); return s; } -STC_INLINE cstr cstr_with_capacity(const size_t cap) { +STC_INLINE cstr cstr_with_capacity(const intptr_t cap) { cstr s; _cstr_init(&s, 0, cap); return s; @@ -150,7 +150,7 @@ STC_INLINE void cstr_drop(cstr* self) { #define SSO_CALL(s, call) (cstr_is_long(s) ? cstr_l_##call : cstr_s_##call) -STC_INLINE void _cstr_set_size(cstr* self, size_t len) +STC_INLINE void _cstr_set_size(cstr* self, intptr_t len) { SSO_CALL(self, set_size(self, len)); } STC_INLINE char* cstr_data(cstr* self) @@ -162,10 +162,10 @@ STC_INLINE const char* cstr_str(const cstr* self) STC_INLINE bool cstr_empty(const cstr* self) { return self->sml.size == 0; } -STC_INLINE size_t cstr_size(const cstr* self) +STC_INLINE intptr_t cstr_size(const cstr* self) { return SSO_CALL(self, size(self)); } -STC_INLINE size_t cstr_capacity(const cstr* self) +STC_INLINE intptr_t cstr_capacity(const cstr* self) { return cstr_is_long(self) ? cstr_l_cap(self) : cstr_s_cap; } // utf8 methods defined in/depending on src/utf8code.c: @@ -182,10 +182,10 @@ STC_INLINE cstr cstr_toupper_sv(csview sv) { return cstr_tocase(sv, 2); } STC_INLINE cstr cstr_tolower(const char* str) - { return cstr_tolower_sv(c_SV(str, strlen(str))); } + { return cstr_tolower_sv(c_SV(str, c_strlen(str))); } STC_INLINE cstr cstr_toupper(const char* str) - { return cstr_toupper_sv(c_SV(str, strlen(str))); } + { return cstr_toupper_sv(c_SV(str, c_strlen(str))); } STC_INLINE void cstr_lowercase(cstr* self) { cstr_take(self, cstr_tolower_sv(cstr_sv(self))); } @@ -198,19 +198,19 @@ STC_INLINE bool cstr_valid_utf8(const cstr* self) // other utf8 -STC_INLINE size_t cstr_u8_size(const cstr* self) +STC_INLINE intptr_t cstr_u8_size(const cstr* self) { return utf8_size(cstr_str(self)); } -STC_INLINE size_t cstr_u8_size_n(const cstr* self, size_t nbytes) +STC_INLINE intptr_t cstr_u8_size_n(const cstr* self, intptr_t nbytes) { return utf8_size_n(cstr_str(self), nbytes); } -STC_INLINE size_t cstr_u8_to_pos(const cstr* self, size_t u8idx) +STC_INLINE intptr_t cstr_u8_to_pos(const cstr* self, intptr_t u8idx) { return utf8_pos(cstr_str(self), u8idx); } -STC_INLINE const char* cstr_u8_at(const cstr* self, size_t u8idx) +STC_INLINE const char* cstr_u8_at(const cstr* self, intptr_t u8idx) { return utf8_at(cstr_str(self), u8idx); } -STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) { +STC_INLINE csview cstr_u8_chr(const cstr* self, intptr_t u8idx) { const char* str = cstr_str(self); csview sv; sv.str = utf8_at(str, u8idx); @@ -222,11 +222,11 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, size_t u8idx) { STC_INLINE cstr_iter cstr_begin(const cstr* self) { csview sv = cstr_sv(self); - if (!sv.size) return c_INIT(cstr_iter){NULL}; - return c_INIT(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}}}; + if (!sv.size) return c_LITERAL(cstr_iter){NULL}; + return c_LITERAL(cstr_iter){.u8 = {{sv.str, utf8_chr_size(sv.str)}}}; } STC_INLINE cstr_iter cstr_end(const cstr* self) { - (void)self; return c_INIT(cstr_iter){NULL}; + (void)self; return c_LITERAL(cstr_iter){NULL}; } STC_INLINE void cstr_next(cstr_iter* it) { it->ref += it->u8.chr.size; @@ -246,8 +246,8 @@ STC_INLINE cstr_iter cstr_advance(cstr_iter it, intptr_t pos) { STC_INLINE void cstr_clear(cstr* self) { _cstr_set_size(self, 0); } -STC_INLINE char* cstr_append_uninit(cstr *self, size_t len) { - size_t sz = cstr_size(self); +STC_INLINE char* cstr_append_uninit(cstr *self, intptr_t len) { + intptr_t sz = cstr_size(self); char* d = cstr_reserve(self, sz + len); if (!d) return NULL; _cstr_set_size(self, sz + len); @@ -262,7 +262,7 @@ STC_INLINE int cstr_icmp(const cstr* s1, const cstr* s2) STC_INLINE bool cstr_eq(const cstr* s1, const cstr* s2) { csview x = cstr_sv(s1), y = cstr_sv(s2); - return x.size == y.size && !memcmp(x.str, y.str, x.size); + return x.size == y.size && !c_memcmp(x.str, y.str, x.size); } @@ -270,7 +270,7 @@ STC_INLINE bool cstr_equals(const cstr* self, const char* str) { return !strcmp(cstr_str(self), str); } STC_INLINE bool cstr_equals_sv(const cstr* self, csview sv) - { return sv.size == cstr_size(self) && !memcmp(cstr_str(self), sv.str, sv.size); } + { return sv.size == cstr_size(self) && !c_memcmp(cstr_str(self), sv.str, sv.size); } STC_INLINE bool cstr_equals_s(const cstr* self, cstr s) { return !cstr_cmp(self, &s); } @@ -279,14 +279,14 @@ STC_INLINE bool cstr_iequals(const cstr* self, const char* str) { return !utf8_icmp(cstr_str(self), str); } -STC_INLINE size_t cstr_find(const cstr* self, const char* search) { +STC_INLINE intptr_t cstr_find(const cstr* self, const char* search) { const char *str = cstr_str(self), *res = strstr((char*)str, search); - return res ? (size_t)(res - str) : c_NPOS; + return res ? (res - str) : c_NPOS; } -STC_API size_t cstr_find_sv(const cstr* self, csview search); +STC_API intptr_t cstr_find_sv(const cstr* self, csview search); -STC_INLINE size_t cstr_find_s(const cstr* self, cstr search) +STC_INLINE intptr_t cstr_find_s(const cstr* self, cstr search) { return cstr_find(self, cstr_str(&search)); } @@ -302,7 +302,7 @@ STC_INLINE bool cstr_contains_s(const cstr* self, cstr search) STC_INLINE bool cstr_starts_with_sv(const cstr* self, csview sub) { if (sub.size > cstr_size(self)) return false; - return !memcmp(cstr_str(self), sub.str, sub.size); + return !c_memcmp(cstr_str(self), sub.str, sub.size); } STC_INLINE bool cstr_starts_with(const cstr* self, const char* sub) { @@ -316,7 +316,7 @@ STC_INLINE bool cstr_starts_with_s(const cstr* self, cstr sub) STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) { csview sv = cstr_sv(self); - size_t len = strlen(sub); + intptr_t len = c_strlen(sub); return len <= sv.size && !utf8_icmp_sv(sv, c_SV(sub, len)); } @@ -324,24 +324,24 @@ STC_INLINE bool cstr_istarts_with(const cstr* self, const char* sub) { STC_INLINE bool cstr_ends_with_sv(const cstr* self, csview sub) { csview sv = cstr_sv(self); if (sub.size > sv.size) return false; - return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); + return !c_memcmp(sv.str + sv.size - sub.size, sub.str, sub.size); } STC_INLINE bool cstr_ends_with_s(const cstr* self, cstr sub) { return cstr_ends_with_sv(self, cstr_sv(&sub)); } STC_INLINE bool cstr_ends_with(const cstr* self, const char* sub) - { return cstr_ends_with_sv(self, c_SV(sub, strlen(sub))); } + { return cstr_ends_with_sv(self, c_SV(sub, c_strlen(sub))); } STC_INLINE bool cstr_iends_with(const cstr* self, const char* sub) { csview sv = cstr_sv(self); - size_t n = strlen(sub); + intptr_t n = c_strlen(sub); return n <= sv.size && !utf8_icmp(sv.str + sv.size - n, sub); } STC_INLINE char* cstr_assign(cstr* self, const char* str) - { return cstr_assign_n(self, str, strlen(str)); } + { return cstr_assign_n(self, str, c_strlen(str)); } STC_INLINE char* cstr_assign_sv(cstr* self, csview sv) { return cstr_assign_n(self, sv.str, sv.size); } @@ -359,11 +359,11 @@ STC_INLINE void cstr_pop(cstr* self) { csview sv = cstr_sv(self); const char* s = sv.str + sv.size; while ((*--s & 0xC0) == 0x80) ; - _cstr_set_size(self, (size_t)(s - sv.str)); + _cstr_set_size(self, (s - sv.str)); } STC_INLINE char* cstr_append(cstr* self, const char* str) - { return cstr_append_n(self, str, strlen(str)); } + { return cstr_append_n(self, str, c_strlen(str)); } STC_INLINE void cstr_append_sv(cstr* self, csview sv) { cstr_append_n(self, sv.str, sv.size); } @@ -373,35 +373,35 @@ STC_INLINE char* cstr_append_s(cstr* self, cstr s) { return cstr_append_n(self, sv.str, sv.size); } -STC_INLINE void cstr_replace_ex(cstr* self, const char* search, const char* repl, unsigned count) { - cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(search, strlen(search)), - c_SV(repl, strlen(repl)), count)); +#define cstr_replace(...) c_MACRO_OVERLOAD(cstr_replace, __VA_ARGS__) +#define cstr_replace_3(self, search, repl) cstr_replace_4(self, search, repl, INT32_MAX) +STC_INLINE void cstr_replace_4(cstr* self, const char* search, const char* repl, int32_t count) { + cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(search, c_strlen(search)), + c_SV(repl, c_strlen(repl)), count)); } -STC_INLINE void cstr_replace(cstr* self, const char* search, const char* repl) - { cstr_replace_ex(self, search, repl, ~0U); } -STC_INLINE void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl) { +STC_INLINE void cstr_replace_at_sv(cstr* self, intptr_t pos, intptr_t len, const csview repl) { char* d = _cstr_internal_move(self, pos + len, pos + repl.size); - memcpy(d + pos, repl.str, repl.size); + c_memcpy(d + pos, repl.str, repl.size); } -STC_INLINE void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* repl) - { cstr_replace_at_sv(self, pos, len, c_SV(repl, strlen(repl))); } +STC_INLINE void cstr_replace_at(cstr* self, intptr_t pos, intptr_t len, const char* repl) + { cstr_replace_at_sv(self, pos, len, c_SV(repl, c_strlen(repl))); } -STC_INLINE void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl) +STC_INLINE void cstr_replace_at_s(cstr* self, intptr_t pos, intptr_t len, cstr repl) { cstr_replace_at_sv(self, pos, len, cstr_sv(&repl)); } -STC_INLINE void cstr_u8_replace_at(cstr* self, size_t bytepos, size_t u8len, csview repl) +STC_INLINE void cstr_u8_replace_at(cstr* self, intptr_t bytepos, intptr_t u8len, csview repl) { cstr_replace_at_sv(self, bytepos, utf8_pos(cstr_str(self) + bytepos, u8len), repl); } -STC_INLINE void cstr_insert(cstr* self, size_t pos, const char* str) - { cstr_replace_at_sv(self, pos, 0, c_SV(str, strlen(str))); } +STC_INLINE void cstr_insert(cstr* self, intptr_t pos, const char* str) + { cstr_replace_at_sv(self, pos, 0, c_SV(str, c_strlen(str))); } -STC_INLINE void cstr_insert_sv(cstr* self, size_t pos, csview sv) +STC_INLINE void cstr_insert_sv(cstr* self, intptr_t pos, csview sv) { cstr_replace_at_sv(self, pos, 0, sv); } -STC_INLINE void cstr_insert_s(cstr* self, size_t pos, cstr s) { +STC_INLINE void cstr_insert_s(cstr* self, intptr_t pos, cstr s) { csview sv = cstr_sv(&s); cstr_replace_at_sv(self, pos, 0, sv); } @@ -425,7 +425,7 @@ cstr cstr_tocase(csview sv, int k) { cstr out = cstr_init(); char *buf = cstr_reserve(&out, sv.size*3/2); const char *end = sv.str + sv.size; - uint32_t cp; size_t sz = 0; + uint32_t cp; intptr_t sz = 0; utf8_decode_t d = {.state=0}; while (sv.str < end) { @@ -451,27 +451,27 @@ STC_DEF uint64_t cstr_hash(const cstr *self) { return cfasthash(sv.str, sv.size); } -STC_DEF size_t cstr_find_sv(const cstr* self, csview search) { +STC_DEF intptr_t cstr_find_sv(const cstr* self, csview search) { csview sv = cstr_sv(self); char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } -STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t pos2) { +STC_DEF char* _cstr_internal_move(cstr* self, const intptr_t pos1, const intptr_t pos2) { cstr_buf r = cstr_buffer(self); if (pos1 != pos2) { - const intptr_t newlen = (intptr_t)(r.size + pos2 - pos1); - if (newlen > (intptr_t)r.cap) + const intptr_t newlen = (r.size + pos2 - pos1); + if (newlen > r.cap) r.data = cstr_reserve(self, r.size*3/2 + pos2 - pos1); - memmove(&r.data[pos2], &r.data[pos1], r.size - pos1); - _cstr_set_size(self, (size_t)newlen); + c_memmove(&r.data[pos2], &r.data[pos1], r.size - pos1); + _cstr_set_size(self, newlen); } return r.data; } -STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_t cap) { +STC_DEF char* _cstr_init(cstr* self, const intptr_t len, const intptr_t cap) { if (cap > cstr_s_cap) { - self->lon.data = (char *)c_MALLOC(cap + 1); + self->lon.data = (char *)c_malloc(cap + 1); cstr_l_set_size(self, len); cstr_l_set_cap(self, cap); return self->lon.data; @@ -485,67 +485,67 @@ STC_DEF void cstr_shrink_to_fit(cstr* self) { if (r.size == r.cap) return; if (r.size > cstr_s_cap) { - self->lon.data = (char *)c_REALLOC(self->lon.data, r.size + 1); + self->lon.data = (char *)c_realloc(self->lon.data, r.size + 1); cstr_l_set_cap(self, r.size); } else if (r.cap > cstr_s_cap) { - memcpy(self->sml.data, r.data, r.size + 1); + c_memcpy(self->sml.data, r.data, r.size + 1); cstr_s_set_size(self, r.size); - c_FREE(r.data); + c_free(r.data); } } -STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { +STC_DEF char* cstr_reserve(cstr* self, const intptr_t cap) { if (cstr_is_long(self)) { if (cap > cstr_l_cap(self)) { - self->lon.data = (char *)c_REALLOC(self->lon.data, cap + 1); + self->lon.data = (char *)c_realloc(self->lon.data, cap + 1); cstr_l_set_cap(self, cap); } return self->lon.data; } /* from short to long: */ if (cap > cstr_s_cap) { - char* data = (char *)c_MALLOC(cap + 1); - const size_t len = cstr_s_size(self); - memcpy(data, self->sml.data, cstr_s_cap + 1); + char* data = (char *)c_malloc(cap + 1); + const intptr_t len = cstr_s_size(self); + c_memcpy(data, self->sml.data, cstr_s_cap + 1); self->lon.data = data; - self->lon.size = len; + self->lon.size = (size_t)len; cstr_l_set_cap(self, cap); return data; } return self->sml.data; } -STC_DEF void cstr_resize(cstr* self, const size_t size, const char value) { +STC_DEF void cstr_resize(cstr* self, const intptr_t size, const char value) { cstr_buf r = cstr_buffer(self); if (size > r.size) { if (size > r.cap) r.data = cstr_reserve(self, size); - memset(r.data + r.size, value, size - r.size); + c_memset(r.data + r.size, value, size - r.size); } _cstr_set_size(self, size); } -STC_DEF size_t cstr_find_at(const cstr* self, const size_t pos, const char* search) { +STC_DEF intptr_t cstr_find_at(const cstr* self, const intptr_t pos, const char* search) { csview sv = cstr_sv(self); if (pos > sv.size) return c_NPOS; const char* res = strstr((char*)sv.str + pos, search); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } -STC_DEF char* cstr_assign_n(cstr* self, const char* str, const size_t len) { +STC_DEF char* cstr_assign_n(cstr* self, const char* str, const intptr_t len) { char* d = cstr_reserve(self, len); - memmove(d, str, len); + c_memmove(d, str, len); _cstr_set_size(self, len); return d; } -STC_DEF char* cstr_append_n(cstr* self, const char* str, const size_t len) { +STC_DEF char* cstr_append_n(cstr* self, const char* str, const intptr_t len) { cstr_buf r = cstr_buffer(self); if (r.size + len > r.cap) { const size_t off = (size_t)(str - r.data); r.data = cstr_reserve(self, r.size*3/2 + len); - if (off <= r.size) str = r.data + off; /* handle self append */ + if (off <= (size_t)r.size) str = r.data + off; /* handle self append */ } - memcpy(r.data + r.size, str, len); + c_memcpy(r.data + r.size, str, len); _cstr_set_size(self, r.size + len); return r.data; } @@ -554,7 +554,7 @@ STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { int c = fgetc(fp); if (c == EOF) return false; - size_t pos = 0; + intptr_t pos = 0; cstr_buf r = cstr_buffer(self); for (;;) { if (c == delim || c == EOF) { @@ -571,13 +571,13 @@ STC_DEF bool cstr_getdelim(cstr *self, const int delim, FILE *fp) { } STC_DEF cstr -cstr_replace_sv(csview in, csview search, csview repl, unsigned count) { +cstr_replace_sv(csview in, csview search, csview repl, int32_t count) { cstr out = cstr_NULL; - size_t from = 0; char* res; - if (!count) count = ~0U; + intptr_t from = 0; char* res; + if (!count) count = INT32_MAX; if (search.size) while (count-- && (res = cstrnstrn(in.str + from, search.str, in.size - from, search.size))) { - const size_t pos = (size_t)(res - in.str); + const intptr_t pos = (res - in.str); cstr_append_n(&out, in.str + from, pos - from); cstr_append_n(&out, repl.str, repl.size); from = pos + search.size; @@ -586,17 +586,17 @@ cstr_replace_sv(csview in, csview search, csview repl, unsigned count) { return out; } -STC_DEF void cstr_erase(cstr* self, const size_t pos, size_t len) { +STC_DEF void cstr_erase(cstr* self, const intptr_t pos, intptr_t len) { cstr_buf r = cstr_buffer(self); if (len > r.size - pos) len = r.size - pos; - memmove(&r.data[pos], &r.data[pos + len], r.size - (pos + len)); + c_memmove(&r.data[pos], &r.data[pos + len], r.size - (pos + len)); _cstr_set_size(self, r.size - len); } -STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len) { +STC_DEF void cstr_u8_erase(cstr* self, const intptr_t bytepos, const intptr_t u8len) { cstr_buf r = cstr_buffer(self); - size_t len = utf8_pos(r.data + bytepos, u8len); - memmove(&r.data[bytepos], &r.data[bytepos + len], r.size - (bytepos + len)); + intptr_t len = utf8_pos(r.data + bytepos, u8len); + c_memmove(&r.data[bytepos], &r.data[bytepos + len], r.size - (bytepos + len)); _cstr_set_size(self, r.size - len); } @@ -608,10 +608,10 @@ STC_DEF void cstr_u8_erase(cstr* self, const size_t bytepos, const size_t u8len) # pragma warning(disable: 4996) #endif -STC_DEF size_t cstr_vfmt(cstr* self, size_t start, const char* fmt, va_list args) { +STC_DEF intptr_t cstr_vfmt(cstr* self, intptr_t start, const char* fmt, va_list args) { va_list args2; va_copy(args2, args); - const size_t n = (size_t)vsnprintf(NULL, (size_t)0, fmt, args); + const int n = vsnprintf(NULL, 0ULL, fmt, args); vsprintf(cstr_reserve(self, start + n) + start, fmt, args2); va_end(args2); _cstr_set_size(self, start + n); @@ -632,19 +632,19 @@ STC_DEF cstr cstr_from_fmt(const char* fmt, ...) { return s; } -STC_DEF size_t cstr_append_fmt(cstr* self, const char* fmt, ...) { +STC_DEF intptr_t cstr_append_fmt(cstr* self, const char* fmt, ...) { va_list args; va_start(args, fmt); - const size_t n = cstr_vfmt(self, cstr_size(self), fmt, args); + const intptr_t n = cstr_vfmt(self, cstr_size(self), fmt, args); va_end(args); return n; } /* NB! self-data in args is UB */ -STC_DEF size_t cstr_printf(cstr* self, const char* fmt, ...) { +STC_DEF intptr_t cstr_printf(cstr* self, const char* fmt, ...) { va_list args; va_start(args, fmt); - const size_t n = cstr_vfmt(self, 0, fmt, args); + const intptr_t n = cstr_vfmt(self, 0, fmt, args); va_end(args); return n; } diff --git a/include/stc/csview.h b/include/stc/csview.h index 1ab34d9a..a30672cd 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -27,48 +27,48 @@ #include "forward.h" #include "utf8.h" -#define csview_NULL c_SV1("") +#define csview_NULL c_SV_1("") #define csview_init() csview_NULL #define csview_drop(p) c_default_drop(p) #define csview_clone(sv) c_default_clone(sv) -#define csview_lit(literal) c_SV1(literal) -#define csview_from_n(str, n) c_SV2(str, n) +#define csview_lit(literal) c_SV_1(literal) +#define csview_from_n(str, n) c_SV_2(str, n) -STC_API size_t csview_find_sv(csview sv, csview search); +STC_API intptr_t csview_find_sv(csview sv, csview search); STC_INLINE csview csview_from(const char* str) - { return c_INIT(csview){str, strlen(str)}; } + { return c_LITERAL(csview){str, c_strlen(str)}; } STC_INLINE void csview_clear(csview* self) { *self = csview_NULL; } -STC_INLINE size_t csview_size(csview sv) { return sv.size; } +STC_INLINE intptr_t csview_size(csview sv) { return sv.size; } STC_INLINE bool csview_empty(csview sv) { return sv.size == 0; } STC_INLINE bool csview_equals(csview sv, const char* str) - { size_t n = strlen(str); return sv.size == n && !memcmp(sv.str, str, n); } + { intptr_t n = c_strlen(str); return sv.size == n && !c_memcmp(sv.str, str, n); } -STC_INLINE size_t csview_find(csview sv, const char* str) - { return csview_find_sv(sv, c_SV(str, strlen(str))); } +STC_INLINE intptr_t csview_find(csview sv, const char* str) + { return csview_find_sv(sv, c_SV(str, c_strlen(str))); } STC_INLINE bool csview_contains(csview sv, const char* str) { return csview_find(sv, str) != c_NPOS; } STC_INLINE bool csview_starts_with(csview sv, const char* str) { - size_t n = strlen(str); - return n > sv.size ? false : !memcmp(sv.str, str, n); + intptr_t n = c_strlen(str); + return n > sv.size ? false : !c_memcmp(sv.str, str, n); } STC_INLINE bool csview_ends_with(csview sv, const char* str) { - size_t n = strlen(str); - return n > sv.size ? false : !memcmp(sv.str + sv.size - n, str, n); + intptr_t n = c_strlen(str); + return n > sv.size ? false : !c_memcmp(sv.str + sv.size - n, str, n); } -STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n) { +STC_INLINE csview csview_substr(csview sv, intptr_t pos, intptr_t n) { if (pos + n > sv.size) n = sv.size - pos; sv.str += pos, sv.size = n; return sv; } -STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) { +STC_INLINE csview csview_slice(csview sv, intptr_t p1, intptr_t p2) { if (p2 > sv.size) p2 = sv.size; sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0; return sv; @@ -76,12 +76,12 @@ STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) { /* utf8 iterator */ STC_INLINE csview_iter csview_begin(const csview* self) { - if (!self->size) return c_INIT(csview_iter){NULL}; - return c_INIT(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, - self->str + self->size}}; + if (!self->size) return c_LITERAL(csview_iter){NULL}; + return c_LITERAL(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, + self->str + self->size}}; } STC_INLINE csview_iter csview_end(const csview* self) { - return c_INIT(csview_iter){.u8 = {{NULL}, self->str + self->size}}; + return c_LITERAL(csview_iter){.u8 = {{NULL}, self->str + self->size}}; } STC_INLINE void csview_next(csview_iter* it) { it->ref += it->u8.chr.size; @@ -99,10 +99,10 @@ STC_INLINE csview_iter csview_advance(csview_iter it, intptr_t pos) { /* utf8 */ -STC_INLINE size_t csview_u8_size(csview sv) +STC_INLINE intptr_t csview_u8_size(csview sv) { return utf8_size_n(sv.str, sv.size); } -STC_INLINE csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len) { +STC_INLINE csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len) { sv.str += bytepos; sv.size = utf8_pos(sv.str, u8len); return sv; @@ -111,12 +111,12 @@ STC_INLINE csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len) { STC_INLINE bool csview_valid_utf8(csview sv) // depends on src/utf8code.c { return utf8_valid_n(sv.str, sv.size); } -STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n); +STC_API csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); -STC_API csview csview_token(csview sv, const char* sep, size_t* start); +STC_API csview csview_token(csview sv, const char* sep, intptr_t* start); #define c_FORTOKEN_SV(it, inputsv, sep) \ - for (struct { csview _inp, token, *ref; const char *_sep; size_t pos; } \ + for (struct { csview _inp, token, *ref; const char *_sep; intptr_t pos; } \ it = {._inp=inputsv, .token=it._inp, .ref=&it.token, ._sep=sep} \ ; it.pos <= it._inp.size && (it.token = csview_token(it._inp, it._sep, &it.pos)).str ; ) @@ -126,27 +126,27 @@ STC_API csview csview_token(csview sv, const char* sep, size_t* start); /* csview interaction with cstr: */ #ifdef CSTR_H_INCLUDED -STC_INLINE csview cstr_substr(const cstr* self, size_t pos, size_t n) +STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, intptr_t n) { return csview_substr(cstr_sv(self), pos, n); } -STC_INLINE csview cstr_slice(const cstr* self, size_t p1, size_t p2) +STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice(cstr_sv(self), p1, p2); } -STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t n) +STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, intptr_t n) { return csview_substr_ex(cstr_sv(self), pos, n); } STC_INLINE csview cstr_slice_ex(const cstr* self, intptr_t p1, intptr_t p2) { return csview_slice_ex(cstr_sv(self), p1, p2); } -STC_INLINE csview cstr_u8_substr(const cstr* self , size_t bytepos, size_t u8len) +STC_INLINE csview cstr_u8_substr(const cstr* self , intptr_t bytepos, intptr_t u8len) { return csview_u8_substr(cstr_sv(self), bytepos, u8len); } #endif /* ---- Container helper functions ---- */ STC_INLINE int csview_cmp(const csview* x, const csview* y) { - size_t n = x->size < y->size ? x->size : y->size; - int c = memcmp(x->str, y->str, n); + intptr_t n = x->size < y->size ? x->size : y->size; + int c = c_memcmp(x->str, y->str, n); return c ? c : (int)(x->size - y->size); } @@ -154,48 +154,48 @@ STC_INLINE int csview_icmp(const csview* x, const csview* y) { return utf8_icmp_sv(*x, *y); } STC_INLINE bool csview_eq(const csview* x, const csview* y) - { return x->size == y->size && !memcmp(x->str, y->str, x->size); } + { return x->size == y->size && !c_memcmp(x->str, y->str, x->size); } STC_API uint64_t csview_hash(const csview *self); /* -------------------------- IMPLEMENTATION ------------------------- */ #if defined(i_implement) -STC_DEF size_t csview_find_sv(csview sv, csview search) { +STC_DEF intptr_t csview_find_sv(csview sv, csview search) { char* res = cstrnstrn(sv.str, search.str, sv.size, search.size); - return res ? (size_t)(res - sv.str) : c_NPOS; + return res ? (res - sv.str) : c_NPOS; } STC_DEF uint64_t csview_hash(const csview *self) { return cfasthash(self->str, self->size); } -STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, size_t n) { +STC_DEF csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n) { if (pos < 0) { - pos += (intptr_t)sv.size; + pos += sv.size; if (pos < 0) pos = 0; } - if ((size_t)pos > sv.size) pos = (intptr_t)sv.size; - if ((size_t)pos + n > sv.size) n = sv.size - (size_t)pos; + if (pos > sv.size) pos = sv.size; + if (pos + n > sv.size) n = sv.size - pos; sv.str += pos, sv.size = n; return sv; } STC_DEF csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) { if (p1 < 0) { - p1 += (intptr_t)sv.size; + p1 += sv.size; if (p1 < 0) p1 = 0; } - if (p2 < 0) p2 += (intptr_t)sv.size; - if (p2 > (intptr_t)sv.size) p2 = (intptr_t)sv.size; - sv.str += p1, sv.size = (size_t)(p2 > p1 ? p2 - p1 : 0); + if (p2 < 0) p2 += sv.size; + if (p2 > sv.size) p2 = sv.size; + sv.str += p1, sv.size = (p2 > p1 ? p2 - p1 : 0); return sv; } -STC_DEF csview csview_token(csview sv, const char* sep, size_t* start) { - size_t sep_size = strlen(sep); +STC_DEF csview csview_token(csview sv, const char* sep, intptr_t* start) { + intptr_t sep_size = c_strlen(sep); csview slice = {sv.str + *start, sv.size - *start}; const char* res = cstrnstrn(slice.str, sep, slice.size, sep_size); - csview tok = {slice.str, res ? (size_t)(res - slice.str) : slice.size}; + csview tok = {slice.str, res ? (res - slice.str) : slice.size}; *start += tok.size + sep_size; return tok; } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index fbacd305..06664b4d 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -80,13 +80,13 @@ typedef i_keyraw _cx_raw; STC_API _cx_self _cx_memb(_init)(void); STC_API void _cx_memb(_drop)(_cx_self* self); STC_API void _cx_memb(_clear)(_cx_self* self); -STC_API bool _cx_memb(_reserve)(_cx_self* self, size_t cap); -STC_API bool _cx_memb(_resize)(_cx_self* self, size_t size, i_key null); +STC_API bool _cx_memb(_reserve)(_cx_self* self, intptr_t cap); +STC_API bool _cx_memb(_resize)(_cx_self* self, intptr_t size, i_key null); STC_API _cx_value* _cx_memb(_push)(_cx_self* self, i_key value); STC_API _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2); STC_API _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); -STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n); +STC_API _cx_iter _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n); #if !defined i_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, _cx_raw raw); @@ -101,7 +101,7 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw) { return _cx_memb(_push)(self, i_keyfrom(raw)); } STC_INLINE _cx_iter -_cx_memb(_emplace_n)(_cx_self* self, const size_t idx, const _cx_raw arr[], const size_t n) { +_cx_memb(_emplace_n)(_cx_self* self, const intptr_t idx, const _cx_raw arr[], const intptr_t n) { return _cx_memb(_emplace_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -114,6 +114,10 @@ _cx_memb(_emplace_at)(_cx_self* self, _cx_iter it, _cx_raw raw) { STC_API _cx_self _cx_memb(_clone)(_cx_self cx); STC_API _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2); +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { @@ -124,8 +128,8 @@ STC_INLINE void _cx_memb(_copy)(_cx_self* self, const _cx_self* other) { } #endif // !i_no_clone -STC_INLINE size_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } -STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } +STC_INLINE intptr_t _cx_memb(_size)(const _cx_self* self) { return self->_len; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* self) { return self->_cap; } STC_INLINE bool _cx_memb(_empty)(const _cx_self* self) { return !self->_len; } STC_INLINE _cx_raw _cx_memb(_value_toraw)(const _cx_value* val) { return i_keyto(val); } STC_INLINE _cx_value* _cx_memb(_front)(const _cx_self* self) { return self->data; } @@ -138,14 +142,14 @@ STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, i_key value) STC_INLINE void _cx_memb(_pop_back)(_cx_self* self) { _cx_memb(_pop)(self); } STC_INLINE _cx_self -_cx_memb(_with_size)(const size_t size, i_key null) { +_cx_memb(_with_size)(const intptr_t size, i_key null) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_resize)(&cx, size, null); return cx; } STC_INLINE _cx_self -_cx_memb(_with_capacity)(const size_t cap) { +_cx_memb(_with_capacity)(const intptr_t cap) { _cx_self cx = _cx_memb(_init)(); _cx_memb(_reserve)(&cx, cap); return cx; @@ -158,11 +162,11 @@ _cx_memb(_shrink_to_fit)(_cx_self* self) { STC_INLINE _cx_iter -_cx_memb(_insert)(_cx_self* self, const size_t idx, i_key value) { +_cx_memb(_insert)(_cx_self* self, const intptr_t idx, i_key value) { return _cx_memb(_insert_range)(self, self->data + idx, &value, &value + 1); } STC_INLINE _cx_iter -_cx_memb(_insert_n)(_cx_self* self, const size_t idx, const _cx_value arr[], const size_t n) { +_cx_memb(_insert_n)(_cx_self* self, const intptr_t idx, const _cx_value arr[], const intptr_t n) { return _cx_memb(_insert_range)(self, self->data + idx, arr, arr + n); } STC_INLINE _cx_iter @@ -171,7 +175,7 @@ _cx_memb(_insert_at)(_cx_self* self, _cx_iter it, i_key value) { } STC_INLINE _cx_iter -_cx_memb(_erase_n)(_cx_self* self, const size_t idx, const size_t n) { +_cx_memb(_erase_n)(_cx_self* self, const intptr_t idx, const intptr_t n) { return _cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n); } STC_INLINE _cx_iter @@ -184,31 +188,31 @@ _cx_memb(_erase_range)(_cx_self* self, _cx_iter i1, _cx_iter i2) { } STC_INLINE const _cx_value* -_cx_memb(_at)(const _cx_self* self, const size_t idx) { +_cx_memb(_at)(const _cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_value* -_cx_memb(_at_mut)(_cx_self* self, const size_t idx) { +_cx_memb(_at_mut)(_cx_self* self, const intptr_t idx) { assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { - size_t n = self->_len; - return c_INIT(_cx_iter){n ? self->data : NULL, self->data + n}; + intptr_t n = self->_len; + return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_INIT(_cx_iter){NULL, self->data + self->_len}; } + { return c_LITERAL(_cx_iter){NULL, self->data + self->_len}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } -STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) +STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, size_t n) { if ((it.ref += n) >= it.end) it.ref = NULL; return it; } -STC_INLINE size_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) - { return (size_t)(it.ref - self->data); } +STC_INLINE intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) + { return (it.ref - self->data); } #if !defined i_no_cmp @@ -255,7 +259,7 @@ _cx_memb(_sort)(_cx_self* self) { STC_DEF _cx_self _cx_memb(_init)(void) { - return c_INIT(_cx_self){NULL}; + return c_LITERAL(_cx_self){NULL}; } STC_DEF void @@ -273,13 +277,13 @@ _cx_memb(_drop)(_cx_self* self) { if (self->_cap == 0) return; _cx_memb(_clear)(self); - c_FREE(self->data); + c_free(self->data); } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t cap) { +_cx_memb(_reserve)(_cx_self* self, const intptr_t cap) { if (cap > self->_cap || (cap && cap == self->_len)) { - _cx_value* d = (_cx_value*)c_REALLOC(self->data, cap*sizeof(i_key)); + _cx_value* d = (_cx_value*)c_realloc(self->data, cap*c_sizeof(i_key)); if (!d) return false; self->data = d; @@ -289,13 +293,13 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { } STC_DEF bool -_cx_memb(_resize)(_cx_self* self, const size_t len, i_key null) { +_cx_memb(_resize)(_cx_self* self, const intptr_t len, i_key null) { if (!_cx_memb(_reserve)(self, len)) return false; - const size_t n = self->_len; - for (size_t i = len; i < n; ++i) + const intptr_t n = self->_len; + for (intptr_t i = len; i < n; ++i) { i_keydrop((self->data + i)); } - for (size_t i = n; i < len; ++i) + for (intptr_t i = n; i < len; ++i) self->data[i] = null; self->_len = len; return true; @@ -312,39 +316,39 @@ _cx_memb(_push)(_cx_self* self, i_key value) { } STC_DEF _cx_iter -_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { +_cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const intptr_t n) { if (n) { if (!pos) pos = self->data + self->_len; - const size_t idx = (size_t)(pos - self->data); + const intptr_t idx = (pos - self->data); if (self->_len + n > self->_cap) { if (!_cx_memb(_reserve)(self, self->_len*3/2 + n)) return _cx_memb(_end)(self); pos = self->data + idx; } - memmove(pos + n, pos, (self->_len - idx)*sizeof *pos); + c_memmove(pos + n, pos, (self->_len - idx)*c_sizeof *pos); self->_len += n; } - return c_INIT(_cx_iter){pos, self->data + self->_len}; + return c_LITERAL(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_iter _cx_memb(_insert_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) - memcpy(it.ref, p1, (size_t)(p2 - p1)*sizeof *p1); + c_memcpy(it.ref, p1, (p2 - p1)*c_sizeof *p1); return it; } STC_DEF _cx_iter _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { - size_t len = (size_t)(p2 - p1); + intptr_t len = (p2 - p1); _cx_value* p = p1, *end = self->data + self->_len; for (; p != p2; ++p) { i_keydrop(p); } - memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); + c_memmove(p1, p2, (end - p2)*c_sizeof *p1); self->_len -= len; - return c_INIT(_cx_iter){p2 == end ? NULL : p1, end - len}; + return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone @@ -358,7 +362,7 @@ _cx_memb(_clone)(_cx_self cx) { STC_DEF _cx_iter _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, const _cx_value* p1, const _cx_value* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyclone((*p1)); @@ -370,7 +374,7 @@ _cx_memb(_copy_range)(_cx_self* self, _cx_value* pos, STC_DEF _cx_iter _cx_memb(_emplace_range)(_cx_self* self, _cx_value* pos, const _cx_raw* p1, const _cx_raw* p2) { - _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (size_t)(p2 - p1)); + _cx_iter it = _cx_memb(_insert_uninit)(self, pos, (p2 - p1)); if (it.ref) for (_cx_value* p = it.ref; p1 != p2; ++p1) *p++ = i_keyfrom((*p1)); @@ -394,20 +398,19 @@ _cx_memb(_find_in)(_cx_iter i1, _cx_iter i2, _cx_raw raw) { STC_DEF _cx_iter _cx_memb(_binary_search_in)(_cx_iter i1, _cx_iter i2, const _cx_raw raw, _cx_iter* lower_bound) { - const _cx_value* p2 = _it2_ptr(i1, i2); + _cx_value* w[2] = {i1.ref, _it2_ptr(i1, i2)}; _cx_iter mid = i1; - while (i1.ref != p2) { - mid.ref = i1.ref + (p2 - i1.ref)/2; + while (w[0] != w[1]) { + mid.ref = w[0] + (w[1] - w[0])/2; const _cx_raw m = i_keyto(mid.ref); const int c = i_cmp((&raw), (&m)); if (!c) return *lower_bound = mid; - else if (c < 0) p2 = mid.ref; - else i1.ref = mid.ref + 1; + w[c < 0] = mid.ref + (c > 0); } - i2.ref = NULL; - *lower_bound = i1.ref == i2.end ? i2 : i1; - return i2; + i1.ref = w[0] == i2.end ? NULL : w[0]; + *lower_bound = i1; + i1.ref = NULL; return i1; } STC_DEF int diff --git a/include/stc/forward.h b/include/stc/forward.h index 60c18c37..d08b71b4 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -23,34 +23,34 @@ #ifndef STC_FORWARD_H_INCLUDED #define STC_FORWARD_H_INCLUDED -#include <stddef.h> +#include <stdint.h> #define forward_carc(CX, VAL) _c_carc_types(CX, VAL) -#define forward_carr2(CX, VAL) _c_carr2_types(CX, VAL) -#define forward_carr3(CX, VAL) _c_carr3_types(CX, VAL) #define forward_cbox(CX, VAL) _c_cbox_types(CX, VAL) #define forward_cdeq(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_clist(CX, VAL) _c_clist_types(CX, VAL) -#define forward_cmap(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, uint32_t, c_true, c_false) -#define forward_cmap_huge(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, size_t, c_true, c_false) -#define forward_cset(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, uint32_t, c_false, c_true) -#define forward_cset_huge(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, size_t, c_false, c_true) -#define forward_csmap(CX, KEY, VAL) _c_aatree_types(CX, KEY, VAL, uint32_t, c_true, c_false) -#define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, uint32_t, c_false, c_true) +#define forward_cmap(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, int32_t, c_true, c_false) +#define forward_cmap64(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, int64_t, c_true, c_false) +#define forward_cset(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, int32_t, c_false, c_true) +#define forward_cset64(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, int64_t, c_false, c_true) +#define forward_csmap(CX, KEY, VAL) _c_aatree_types(CX, KEY, VAL, int32_t, c_true, c_false) +#define forward_csset(CX, KEY) _c_aatree_types(CX, KEY, KEY, int32_t, c_false, c_true) #define forward_cstack(CX, VAL) _c_cstack_types(CX, VAL) #define forward_cpque(CX, VAL) _c_cpque_types(CX, VAL) #define forward_cqueue(CX, VAL) _c_cdeq_types(CX, VAL) #define forward_cvec(CX, VAL) _c_cvec_types(CX, VAL) +// csview typedef const char csview_value; -typedef struct { csview_value* str; size_t size; } csview; +typedef struct { csview_value* str; intptr_t size; } csview; typedef union { csview_value* ref; struct { csview chr; csview_value* end; } u8; } csview_iter; +// cstr typedef char cstr_value; -typedef struct { cstr_value* data; size_t size, cap; } cstr_buf; +typedef struct { cstr_value* data; intptr_t size, cap; } cstr_buf; typedef union { struct { cstr_value data[sizeof(cstr_buf) - 1]; unsigned char size; } sml; struct { cstr_value* data; size_t size, ncap; } lon; @@ -71,16 +71,6 @@ typedef union { catomic_long* use_count; \ } SELF -#define _c_carr2_types(SELF, VAL) \ - typedef VAL SELF##_value; \ - typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value **data; size_t xdim, ydim; } SELF - -#define _c_carr3_types(SELF, VAL) \ - typedef VAL SELF##_value; \ - typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value ***data; size_t xdim, ydim, zdim; } SELF - #define _c_cbox_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { \ @@ -90,7 +80,7 @@ typedef union { #define _c_cdeq_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value *_base, *data; size_t _len, _cap; } SELF + typedef struct { SELF##_value *_base, *data; intptr_t _len, _cap; } SELF #define _c_clist_types(SELF, VAL) \ typedef VAL SELF##_value; \ @@ -108,7 +98,7 @@ typedef union { #define _c_chash_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ + typedef SZ SELF##_sizet; \ \ typedef SET_ONLY( SELF##_key ) \ MAP_ONLY( struct SELF##_value ) \ @@ -127,40 +117,13 @@ typedef union { typedef struct { \ SELF##_value* table; \ uint8_t* _hashx; \ - SELF##_size_t size, bucket_count; \ + SELF##_sizet size, bucket_count; \ } SELF -#if defined STC_CSMAP_V1 -#define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ - typedef KEY SELF##_key; \ - typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ - typedef struct SELF##_node SELF##_node; \ -\ - typedef SET_ONLY( SELF##_key ) \ - MAP_ONLY( struct SELF##_value ) \ - SELF##_value; \ -\ - typedef struct { \ - SELF##_value *ref; \ - bool inserted, nomem_error; \ - } SELF##_result; \ -\ - typedef struct { \ - SELF##_value *ref; \ - int _top; \ - SELF##_node *_tn, *_st[36]; \ - } SELF##_iter; \ -\ - typedef struct { \ - SELF##_node *root; \ - SELF##_size_t size; \ - } SELF -#else #define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ - typedef SZ SELF##_size_t; \ + typedef SZ SELF##_sizet; \ typedef struct SELF##_node SELF##_node; \ \ typedef SET_ONLY( SELF##_key ) \ @@ -176,32 +139,31 @@ typedef union { SELF##_value *ref; \ SELF##_node *_d; \ int _top; \ - SELF##_size_t _tn, _st[36]; \ + SELF##_sizet _tn, _st[36]; \ } SELF##_iter; \ \ typedef struct { \ SELF##_node *nodes; \ - SELF##_size_t root, disp, head, size, cap; \ + SELF##_sizet root, disp, head, size, cap; \ } SELF -#endif #define _c_cstack_fixed(SELF, VAL, CAP) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value data[CAP]; size_t _len; } SELF + typedef struct { SELF##_value data[CAP]; intptr_t _len; } SELF #define _c_cstack_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value* data; size_t _len, _cap; } SELF + typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF #define _c_cvec_types(SELF, VAL) \ typedef VAL SELF##_value; \ typedef struct { SELF##_value *ref, *end; } SELF##_iter; \ - typedef struct { SELF##_value *data; size_t _len, _cap; } SELF + typedef struct { SELF##_value *data; intptr_t _len, _cap; } SELF #define _c_cpque_types(SELF, VAL) \ typedef VAL SELF##_value; \ - typedef struct { SELF##_value* data; size_t _len, _cap; } SELF + typedef struct { SELF##_value* data; intptr_t _len, _cap; } SELF #endif // STC_FORWARD_H_INCLUDED diff --git a/include/stc/priv/lowcase.h b/include/stc/priv/altnames.h index 8fdadd75..024868c2 100644 --- a/include/stc/priv/lowcase.h +++ b/include/stc/priv/altnames.h @@ -23,14 +23,6 @@ #define c_alloc c_ALLOC #define c_alloc_n c_ALLOC_N #define c_new c_NEW -#define c_malloc c_MALLOC -#define c_calloc c_CALLOC -#define c_realloc c_REALLOC -#define c_free c_FREE -#define c_delete c_DELETE -#define c_swap c_SWAP -#define c_container_of c_CONTAINER_OF -#define c_static_assert c_STATIC_ASSERT #define c_arraylen c_ARRAYLEN #define c_forlist c_FORLIST #define c_forrange c_FORRANGE @@ -45,14 +37,4 @@ #define c_with c_WITH #define c_scope c_SCOPE #define c_defer c_DEFER -#define c_drop c_DROP #define c_sv c_SV -#define c_ARGsv c_ARGSV -#define c_find_if c_FIND_IF -#define c_erase_if c_ERASE_IF -#define c_flt_take c_FLT_TAKE -#define c_flt_skip c_FLT_SKIP -#define c_flt_skipwhile c_FLT_SKIPWHILE -#define c_flt_takewhile c_FLT_TAKEWHILE -#define cstr_new(lit) cstr_lit(lit) -#define cstr_null cstr_NULL diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 9634fe2c..6eb82bcb 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -44,11 +44,8 @@ #define _i_prefix #endif -#ifdef i_size - #define _i_expandby 2 -#else - #define i_size uint32_t - #define _i_expandby 1 +#ifndef i_size + #define i_size intptr_t #endif #if !(defined i_key || defined i_key_str || defined i_key_ssv || \ diff --git a/include/stc/utf8.h b/include/stc/utf8.h index f30e76ac..da738fc6 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -23,9 +23,9 @@ extern uint32_t utf8_tolower(uint32_t c); extern uint32_t utf8_toupper(uint32_t c); extern bool utf8_iscased(uint32_t c); extern bool utf8_isword(uint32_t c); -extern bool utf8_valid_n(const char* s, size_t nbytes); +extern bool utf8_valid_n(const char* s, intptr_t nbytes); extern int utf8_icmp_sv(csview s1, csview s2); -extern unsigned utf8_encode(char *out, uint32_t c); +extern int utf8_encode(char *out, uint32_t c); extern uint32_t utf8_peek_off(const char *s, int offset); STC_INLINE bool utf8_isupper(uint32_t c) @@ -35,7 +35,7 @@ STC_INLINE bool utf8_islower(uint32_t c) { return utf8_toupper(c) != c; } STC_INLINE bool utf8_isalnum(uint32_t c) { - if (c < 128) return isalnum(c) != 0; + if (c < 128) return isalnum((int)c) != 0; return utf8_isalpha(c) || utf8_isgroup(U8G_Nd, c); } @@ -45,7 +45,7 @@ STC_INLINE bool utf8_isblank(uint32_t c) { } STC_INLINE bool utf8_isspace(uint32_t c) { - if (c < 128) return isspace(c) != 0; + if (c < 128) return isspace((int)c) != 0; return ((c == 8232) | (c == 8233)) || utf8_isgroup(U8G_Zs, c); } @@ -68,17 +68,17 @@ STC_INLINE uint32_t utf8_peek(const char* s) { /* case-insensitive utf8 string comparison */ STC_INLINE int utf8_icmp(const char* s1, const char* s2) { - return utf8_icmp_sv(c_SV(s1, ~(size_t)0), c_SV(s2, ~(size_t)0)); + return utf8_icmp_sv(c_SV(s1, INTPTR_MAX), c_SV(s2, INTPTR_MAX)); } STC_INLINE bool utf8_valid(const char* s) { - return utf8_valid_n(s, ~(size_t)0); + return utf8_valid_n(s, INTPTR_MAX); } /* following functions are independent but assume valid utf8 strings: */ /* number of bytes in the utf8 codepoint from s */ -STC_INLINE unsigned utf8_chr_size(const char *s) { +STC_INLINE int utf8_chr_size(const char *s) { unsigned b = (uint8_t)*s; if (b < 0x80) return 1; /*if (b < 0xC2) return 0;*/ @@ -89,32 +89,32 @@ STC_INLINE unsigned utf8_chr_size(const char *s) { } /* number of codepoints in the utf8 string s */ -STC_INLINE size_t utf8_size(const char *s) { - size_t size = 0; +STC_INLINE intptr_t utf8_size(const char *s) { + intptr_t size = 0; while (*s) size += (*++s & 0xC0) != 0x80; return size; } -STC_INLINE size_t utf8_size_n(const char *s, size_t nbytes) { - size_t size = 0; +STC_INLINE intptr_t utf8_size_n(const char *s, intptr_t nbytes) { + intptr_t size = 0; while ((nbytes-- != 0) & (*s != 0)) { size += (*++s & 0xC0) != 0x80; } return size; } -STC_INLINE const char* utf8_at(const char *s, size_t index) { +STC_INLINE const char* utf8_at(const char *s, intptr_t index) { while ((index > 0) & (*s != 0)) index -= (*++s & 0xC0) != 0x80; return s; } -STC_INLINE size_t utf8_pos(const char* s, size_t index) - { return (size_t)(utf8_at(s, index) - s); } +STC_INLINE intptr_t utf8_pos(const char* s, intptr_t index) + { return (intptr_t)(utf8_at(s, index) - s); } #endif // UTF8_H_INCLUDED #if defined i_extern || defined STC_EXTERN -# include "../src/utf8code.c" +# include "../../src/utf8code.c" # undef i_extern #endif |
