From 5aa48d538569463ffeda976d21f79edc5f276be4 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 19 Jan 2023 19:27:07 +0100 Subject: Add a from_n() method to containers (and put_n() to maps), to support new initialization. --- include/stc/ccommon.h | 4 ++++ include/stc/cdeq.h | 5 ++++- include/stc/clist.h | 4 ++++ include/stc/cmap.h | 19 +++++++++++++++++-- include/stc/cpque.h | 6 ++++++ include/stc/csmap.h | 16 ++++++++++++++++ include/stc/cspan.h | 13 +++++-------- include/stc/cstack.h | 15 ++++++++++----- include/stc/cvec.h | 4 ++++ 9 files changed, 70 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index edf4b2cb..d7b68d1f 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -113,6 +113,10 @@ #define c_no_hash (1<<6) #define c_no_lookup (c_no_cmp|c_no_eq|c_no_hash) +#define c_initialize(C, ...) \ + C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) +#define c_literal(C, ...) (*(C[]){c_initialize(C, __VA_ARGS__)}) + /* Generic algorithms */ typedef const char* crawstr; diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 1fe52548..b555da63 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -48,7 +48,10 @@ 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, size_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_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, diff --git a/include/stc/clist.h b/include/stc/clist.h index 37fa447a..ac687fc9 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -134,6 +134,10 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #endif // !i_no_emplace STC_INLINE _cx_self _cx_memb(_init)(void) { return c_INIT(_cx_self){NULL}; } +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) + { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) + { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_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); } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index df3ce6fc..e63adb77 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -195,8 +195,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, size_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, size_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) diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 28b5eb86..f1f03f8e 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -47,6 +47,12 @@ 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}; } +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_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 size_t cap) { if (cap != self->_len && cap <= self->_cap) return true; _cx_value *d = (_cx_value *)c_REALLOC(self->data, cap*sizeof *d); diff --git a/include/stc/csmap.h b/include/stc/csmap.h index e5df0fe2..75f10308 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -294,6 +294,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, size_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, size_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) { diff --git a/include/stc/cspan.h b/include/stc/cspan.h index a4c8f5cb..9969a670 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -48,7 +48,7 @@ int demo2() { puts(""); // use a temporary Intspan object. - c_FORFILTER (i, Intspan, cspan_literal(Intspan, {10, 20, 30, 23, 22, 21}) + c_FORFILTER (i, Intspan, c_literal(Intspan, {10, 20, 30, 23, 22, 21}) , c_FLT_SKIPWHILE(i, *i.ref < 25) && (*i.ref & 1) == 0 // even only , c_FLT_TAKE(i, 2)) // break after 2 @@ -66,6 +66,9 @@ int demo2() { typedef struct { Self##_value *ref, *end; } Self##_iter; \ typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ \ + STC_INLINE Self Self##_from_n(Self##_raw* raw, const size_t n) { \ + return (Self){.data=raw, .dim={(uint32_t)n}}; \ + } \ 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}; \ @@ -90,17 +93,11 @@ int demo2() { /* 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, .dim={(uint32_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, ...) \ diff --git a/include/stc/cstack.h b/include/stc/cstack.h index f4a86156..c1332769 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 @@ -106,8 +106,7 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return false; } -STC_INLINE _cx_value* -_cx_memb(_append_uninit)(_cx_self *self, size_t n) { +STC_INLINE _cx_value* _cx_memb(_append_uninit)(_cx_self *self, size_t n) { size_t len = self->_len; if (!_cx_memb(_reserve)(self, len + n)) return NULL; self->_len += n; @@ -137,6 +136,12 @@ 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 void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } + +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_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, size_t idx) { assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index fbacd305..7f8365d7 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -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, size_t n) + { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_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) { -- cgit v1.2.3 From f8accdbcee0b397ad6ba2f2c2c64575a003e71e5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 19 Jan 2023 21:20:16 +0100 Subject: Finish last commit. Most safe function macros are now preferred lowercase, whereas flow control macros (c_FOREACH, ..) are preferred uppercase. --- docs/ccommon_api.md | 84 +++++++++++++++++++++++++--------------------- docs/cmap_api.md | 2 +- docs/csview_api.md | 2 +- docs/cvec_api.md | 2 +- include/stc/algo/crange.h | 2 +- include/stc/algo/csort.h | 6 ++-- include/stc/algo/filter.h | 12 +++---- include/stc/carc.h | 4 +-- include/stc/cbits.h | 4 +-- include/stc/cbox.h | 6 ++-- include/stc/ccommon.h | 33 +++++++++--------- include/stc/cdeq.h | 8 ++--- include/stc/clist.h | 8 ++--- include/stc/cmap.h | 10 +++--- include/stc/cpque.h | 2 +- include/stc/crandom.h | 4 +-- include/stc/csmap.h | 4 +-- include/stc/cspan.h | 13 ++++--- include/stc/cstack.h | 4 +-- include/stc/cstr.h | 16 ++++----- include/stc/csview.h | 10 +++--- include/stc/cvec.h | 10 +++--- include/stc/priv/lowcase.h | 26 +++++++------- misc/archived/csmap.h | 6 ++-- misc/archived/cstr.h | 4 +-- misc/examples/box.c | 2 +- misc/examples/city.c | 2 +- misc/examples/forfilter.c | 20 +++++------ misc/examples/forloops.c | 10 +++--- misc/examples/functor.c | 2 +- misc/examples/music_arc.c | 2 +- misc/examples/new_sptr.c | 2 +- misc/examples/person_arc.c | 2 +- misc/examples/prime.c | 2 +- misc/examples/sso_substr.c | 2 +- misc/examples/vikings.c | 4 +-- 36 files changed, 170 insertions(+), 162 deletions(-) (limited to 'include') diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index b610ff04..6f09a130 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -225,10 +225,10 @@ Iterate containers with stop-criteria and chained range filtering. | Built-in filter | Description | |:----------------------------------|:-------------------------------------| -| `c_FLT_SKIP(it, numItems)` | Skip numItems | -| `c_FLT_TAKE(it, numItems)` | Take numItems | -| `c_FLT_SKIPWHILE(it, predicate)` | Skip items until predicate is false | -| `c_FLT_TAKEWHILE(it, predicate)` | Take items until predicate is false | +| `c_flt_skip(it, numItems)` | Skip numItems | +| `c_flt_take(it, numItems)` | Take numItems | +| `c_flt_skipwhile(it, predicate)` | Skip items until predicate is false | +| `c_flt_takewhile(it, predicate)` | Take items until predicate is false | `it.index` holds the index of the source item, and `it.count` the current number of items taken. ```c @@ -249,9 +249,9 @@ int main() { c_FORFILTER (i, IVec, vec, isOdd(*i.ref) - && c_FLT_SKIP(i, 100) // built-in + && c_flt_skip(i, 100) // built-in && isPrime(*i.ref) - , c_FLT_TAKE(i, 10)) { // breaks loop on false. + , c_flt_take(i, 10)) { // breaks loop on false. printf(" %d", *i.ref); } puts(""); @@ -259,7 +259,18 @@ int main() { } // Out: 1000211 1000213 1000231 1000249 1000253 1000273 1000289 1000291 1000303 1000313 ``` -Note that `c_FLT_TAKE()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed. +Note that `c_flt_take()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed. + +### c_initialize + +Create a container from a literal initializer list. +**c_initialize** a container or a cspan with an initializer list. +```c +#define i_val_str +#include +... +cset_str myset = c_initialize(cset_str, {"This", "is", "the", "story"}); +``` ### crange **crange** is a number sequence generator type. The **crange_value** type is `long long`. Below, *start*, *stop*, *step* are type *crange_value*: @@ -284,48 +295,33 @@ c_FORFILTER (i, crange, r1 printf("2"); c_FORFILTER (i, crange, crange_literal(3, INT64_MAX, 2) , isPrime(*i.ref) - , c_FLT_TAKE(10)) + , c_flt_take(10)) printf(" %lld", *i.ref); // 2 3 5 7 11 13 17 19 23 29 31 ``` -### c_FIND_IF, c_ERASE_IF +### c_find_if, c_erase_if, c_swap, c_drop Find or erase linearily in containers using a predicate ```c // Search vec for first value > 2: cvec_i_iter i; -c_FIND_IF(i, cvec_i, vec, *i.ref > 2); +c_find_if(i, cvec_i, vec, *i.ref > 2); if (i.ref) printf("%d\n", *i.ref); // Search map for a string containing "hello" and erase it: cmap_str_iter it, it1 = ..., it2 = ...; -c_FIND_IF(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); +c_find_if(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello")); if (it.ref) cmap_str_erase_at(&map, it); // Erase all strings containing "hello": // Note 1: iter i need not be declared. // Note 2: variables index and count can be accessed in predicate. -c_ERASE_IF(i, csmap_str, map, cstr_contains(i.ref, "hello")); -``` +c_erase_if(i, csmap_str, map, cstr_contains(i.ref, "hello")); -### c_NEW, c_ALLOC, c_ALLOC_N, c_DROP +// Safe macro for swapping internals of two objects of same type: +c_swap(cmap_int, &map1, &map2); -| Usage | Meaning | -|:-------------------------------|:----------------------------------------| -| `c_NEW (type, value)` | Move value to a new object on the heap | -| `c_ALLOC (type)` | `(type *) c_MALLOC(sizeof(type))` | -| `c_ALLOC_N (type, N)` | `(type *) c_MALLOC((N)*sizeof(type))` | -| `c_DROP (ctype, &c1, ..., &cN)` | `ctype_drop(&c1); ... ctype_drop(&cN)` | - -```c -struct Pnt { double x, y, z; }; -struct Pnt *pnt = c_NEW (struct Pnt, {1.2, 3.4, 5.6}); -c_FREE(pnt); - -int* array = c_ALLOC_N (int, 100); -c_FREE(array); - -cstr a = cstr_lit("Hello"), b = cstr_lit("World"); -c_DROP(cstr, &a, &b); +// Drop multiple containers of same type: +c_drop(cvec_i, &vec1, &vec2, &vec3); ``` ### General predefined template parameter functions @@ -341,17 +337,29 @@ bool crawstr_eq(const crawstr* x, const crawstr* y); uint64_t crawstr_hash(const crawstr* x); ``` -### c_MALLOC, c_CALLOC, c_REALLOC, c_FREE +### c_NEW, c_ALLOC, c_ALLOC_N + +| Usage | Meaning | +|:----------------------------|:-------------------------------------------| +| `c_NEW (type, value)` | Allocate and init a new object on the heap | +| `c_ALLOC (type)` | `(type *) c_MALLOC(sizeof(type))` | +| `c_ALLOC_N (type, N)` | `(type *) c_MALLOC((N)*sizeof(type))` | + +```c +struct Pnt { double x, y, z; }; +struct Pnt *pnt = c_NEW(struct Pnt, {1.2, 3.4, 5.6}); +c_FREE(pnt); + +int* array = c_ALLOC_N(int, 100); +c_FREE(array); +``` + +### c_MALLOC, c_CALLOC, c_REALLOC, c_FREE: customizable allocators Memory allocator for the entire library. Macros can be overridden by the user. -### c_SWAP, c_ARRAYLEN -- **c_SWAP(T, xp, yp)**: Safe macro for swapping internals of two objects of same type. +### c_ARRAYLEN - **c_ARRAYLEN(array)**: Return number of elements in an array. ```c -cmap_int map1 = {0}, map2 = {0}; -... -c_SWAP(cmap_int, &map1, &map2); - int array[] = {1, 2, 3, 4}; size_t n = c_ARRAYLEN(array); ``` diff --git a/docs/cmap_api.md b/docs/cmap_api.md index a33715fc..3848e67e 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -353,7 +353,7 @@ typedef struct Viking { } Viking; static inline void Viking_drop(Viking* v) { - c_DROP(cstr, &v->name, &v->country); + c_drop(cstr, &v->name, &v->country); } // Define Viking raw struct with cmp, hash, and convertion functions between Viking and RViking structs: diff --git a/docs/csview_api.md b/docs/csview_api.md index b5508ace..4085a708 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -135,7 +135,7 @@ int main () cstr s3 = cstr_from_sv(cstr_substr(&s1, 0, 6)); // "Apples" printf("%s %s\n", cstr_str(&s2), cstr_str(&s3)); - c_DROP(cstr, &str1, &s1, &s2, &s3); + c_drop(cstr, &str1, &s1, &s2, &s3); } ``` Output: diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 92629c8b..08c404af 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -222,6 +222,6 @@ int main(void) { c_FOREACH (i, UVec, vec2) printf("%s: %d\n", cstr_str(&i.ref->name), i.ref->id); - c_DROP(UVec, &vec, &vec2); // cleanup + c_drop(UVec, &vec, &vec2); // cleanup } ``` \ No newline at end of file diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 63242a54..5e1610d9 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(""); } diff --git a/include/stc/algo/csort.h b/include/stc/algo/csort.h index 2cd7b548..9c9bcd5b 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); diff --git a/include/stc/algo/filter.h b/include/stc/algo/filter.h index e941be2e..0f850f41 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,10 +54,10 @@ 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__) diff --git a/include/stc/carc.h b/include/stc/carc.h index 614af18e..fa2ee3b4 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_COMPOUND(_cx_self){NULL, NULL}; } STC_INLINE long _cx_memb(_use_count)(const _cx_self* self) { return self->use_count ? *self->use_count : 0; } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 6a71487f..b0cb7a9c 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -123,7 +123,7 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons struct { uint64_t *data64; size_t _size; } typedef i_type; -STC_INLINE cbits cbits_init(void) { return c_INIT(cbits){NULL}; } +STC_INLINE cbits cbits_init(void) { return c_COMPOUND(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; } @@ -195,7 +195,7 @@ 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_COMPOUND(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; } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 95dfdf8f..6dc41ed1 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_COMPOUND(_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_COMPOUND(_cx_self){p}; } // c++: std::make_unique(val) STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) { diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index d7b68d1f..09befa56 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -71,12 +71,12 @@ #define c_ALLOC(T) static_cast(c_MALLOC(sizeof(T))) #define c_ALLOC_N(T, n) static_cast(c_MALLOC(sizeof(T)*(n))) #define c_NEW(T, ...) new (c_ALLOC(T)) T(__VA_ARGS__) - #define c_INIT(T) T + #define c_COMPOUND(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_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T))) - #define c_INIT(T) (T) + #define c_COMPOUND(T) (T) #endif #ifndef c_MALLOC #define c_MALLOC(sz) malloc(sz) @@ -85,12 +85,11 @@ #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, \ +#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]) // 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)) @@ -115,18 +114,18 @@ #define c_initialize(C, ...) \ C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) -#define c_literal(C, ...) (*(C[]){c_initialize(C, __VA_ARGS__)}) -/* Generic algorithms */ +/* Function macros and various others */ 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 c_ARRAYLEN(a) (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_SV2(str, n) (c_COMPOUND(csview){str, n}) #define c_ARGSV(sv) (int)(sv).size, (sv).str /* use with "%.*s" */ #define c_PAIR(ref) (ref)->first, (ref)->second @@ -166,6 +165,8 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, return NULL; } +/* Control block macros */ + #define c_FOREACH(...) c_MACRO_OVERLOAD(c_FOREACH, __VA_ARGS__) #define c_FOREACH3(it, C, cnt) \ for (C##_iter it = C##_begin(&cnt); it.ref; C##_next(&it)) @@ -208,7 +209,6 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #define c_WITH3(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) \ @@ -223,13 +223,16 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, c_WITH2(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 { \ +/* 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_if4(it, C, cnt, pred) do { \ size_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 { \ +#define c_find_if5(it, C, start, end, pred) do { \ size_t index = 0; \ const C##_value* _endref = (end).ref; \ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ @@ -237,7 +240,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, 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) { \ if (pred) it = C##_erase_at(&cnt, it); \ diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index b555da63..befb4146 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -101,11 +101,11 @@ STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _ 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}; + return c_COMPOUND(_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_COMPOUND(_cx_iter){NULL, self->data + self->_len}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } @@ -345,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_COMPOUND(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_value* @@ -377,7 +377,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { { 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}; + return c_COMPOUND(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone diff --git a/include/stc/clist.h b/include/stc/clist.h index ac687fc9..de32b931 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); @@ -133,7 +133,7 @@ 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 _cx_self _cx_memb(_init)(void) { return c_COMPOUND(_cx_self){NULL}; } STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); } STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) @@ -161,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_COMPOUND(_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_COMPOUND(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { diff --git a/include/stc/cmap.h b/include/stc/cmap.h index e63adb77..c2f8cf3e 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -107,7 +107,7 @@ STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawke 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_COMPOUND(_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; } @@ -166,7 +166,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_COMPOUND(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } STC_INLINE void @@ -222,7 +222,7 @@ STC_INLINE _cx_iter _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_COMPOUND(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { @@ -240,7 +240,7 @@ STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { size_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) - return c_INIT(_cx_iter){self->table + idx, + return c_COMPOUND(_cx_iter){self->table + idx, self->table + self->bucket_count, self->_hashx + idx}; return _cx_memb(_end)(self); @@ -430,7 +430,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { m.table[b.idx] = *e; m._hashx[b.idx] = (uint8_t)b.hx; } - c_SWAP(_cx_self, self, &m); + c_swap(_cx_self, self, &m); } c_FREE(m._hashx); c_FREE(m.table); diff --git a/include/stc/cpque.h b/include/stc/cpque.h index f1f03f8e..83fcb684 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -45,7 +45,7 @@ STC_API void _cx_memb(_erase_at)(_cx_self* self, size_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_COMPOUND(_cx_self){NULL}; } STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } diff --git a/include/stc/crandom.h b/include/stc/crandom.h index 733c3ec5..9cb23e93 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -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_COMPOUND(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_COMPOUND(stc64_normalf_t){mean, stddev, 0.0, 0}; } /* -------------------------- IMPLEMENTATION ------------------------- */ diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 75f10308..67b2265e 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -143,7 +143,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_COMPOUND(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } @@ -231,7 +231,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { _cx_node* nodes = (_cx_node*)c_REALLOC(self->nodes, (cap + 1)*sizeof(_cx_node)); if (!nodes) return false; - nodes[0] = c_INIT(_cx_node){{0, 0}, 0}; + nodes[0] = c_COMPOUND(_cx_node){{0, 0}, 0}; self->nodes = nodes; self->cap = (i_size)cap; return true; diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 9969a670..88e10b07 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -40,18 +40,17 @@ int demo1() { } 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, c_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(""); } @@ -86,7 +85,7 @@ int demo2() { #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_rank_ok(self, rank) c_static_assert(cspan_rank(self) == rank) #define cspan_make(array, ...) \ {.data=array, .dim={__VA_ARGS__}} @@ -96,7 +95,7 @@ int demo2() { {.data=(container)->data, .dim={(uint32_t)(container)->_len}} #define cspan_from_array(array) \ - {.data=(array) + c_STATIC_ASSERT(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}} + {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}} #define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) #define cspan_rank(self) c_ARRAYLEN((self)->dim) diff --git a/include/stc/cstack.h b/include/stc/cstack.h index c1332769..a40b74de 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -175,12 +175,12 @@ 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_COMPOUND(_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_COMPOUND(_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; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 46ad2fcd..b500065e 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -71,7 +71,7 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_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_COMPOUND(cstr){{{0}, 0}}) #define cstr_toraw(self) cstr_str(self) STC_API char* cstr_reserve(cstr* self, size_t cap); @@ -90,12 +90,12 @@ STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, unsigned 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_COMPOUND(cstr_buf){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} + : c_COMPOUND(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_COMPOUND(csview){s->lon.data, cstr_l_size(s)} + : c_COMPOUND(csview){s->sml.data, cstr_s_size(s)}; } STC_INLINE cstr cstr_init(void) @@ -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_COMPOUND(cstr_iter){NULL}; + return c_COMPOUND(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_COMPOUND(cstr_iter){NULL}; } STC_INLINE void cstr_next(cstr_iter* it) { it->ref += it->u8.chr.size; diff --git a/include/stc/csview.h b/include/stc/csview.h index 1ab34d9a..fb860a66 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -37,7 +37,7 @@ STC_API size_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_COMPOUND(csview){str, strlen(str)}; } STC_INLINE void csview_clear(csview* self) { *self = csview_NULL; } STC_INLINE size_t csview_size(csview sv) { return sv.size; } @@ -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_COMPOUND(csview_iter){NULL}; + return c_COMPOUND(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_COMPOUND(csview_iter){.u8 = {{NULL}, self->str + self->size}}; } STC_INLINE void csview_next(csview_iter* it) { it->ref += it->u8.chr.size; diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 7f8365d7..8010aea3 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -199,11 +199,11 @@ _cx_memb(_at_mut)(_cx_self* self, const size_t 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}; + return c_COMPOUND(_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_COMPOUND(_cx_iter){NULL, self->data + self->_len}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { if (++it->ref == it->end) it->ref = NULL; } @@ -259,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_COMPOUND(_cx_self){NULL}; } STC_DEF void @@ -328,7 +328,7 @@ _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { memmove(pos + n, pos, (self->_len - idx)*sizeof *pos); self->_len += n; } - return c_INIT(_cx_iter){pos, self->data + self->_len}; + return c_COMPOUND(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_iter @@ -348,7 +348,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { { i_keydrop(p); } memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); self->_len -= len; - return c_INIT(_cx_iter){p2 == end ? NULL : p1, end - len}; + return c_COMPOUND(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone diff --git a/include/stc/priv/lowcase.h b/include/stc/priv/lowcase.h index 8fdadd75..cb6ed991 100644 --- a/include/stc/priv/lowcase.h +++ b/include/stc/priv/lowcase.h @@ -27,10 +27,6 @@ #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 +41,16 @@ #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 + +#define c_DROP c_drop +#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 c_DELETE c_delete +#define c_SWAP c_swap +#define c_CONTAINER_OF c_container_of +#define c_STATIC_ASSERT c_static_assert diff --git a/misc/archived/csmap.h b/misc/archived/csmap.h index 5c9fba6b..2daec3b9 100644 --- a/misc/archived/csmap.h +++ b/misc/archived/csmap.h @@ -129,8 +129,8 @@ _cx_memb(_clear)(_cx_self* self) STC_INLINE _cx_raw _cx_memb(_value_toraw)(_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_COMPOUND(_cx_raw){i_keyto((&val->first)), + i_valto((&val->second))} ); } STC_INLINE int @@ -470,7 +470,7 @@ _cx_memb(_clone_r_)(_cx_node *tn) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { - return c_INIT(_cx_self){_cx_memb(_clone_r_)(cx.root), cx.size}; + return c_COMPOUND(_cx_self){_cx_memb(_clone_r_)(cx.root), cx.size}; } #endif // !i_no_clone diff --git a/misc/archived/cstr.h b/misc/archived/cstr.h index e074b6e4..1a5b0da4 100644 --- a/misc/archived/cstr.h +++ b/misc/archived/cstr.h @@ -67,7 +67,7 @@ STC_INLINE cstr cstr_init() { return cstr_NULL; } STC_INLINE const char* cstr_str(const cstr* self) { return self->str; } #define cstr_toraw(self) (self)->str STC_INLINE csview cstr_sv(const cstr* self) - { return c_INIT(csview){self->str, _cstr_p(self)->size}; } + { return c_COMPOUND(csview){self->str, _cstr_p(self)->size}; } #define cstr_lit(literal) \ cstr_from_n(literal, crawstr_len(literal)) STC_INLINE cstr cstr_from(const char* str) @@ -118,7 +118,7 @@ STC_INLINE bool cstr_getline(cstr *self, FILE *stream) STC_INLINE cstr_buf cstr_buffer(cstr* s) { cstr_priv* p = _cstr_p(s); - return c_INIT(cstr_buf){s->str, p->size, p->cap}; + return c_COMPOUND(cstr_buf){s->str, p->size, p->cap}; } STC_INLINE cstr cstr_with_capacity(const size_t cap) { diff --git a/misc/examples/box.c b/misc/examples/box.c index 446a7603..3a7a8139 100644 --- a/misc/examples/box.c +++ b/misc/examples/box.c @@ -24,7 +24,7 @@ Person Person_clone(Person p) { void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_DROP(cstr, &p->name, &p->last); + c_drop(cstr, &p->name, &p->last); } #define i_type PBox diff --git a/misc/examples/city.c b/misc/examples/city.c index 7f355bdf..e24c90de 100644 --- a/misc/examples/city.c +++ b/misc/examples/city.c @@ -44,7 +44,7 @@ City City_clone(City c) { void City_drop(City* c) { printf("drop %s\n", cstr_str(&c->name)); - c_DROP(cstr, &c->name, &c->country); + c_drop(cstr, &c->name, &c->country); } diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index 05d0bc28..e041fd68 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -33,12 +33,12 @@ void demo1(void) int res, sum = 0; c_FORFILTER (i, IVec, vec - , c_FLT_SKIPWHILE(i, *i.ref != 80) - && c_FLT_SKIP(i, 1) - && c_FLT_SKIPWHILE(i, *i.ref != 80) + , c_flt_skipwhile(i, *i.ref != 80) + && c_flt_skip(i, 1) + && c_flt_skipwhile(i, *i.ref != 80) && flt_isEven(i) && flt_skipValue(i, 80) - , c_FLT_TAKE(i, 5) // short-circuit + , c_flt_take(i, 5) // short-circuit ){ sum += res = flt_square(i); printf(" %d", res); @@ -66,9 +66,9 @@ void demo2(void) puts("demo2:"); c_FORFILTER (x, crange, crange_literal(INT64_MAX) - , c_FLT_SKIPWHILE(x, *x.ref != 11) + , c_flt_skipwhile(x, *x.ref != 11) && *x.ref % 2 != 0 - , c_FLT_TAKE(x, 5)) + , c_flt_take(x, 5)) IVec_push(&vector, *x.ref * *x.ref); c_FOREACH (x, IVec, vector) printf(" %d", *x.ref); @@ -128,11 +128,11 @@ void demo5(void) puts("demo5:"); crange r1 = crange_make(1963, INT32_MAX); c_FORFILTER (i, crange, r1 - , c_FLT_SKIP(i,15) - && c_FLT_SKIPWHILE(i, flt_mid_decade(i)) - && c_FLT_SKIP(i,30) + , c_flt_skip(i,15) + && c_flt_skipwhile(i, flt_mid_decade(i)) + && c_flt_skip(i,30) && flt_even(i) - , c_FLT_TAKE(i,10)) + , c_flt_take(i,10)) printf(" %lld", *i.ref); puts(""); } diff --git a/misc/examples/forloops.c b/misc/examples/forloops.c index 69c1c193..cd3c4d9e 100644 --- a/misc/examples/forloops.c +++ b/misc/examples/forloops.c @@ -68,12 +68,12 @@ int main() puts("\n\nc_forfilter:"); c_FORFILTER (i, IVec, vec - , c_FLT_SKIPWHILE(i, *i.ref != 65) - && c_FLT_TAKEWHILE(i, *i.ref != 280) - && c_FLT_SKIPWHILE(i, isOdd(i)) + , c_flt_skipwhile(i, *i.ref != 65) + && c_flt_takewhile(i, *i.ref != 280) + && c_flt_skipwhile(i, isOdd(i)) && isOdd(i) - && c_FLT_SKIP(i, 2) - , c_FLT_TAKE(i, 1)) + && c_flt_skip(i, 2) + , c_flt_take(i, 1)) printf(" %d", *i.ref); puts(""); // 189 diff --git a/misc/examples/functor.c b/misc/examples/functor.c index 54e2702d..f1db3644 100644 --- a/misc/examples/functor.c +++ b/misc/examples/functor.c @@ -23,7 +23,7 @@ struct { #define i_type ipque #define i_val int #define i_opt c_is_forward // needed to avoid re-type-define container type -#define i_less_functor(self, x, y) c_CONTAINER_OF(self, IPQueue, Q)->less(x, y) +#define i_less_functor(self, x, y) c_container_of(self, IPQueue, Q)->less(x, y) #include void print_queue(const char* name, IPQueue q) { diff --git a/misc/examples/music_arc.c b/misc/examples/music_arc.c index 9896c591..fad7ddcf 100644 --- a/misc/examples/music_arc.c +++ b/misc/examples/music_arc.c @@ -16,7 +16,7 @@ Song Song_make(const char* artist, const char* title) void Song_drop(Song* s) { printf("drop: %s\n", cstr_str(&s->title)); - c_DROP(cstr, &s->artist, &s->title); + c_drop(cstr, &s->artist, &s->title); } // Define the reference counted type diff --git a/misc/examples/new_sptr.c b/misc/examples/new_sptr.c index 5441ae01..116827a4 100644 --- a/misc/examples/new_sptr.c +++ b/misc/examples/new_sptr.c @@ -43,7 +43,7 @@ Person Person_clone(Person p) { } void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_DROP(cstr, &p->name, &p->last); + c_drop(cstr, &p->name, &p->last); } diff --git a/misc/examples/person_arc.c b/misc/examples/person_arc.c index f782c9da..4d9c2a51 100644 --- a/misc/examples/person_arc.c +++ b/misc/examples/person_arc.c @@ -24,7 +24,7 @@ Person Person_clone(Person p) { void Person_drop(Person* p) { printf("drop: %s %s\n", cstr_str(&p->name), cstr_str(&p->last)); - c_DROP(cstr, &p->name, &p->last); + c_drop(cstr, &p->name, &p->last); } #define i_type PSPtr diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 40ccc299..4a6b0f68 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -45,7 +45,7 @@ int main(void) puts("Show the last 50 primes using a temporary crange generator:"); c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2) , cbits_test(&primes, *i.ref>>1) - , c_FLT_TAKE(i, 50)) { + , c_flt_take(i, 50)) { printf("%lld ", *i.ref); if (i.count % 10 == 0) puts(""); } diff --git a/misc/examples/sso_substr.c b/misc/examples/sso_substr.c index 8e7450ba..be372a8d 100644 --- a/misc/examples/sso_substr.c +++ b/misc/examples/sso_substr.c @@ -16,5 +16,5 @@ int main () cstr s3 = cstr_from_sv(cstr_substr_ex(&str, 0, 6)); // "apples" printf("%s %s: %d, %d\n", cstr_str(&s2), cstr_str(&s3), cstr_is_long(&str), cstr_is_long(&s2)); - c_DROP (cstr, &str, &s2, &s3); + c_drop (cstr, &str, &s2, &s3); } diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c index c65a9caa..a050b324 100644 --- a/misc/examples/vikings.c +++ b/misc/examples/vikings.c @@ -23,11 +23,11 @@ static inline int RViking_cmp(const RViking* rx, const RViking* ry) { } static inline Viking Viking_from(RViking raw) { // note: parameter is by value - return c_INIT(Viking){cstr_from(raw.name), cstr_from(raw.country)}; + return c_COMPOUND(Viking){cstr_from(raw.name), cstr_from(raw.country)}; } static inline RViking Viking_toraw(const Viking* vp) { - return c_INIT(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; + return c_COMPOUND(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; } // With this in place, we define the Viking => int hash map type: -- cgit v1.2.3 From 9e6b763fb28c783b8d856442a6929026257bff92 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 20 Jan 2023 10:00:29 +0100 Subject: Renamed macro function c_initialize() to c_init(). Minor internal improvements. --- README.md | 2 +- include/stc/ccommon.h | 4 ++-- include/stc/priv/altnames.h | 56 +++++++++++++++++++++++++++++++++++++++++++++ include/stc/priv/lowcase.h | 56 --------------------------------------------- misc/examples/printspan.c | 41 ++++++++++++++++++--------------- 5 files changed, 81 insertions(+), 78 deletions(-) create mode 100644 include/stc/priv/altnames.h delete mode 100644 include/stc/priv/lowcase.h (limited to 'include') diff --git a/README.md b/README.md index f184e597..2ba88048 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ STC - Smart Template Containers for C News: Version 4.1 Beta (Dec 2022) ------------------------------------------------ -- Major change is uppercase macros in ccommon.h. Lowercase macros are [still supported](include/stc/priv/lowcase.h). +- Major change is uppercase macros in ccommon.h. Lowercase macros are [still supported](include/stc/priv/altnames.h). - [See detailed changes for version 4](#version-4). Introduction diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 09befa56..35bc9335 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -29,7 +29,7 @@ #include #include #include -#include "priv/lowcase.h" +#include "priv/altnames.h" #if SIZE_MAX == UINT32_MAX #define c_ZU PRIu32 @@ -112,7 +112,7 @@ #define c_no_hash (1<<6) #define c_no_lookup (c_no_cmp|c_no_eq|c_no_hash) -#define c_initialize(C, ...) \ +#define c_init(C, ...) \ C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) /* Function macros and various others */ diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h new file mode 100644 index 00000000..7f58384c --- /dev/null +++ b/include/stc/priv/altnames.h @@ -0,0 +1,56 @@ +/* MIT License + * + * Copyright (c) 2023 Tyge Løvset + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#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_arraylen c_ARRAYLEN +#define c_forlist c_FORLIST +#define c_forrange c_FORRANGE +#define c_foreach c_FOREACH +#define c_forwhile c_FORWHILE +#define c_forpair c_FORPAIR +#define c_forfilter c_FORFILTER +#define c_formatch c_FORMATCH +#define c_fortoken c_FORTOKEN +#define c_fortoken_sv c_FORTOKEN_SV +#define c_auto c_AUTO +#define c_with c_WITH +#define c_scope c_SCOPE +#define c_defer c_DEFER +#define c_sv c_SV + +#define c_DROP c_drop +#define c_DELETE c_delete +#define c_SWAP c_swap +#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 c_CONTAINER_OF c_container_of +#define c_STATIC_ASSERT c_static_assert diff --git a/include/stc/priv/lowcase.h b/include/stc/priv/lowcase.h deleted file mode 100644 index cb6ed991..00000000 --- a/include/stc/priv/lowcase.h +++ /dev/null @@ -1,56 +0,0 @@ -/* MIT License - * - * Copyright (c) 2023 Tyge Løvset - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -#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_arraylen c_ARRAYLEN -#define c_forlist c_FORLIST -#define c_forrange c_FORRANGE -#define c_foreach c_FOREACH -#define c_forwhile c_FORWHILE -#define c_forpair c_FORPAIR -#define c_forfilter c_FORFILTER -#define c_formatch c_FORMATCH -#define c_fortoken c_FORTOKEN -#define c_fortoken_sv c_FORTOKEN_SV -#define c_auto c_AUTO -#define c_with c_WITH -#define c_scope c_SCOPE -#define c_defer c_DEFER -#define c_sv c_SV - -#define c_DROP c_drop -#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 c_DELETE c_delete -#define c_SWAP c_swap -#define c_CONTAINER_OF c_container_of -#define c_STATIC_ASSERT c_static_assert diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index 6292cecf..8d0b0321 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -12,40 +12,43 @@ #include #include -using_cspan(ispan, int, 1); +using_cspan(intspan, int, 1); -void printMe(ispan container) { - printf("%d\n", (int)cspan_size(&container)); - c_FOREACH (e, ispan, container) printf("%d ", *e.ref); +void printMe(intspan container) { + printf("%d:", (int)cspan_size(&container)); + c_FOREACH (e, intspan, container) printf(" %d", *e.ref); puts(""); } -int main() { +int main() +{ c_AUTO (cvec_int, vec) c_AUTO (cstack_int, stk) c_AUTO (cdeq_int, deq) c_AUTO (cset_str, set) { - int arr[] = {1, 2, 3, 4, 5}; - ispan sp = cspan_from_array(arr); - printMe((ispan)cspan_subspan(&sp, 1, 3)); + printMe( c_init(intspan, {1, 2, 3}) ); - printMe(c_initialize(ispan, {1, 2, 3, 4})); + int arr[] = {1, 2, 3, 4, 5, 6}; + intspan span = cspan_from_array(arr); + printMe( (intspan)cspan_subspan(&span, 1, 4) ); - printMe((ispan)cspan_from_array(arr)); + c_FORLIST (i, int, {1, 2, 3, 4, 5}) + cvec_int_push(&vec, *i.ref); + printMe( (intspan)cspan_from(&vec) ); - c_FORLIST (i, int, {1, 2, 3, 4, 5, 6}) cvec_int_push(&vec, *i.ref); - printMe((ispan)cspan_from(&vec)); + printMe( span ); - stk = c_initialize(cstack_int, {1, 2, 3, 4, 5, 6, 7}); - printMe((ispan)cspan_from(&stk)); + stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7}); + printMe( (intspan)cspan_from(&stk) ); - deq = c_initialize(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); - printMe((ispan)cspan_from(&deq)); + deq = c_init(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); + printMe( (intspan)cspan_from(&deq) ); - set = c_initialize(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"}); - printf("%d\n", (int)cset_str_size(&set)); - c_FOREACH (e, cset_str, set) printf("%s ", cstr_str(e.ref)); + set = c_init(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"}); + printf("%d:", (int)cset_str_size(&set)); + c_FOREACH (e, cset_str, set) + printf(" %s", cstr_str(e.ref)); puts(""); } } -- cgit v1.2.3 From e97d06f95a65edd4fdee53555398f7b636a72ab5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 20 Jan 2023 19:50:52 +0100 Subject: Renamed cspan_at2 .. at4 to cspan_subdim2 .. Improved cspan docs. --- docs/cspan_api.md | 93 +++++++++++++++++++++++++----------------------- include/stc/cspan.h | 16 ++++----- misc/examples/multidim.c | 2 +- 3 files changed, 58 insertions(+), 53 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 8cabd97e..d6caff4e 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -10,78 +10,83 @@ See the c++ classes [std::span](https://en.cppreference.com/w/cpp/container/span ```c #include -using_cspan(SpanType, ValueType, Rank); // define SpanType with ValueType elements. - // Rank is number of dimensions (max 4) +using_cspan(SpanType, ValueType, Rank); // define SpanType with ValueType elements. + // Rank is number of dimensions (max 4) // Shorthands: -using_cspan2(S, ValueType); // define span types S, S2 with ranks 1, 2. -using_cspan3(S, ValueType); // define span types S, S2, S3 with ranks 1, 2, 3. -using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. +using_cspan2(S, ValueType); // define span types S, S2 with ranks 1, 2. +using_cspan3(S, ValueType); // define span types S, S2, S3 with ranks 1, 2, 3. +using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. ``` ## Methods Note that `cspan_make()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. ```c -SpanType{N} cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan +SpanTypeN cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array -void cspan_resize(SpanType{N}* self, size_t xdim, ...); // change the extent of each dimension +void cspan_resize(SpanTypeN* self, size_t xdim, ...); // change the extent of each dimension -size_t cspan_size(const SpanType{N}* self); // return number of elements -unsigned cspan_rank(const SpanType{N}* self); // return number of dimensions -size_t cspan_index(const SpanType{N}* self, size_t x, ...); // index of element +size_t cspan_size(const SpanTypeN* self); // return number of elements +unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions +size_t cspan_index(const SpanTypeN* self, size_t x, ...); // index of element -ValueType* cspan_at(SpanType{N}* self, size_t x, ...); // at(): num of args decides input SpanType{N}. -SpanType cspan_at2(SpanType2* self, size_t x); // return a 1D subarray cspan. -SpanType{N} cspan_at3(SpanType3* self, size_t x, ...); // atN(): N decides input SpanType, -SpanType{N} cspan_at4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanType{N}. - -SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); // return a slice of a 1D cspan -SpanType2 cspan_subspan2(const SpanType2 self, size_t offset, size_t count); // return a slice of a 2D cspan -SpanType3 cspan_subspan3(const SpanType3 self, size_t offset, size_t count); // return a slice of a 3D cspan -SpanType4 cspan_subspan4(const SpanType4 self, size_t offset, size_t count); // return a slice of a 4D cspan - -SpanType{N}_iter SpanType_begin(const SpanType{N}* self); -SpanType{N}_iter SpanType_end(const SpanType{N}* self); -void SpanType_next(SpanType{N}_iter* it); +ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args decides input SpanTypeN. +ValueType* cspan_front(SpanTypeN* self); +ValueType* cspan_back(SpanTypeN* self); + +SpanType cspan_subdim2(SpanType2* self, size_t x); // return a cspan from a 2D SpanType. +SpanTypeN cspan_subdim3(SpanType3* self, size_t x, ...); // subdimN(): N decides input SpanType, +SpanTypeN cspan_subdim4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanTypeN. + + // return a subspan of same rank: +SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); +SpanType2 cspan_subspan2(const SpanType2 self, size_t offset, size_t count); +SpanType3 cspan_subspan3(const SpanType3 self, size_t offset, size_t count); +SpanType4 cspan_subspan4(const SpanType4 self, size_t offset, size_t count); + +SpanTypeN_iter SpanType_begin(const SpanTypeN* self); +SpanTypeN_iter SpanType_end(const SpanTypeN* self); +void SpanType_next(SpanTypeN_iter* it); ``` ## Types -| Type name | Type definition | Used to represent... | -|:--------------------|:-----------------------------------------------|:---------------------| -| SpanType{N} | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | -| SpanType{N}`_value` | `ValueType` | The ValueType | -| SpanType{N}`_iter` | `struct { ValueType *ref; ... }` | Iterator type | +| Type name | Type definition | Used to represent... | +|:------------------|:-----------------------------------------------|:---------------------| +| SpanTypeN | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | +| SpanTypeN`_value` | `ValueType` | The ValueType | +| SpanTypeN`_iter` | `struct { ValueType *ref; ... }` | Iterator type | ## Example ```c #include +#include #define i_val float #include -#include -using_cspan3(FS, float); // Shorthand to define span types FS, FS2, and FS3. +using_cspan3(Span, float); // Shorthand to define span types Span, Span2, and Span3. int main() { int xd = 6, yd = 4, zd = 3; - c_AUTO (cstack_float, vec) { + c_AUTO (cstack_float, vec) + { c_FORRANGE (i, xd*yd*zd) cstack_float_push(&vec, i); // define "span3[xd][yd][zd]" - FS3 span3 = cspan_make(vec.data, xd, yd, zd); - *cspan_at(&span3, 4, 3, 2) = 3.14f; + Span3 span3 = cspan_make(vec.data, xd, yd, zd); + *cspan_at(&span3, 4, 3, 2) = 3.14159f; printf("index: %d", (int)cspan_index(&span3, 4, 3, 2)); - FS span1 = cspan_at3(&span3, 4, 3); + Span span1 = cspan_subdim3(&span3, 4, 3); printf("\niterate span1: "); - c_FOREACH (i, FS, span1) + c_FOREACH (i, Span, span1) printf("%g ", *i.ref); - FS2 span2 = cspan_at3(&span3, 4); + Span2 span2 = cspan_subdim3(&span3, 4); printf("\niterate span2: "); - c_FOREACH (i, FS2, span2) + c_FOREACH (i, Span2, span2) printf("%g ", *i.ref); puts("\niterate span3 by dimensions:"); @@ -103,16 +108,16 @@ int main() Output: ``` index: 59 -iterate span1: 57 58 3.14 -iterate span2: 48 49 50 51 52 53 54 55 56 57 58 3.14 +iterate span1: 57 58 3.14159 +iterate span2: 48 49 50 51 52 53 54 55 56 57 58 3.14159 iterate span3 by dimensions: - 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11 | + 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11 | 12 13 14 | 15 16 17 | 18 19 20 | 21 22 23 | 24 25 26 | 27 28 29 | 30 31 32 | 33 34 35 | 36 37 38 | 39 40 41 | 42 43 44 | 45 46 47 | - 48 49 50 | 51 52 53 | 54 55 56 | 57 58 3.14 | + 48 49 50 | 51 52 53 | 54 55 56 | 57 58 3.14159 | 60 61 62 | 63 64 65 | 66 67 68 | 69 70 71 | -3.14 -3.14 -3.14 +3.14159 +3.14159 +3.14159 ``` diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 88e10b07..96439c64 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -120,19 +120,19 @@ int demo2() { #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) \ +#define cspan_subdim4(...) c_MACRO_OVERLOAD(cspan_subdim4, __VA_ARGS__) +#define cspan_subdim3(...) c_MACRO_OVERLOAD(cspan_subdim3, __VA_ARGS__) +#define cspan_subdim2(self, x) \ {.data=cspan_at(self, x, 0), .dim={(self)->dim[1]}} -#define cspan_at32(self, x) \ +#define cspan_subdim32(self, x) \ {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}} -#define cspan_at33(self, x, y) \ +#define cspan_subdim33(self, x, y) \ {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} -#define cspan_at42(self, x) \ +#define cspan_subdim42(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) \ +#define cspan_subdim43(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) \ +#define cspan_subdim44(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) diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 945741b1..e882d62a 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -36,7 +36,7 @@ int main() puts(""); // or iterate a subspan... - ispan2 sub = cspan_at3(&ms3, 1); + ispan2 sub = cspan_subdim3(&ms3, 1); printf("sub: "); c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); -- cgit v1.2.3 From af74e5f44af8f03ec3b0b2ffd3fd577210707e49 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 22 Jan 2023 16:22:51 +0100 Subject: Added cspan_init() for static initializing and a minor fix. Added cregex replace tests. --- include/stc/cspan.h | 6 +++- misc/examples/multidim.c | 73 ++++++++++++++++++++++-------------------------- misc/tests/cregex_test.c | 47 +++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 40 deletions(-) (limited to 'include') diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 96439c64..1e12781e 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -61,7 +61,7 @@ int demo2() { #include "ccommon.h" #define using_cspan(Self, T, RANK) \ - typedef T Self##_value, Self##_raw; \ + typedef T Self##_value; typedef T Self##_raw; \ typedef struct { Self##_value *ref, *end; } Self##_iter; \ typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ \ @@ -90,6 +90,10 @@ int demo2() { #define cspan_make(array, ...) \ {.data=array, .dim={__VA_ARGS__}} +/* For static initialization use , cspan_init(). c_init() only works for non-static. */ +#define cspan_init(SpanType, ...) \ + {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} + /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ {.data=(container)->data, .dim={(uint32_t)(container)->_len}} diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index e882d62a..69f818cc 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -5,53 +5,48 @@ #include using_cspan3(ispan, int); -/* -using_cspan(ispan, int, 1); -using_cspan(ispan2, int, 2); -using_cspan(ispan3, int, 3); -*/ int main() { - cstack_int v = {0}; - c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) - cstack_int_push(&v, *i.ref); + cstack_int v = {0}; + c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) + cstack_int_push(&v, *i.ref); - // View data as contiguous memory representing 12 ints - ispan ms1 = cspan_from(&v); - // View data as contiguous memory representing 2 rows of 6 ints each - ispan2 ms2 = cspan_make(v.data, 2, 6); - // View the same data as a 3D array 2 x 3 x 2 - ispan3 ms3 = cspan_make(v.data, 2, 3, 2); + // View data as contiguous memory representing 12 ints + ispan ms1 = cspan_from(&v); + // View data as contiguous memory representing 2 rows of 6 ints each + ispan2 ms2 = cspan_make(v.data, 2, 6); + // View the same data as a 3D array 2 x 3 x 2 + ispan3 ms3 = cspan_make(v.data, 2, 3, 2); - // write data using 2D view - for (unsigned i=0; i != ms2.dim[0]; i++) - for (unsigned j=0; j != ms2.dim[1]; j++) - *cspan_at(&ms2, i, j) = i*1000 + j; + // write data using 2D view + for (unsigned i=0; i != ms2.dim[0]; i++) + for (unsigned j=0; j != ms2.dim[1]; j++) + *cspan_at(&ms2, i, j) = i*1000 + j; - // print all items using 1D view - printf("all: "); - for (unsigned i=0; i != cspan_size(&ms1); i++) - printf(" %d", *cspan_at(&ms1, i)); - puts(""); + // print all items using 1D view + printf("all: "); + for (unsigned i=0; i != cspan_size(&ms1); i++) + printf(" %d", *cspan_at(&ms1, i)); + puts(""); - // or iterate a subspan... - ispan2 sub = cspan_subdim3(&ms3, 1); - printf("sub: "); - c_FOREACH (i, ispan2, sub) - printf(" %d", *i.ref); - puts(""); + // or iterate a subspan... + ispan2 sub = cspan_subdim3(&ms3, 1); + printf("sub: "); + c_FOREACH (i, ispan2, sub) + printf(" %d", *i.ref); + puts(""); - // read back using 3D view - for (unsigned i=0; i != ms3.dim[0]; i++) - { - printf("slice @ i = %u\n", i); - for (unsigned j=0; j != ms3.dim[1]; j++) + // read back using 3D view + for (unsigned i=0; i != ms3.dim[0]; i++) { - for (unsigned k=0; k != ms3.dim[2]; k++) - printf("%d ", *cspan_at(&ms3, i, j, k)); - puts(""); + printf("slice @ i = %u\n", i); + for (unsigned j=0; j != ms3.dim[1]; j++) + { + for (unsigned k=0; k != ms3.dim[2]; k++) + printf("%d ", *cspan_at(&ms3, i, j, k)); + puts(""); + } } - } - cstack_int_drop(&v); + cstack_int_drop(&v); } diff --git a/misc/tests/cregex_test.c b/misc/tests/cregex_test.c index 8ac268f1..bd13d16a 100644 --- a/misc/tests/cregex_test.c +++ b/misc/tests/cregex_test.c @@ -233,3 +233,50 @@ CTEST(cregex, captures_cap) ASSERT_TRUE(cregex_is_match(&re, "abcdcdcd")); } } + +static bool add_10_years(int i, csview match, cstr* out) { + if (i == 1) { // group 1 matches year + int year; + sscanf(match.str, "%4d", &year); // scan 4 chars only + cstr_printf(out, "%04d", year + 10); + return true; + } + return false; +} + +CTEST(cregex, replace) +{ + const char* pattern = "\\b(\\d\\d\\d\\d)-(1[0-2]|0[1-9])-(3[01]|[12][0-9]|0[1-9])\\b"; + const char* input = "start date: 2015-12-31, end date: 2022-02-28"; + + c_AUTO (cstr, str) { + /* replace with a fixed string, extended all-in-one call: */ + cstr_take(&str, cregex_replace_pattern(pattern, input, "YYYY-MM-DD")); + ASSERT_TRUE(cstr_equals(&str, "start date: YYYY-MM-DD, end date: YYYY-MM-DD")); + + /* US date format, and add 10 years to dates: */ + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); + ASSERT_TRUE(cstr_equals(&str, "start date: 2025/31/12, end date: 2032/28/02")); + + /* Wrap first date inside []: */ + cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT)); + ASSERT_TRUE(cstr_equals(&str, "start date: [2015-12-31], end date: 2022-02-28")); + + /* Wrap all words in ${} */ + cstr_take(&str, cregex_replace_pattern("[a-z]+", "52 apples and 31 mangoes", "$${$0}")); + ASSERT_TRUE(cstr_equals(&str, "52 ${apples} ${and} 31 ${mangoes}")); + + /* Compile RE separately */ + c_WITH (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) { + ASSERT_EQ(cregex_captures(&re), 4); + + /* European date format. */ + cstr_take(&str, cregex_replace(&re, input, "$3.$2.$1")); + ASSERT_TRUE(cstr_equals(&str, "start date: 31.12.2015, end date: 28.02.2022")); + + /* Strip out everything but the matches */ + cstr_take(&str, cregex_replace_sv(&re, csview_from(input), "$3.$2.$1;", 0, NULL, CREG_R_STRIP)); + ASSERT_TRUE(cstr_equals(&str, "31.12.2015;28.02.2022;")); + } + } +} -- cgit v1.2.3 From d23fb483c4fc98a68eb9cdb0d544002c26accb0a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 23 Jan 2023 00:00:15 +0100 Subject: Renamed c_init() to c_make(). --- include/stc/ccommon.h | 6 +++--- include/stc/cspan.h | 2 +- misc/examples/printspan.c | 17 ++++++++++------- 3 files changed, 14 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 35bc9335..286450ec 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -112,10 +112,10 @@ #define c_no_hash (1<<6) #define c_no_lookup (c_no_cmp|c_no_eq|c_no_hash) -#define c_init(C, ...) \ - C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) +/* Function macros and others */ -/* Function macros and various others */ +#define c_make(C, ...) \ + C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) typedef const char* crawstr; #define crawstr_cmp(xp, yp) strcmp(*(xp), *(yp)) diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 1e12781e..502f1b71 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -90,7 +90,7 @@ int demo2() { #define cspan_make(array, ...) \ {.data=array, .dim={__VA_ARGS__}} -/* For static initialization use , cspan_init(). c_init() only works for non-static. */ +/* For static initialization use , cspan_init(). c_make() only works for non-static. */ #define cspan_init(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index 8d0b0321..227fd2ed 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -27,25 +27,28 @@ int main() c_AUTO (cdeq_int, deq) c_AUTO (cset_str, set) { - printMe( c_init(intspan, {1, 2, 3}) ); + intspan sp1 = cspan_init(intspan, {1, 2}); + printMe( sp1 ); + + printMe( c_make(intspan, {1, 2, 3}) ); int arr[] = {1, 2, 3, 4, 5, 6}; - intspan span = cspan_from_array(arr); - printMe( (intspan)cspan_subspan(&span, 1, 4) ); + intspan sp2 = cspan_from_array(arr); + printMe( (intspan)cspan_subspan(&sp2, 1, 4) ); c_FORLIST (i, int, {1, 2, 3, 4, 5}) cvec_int_push(&vec, *i.ref); printMe( (intspan)cspan_from(&vec) ); - printMe( span ); + printMe( sp2 ); - stk = c_init(cstack_int, {1, 2, 3, 4, 5, 6, 7}); + stk = c_make(cstack_int, {1, 2, 3, 4, 5, 6, 7}); printMe( (intspan)cspan_from(&stk) ); - deq = c_init(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); + deq = c_make(cdeq_int, {1, 2, 3, 4, 5, 6, 7, 8}); printMe( (intspan)cspan_from(&deq) ); - set = c_init(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"}); + set = c_make(cset_str, {"1", "2", "3", "4", "5", "6", "7", "8", "9"}); printf("%d:", (int)cset_str_size(&set)); c_FOREACH (e, cset_str, set) printf(" %s", cstr_str(e.ref)); -- cgit v1.2.3 From c2d862f377f9bf2f276db7ba41ea835117603881 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 23 Jan 2023 17:15:20 +0100 Subject: Changed c_OVERLOADMACRO() internally. --- include/stc/algo/crange.h | 6 +++--- include/stc/algo/filter.h | 4 ++-- include/stc/ccommon.h | 42 +++++++++++++++++++++--------------------- include/stc/cspan.h | 10 +++++----- include/stc/csview.h | 6 +++--- 5 files changed, 34 insertions(+), 34 deletions(-) (limited to 'include') diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h index 5e1610d9..39236ae0 100644 --- a/include/stc/algo/crange.h +++ b/include/stc/algo/crange.h @@ -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/filter.h b/include/stc/algo/filter.h index 0f850f41..9037c984 100644 --- a/include/stc/algo/filter.h +++ b/include/stc/algo/filter.h @@ -61,10 +61,10 @@ int main() #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/ccommon.h b/include/stc/ccommon.h index 286450ec..9315dd99 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -56,7 +56,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__ @@ -124,8 +124,8 @@ typedef const char* crawstr; #define c_ARRAYLEN(a) (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_COMPOUND(csview){str, n}) +#define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit)) +#define c_SV_2(str, n) (c_COMPOUND(csview){str, n}) #define c_ARGSV(sv) (int)(sv).size, (sv).str /* use with "%.*s" */ #define c_PAIR(ref) (ref)->first, (ref)->second @@ -168,9 +168,9 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, /* 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)) @@ -185,11 +185,11 @@ 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 @@ -205,34 +205,34 @@ 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_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))) /* 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_if4(it, C, cnt, pred) do { \ +#define c_find_if_4(it, C, cnt, pred) do { \ size_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 { \ +#define c_find_if_5(it, C, start, end, pred) do { \ size_t index = 0; \ const C##_value* _endref = (end).ref; \ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 502f1b71..5ba408a8 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -128,15 +128,15 @@ int demo2() { #define cspan_subdim3(...) c_MACRO_OVERLOAD(cspan_subdim3, __VA_ARGS__) #define cspan_subdim2(self, x) \ {.data=cspan_at(self, x, 0), .dim={(self)->dim[1]}} -#define cspan_subdim32(self, x) \ +#define cspan_subdim3_2(self, x) \ {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}} -#define cspan_subdim33(self, x, y) \ +#define cspan_subdim3_3(self, x, y) \ {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} -#define cspan_subdim42(self, x) \ +#define cspan_subdim4_2(self, x) \ {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}} -#define cspan_subdim43(self, x, y) \ +#define cspan_subdim4_3(self, x, y) \ {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}} -#define cspan_subdim44(self, x, y, z) \ +#define cspan_subdim4_4(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) diff --git a/include/stc/csview.h b/include/stc/csview.h index fb860a66..c53cd1c0 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -27,12 +27,12 @@ #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); -- cgit v1.2.3 From 782ab14cac327134614188676a196fcfcb9d0ba1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 24 Jan 2023 20:19:42 +0100 Subject: Added python single-header generator + fixed include mistake. --- include/stc/cregex.h | 4 +-- include/stc/utf8.h | 2 +- misc/archived/new_arr.c | 57 ----------------------------------- src/libstc.c | 11 ------- src/singleheader.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+), 71 deletions(-) delete mode 100644 misc/archived/new_arr.c delete mode 100644 src/libstc.c create mode 100644 src/singleheader.py (limited to 'include') diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 4bb17938..24faba6f 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -142,8 +142,8 @@ void cregex_drop(cregex* self); #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/utf8.h b/include/stc/utf8.h index f30e76ac..ea661bb6 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -115,6 +115,6 @@ STC_INLINE size_t utf8_pos(const char* s, size_t index) #endif // UTF8_H_INCLUDED #if defined i_extern || defined STC_EXTERN -# include "../src/utf8code.c" +# include "../../src/utf8code.c" # undef i_extern #endif diff --git a/misc/archived/new_arr.c b/misc/archived/new_arr.c deleted file mode 100644 index 1006439d..00000000 --- a/misc/archived/new_arr.c +++ /dev/null @@ -1,57 +0,0 @@ -#include - -#define i_val int -#include "carr2.h" - -#define i_val int -#include "carr3.h" - -#define i_val_str -#include "carr2.h" - -int main() -{ - int w = 7, h = 5, d = 3; - - c_WITH (carr2_int volume = carr2_int_new_uninit(w, h), carr2_int_drop(&volume)) - { - int *dat = carr2_int_data(&volume); - for (size_t i = 0; i < carr2_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - printf(" %d", volume.data[x][y]); - puts(""); - - c_FOREACH (i, carr2_int, volume) - printf(" %d", *i.ref); - puts("\n"); - } - - c_WITH (carr3_int volume = carr3_int_new_uninit(w, h, d), carr3_int_drop(&volume)) - { - int *dat = carr3_int_data(&volume); - for (size_t i = 0; i < carr3_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - for (size_t z = 0; z < volume.zdim; ++z) - printf(" %d", volume.data[x][y][z]); - puts(""); - - c_FOREACH (i, carr3_int, volume) - printf(" %d", *i.ref); - puts(""); - } - - c_WITH (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) - { - cstr_assign(&text2d.data[2][1], "hello"); - cstr_assign(&text2d.data[4][0], "world"); - - c_FOREACH (i, carr2_str, text2d) - printf("line: %s\n", cstr_str(i.ref)); - } -} diff --git a/src/libstc.c b/src/libstc.c deleted file mode 100644 index e8980a7a..00000000 --- a/src/libstc.c +++ /dev/null @@ -1,11 +0,0 @@ -#define STC_EXTERN // implement common extern, non-templated functions and dependencies. - -#define i_val int -#define i_header // don't implement clist_int itself, just dummy declare it. -#include "../include/stc/clist.h" - -#define STC_IMPLEMENT // implement the following. - -#include "../include/stc/cregex.h" -#include "../include/stc/csview.h" -//#include "../include/stc/crandom.h" diff --git a/src/singleheader.py b/src/singleheader.py new file mode 100644 index 00000000..f5272cfb --- /dev/null +++ b/src/singleheader.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +import re +import sys +import os +from os.path import dirname, join as path_join, abspath, basename, exists + +extra_paths = [path_join(dirname(abspath(__file__)), "include")] + + +def find_file(included_name, current_file): + current_dir = dirname(abspath(current_file)) + for idir in [current_dir] + extra_paths: + try_path = path_join(idir, included_name) + if exists(try_path): + return try_path + return None + + +def process_file( + file_path, + out_lines=[], + processed_files=[], +): + out_lines += "// ### BEGIN_FILE_INCLUDE: " + basename(file_path) + '\n' + comment_block = False + with open(file_path, "r") as f: + for line in f: + is_comment = comment_block + if re.search('/\*.*?\*/', line): + pass + elif re.search('^\\s*/\*', line): + comment_block, is_comment = True, True + elif re.search('\*/', line): + comment_block = False + + if is_comment: + continue + + m_inc = re.search('^\\s*# *include\\s*[<"](.+)[>"]', line) if not is_comment else False + if m_inc: + inc_name = m_inc.group(1) + inc_path = find_file(inc_name, file_path) + if inc_path not in processed_files: + if inc_path is not None: + processed_files += [inc_path] + process_file( + inc_path, + out_lines, + processed_files, + ) + else: + # assume it's a system header + out_lines += [line] + continue + m_once = re.match('^\\s*# *pragma once\\s*', line) if not is_comment else False + # ignore pragma once; we're handling it here + if m_once: + continue + # otherwise, just add the line to the output + if line[-1] != '\n': + line += '\n' + out_lines += [line] + out_lines += "// ### END_FILE_INCLUDE: " + basename(file_path) + '\n' + return ( + "".join(out_lines) + ) + + +if __name__ == "__main__": + print( + process_file( + abspath(sys.argv[1]), + [], + # We use an include guard instead of `#pragma once` because Godbolt will + # cause complaints about `#pragma once` when they are used in URL includes. + [abspath(sys.argv[1])], + ) + ) -- cgit v1.2.3 From af4c2d6d2f3353364632701ef8208a9dcbe18623 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 25 Jan 2023 08:37:23 +0100 Subject: Reverted cspan_subdim*() to cspan_at*(). Added cspan_flatten() constructor. --- docs/cspan_api.md | 11 ++++++----- include/stc/cspan.h | 19 +++++++++++-------- misc/examples/multidim.c | 2 +- 3 files changed, 18 insertions(+), 14 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index d6caff4e..ce9236c3 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -25,6 +25,7 @@ SpanTypeN cspan_make(ValueType* data, size_t xdim, ...); // make SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array +SpanType cspan_flatten(SpanTypeN* span); // create a 1D cspan from a multidim span void cspan_resize(SpanTypeN* self, size_t xdim, ...); // change the extent of each dimension size_t cspan_size(const SpanTypeN* self); // return number of elements @@ -35,9 +36,9 @@ ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); -SpanType cspan_subdim2(SpanType2* self, size_t x); // return a cspan from a 2D SpanType. -SpanTypeN cspan_subdim3(SpanType3* self, size_t x, ...); // subdimN(): N decides input SpanType, -SpanTypeN cspan_subdim4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanTypeN. +SpanType cspan_at2(SpanType2* self, size_t x); // return a cspan from a 2D SpanType. +SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // atN(): N decides input SpanType, +SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanTypeN. // return a subspan of same rank: SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); @@ -79,12 +80,12 @@ int main() *cspan_at(&span3, 4, 3, 2) = 3.14159f; printf("index: %d", (int)cspan_index(&span3, 4, 3, 2)); - Span span1 = cspan_subdim3(&span3, 4, 3); + Span span1 = cspan_at3(&span3, 4, 3); printf("\niterate span1: "); c_FOREACH (i, Span, span1) printf("%g ", *i.ref); - Span2 span2 = cspan_subdim3(&span3, 4); + Span2 span2 = cspan_at3(&span3, 4); printf("\niterate span2: "); c_FOREACH (i, Span2, span2) printf("%g ", *i.ref); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 5ba408a8..2b600a2a 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -94,6 +94,9 @@ int demo2() { #define cspan_init(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} +#define cspan_flatten(span) \ + {.data=(span)->data, .dim={(uint32_t)cspan_size(span)}} + /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ {.data=(container)->data, .dim={(uint32_t)(container)->_len}} @@ -124,19 +127,19 @@ int demo2() { #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_subdim4(...) c_MACRO_OVERLOAD(cspan_subdim4, __VA_ARGS__) -#define cspan_subdim3(...) c_MACRO_OVERLOAD(cspan_subdim3, __VA_ARGS__) -#define cspan_subdim2(self, x) \ +#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_subdim3_2(self, x) \ +#define cspan_at3_2(self, x) \ {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}} -#define cspan_subdim3_3(self, x, y) \ +#define cspan_at3_3(self, x, y) \ {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} -#define cspan_subdim4_2(self, x) \ +#define cspan_at4_2(self, x) \ {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}} -#define cspan_subdim4_3(self, x, y) \ +#define cspan_at4_3(self, x, y) \ {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}} -#define cspan_subdim4_4(self, x, y, z) \ +#define cspan_at4_4(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) diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 69f818cc..3eef1497 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -31,7 +31,7 @@ int main() puts(""); // or iterate a subspan... - ispan2 sub = cspan_subdim3(&ms3, 1); + ispan2 sub = cspan_at3(&ms3, 1); printf("sub: "); c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); -- cgit v1.2.3 From 68fd366ceaa919293d348ce15c2596d485160cec Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 25 Jan 2023 20:18:02 +0100 Subject: Updates on cspan ++. --- docs/cspan_api.md | 21 ++-- include/stc/cspan.h | 8 +- misc/examples/forfilter.c | 8 +- misc/examples/gauss1.c | 2 +- misc/examples/multidim.c | 4 +- misc/examples/prime.c | 3 +- misc/examples/printspan.c | 2 +- misc/examples/shape.c | 12 +-- src/utf8code.c | 243 ++++++++++++++++++++++++---------------------- 9 files changed, 156 insertions(+), 147 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index ce9236c3..6da2acca 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -18,27 +18,28 @@ using_cspan3(S, ValueType); // define span types S, S2, S3 with rank using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. ``` ## Methods -Note that `cspan_make()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type +Note that `cspan_multidim()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. ```c -SpanTypeN cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan -SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a compatible STC container -SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from a C array +SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1d-dimensional cspan from values +SpanTypeN cspan_multidim(ValueType* data, size_t xdim, ...); // create a multi-dimensional cspan +SpanType cspan_from(STCContainer* cnt); // create a 1d cspan from a compatible STC container +SpanType cspan_from_array(ValueType array[]); // create a 1d cspan from a C array -SpanType cspan_flatten(SpanTypeN* span); // create a 1D cspan from a multidim span +SpanType cspan_flatten(SpanTypeN* span); // create a 1d cspan from a multidim span void cspan_resize(SpanTypeN* self, size_t xdim, ...); // change the extent of each dimension size_t cspan_size(const SpanTypeN* self); // return number of elements unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions size_t cspan_index(const SpanTypeN* self, size_t x, ...); // index of element -ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args decides input SpanTypeN. +ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args specifies rank of input span. ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); -SpanType cspan_at2(SpanType2* self, size_t x); // return a cspan from a 2D SpanType. -SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // atN(): N decides input SpanType, -SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // and num of args decides returned SpanTypeN. +SpanType cspan_at2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span. +SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1 or 2d subspan from a 3d span. +SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. // return a subspan of same rank: SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); @@ -76,7 +77,7 @@ int main() cstack_float_push(&vec, i); // define "span3[xd][yd][zd]" - Span3 span3 = cspan_make(vec.data, xd, yd, zd); + Span3 span3 = cspan_multidim(vec.data, xd, yd, zd); *cspan_at(&span3, 4, 3, 2) = 3.14159f; printf("index: %d", (int)cspan_index(&span3, 4, 3, 2)); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 2b600a2a..5b0358e2 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -30,7 +30,7 @@ using_cspan(Intspan, int, 1); int demo1() { float raw[4*5]; - Span2f ms = cspan_make(raw, 4, 5); + Span2f ms = cspan_multidim(raw, 4, 5); for (size_t i=0; isecond * StdDev * Scale * 2.5 / N); + int n = (int)(i.ref->second * StdDev * Scale * 2.5 / (double)N); if (n > 0) { printf("%4d ", i.ref->first); c_FORRANGE (n) printf("*"); diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 3eef1497..8a5492dc 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -15,9 +15,9 @@ int main() // View data as contiguous memory representing 12 ints ispan ms1 = cspan_from(&v); // View data as contiguous memory representing 2 rows of 6 ints each - ispan2 ms2 = cspan_make(v.data, 2, 6); + ispan2 ms2 = cspan_multidim(v.data, 2, 6); // View the same data as a 3D array 2 x 3 x 2 - ispan3 ms3 = cspan_make(v.data, 2, 3, 2); + ispan3 ms3 = cspan_multidim(v.data, 2, 3, 2); // write data using 2D view for (unsigned i=0; i != ms2.dim[0]; i++) diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 4a6b0f68..5c8d65d3 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -43,7 +43,8 @@ int main(void) puts(""); puts("Show the last 50 primes using a temporary crange generator:"); - c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2) + crange R = crange_make(n - 1, 0, -2); + c_FORFILTER (i, crange, R , cbits_test(&primes, *i.ref>>1) , c_flt_take(i, 50)) { printf("%lld ", *i.ref); diff --git a/misc/examples/printspan.c b/misc/examples/printspan.c index 227fd2ed..82b54367 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -27,7 +27,7 @@ int main() c_AUTO (cdeq_int, deq) c_AUTO (cset_str, set) { - intspan sp1 = cspan_init(intspan, {1, 2}); + intspan sp1 = cspan_make(intspan, {1, 2}); printMe( sp1 ); printMe( c_make(intspan, {1, 2, 3}) ); diff --git a/misc/examples/shape.c b/misc/examples/shape.c index ed0c3fe1..75a5e174 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -50,12 +50,12 @@ typedef struct { Point p[3]; } Triangle; -static struct ShapeAPI Triangle_api; +extern struct ShapeAPI Triangle_api; Triangle Triangle_from(Point a, Point b, Point c) { - Triangle t = {{.api=&Triangle_api}, .p={a, b, c}}; + Triangle t = {.shape={.api=&Triangle_api}, .p={a, b, c}}; return t; } @@ -68,7 +68,7 @@ static void Triangle_draw(const Shape* shape) self->p[2].x, self->p[2].y); } -static struct ShapeAPI Triangle_api = { +struct ShapeAPI Triangle_api = { .drop = Shape_drop, .draw = Triangle_draw, }; @@ -85,12 +85,12 @@ typedef struct { PointVec points; } Polygon; -static struct ShapeAPI Polygon_api; +extern struct ShapeAPI Polygon_api; Polygon Polygon_init(void) { - Polygon p = {{.api=&Polygon_api}, .points=PointVec_init()}; + Polygon p = {.shape={.api=&Polygon_api}, .points=PointVec_init()}; return p; } @@ -116,7 +116,7 @@ static void Polygon_draw(const Shape* shape) puts(""); } -static struct ShapeAPI Polygon_api = { +struct ShapeAPI Polygon_api = { .drop = Polygon_drop, .draw = Polygon_draw, }; diff --git a/src/utf8code.c b/src/utf8code.c index ecf79880..3ad47941 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -122,13 +122,18 @@ typedef struct { int nr16; } UGroup; -static const UGroup unicode_groups[U8G_SIZE]; +#ifndef __cplusplus +static +#else +extern +#endif +const UGroup _utf8_unicode_groups[U8G_SIZE]; bool utf8_isgroup(int group, uint32_t c) { - for (int j=0; j Date: Thu, 26 Jan 2023 09:51:02 +0100 Subject: Updated API for cregex. --- docs/cregex_api.md | 17 ++++----- include/stc/ccommon.h | 2 +- include/stc/cregex.h | 81 ++++++++++++++++++++++++++----------------- misc/examples/regex_replace.c | 6 ++-- src/cregex.c | 17 +++++---- 5 files changed, 71 insertions(+), 52 deletions(-) (limited to 'include') diff --git a/docs/cregex_api.md b/docs/cregex_api.md index c01f324c..3d4392b0 100644 --- a/docs/cregex_api.md +++ b/docs/cregex_api.md @@ -23,33 +23,34 @@ enum { }; cregex cregex_init(void); -cregex cregex_from(const char* pattern, int cflags); +cregex cregex_from(const char* pattern, int cflags = CREG_DEFAULT); /* return CREG_OK, or negative error code on failure */ -int cregex_compile(cregex *self, const char* pattern, int cflags); +int cregex_compile(cregex *self, const char* pattern, int cflags = CREG_DEFAULT); /* num. of capture groups in regex. 0 if RE is invalid. First group is the full match */ int cregex_captures(const cregex* self); /* return CREG_OK, CREG_NOMATCH, or CREG_MATCHERROR */ -int cregex_find(const cregex* re, const char* input, csview match[], int mflags); +int cregex_find(const cregex* re, const char* input, csview match[], int mflags = CREG_DEFAULT); /* Search inside input string-view only */ int cregex_find_sv(const cregex* re, csview input, csview match[]); /* All-in-one search (compile + find + drop) */ -int cregex_find_pattern(const char* pattern, const char* input, csview match[], int cmflags); +int cregex_find_pattern(const char* pattern, const char* input, csview match[], int cmflags = CREG_DEFAULT); /* Check if there are matches in input */ bool cregex_is_match(const cregex* re, const char* input); /* Replace all matches in input */ -cstr cregex_replace(const cregex* re, const char* input, const char* replace); +cstr cregex_replace(const cregex* re, const char* input, const char* replace, count = MAX_INT); /* Replace count matches in input string-view. Optionally transform replacement with mfun. */ +cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count = MAX_INT); cstr cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); /* All-in-one replacement (compile + find/replace + drop) */ -cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace); -cstr cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count, - bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); +cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, count = MAX_INT); +cstr cregex_replace_pattern(const char* pattern, const char* input, const char* replace, unsigned count, + bool(*mfun)(int capgrp, csview match, cstr* mstr), int rflags); void cregex_drop(cregex* self); /* destroy */ ``` diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 9315dd99..b61c1972 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -56,7 +56,7 @@ /* Macro overloading feature support based on: https://rextester.com/ONP80107 */ #define c_MACRO_OVERLOAD(name, ...) \ - c_PASTE(c_CONCAT(name, _), c_NUMARGS(__VA_ARGS__))(__VA_ARGS__) + c_PASTE(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__ diff --git a/include/stc/cregex.h b/include/stc/cregex.h index 24faba6f..206c319d 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -33,6 +33,7 @@ THE SOFTWARE. #include #include #include "forward.h" // csview +#include "ccommon.h" enum { CREG_DEFAULT = 0, @@ -81,64 +82,82 @@ 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 */ +unsigned 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, ~0U) +#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, unsigned 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) { +/* 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, ~0U) + +STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char* replace, unsigned count) { csview sv = {input, strlen(input)}; - return cregex_replace_sv(re, sv, replace, ~0U, NULL, CREG_DEFAULT); + 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, ~0U) +#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, unsigned 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 diff --git a/misc/examples/regex_replace.c b/misc/examples/regex_replace.c index a170856b..ec38ca56 100644 --- a/misc/examples/regex_replace.c +++ b/misc/examples/regex_replace.c @@ -26,15 +26,15 @@ int main() printf("fixed: %s\n", cstr_str(&str)); /* US date format, and add 10 years to dates: */ - cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "$1/$3/$2", 0, add_10_years, CREG_DEFAULT)); printf("us+10: %s\n", cstr_str(&str)); /* Wrap first date inside []: */ - cstr_take(&str, cregex_replace_pattern_ex(pattern, input, "[$0]", 1, NULL, CREG_DEFAULT)); + cstr_take(&str, cregex_replace_pattern(pattern, input, "[$0]")); printf("brack: %s\n", cstr_str(&str)); /* Shows how to compile RE separately */ - c_WITH (cregex re = cregex_from(pattern, CREG_DEFAULT), cregex_drop(&re)) { + c_WITH (cregex re = cregex_from(pattern), cregex_drop(&re)) { if (cregex_captures(&re) == 0) continue; /* break c_WITH */ /* European date format. */ diff --git a/src/cregex.c b/src/cregex.c index d2b54ef1..cf2f8eaf 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -1239,7 +1239,7 @@ _build_subst(const char* replace, unsigned nmatch, const csview match[], */ int -cregex_compile(cregex *self, const char* pattern, int cflags) { +cregex_compile_3(cregex *self, const char* pattern, int cflags) { _Parser par; self->prog = _regcomp1(self->prog, &par, pattern, cflags); return self->error = par.error; @@ -1251,8 +1251,7 @@ cregex_captures(const cregex* self) { } int -cregex_find(const cregex* re, const char* input, - csview match[], int mflags) { +cregex_find_4(const cregex* re, const char* input, csview match[], int mflags) { int res = _regexec(re->prog, input, cregex_captures(re), match, mflags); switch (res) { case 1: return CREG_OK; @@ -1262,8 +1261,8 @@ cregex_find(const cregex* re, const char* input, } int -cregex_find_pattern(const char* pattern, const char* input, - csview match[], int cmflags) { +cregex_find_pattern_4(const char* pattern, const char* input, + csview match[], int cmflags) { cregex re = cregex_init(); int res = cregex_compile(&re, pattern, cmflags); if (res != CREG_OK) return res; @@ -1273,8 +1272,8 @@ cregex_find_pattern(const char* pattern, const char* input, } cstr -cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned count, - bool (*mfun)(int, csview, cstr*), int rflags) { +cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigned count, + bool (*mfun)(int, csview, cstr*), int rflags) { cstr out = cstr_NULL; cstr subst = cstr_NULL; csview match[CREG_MAX_CAPTURES]; @@ -1296,8 +1295,8 @@ cregex_replace_sv(const cregex* re, csview input, const char* replace, unsigned } cstr -cregex_replace_pattern_ex(const char* pattern, const char* input, const char* replace, unsigned count, - bool (*mfun)(int, csview, cstr*), int crflags) { +cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, unsigned count, + bool (*mfun)(int, csview, cstr*), int crflags) { cregex re = cregex_init(); if (cregex_compile(&re, pattern, crflags) != CREG_OK) assert(0); -- cgit v1.2.3 From 5c1b1616f5a20a54c32e57b0bea49f366441356b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 27 Jan 2023 14:39:02 +0100 Subject: Added stride to cspan (md), prepared for cspan_slice() function. --- include/stc/cspan.h | 50 ++++++++++++++++++++++++++---------------------- misc/examples/multidim.c | 22 ++++++++++++++++----- misc/tests/ctest.h | 11 ++++------- 3 files changed, 48 insertions(+), 35 deletions(-) (limited to 'include') diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 5b0358e2..fabefc06 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -63,7 +63,11 @@ int demo2() { #define using_cspan(Self, T, RANK) \ typedef T Self##_value; typedef T Self##_raw; \ typedef struct { Self##_value *ref, *end; } Self##_iter; \ - typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \ + typedef struct { \ + Self##_value *data; \ + uint32_t dim[RANK]; \ + cspan_idx##RANK stride; \ + } Self; \ \ STC_INLINE Self Self##_from_n(Self##_raw* raw, const size_t n) { \ return (Self){.data=raw, .dim={(uint32_t)n}}; \ @@ -84,11 +88,13 @@ int demo2() { #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) +typedef struct { uint32_t d[1]; } cspan_idx1; +typedef struct { uint32_t d[2]; } cspan_idx2; +typedef struct { uint32_t d[3]; } cspan_idx3; +typedef struct { uint32_t d[4]; } cspan_idx4; #define cspan_multidim(array, ...) \ - {.data=array, .dim={__VA_ARGS__}} + {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} /* For static initialization, use cspan_make(). c_make() for non-static only. */ #define cspan_make(SpanType, ...) \ @@ -107,12 +113,7 @@ int demo2() { #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__))) + c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__) #define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__)) #define cspan_front(self) ((self)->data) @@ -121,40 +122,43 @@ int demo2() { #define cspan_subspan(self, offset, count) \ {.data=cspan_at(self, offset), .dim={count}} #define cspan_subspan2(self, offset, count) \ - {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[1]}} + {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[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]}} + {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, .stride={(self)->stride}} #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]}} + {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}, .stride={(self)->stride}} #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_at3_2(self, x) \ - {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}} + {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}, \ + .stride={.d={0, (self)->stride.d[2]}}} #define cspan_at3_3(self, x, y) \ {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} #define cspan_at4_2(self, x) \ - {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}} + {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ + .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3]}}} #define cspan_at4_3(self, x, y) \ - {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}} + {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, \ + .stride={.d={0, (self)->stride.d[3]}}} #define cspan_at4_4(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) +STC_INLINE size_t _cspan_i1(const uint32_t dim[1], const cspan_idx1 stri, 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_i2(const uint32_t dim[2], const cspan_idx2 stri, uint32_t x, uint32_t y) + { c_ASSERT(x < dim[0] && y < dim[1]); return stri.d[1]*x + y; } -STC_INLINE size_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) { +STC_INLINE size_t _cspan_i3(const uint32_t dim[3], const cspan_idx3 stri, 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; + return 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) { +STC_INLINE size_t _cspan_i4(const uint32_t dim[4], const cspan_idx4 stri, 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; + return 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]; diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 8a5492dc..d69a04fb 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -9,15 +9,27 @@ using_cspan3(ispan, int); int main() { cstack_int v = {0}; - c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12}) + c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}) cstack_int_push(&v, *i.ref); - // View data as contiguous memory representing 12 ints + // View data as contiguous memory representing 24 ints ispan ms1 = cspan_from(&v); + + // View the same data as a 3D array 2 x 3 x 4 + ispan3 ms3 = cspan_multidim(v.data, 2, 2, 6); + // View data as contiguous memory representing 2 rows of 6 ints each - ispan2 ms2 = cspan_multidim(v.data, 2, 6); - // View the same data as a 3D array 2 x 3 x 2 - ispan3 ms3 = cspan_multidim(v.data, 2, 3, 2); + ispan2 ms2 = cspan_at3(&ms3, 0); + ms2.data = cspan_at(&ms2, 1, 1); + ms2.dim[0] = 2; + ms2.dim[1] = 3; + for (unsigned i=0; i != ms2.dim[0]; i++) { + for (unsigned j=0; j != ms2.dim[1]; j++) + printf(" %2d", *cspan_at(&ms2, i, j)); + puts(""); + } + + ms2 = (ispan2)cspan_at3(&ms3, 0); // write data using 2D view for (unsigned i=0; i != ms2.dim[0]; i++) diff --git a/misc/tests/ctest.h b/misc/tests/ctest.h index 7edd41cc..294a6ca2 100644 --- a/misc/tests/ctest.h +++ b/misc/tests/ctest.h @@ -113,14 +113,14 @@ struct ctest { #ifdef __cplusplus #define CTEST_SETUP(sname) \ - template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) + template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) #define CTEST_TEARDOWN(sname) \ - template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* data) + template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) #define CTEST_FIXTURE(sname) \ - template void CTEST_IMPL_SETUP_FNAME(sname)(T* data) { } \ - template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* data) { } \ + template void CTEST_IMPL_SETUP_FNAME(sname)(T* self) { } \ + template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* self) { } \ struct CTEST_IMPL_DATA_SNAME(sname) #define CTEST_IMPL_CTEST(sname, tname, tskip) \ @@ -176,7 +176,6 @@ void CTEST_ERR(const char* fmt, ...) CTEST_IMPL_FORMAT_PRINTF(1, 2); // doesn't #define CTEST_F(sname, tname) CTEST_IMPL_CTEST_F(sname, tname, 0) #define CTEST_F_SKIP(sname, tname) CTEST_IMPL_CTEST_F(sname, tname, 1) - void assert_str(const char* cmp, const char* exp, const char* real, const char* caller, int line); #define ASSERT_STREQ(exp, real) assert_str("==", exp, real, __FILE__, __LINE__) #define ASSERT_STRNE(exp, real) assert_str("!=", exp, real, __FILE__, __LINE__) @@ -186,8 +185,6 @@ void assert_str(const char* cmp, const char* exp, const char* real, const char* void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const char* caller, int line); #define ASSERT_WSTREQ(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__) #define ASSERT_WSTRNE(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__) -#define ASSERT_SUBWSTR(substr, real) assert_wstr("=~", substr, real, __FILE__, __LINE__) -#define ASSERT_NOT_SUBWSTR(substr, real) assert_wstr("!~", substr, real, __FILE__, __LINE__) void assert_data(const unsigned char* exp, size_t expsize, const unsigned char* real, size_t realsize, -- cgit v1.2.3 From 8c81c9642b5f7564f06654562d929703f2d2dd0a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 27 Jan 2023 15:09:59 +0100 Subject: (Reverted) c_COMPOUND() to c_LITERAL(). --- include/stc/carc.h | 2 +- include/stc/cbits.h | 4 ++-- include/stc/cbox.h | 4 ++-- include/stc/ccommon.h | 6 +++--- include/stc/cdeq.h | 8 ++++---- include/stc/clist.h | 6 +++--- include/stc/cmap.h | 8 ++++---- include/stc/cpque.h | 2 +- include/stc/crandom.h | 4 ++-- include/stc/csmap.h | 4 ++-- include/stc/cstack.h | 4 ++-- include/stc/cstr.h | 16 ++++++++-------- include/stc/csview.h | 8 ++++---- include/stc/cvec.h | 10 +++++----- misc/archived/csmap.h | 4 ++-- misc/archived/cstr.h | 4 ++-- misc/examples/vikings.c | 4 ++-- 17 files changed, 49 insertions(+), 49 deletions(-) (limited to 'include') diff --git a/include/stc/carc.h b/include/stc/carc.h index fa2ee3b4..4f55aefb 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.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_COMPOUND(_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; } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index b0cb7a9c..232be16b 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -123,7 +123,7 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons struct { uint64_t *data64; size_t _size; } typedef i_type; -STC_INLINE cbits cbits_init(void) { return c_COMPOUND(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; } @@ -195,7 +195,7 @@ 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_COMPOUND(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; } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 6dc41ed1..0b6c29ec 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -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_COMPOUND(_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_COMPOUND(_cx_self){p}; } + { return c_LITERAL(_cx_self){p}; } // c++: std::make_unique(val) STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) { diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index b61c1972..34d297e3 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -71,12 +71,12 @@ #define c_ALLOC(T) static_cast(c_MALLOC(sizeof(T))) #define c_ALLOC_N(T, n) static_cast(c_MALLOC(sizeof(T)*(n))) #define c_NEW(T, ...) new (c_ALLOC(T)) T(__VA_ARGS__) - #define c_COMPOUND(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_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T))) - #define c_COMPOUND(T) (T) + #define c_LITERAL(T) (T) #endif #ifndef c_MALLOC #define c_MALLOC(sz) malloc(sz) @@ -125,7 +125,7 @@ typedef const char* crawstr; #define c_ARRAYLEN(a) (sizeof(a)/sizeof 0[a]) #define c_SV(...) c_MACRO_OVERLOAD(c_SV, __VA_ARGS__) #define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit)) -#define c_SV_2(str, n) (c_COMPOUND(csview){str, n}) +#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 diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index befb4146..d17182b1 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -101,11 +101,11 @@ STC_INLINE void _cx_memb(_pop_front)(_cx_self* self) // == _pop() when _ STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { size_t n = self->_len; - return c_COMPOUND(_cx_iter){n ? self->data : NULL, self->data + n}; + 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_COMPOUND(_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; } @@ -345,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_COMPOUND(_cx_iter){pos, self->data + self->_len}; + return c_LITERAL(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_value* @@ -377,7 +377,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { { i_keydrop(p); } memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); self->_len -= (size_t)len; - return c_COMPOUND(_cx_iter){p2 == end ? NULL : p1, end - len}; + return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone diff --git a/include/stc/clist.h b/include/stc/clist.h index de32b931..fc4d8540 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -133,7 +133,7 @@ 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_COMPOUND(_cx_self){NULL}; } +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, size_t n) { while (n--) _cx_memb(_push_back)(self, i_keyfrom(*raw++)); } STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) @@ -161,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_COMPOUND(_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_COMPOUND(_cx_iter){NULL}; } + { return c_LITERAL(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { diff --git a/include/stc/cmap.h b/include/stc/cmap.h index c2f8cf3e..49d7d159 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -107,7 +107,7 @@ STC_API chash_bucket_t _cx_memb(_bucket_)(const _cx_self* self, const _cx_rawke 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_COMPOUND(_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; } @@ -166,7 +166,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_COMPOUND(_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 @@ -222,7 +222,7 @@ STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) - { return c_COMPOUND(_cx_iter){NULL}; } + { return c_LITERAL(_cx_iter){NULL}; } STC_INLINE void _cx_memb(_next)(_cx_iter* it) { @@ -240,7 +240,7 @@ STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { size_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) - return c_COMPOUND(_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); diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 83fcb684..8bb70216 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -45,7 +45,7 @@ STC_API void _cx_memb(_erase_at)(_cx_self* self, size_t idx); STC_API void _cx_memb(_push)(_cx_self* self, _cx_value value); STC_INLINE _cx_self _cx_memb(_init)(void) - { return c_COMPOUND(_cx_self){NULL}; } + { return c_LITERAL(_cx_self){NULL}; } STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) { while (n--) _cx_memb(_push)(self, i_keyfrom(*raw++)); } diff --git a/include/stc/crandom.h b/include/stc/crandom.h index 9cb23e93..5641d788 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -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_COMPOUND(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_COMPOUND(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/csmap.h b/include/stc/csmap.h index 67b2265e..fba08eba 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -143,7 +143,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_COMPOUND(_cx_raw){i_keyto((&val->first)), + _i_MAP_ONLY( c_LITERAL(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } @@ -231,7 +231,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t cap) { _cx_node* nodes = (_cx_node*)c_REALLOC(self->nodes, (cap + 1)*sizeof(_cx_node)); if (!nodes) return false; - nodes[0] = c_COMPOUND(_cx_node){{0, 0}, 0}; + nodes[0] = c_LITERAL(_cx_node){{0, 0}, 0}; self->nodes = nodes; self->cap = (i_size)cap; return true; diff --git a/include/stc/cstack.h b/include/stc/cstack.h index a40b74de..2a4d620d 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -175,12 +175,12 @@ 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_COMPOUND(_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_COMPOUND(_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; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index b500065e..04091968 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -71,7 +71,7 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); /**************************** PUBLIC API **********************************/ #define cstr_lit(literal) cstr_from_n(literal, crawstr_len(literal)) -#define cstr_NULL (c_COMPOUND(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); @@ -90,12 +90,12 @@ STC_API cstr cstr_replace_sv(csview sv, csview search, csview repl, unsigned STC_INLINE cstr_buf cstr_buffer(cstr* s) { return cstr_is_long(s) - ? c_COMPOUND(cstr_buf){s->lon.data, cstr_l_size(s), cstr_l_cap(s)} - : c_COMPOUND(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_COMPOUND(csview){s->lon.data, cstr_l_size(s)} - : c_COMPOUND(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) @@ -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_COMPOUND(cstr_iter){NULL}; - return c_COMPOUND(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_COMPOUND(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; diff --git a/include/stc/csview.h b/include/stc/csview.h index c53cd1c0..4df735b3 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -37,7 +37,7 @@ STC_API size_t csview_find_sv(csview sv, csview search); STC_INLINE csview csview_from(const char* str) - { return c_COMPOUND(csview){str, strlen(str)}; } + { return c_LITERAL(csview){str, strlen(str)}; } STC_INLINE void csview_clear(csview* self) { *self = csview_NULL; } STC_INLINE size_t csview_size(csview sv) { return sv.size; } @@ -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_COMPOUND(csview_iter){NULL}; - return c_COMPOUND(csview_iter){.u8 = {{self->str, utf8_chr_size(self->str)}, + 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_COMPOUND(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; diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 8010aea3..0905526a 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -199,11 +199,11 @@ _cx_memb(_at_mut)(_cx_self* self, const size_t idx) { STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { size_t n = self->_len; - return c_COMPOUND(_cx_iter){n ? self->data : NULL, self->data + n}; + 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_COMPOUND(_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; } @@ -259,7 +259,7 @@ _cx_memb(_sort)(_cx_self* self) { STC_DEF _cx_self _cx_memb(_init)(void) { - return c_COMPOUND(_cx_self){NULL}; + return c_LITERAL(_cx_self){NULL}; } STC_DEF void @@ -328,7 +328,7 @@ _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { memmove(pos + n, pos, (self->_len - idx)*sizeof *pos); self->_len += n; } - return c_COMPOUND(_cx_iter){pos, self->data + self->_len}; + return c_LITERAL(_cx_iter){pos, self->data + self->_len}; } STC_DEF _cx_iter @@ -348,7 +348,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { { i_keydrop(p); } memmove(p1, p2, (size_t)(end - p2)*sizeof *p1); self->_len -= len; - return c_COMPOUND(_cx_iter){p2 == end ? NULL : p1, end - len}; + return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } #if !defined i_no_clone diff --git a/misc/archived/csmap.h b/misc/archived/csmap.h index 2daec3b9..6f3ee98b 100644 --- a/misc/archived/csmap.h +++ b/misc/archived/csmap.h @@ -129,7 +129,7 @@ _cx_memb(_clear)(_cx_self* self) STC_INLINE _cx_raw _cx_memb(_value_toraw)(_cx_value* val) { return _i_SET_ONLY( i_keyto(val) ) - _i_MAP_ONLY( c_COMPOUND(_cx_raw){i_keyto((&val->first)), + _i_MAP_ONLY( c_LITERAL(_cx_raw){i_keyto((&val->first)), i_valto((&val->second))} ); } @@ -470,7 +470,7 @@ _cx_memb(_clone_r_)(_cx_node *tn) { STC_DEF _cx_self _cx_memb(_clone)(_cx_self cx) { - return c_COMPOUND(_cx_self){_cx_memb(_clone_r_)(cx.root), cx.size}; + return c_LITERAL(_cx_self){_cx_memb(_clone_r_)(cx.root), cx.size}; } #endif // !i_no_clone diff --git a/misc/archived/cstr.h b/misc/archived/cstr.h index 1a5b0da4..f4e9dde2 100644 --- a/misc/archived/cstr.h +++ b/misc/archived/cstr.h @@ -67,7 +67,7 @@ STC_INLINE cstr cstr_init() { return cstr_NULL; } STC_INLINE const char* cstr_str(const cstr* self) { return self->str; } #define cstr_toraw(self) (self)->str STC_INLINE csview cstr_sv(const cstr* self) - { return c_COMPOUND(csview){self->str, _cstr_p(self)->size}; } + { return c_LITERAL(csview){self->str, _cstr_p(self)->size}; } #define cstr_lit(literal) \ cstr_from_n(literal, crawstr_len(literal)) STC_INLINE cstr cstr_from(const char* str) @@ -118,7 +118,7 @@ STC_INLINE bool cstr_getline(cstr *self, FILE *stream) STC_INLINE cstr_buf cstr_buffer(cstr* s) { cstr_priv* p = _cstr_p(s); - return c_COMPOUND(cstr_buf){s->str, p->size, p->cap}; + return c_LITERAL(cstr_buf){s->str, p->size, p->cap}; } STC_INLINE cstr cstr_with_capacity(const size_t cap) { diff --git a/misc/examples/vikings.c b/misc/examples/vikings.c index a050b324..86d60519 100644 --- a/misc/examples/vikings.c +++ b/misc/examples/vikings.c @@ -23,11 +23,11 @@ static inline int RViking_cmp(const RViking* rx, const RViking* ry) { } static inline Viking Viking_from(RViking raw) { // note: parameter is by value - return c_COMPOUND(Viking){cstr_from(raw.name), cstr_from(raw.country)}; + return c_LITERAL(Viking){cstr_from(raw.name), cstr_from(raw.country)}; } static inline RViking Viking_toraw(const Viking* vp) { - return c_COMPOUND(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; + return c_LITERAL(RViking){cstr_str(&vp->name), cstr_str(&vp->country)}; } // With this in place, we define the Viking => int hash map type: -- cgit v1.2.3 From e1c95c261e973b97620ec15b3dfb3cb25f6df916 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 27 Jan 2023 16:10:45 +0100 Subject: Removed branch in cvec_lower_bound(). --- include/stc/cvec.h | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 0905526a..2ff2c58a 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -398,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 -- cgit v1.2.3 From b7b090148811935a3f4b069ad2b9481bbbe5d46d Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 27 Jan 2023 19:19:06 +0100 Subject: Added cspan_slice() function. --- include/stc/cspan.h | 56 +++++++++++++++++++++++++++++++++++++++++++++---- misc/tests/cspan_test.c | 24 +++++++++++++++++++++ 2 files changed, 76 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/stc/cspan.h b/include/stc/cspan.h index fabefc06..a4573b07 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -119,14 +119,20 @@ typedef struct { uint32_t d[4]; } cspan_idx4; #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) +// cspan_subspan: + #define cspan_subspan(self, offset, count) \ {.data=cspan_at(self, offset), .dim={count}} #define cspan_subspan2(self, offset, count) \ {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[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]}, .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, \ + .stride={(self)->stride}} #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]}, .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ + .stride={(self)->stride}} + +// cspan_atN: #define cspan_at4(...) c_MACRO_OVERLOAD(cspan_at4, __VA_ARGS__) #define cspan_at3(...) c_MACRO_OVERLOAD(cspan_at3, __VA_ARGS__) @@ -146,6 +152,15 @@ typedef struct { uint32_t d[4]; } cspan_idx4; #define cspan_at4_4(self, x, y, z) \ {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} +// cspan_slice: + +#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__}) + +#define cspan_slice(self, ...) \ + ((void)((self)->data += c_PASTE(_cspan_slice, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__))) + +// FUNCTIONS + STC_INLINE size_t _cspan_i1(const uint32_t dim[1], const cspan_idx1 stri, uint32_t x) { c_ASSERT(x < dim[0]); return x; } @@ -156,14 +171,47 @@ STC_INLINE size_t _cspan_i3(const uint32_t dim[3], const cspan_idx3 stri, uint32 c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]); return stri.d[2]*(stri.d[1]*x + y) + z; } -STC_INLINE size_t _cspan_i4(const uint32_t dim[4], const cspan_idx4 stri, 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]); +STC_INLINE size_t _cspan_i4(const uint32_t dim[4], const cspan_idx4 stri, uint32_t x, uint32_t y, \ + uint32_t z, uint32_t w) { + c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2] && w < dim[3]); return 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]; return sz; } +STC_INLINE size_t _cspan_slice2(uint32_t dim[2], const cspan_idx2 stri, const uint32_t x[2], const uint32_t y[2]) { + const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1]; + const size_t ret = _cspan_i2(dim, stri, x[0], y[0]); + + c_ASSERT(x1 <= dim[0] && y1 <= dim[1]); + dim[0] = x1 - x[0], dim[1] = y1 - y[0]; + return ret; +} + +STC_INLINE size_t _cspan_slice3(uint32_t dim[3], const cspan_idx3 stri, const uint32_t x[2], const uint32_t y[2], + const uint32_t z[2]) { + const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1], z1 = z[1] ? z[1] : dim[2]; + const size_t ret = stri.d[2]*(stri.d[1]*x[0] + y[0]) + z[0]; + + c_ASSERT(x1 <= dim[0] && y1 <= dim[1] && z1 <= dim[2]); + dim[0] = x1 - x[0], dim[1] = y1 - y[0], dim[2] = z1 - z[0]; + return ret; +} + +STC_INLINE size_t _cspan_slice4(uint32_t dim[4], const cspan_idx4 stri, const uint32_t x[2], const uint32_t y[2], + const uint32_t z[2], const uint32_t w[2]) { + const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1]; + const uint32_t z1 = z[1] ? z[1] : dim[2], w1 = w[1] ? w[1] : dim[3]; + const size_t ret = stri.d[3]*(stri.d[2]*(stri.d[1]*x[0] + y[0]) + z[0]) + w[0]; + + c_ASSERT(x1 <= dim[0] && y1 <= dim[1] && z1 <= dim[2] && w1 <= dim[3]); + dim[0] = x1 - x[0], dim[1] = y1 - y[0]; + dim[2] = z1 - z[0], dim[3] = w1 - w[0]; + return ret; +} + #endif diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 8d6d47c2..6b85af00 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -19,3 +19,27 @@ CTEST(cspan, subdim) { } } } + +CTEST(cspan, slice) { + int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; + intspan2 m1 = cspan_multidim(array, 3, 4); + + size_t sum1 = 0; + for (size_t i = 0; i < m1.dim[0]; ++i) { + for (size_t j = 0; j < m1.dim[1]; ++j) { + sum1 += *cspan_at(&m1, i, j); + } + } + + intspan2 m2 = m1; + cspan_slice(&m2, c_SLICE(0), c_SLICE(2,4)); + + size_t sum2 = 0; + for (size_t i = 0; i < m2.dim[0]; ++i) { + for (size_t j = 0; j < m2.dim[1]; ++j) { + sum2 += *cspan_at(&m2, i, j); + } + } + ASSERT_EQ(78, sum1); + ASSERT_EQ(45, sum2); +} -- cgit v1.2.3 From 5d5c70ff725b4414b6319040f8179689fb645562 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Sat, 28 Jan 2023 14:43:42 +0100 Subject: Added flat iteration over a sliced cspan. --- docs/cspan_api.md | 5 ++++- include/stc/cspan.h | 35 +++++++++++++++++++++--------- misc/examples/multidim.c | 55 ++++++++++++++++++++++++++---------------------- 3 files changed, 59 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 6da2acca..c0b86239 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -38,9 +38,12 @@ ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); SpanType cspan_at2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span. -SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1 or 2d subspan from a 3d span. +SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1d or 2d subspan from a 3d span. SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. +void cspan_slice(SpanTypeN* self, uint32_t xslice[2], ...); // slice multidim span into a md subspan. +uint32_t[2] c_SLICE(a, b) // use to specify a:b xslice, ... b is optional. + // return a subspan of same rank: SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); SpanType2 cspan_subspan2(const SpanType2 self, size_t offset, size_t count); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index a4573b07..68a06541 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -62,27 +62,28 @@ int demo2() { #define using_cspan(Self, T, RANK) \ typedef T Self##_value; typedef T Self##_raw; \ - typedef struct { Self##_value *ref, *end; } Self##_iter; \ typedef struct { \ Self##_value *data; \ uint32_t dim[RANK]; \ cspan_idx##RANK stride; \ } Self; \ + typedef struct { Self##_value *ref; uint32_t pos[RANK]; const Self *_s; } Self##_iter; \ \ STC_INLINE Self Self##_from_n(Self##_raw* raw, const size_t n) { \ return (Self){.data=raw, .dim={(uint32_t)n}}; \ } \ 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->dim, it->_s->stride.d); \ + if (it->pos[0] == it->_s->dim[0]) it->ref = NULL; \ + } \ struct stc_nostruct #define using_cspan2(Self, T) using_cspan(Self, T, 1); using_cspan(Self##2, T, 2) @@ -100,9 +101,6 @@ typedef struct { uint32_t d[4]; } cspan_idx4; #define cspan_make(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} -#define cspan_flatten(span) \ - {.data=(span)->data, .dim={(uint32_t)cspan_size(span)}} - /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ {.data=(container)->data, .dim={(uint32_t)(container)->_len}} @@ -153,8 +151,9 @@ typedef struct { uint32_t d[4]; } cspan_idx4; {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} // cspan_slice: +// e.g.: cspan_slice(&ms3, c_SLICE(1,3), c_SLICE(0), c_SLICE(1,4)); -#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__}) +#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__})z #define cspan_slice(self, ...) \ ((void)((self)->data += c_PASTE(_cspan_slice, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__))) @@ -183,6 +182,22 @@ STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { return sz; } +#define _cspan_next_1(r, pos, d, s) (++pos[0], 1) +#define _cspan_next_3 _cspan_next_2 +#define _cspan_next_4 _cspan_next_2 + +static size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[], const uint32_t stride[]) { + size_t off = 1, rs = 1; + ++pos[rank - 1]; + while (--rank && pos[rank] == dim[rank]) { + pos[rank] = 0, ++pos[rank - 1]; + const size_t ds = rs*dim[rank]; + rs *= stride[rank]; + off += rs - ds; + } + return off; +} + STC_INLINE size_t _cspan_slice2(uint32_t dim[2], const cspan_idx2 stri, const uint32_t x[2], const uint32_t y[2]) { const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1]; const size_t ret = _cspan_i2(dim, stri, x[0], y[0]); diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index d69a04fb..f3d9e865 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -16,49 +16,54 @@ int main() ispan ms1 = cspan_from(&v); // View the same data as a 3D array 2 x 3 x 4 - ispan3 ms3 = cspan_multidim(v.data, 2, 2, 6); + ispan3 ms3 = cspan_multidim(v.data, 2, 3, 4); - // View data as contiguous memory representing 2 rows of 6 ints each - ispan2 ms2 = cspan_at3(&ms3, 0); - ms2.data = cspan_at(&ms2, 1, 1); - ms2.dim[0] = 2; - ms2.dim[1] = 3; - for (unsigned i=0; i != ms2.dim[0]; i++) { - for (unsigned j=0; j != ms2.dim[1]; j++) - printf(" %2d", *cspan_at(&ms2, i, j)); + puts("ms3:"); + for (unsigned i=0; i != ms3.dim[0]; i++) { + for (unsigned j=0; j != ms3.dim[1]; j++) { + for (unsigned k=0; k != ms3.dim[2]; k++) { + printf(" %2d", *cspan_at(&ms3, i, j, k)); + } + puts(""); + } + puts(""); + } + puts("ss3 = ms3[:, 1:3, 1:3]"); + ispan3 ss3 = ms3; + cspan_slice(&ss3, c_SLICE(0), c_SLICE(1,3), c_SLICE(1,3)); + + for (unsigned i=0; i != ss3.dim[0]; i++) { + for (unsigned j=0; j != ss3.dim[1]; j++) { + for (unsigned k=0; k != ss3.dim[2]; k++) { + printf(" %2d", *cspan_at(&ss3, i, j, k)); + } + puts(""); + } puts(""); } - ms2 = (ispan2)cspan_at3(&ms3, 0); + puts("Iterate ss3 flat:"); + c_FOREACH (i, ispan3, ss3) + printf(" %d", *i.ref); + puts(""); + + ispan2 ms2 = cspan_at3(&ms3, 0); // write data using 2D view for (unsigned i=0; i != ms2.dim[0]; i++) for (unsigned j=0; j != ms2.dim[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; - // print all items using 1D view - printf("all: "); + puts("\nview data as 1D view:"); for (unsigned i=0; i != cspan_size(&ms1); i++) printf(" %d", *cspan_at(&ms1, i)); puts(""); - // or iterate a subspan... + puts("iterate subspan ms3[1]:"); ispan2 sub = cspan_at3(&ms3, 1); - printf("sub: "); c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); puts(""); - // read back using 3D view - for (unsigned i=0; i != ms3.dim[0]; i++) - { - printf("slice @ i = %u\n", i); - for (unsigned j=0; j != ms3.dim[1]; j++) - { - for (unsigned k=0; k != ms3.dim[2]; k++) - printf("%d ", *cspan_at(&ms3, i, j, k)); - puts(""); - } - } cstack_int_drop(&v); } -- cgit v1.2.3 From 8b7561225a22bf15958a2d19da29c9822811fce6 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Sat, 28 Jan 2023 14:49:30 +0100 Subject: Fix two minor blips. --- include/stc/cspan.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 68a06541..41ad8c8b 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -153,7 +153,7 @@ typedef struct { uint32_t d[4]; } cspan_idx4; // cspan_slice: // e.g.: cspan_slice(&ms3, c_SLICE(1,3), c_SLICE(0), c_SLICE(1,4)); -#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__})z +#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__}) #define cspan_slice(self, ...) \ ((void)((self)->data += c_PASTE(_cspan_slice, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__))) @@ -186,7 +186,7 @@ STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { #define _cspan_next_3 _cspan_next_2 #define _cspan_next_4 _cspan_next_2 -static size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[], const uint32_t stride[]) { +static inline size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[], const uint32_t stride[]) { size_t off = 1, rs = 1; ++pos[rank - 1]; while (--rank && pos[rank] == dim[rank]) { -- cgit v1.2.3 From a344b43728ff40a2e1ee25f2f1b592f33432aee7 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Sat, 28 Jan 2023 22:07:28 +0100 Subject: Generalized cspan_slice(), and improved usage/ergonomics. --- docs/cspan_api.md | 3 +-- include/stc/cspan.h | 52 ++++++++++++++++++------------------------------ misc/examples/multidim.c | 2 +- 3 files changed, 21 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index c0b86239..32ef49c4 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -41,8 +41,7 @@ SpanType cspan_at2(SpanType2* self, size_t x); // retur SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1d or 2d subspan from a 3d span. SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. -void cspan_slice(SpanTypeN* self, uint32_t xslice[2], ...); // slice multidim span into a md subspan. -uint32_t[2] c_SLICE(a, b) // use to specify a:b xslice, ... b is optional. +void cspan_slice(SpanTypeN* self, {x0,x1}, {y0,y1},...); // slice multidim span into a md subspan. // return a subspan of same rank: SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 41ad8c8b..19dd74d4 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -151,12 +151,13 @@ typedef struct { uint32_t d[4]; } cspan_idx4; {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} // cspan_slice: -// e.g.: cspan_slice(&ms3, c_SLICE(1,3), c_SLICE(0), c_SLICE(1,4)); - -#define c_SLICE(...) ((const uint32_t[2]){__VA_ARGS__}) +// e.g.: cspan_slice(&ms3, {1,3}, {0}, {1,4}); #define cspan_slice(self, ...) \ - ((void)((self)->data += c_PASTE(_cspan_slice, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__))) + ((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \ + (const uint32_t[][2]){__VA_ARGS__}) + \ + c_static_assert(cspan_rank(self) == \ + sizeof((const uint32_t[][2]){__VA_ARGS__})/8))) // FUNCTIONS @@ -198,35 +199,20 @@ static inline size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[ return off; } -STC_INLINE size_t _cspan_slice2(uint32_t dim[2], const cspan_idx2 stri, const uint32_t x[2], const uint32_t y[2]) { - const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1]; - const size_t ret = _cspan_i2(dim, stri, x[0], y[0]); - - c_ASSERT(x1 <= dim[0] && y1 <= dim[1]); - dim[0] = x1 - x[0], dim[1] = y1 - y[0]; - return ret; -} - -STC_INLINE size_t _cspan_slice3(uint32_t dim[3], const cspan_idx3 stri, const uint32_t x[2], const uint32_t y[2], - const uint32_t z[2]) { - const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1], z1 = z[1] ? z[1] : dim[2]; - const size_t ret = stri.d[2]*(stri.d[1]*x[0] + y[0]) + z[0]; - - c_ASSERT(x1 <= dim[0] && y1 <= dim[1] && z1 <= dim[2]); - dim[0] = x1 - x[0], dim[1] = y1 - y[0], dim[2] = z1 - z[0]; - return ret; -} - -STC_INLINE size_t _cspan_slice4(uint32_t dim[4], const cspan_idx4 stri, const uint32_t x[2], const uint32_t y[2], - const uint32_t z[2], const uint32_t w[2]) { - const uint32_t x1 = x[1] ? x[1] : dim[0], y1 = y[1] ? y[1] : dim[1]; - const uint32_t z1 = z[1] ? z[1] : dim[2], w1 = w[1] ? w[1] : dim[3]; - const size_t ret = stri.d[3]*(stri.d[2]*(stri.d[1]*x[0] + y[0]) + z[0]) + w[0]; - - c_ASSERT(x1 <= dim[0] && y1 <= dim[1] && z1 <= dim[2] && w1 <= dim[3]); - dim[0] = x1 - x[0], dim[1] = y1 - y[0]; - dim[2] = z1 - z[0], dim[3] = w1 - w[0]; - return ret; +STC_INLINE size_t _cspan_slice(int rank, uint32_t dim[], const uint32_t stri[], const uint32_t a[][2]) { + uint32_t t = a[0][1] ? a[0][1] : dim[0]; + c_ASSERT(t <= dim[0]); + dim[0] = t - a[0][0]; + + size_t off = a[0][0]; + for (int i = 1; i < rank; ++i) { + off *= stri[i]; + off += a[i][0]; + t = a[i][1] ? a[i][1] : dim[i]; + c_ASSERT(t <= dim[i]); + dim[i] = t - a[i][0]; + } + return off; } #endif diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index f3d9e865..e25d727e 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -30,7 +30,7 @@ int main() } puts("ss3 = ms3[:, 1:3, 1:3]"); ispan3 ss3 = ms3; - cspan_slice(&ss3, c_SLICE(0), c_SLICE(1,3), c_SLICE(1,3)); + cspan_slice(&ss3, {0}, {1,3}, {1,3}); for (unsigned i=0; i != ss3.dim[0]; i++) { for (unsigned j=0; j != ss3.dim[1]; j++) { -- cgit v1.2.3 From 209bf743e0c1253a4bc81d2ffb6897f657a84c8a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 29 Jan 2023 17:24:33 +0100 Subject: Some method renaming in cspan: cspan_multidim() ctor => cspan_md(). cspan_atN() => cspan_submdN(). cstr_replace_ex() merged as overload of cstr_replace(). --- docs/cspan_api.md | 110 ++++++++++++++++++++++++++++++++++---------- docs/cstr_api.md | 5 +- include/stc/cspan.h | 24 +++++----- include/stc/cstr.h | 6 +-- include/stc/priv/altnames.h | 12 ----- misc/examples/demos.c | 2 +- misc/examples/multidim.c | 6 +-- misc/tests/cspan_test.c | 39 ++++++++++++++-- 8 files changed, 141 insertions(+), 63 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 32ef49c4..c4cb7c16 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -1,9 +1,8 @@ # STC [cspan](../include/stc/cspan.h): Multi-dimensional Array View ![Array](pics/array.jpg) -The **cspan** is templated non-owning multi-dimensional view of an array. - -See the c++ classes [std::span](https://en.cppreference.com/w/cpp/container/span) and +The **cspan** is templated non-owning multi-dimensional view of an array. See the c++ classes +[std::span](https://en.cppreference.com/w/cpp/container/span) and [std::mdspan](https://en.cppreference.com/w/cpp/container/mdspan) for similar functionality. ## Header file and declaration @@ -18,17 +17,14 @@ using_cspan3(S, ValueType); // define span types S, S2, S3 with rank using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. ``` ## Methods -Note that `cspan_multidim()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type +Note that `cspan_md()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. ```c +SpanTypeN cspan_md(ValueType* data, size_t xdim, ...); // create a multi-dimensional cspan SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1d-dimensional cspan from values -SpanTypeN cspan_multidim(ValueType* data, size_t xdim, ...); // create a multi-dimensional cspan SpanType cspan_from(STCContainer* cnt); // create a 1d cspan from a compatible STC container SpanType cspan_from_array(ValueType array[]); // create a 1d cspan from a C array -SpanType cspan_flatten(SpanTypeN* span); // create a 1d cspan from a multidim span -void cspan_resize(SpanTypeN* self, size_t xdim, ...); // change the extent of each dimension - size_t cspan_size(const SpanTypeN* self); // return number of elements unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions size_t cspan_index(const SpanTypeN* self, size_t x, ...); // index of element @@ -37,13 +33,15 @@ ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); -SpanType cspan_at2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span. -SpanTypeN cspan_at3(SpanType3* self, size_t x, ...); // return a 1d or 2d subspan from a 3d span. -SpanTypeN cspan_at4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. + // return a subspan of lower rank: +SpanType cspan_submd2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span. +SpanTypeN cspan_submd3(SpanType3* self, size_t x, ...); // return a 1d or 2d subspan from a 3d span. +SpanTypeN cspan_submd4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. + // return a sliced span of same rank: void cspan_slice(SpanTypeN* self, {x0,x1}, {y0,y1},...); // slice multidim span into a md subspan. - // return a subspan of same rank: + // return a subspan of same rank. Like e.g. cspan_slice(&ms3, {offset, offset+count}, {0}, {0}); SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); SpanType2 cspan_subspan2(const SpanType2 self, size_t offset, size_t count); SpanType3 cspan_subspan3(const SpanType3 self, size_t offset, size_t count); @@ -61,9 +59,73 @@ void SpanType_next(SpanTypeN_iter* it); | SpanTypeN`_value` | `ValueType` | The ValueType | | SpanTypeN`_iter` | `struct { ValueType *ref; ... }` | Iterator type | -## Example +## Example 1 + +The *cspan_slice()* function is similar to pythons numpy multi-dimensional arrays slicing, e.g.: +```py +import numpy as np +ms3 = np.array((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24), int) + +ms3 = np.reshape(ms3, (2, 3, 4)) +ss3 = ms3[:, 1:3, 2:] +ss2 = ss3[1] + +for i in range(ss2.shape[0]): + for j in range(ss2.shape[1]): + print(" {}".format(ss2[i, j]), end='') +print('') + +for i in ss2.flat: + print(" {}".format(i), end='') +# 19 20 23 24 +# 19 20 23 24 +``` +... can be done in C with STC: +```c +#include +#include +using_cspan3(myspan, int); // define myspan, myspan2, myspan3. + +int main() { + int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; + + myspan3 ms3 = cspan_md(arr, 2, 3, 4), ss3 = ms3; + cspan_slice(&ss3, {0}, {1,3}, {2,}); + myspan2 ss2 = cspan_submd3(&ss3, 1); + + c_FORRANGE (i, ss2.dim[0]) + c_FORRANGE (j, ss2.dim[1]) + fmt_print(" {}", *cspan_at(&ss2, i, j)); + puts(""); + + c_FOREACH (i, myspan2, ss2) + fmt_print(" {}", *i.ref); +} +``` +... or (mostly) in C++23: +```c++ +#include +#include +#include + +int main() { + int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; + + std::mdspan ms3(arr, 2, 3, 4); + auto ss3 = std::submdspan(ms3, std::full_extent, std::tuple{1,3}, std::tuple{2,4}); + auto ss2 = std::submdspan(ss3, 1, std::full_extent, std::full_extent); + + for (std::size_t i = 0; i < ss2.extent(0); ++i) + for (std::size_t j = 0; j < ss2.extent(1); ++j) + std::print(" {}", ss2[i, j]); + std::println(); + + // mdspan can't printed as a flat array, afaik. +} +``` +## Example 2 ```c -#include +#include #include #define i_val float #include @@ -79,33 +141,33 @@ int main() cstack_float_push(&vec, i); // define "span3[xd][yd][zd]" - Span3 span3 = cspan_multidim(vec.data, xd, yd, zd); + Span3 span3 = cspan_md(vec.data, xd, yd, zd); *cspan_at(&span3, 4, 3, 2) = 3.14159f; - printf("index: %d", (int)cspan_index(&span3, 4, 3, 2)); + fmt_print("index: {}", cspan_index(&span3, 4, 3, 2)); - Span span1 = cspan_at3(&span3, 4, 3); + Span span1 = cspan_submd3(&span3, 4, 3); printf("\niterate span1: "); c_FOREACH (i, Span, span1) - printf("%g ", *i.ref); + fmt_print("{} ", *i.ref); - Span2 span2 = cspan_at3(&span3, 4); + Span2 span2 = cspan_submd3(&span3, 4); printf("\niterate span2: "); c_FOREACH (i, Span2, span2) - printf("%g ", *i.ref); + fmt_print("{} ", *i.ref); puts("\niterate span3 by dimensions:"); c_FORRANGE (i, span3.dim[0]) { c_FORRANGE (j, span3.dim[1]) { c_FORRANGE (k, span3.dim[2]) - printf(" %2g", *cspan_at(&span3, i, j, k)); + fmt_printf(" {:2}", *cspan_at(&span3, i, j, k)); printf(" |"); } puts(""); } - printf("%g\n", *cspan_at(&span3, 4, 3, 2)); - printf("%g\n", *cspan_at(&span2, 3, 2)); - printf("%g\n", *cspan_at(&span1, 2)); + fmt_println("{}", *cspan_at(&span3, 4, 3, 2)); + fmt_println("{}", *cspan_at(&span2, 3, 2)); + fmt_println("{}", *cspan_at(&span1, 2)); } } ``` diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 4f895549..48999654 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -68,8 +68,7 @@ void cstr_insert_s(cstr* self, size_t pos, cstr ins); void cstr_erase(cstr* self, size_t pos, size_t len); // erase len bytes from pos -void cstr_replace(cstr* self, const char* search, const char* repl); -void cstr_replace_ex(cstr* self, const char* search, const char* repl, unsigned count); +void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count = MAX_INT); cstr cstr_replace_sv(csview in, csview search, csview repl, unsigned count); void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* repl); // replace at a position void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl); @@ -174,7 +173,7 @@ int main() { cstr_erase(&s1, 7, 5); // -nine printf("%s\n", cstr_str(&s1)); - cstr_replace_ex(&s1, "seven", "four", 1); + cstr_replace(&s1, "seven", "four", 1); printf("%s\n", cstr_str(&s1)); // reassign: diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 19dd74d4..7886c9f6 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -30,7 +30,7 @@ using_cspan(Intspan, int, 1); int demo1() { float raw[4*5]; - Span2f ms = cspan_multidim(raw, 4, 5); + Span2f ms = cspan_md(raw, 4, 5); for (size_t i=0; idata) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) -// cspan_subspan: +// cspan_subspanN: #define cspan_subspan(self, offset, count) \ {.data=cspan_at(self, offset), .dim={count}} @@ -130,24 +130,24 @@ typedef struct { uint32_t d[4]; } cspan_idx4; {.data=cspan_at(self, offset, 0, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ .stride={(self)->stride}} -// cspan_atN: +// cspan_submdN: -#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) \ +#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), .dim={(self)->dim[1]}} -#define cspan_at3_2(self, x) \ +#define cspan_submd3_2(self, x) \ {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}, \ .stride={.d={0, (self)->stride.d[2]}}} -#define cspan_at3_3(self, x, y) \ +#define cspan_submd3_3(self, x, y) \ {.data=cspan_at(self, x, y, 0), .dim={(self)->dim[2]}} -#define cspan_at4_2(self, x) \ +#define cspan_submd4_2(self, x) \ {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3]}}} -#define cspan_at4_3(self, x, y) \ +#define cspan_submd4_3(self, x, y) \ {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, \ .stride={.d={0, (self)->stride.d[3]}}} -#define cspan_at4_4(self, x, y, z) \ +#define cspan_submd4_4(self, x, y, z) \ {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} // cspan_slice: diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 04091968..071e40e8 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -373,12 +373,12 @@ 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) { +#define cstr_replace(...) c_MACRO_OVERLOAD(cstr_replace, __VA_ARGS__) +#define cstr_replace_3(self, search, repl) cstr_replace_4(self, search, repl, ~0U) +STC_INLINE void cstr_replace_4(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)); } -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) { char* d = _cstr_internal_move(self, pos + len, pos + repl.size); diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h index 7f58384c..695d3ebc 100644 --- a/include/stc/priv/altnames.h +++ b/include/stc/priv/altnames.h @@ -42,15 +42,3 @@ #define c_scope c_SCOPE #define c_defer c_DEFER #define c_sv c_SV - -#define c_DROP c_drop -#define c_DELETE c_delete -#define c_SWAP c_swap -#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 c_CONTAINER_OF c_container_of -#define c_STATIC_ASSERT c_static_assert diff --git a/misc/examples/demos.c b/misc/examples/demos.c index 4455b840..c92f95b2 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -13,7 +13,7 @@ void stringdemo1() cstr_erase(&cs, 7, 5); // -nine printf("%s.\n", cstr_str(&cs)); - cstr_replace_ex(&cs, "seven", "four", 1); + cstr_replace(&cs, "seven", "four", 1); printf("%s.\n", cstr_str(&cs)); cstr_take(&cs, cstr_from_fmt("%s *** %s", cstr_str(&cs), cstr_str(&cs))); diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index e25d727e..b7ee46fb 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -16,7 +16,7 @@ int main() ispan ms1 = cspan_from(&v); // View the same data as a 3D array 2 x 3 x 4 - ispan3 ms3 = cspan_multidim(v.data, 2, 3, 4); + ispan3 ms3 = cspan_md(v.data, 2, 3, 4); puts("ms3:"); for (unsigned i=0; i != ms3.dim[0]; i++) { @@ -47,7 +47,7 @@ int main() printf(" %d", *i.ref); puts(""); - ispan2 ms2 = cspan_at3(&ms3, 0); + ispan2 ms2 = cspan_submd3(&ms3, 0); // write data using 2D view for (unsigned i=0; i != ms2.dim[0]; i++) @@ -60,7 +60,7 @@ int main() puts(""); puts("iterate subspan ms3[1]:"); - ispan2 sub = cspan_at3(&ms3, 1); + ispan2 sub = cspan_submd3(&ms3, 1); c_FOREACH (i, ispan2, sub) printf(" %d", *i.ref); puts(""); diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 6b85af00..32634795 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -7,12 +7,12 @@ using_cspan3(intspan, int); CTEST(cspan, subdim) { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; - intspan3 m = cspan_multidim(array, 2, 2, 3); + intspan3 m = cspan_md(array, 2, 2, 3); for (size_t i = 0; i < m.dim[0]; ++i) { - intspan2 sub_i = cspan_at3(&m, i); + intspan2 sub_i = cspan_submd3(&m, i); for (size_t j = 0; j < m.dim[1]; ++j) { - intspan sub_i_j = cspan_at2(&sub_i, j); + intspan sub_i_j = cspan_submd2(&sub_i, j); for (size_t k = 0; k < m.dim[2]; ++k) { ASSERT_EQ(*cspan_at(&sub_i_j, k), *cspan_at(&m, i, j, k)); } @@ -22,7 +22,7 @@ CTEST(cspan, subdim) { CTEST(cspan, slice) { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; - intspan2 m1 = cspan_multidim(array, 3, 4); + intspan2 m1 = cspan_md(array, 3, 4); size_t sum1 = 0; for (size_t i = 0; i < m1.dim[0]; ++i) { @@ -32,7 +32,7 @@ CTEST(cspan, slice) { } intspan2 m2 = m1; - cspan_slice(&m2, c_SLICE(0), c_SLICE(2,4)); + cspan_slice(&m2, {0}, {2,4}); size_t sum2 = 0; for (size_t i = 0; i < m2.dim[0]; ++i) { @@ -43,3 +43,32 @@ CTEST(cspan, slice) { ASSERT_EQ(78, sum1); ASSERT_EQ(45, sum2); } + +#define i_val int +#include + +CTEST(cspan, slice2) { + c_AUTO (cstack_int, stack) + { + c_FORRANGE (i, 10*20*30) + cstack_int_push(&stack, i); + + intspan3 ms3 = cspan_md(stack.data, 10, 20, 30); + cspan_slice(&ms3, {1,4}, {3,7}, {20,24}); + + size_t sum = 0; + for (size_t i = 0; i < ms3.dim[0]; ++i) { + for (size_t j = 0; j < ms3.dim[1]; ++j) { + for (size_t k = 0; k < ms3.dim[2]; ++k) { + sum += *cspan_at(&ms3, i, j, k); + } + } + } + ASSERT_EQ(65112, sum); + + sum = 0; + c_FOREACH (i, intspan3, ms3) + sum += *i.ref; + ASSERT_EQ(65112, sum); + } +} -- cgit v1.2.3 From 5bbcae2a3add163ea3b7a91d65fda6836c18f410 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 31 Jan 2023 12:53:46 +0100 Subject: Updates, and prepare for the big unsigned ==> signed transformation. --- include/c11/fmt.h | 9 ++-- include/stc/ccommon.h | 18 ++++++- include/stc/cspan.h | 110 +++++++++++++++++++++++++------------------ misc/benchmarks/build_all.sh | 2 +- misc/examples/multidim.c | 20 ++++---- misc/examples/new_map.c | 17 +++---- misc/tests/cspan_test.c | 53 +++++++++++++++++++++ misc/tests/ctest.h | 51 ++++++-------------- 8 files changed, 173 insertions(+), 107 deletions(-) (limited to 'include') diff --git a/include/c11/fmt.h b/include/c11/fmt.h index ff0cb25c..435193f8 100644 --- a/include/c11/fmt.h +++ b/include/c11/fmt.h @@ -59,6 +59,7 @@ int main() { } */ #include +#include #include #define fmt_OVERLOAD(name, ...) \ @@ -86,7 +87,7 @@ int main() { typedef struct { char* data; - size_t cap, len; + intptr_t cap, len; _Bool stream; } fmt_buffer; @@ -202,11 +203,11 @@ FMT_API void _fmt_bprint(fmt_buffer* buf, const char* fmt, ...) { va_copy(args2, args); const int n = vsnprintf(NULL, 0U, fmt, args); if (n < 0) goto done2; - const size_t pos = buf->stream ? buf->len : 0U; + const intptr_t pos = buf->stream ? buf->len : 0; buf->len = pos + n; if (buf->len > buf->cap) { buf->cap = buf->len + buf->cap/2; - buf->data = (char*)realloc(buf->data, buf->cap + 1); + buf->data = (char*)realloc(buf->data, (size_t)buf->cap + 1U); } vsprintf(buf->data + pos, fmt, args2); done2: va_end(args2); @@ -252,7 +253,7 @@ FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...) { if (!strchr("csdioxXufFeEaAgGnp", fmt[-1])) while (*arg) *p++ = *arg++; if (p0 && (p[-1] == 's' || p[-1] == 'c')) /* left-align str */ - memmove(p0 + 1, p0, p++ - p0), *p0 = '-'; + memmove(p0 + 1, p0, (size_t)(p++ - p0)), *p0 = '-'; fmt += *fmt == '}'; continue; } diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 34d297e3..7c25d397 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -78,18 +78,34 @@ #define c_NEW(T, ...) ((T*)memcpy(c_ALLOC(T), (T[]){__VA_ARGS__}, sizeof(T))) #define c_LITERAL(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) #endif +#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_LTu(a, b) ((size_t)(a) < (size_t)(b)) +#define c_u2i(u) ((intptr_t)((u) + 0*sizeof((u) == 1U))) +#define c_i2u(i) ((size_t)(i) + 0*sizeof((i) == 1)) +#define c_sizeof(x) (intptr_t)sizeof(x) +#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_memcmp(d, s, ilen) memcmp(d, s, c_i2u(ilen)) +#define c_strlen(str) (intptr_t)strlen(str) +#define c_strncmp(d, s, ilen) strncmp(d, s, c_i2u(ilen)) // 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)) @@ -122,7 +138,7 @@ typedef const char* crawstr; #define crawstr_hash(p) cstrhash(*(p)) #define crawstr_len(literal) (sizeof("" literal) - 1U) -#define c_ARRAYLEN(a) (sizeof(a)/sizeof 0[a]) +#define c_ARRAYLEN(a) (intptr_t)(sizeof(a)/sizeof 0[a]) #define c_SV(...) c_MACRO_OVERLOAD(c_SV, __VA_ARGS__) #define c_SV_1(lit) c_SV_2(lit, crawstr_len(lit)) #define c_SV_2(str, n) (c_LITERAL(csview){str, n}) diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 7886c9f6..a5f3fdc7 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -32,8 +32,8 @@ int demo1() { float raw[4*5]; Span2f ms = cspan_md(raw, 4, 5); - for (size_t i=0; idata, .pos={0}, ._s=self}; \ @@ -86,13 +88,15 @@ int demo2() { } \ 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) -typedef struct { uint32_t d[1]; } cspan_idx1; -typedef struct { uint32_t d[2]; } cspan_idx2; -typedef struct { uint32_t d[3]; } cspan_idx3; -typedef struct { uint32_t d[4]; } cspan_idx4; +#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) +#define using_cspan5(Self, T) using_cspan4(Self, T); using_cspan_4(Self##4, T, 5) +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; #define cspan_md(array, ...) \ {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} @@ -103,7 +107,7 @@ typedef struct { uint32_t d[4]; } cspan_idx4; /* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */ #define cspan_from(container) \ - {.data=(container)->data, .dim={(uint32_t)(container)->_len}} + {.data=(container)->data, .dim={(int32_t)(container)->_len}} #define cspan_from_array(array) \ {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .dim={c_ARRAYLEN(array)}} @@ -117,20 +121,16 @@ typedef struct { uint32_t d[4]; } cspan_idx4; #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) -// cspan_subspanN: +// cspan_subspanN. for N > 3, use cspan_slice(&ms4, {offset, offset + count}, {0}, {0}, {0}); #define cspan_subspan(self, offset, count) \ {.data=cspan_at(self, offset), .dim={count}} #define cspan_subspan2(self, offset, count) \ {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[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]}, \ - .stride={(self)->stride}} -#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]}, \ - .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0, 0), .dim={count, (self)->dim[1], (self)->dim[2]}, .stride={(self)->stride}} -// cspan_submdN: +// cspan_submdN: return reduced rank #define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__) #define cspan_submd3(...) c_MACRO_OVERLOAD(cspan_submd3, __VA_ARGS__) @@ -145,40 +145,55 @@ typedef struct { uint32_t d[4]; } cspan_idx4; {.data=cspan_at(self, x, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[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), .dim={(self)->dim[2], (self)->dim[3]}, \ - .stride={.d={0, (self)->stride.d[3]}}} + {.data=cspan_at(self, x, y, 0, 0), .dim={(self)->dim[2], (self)->dim[3]}, .stride={.d={0, (self)->stride.d[3]}}} #define cspan_submd4_4(self, x, y, z) \ {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} +#define cspan_submd5_2(self, x) \ + {.data=cspan_at(self, x, 0, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3], (self)->dim[4]}, \ + .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3], (self)->stride.d[4]}}} +#define cspan_submd5_3(self, x, y) \ + {.data=cspan_at(self, x, y, 0, 0, 0), .dim={(self)->dim[2], (self)->dim[3], (self)->dim[4]}, \ + .stride={.d={0, (self)->stride.d[3], (self)->stride.d[4]}}} +#define cspan_submd5_4(self, x, y, z) \ + {.data=cspan_at(self, x, y, z, 0, 0), .dim={(self)->dim[3], (self)->dim[4]}, .stride={.d={0, (self)->stride.d[4]}}} +#define cspan_submd5_5(self, x, y, z, w) \ + {.data=cspan_at(self, x, y, z, w, 0), .dim={(self)->dim[4]}} + // cspan_slice: // e.g.: cspan_slice(&ms3, {1,3}, {0}, {1,4}); #define cspan_slice(self, ...) \ ((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \ - (const uint32_t[][2]){__VA_ARGS__}) + \ + (const int32_t[][2]){__VA_ARGS__}) + \ c_static_assert(cspan_rank(self) == \ - sizeof((const uint32_t[][2]){__VA_ARGS__})/8))) + sizeof((const int32_t[][2]){__VA_ARGS__})/8))) // FUNCTIONS -STC_INLINE size_t _cspan_i1(const uint32_t dim[1], const cspan_idx1 stri, uint32_t x) - { c_ASSERT(x < dim[0]); return x; } +STC_INLINE intptr_t _cspan_i1(const int32_t dim[1], const cspan_idx1 stri, int32_t x) + { c_ASSERT(c_LTu(x, dim[0])); return x; } -STC_INLINE size_t _cspan_i2(const uint32_t dim[2], const cspan_idx2 stri, uint32_t x, uint32_t y) - { c_ASSERT(x < dim[0] && y < dim[1]); return stri.d[1]*x + y; } +STC_INLINE intptr_t _cspan_i2(const int32_t dim[2], const cspan_idx2 stri, int32_t x, int32_t y) + { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1])); return (intptr_t)stri.d[1]*x + y; } -STC_INLINE size_t _cspan_i3(const uint32_t dim[3], const cspan_idx3 stri, uint32_t x, uint32_t y, uint32_t z) { - c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]); - return stri.d[2]*(stri.d[1]*x + y) + z; +STC_INLINE intptr_t _cspan_i3(const int32_t dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { + c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2])); + return (intptr_t)stri.d[2]*(stri.d[1]*x + y) + z; +} +STC_INLINE intptr_t _cspan_i4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, + int32_t z, int32_t w) { + c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3])); + return (intptr_t)stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w; } -STC_INLINE size_t _cspan_i4(const uint32_t dim[4], const cspan_idx4 stri, uint32_t x, uint32_t y, \ - uint32_t z, uint32_t w) { - c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2] && w < dim[3]); - return stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w; +STC_INLINE intptr_t _cspan_i5(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, int32_t z, + int32_t w, int32_t v) { + c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3]) && c_LTu(v, dim[4])); + return (intptr_t)stri.d[4]*(stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w) + v; } -STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { - size_t sz = dim[0]; +STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { + intptr_t sz = dim[0]; while (rank-- > 1) sz *= dim[rank]; return sz; } @@ -186,30 +201,31 @@ STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) { #define _cspan_next_1(r, pos, d, s) (++pos[0], 1) #define _cspan_next_3 _cspan_next_2 #define _cspan_next_4 _cspan_next_2 +#define _cspan_next_5 _cspan_next_2 -static inline size_t _cspan_next_2(int rank, uint32_t pos[], const uint32_t dim[], const uint32_t stride[]) { - size_t off = 1, rs = 1; +STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) { + intptr_t off = 1, rs = 1; ++pos[rank - 1]; while (--rank && pos[rank] == dim[rank]) { pos[rank] = 0, ++pos[rank - 1]; - const size_t ds = rs*dim[rank]; + const intptr_t ds = rs*dim[rank]; rs *= stride[rank]; off += rs - ds; } return off; } -STC_INLINE size_t _cspan_slice(int rank, uint32_t dim[], const uint32_t stri[], const uint32_t a[][2]) { - uint32_t t = a[0][1] ? a[0][1] : dim[0]; - c_ASSERT(t <= dim[0]); +STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], const int32_t a[][2]) { + int32_t t = a[0][1] ? a[0][1] : dim[0]; + c_ASSERT(!c_LTu(dim[0], t)); dim[0] = t - a[0][0]; - size_t off = a[0][0]; + intptr_t off = a[0][0]; for (int i = 1; i < rank; ++i) { off *= stri[i]; off += a[i][0]; t = a[i][1] ? a[i][1] : dim[i]; - c_ASSERT(t <= dim[i]); + c_ASSERT(!c_LTu(dim[i], t)); dim[i] = t - a[i][0]; } return off; diff --git a/misc/benchmarks/build_all.sh b/misc/benchmarks/build_all.sh index 54340998..869559ba 100644 --- a/misc/benchmarks/build_all.sh +++ b/misc/benchmarks/build_all.sh @@ -1,5 +1,5 @@ #!/bin/bash -cc='g++ -I../../include -s -O3 -Wall -pedantic -x c++ -std=c++20' +cc='g++ -I../../include -s -O3 -Wall -pedantic -x c++ -std=c++2a' #cc='clang++ -I../include -s -O3 -Wall -pedantic -x c++ -std=c++20' #cc='cl -nologo -I../include -O2 -TP -EHsc -std:c++20' run=0 diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index b7ee46fb..3bc1feec 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -9,7 +9,7 @@ using_cspan3(ispan, int); int main() { cstack_int v = {0}; - c_FORLIST (i, unsigned, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}) + c_FORLIST (i, int, {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}) cstack_int_push(&v, *i.ref); // View data as contiguous memory representing 24 ints @@ -19,9 +19,9 @@ int main() ispan3 ms3 = cspan_md(v.data, 2, 3, 4); puts("ms3:"); - for (unsigned i=0; i != ms3.dim[0]; i++) { - for (unsigned j=0; j != ms3.dim[1]; j++) { - for (unsigned k=0; k != ms3.dim[2]; k++) { + for (int i=0; i != ms3.dim[0]; i++) { + for (int j=0; j != ms3.dim[1]; j++) { + for (int k=0; k != ms3.dim[2]; k++) { printf(" %2d", *cspan_at(&ms3, i, j, k)); } puts(""); @@ -32,9 +32,9 @@ int main() ispan3 ss3 = ms3; cspan_slice(&ss3, {0}, {1,3}, {1,3}); - for (unsigned i=0; i != ss3.dim[0]; i++) { - for (unsigned j=0; j != ss3.dim[1]; j++) { - for (unsigned k=0; k != ss3.dim[2]; k++) { + for (int i=0; i != ss3.dim[0]; i++) { + for (int j=0; j != ss3.dim[1]; j++) { + for (int k=0; k != ss3.dim[2]; k++) { printf(" %2d", *cspan_at(&ss3, i, j, k)); } puts(""); @@ -50,12 +50,12 @@ int main() ispan2 ms2 = cspan_submd3(&ms3, 0); // write data using 2D view - for (unsigned i=0; i != ms2.dim[0]; i++) - for (unsigned j=0; j != ms2.dim[1]; j++) + for (int i=0; i != ms2.dim[0]; i++) + for (int j=0; j != ms2.dim[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; puts("\nview data as 1D view:"); - for (unsigned i=0; i != cspan_size(&ms1); i++) + for (int i=0; i != cspan_size(&ms1); i++) printf(" %d", *cspan_at(&ms1, i)); puts(""); diff --git a/misc/examples/new_map.c b/misc/examples/new_map.c index 72705eb2..9cee987d 100644 --- a/misc/examples/new_map.c +++ b/misc/examples/new_map.c @@ -48,24 +48,25 @@ int main() c_AUTO (cset_str, sset) { cmap_int_insert(&map, 123, 321); + cmap_int_insert(&map, 456, 654); + cmap_int_insert(&map, 789, 987); - c_FORLIST (i, cmap_pnt_raw, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}) - cmap_pnt_insert(&pmap, c_PAIR(i.ref)); + pmap = c_make(cmap_pnt, {{{42, 14}, 1}, {{32, 94}, 2}, {{62, 81}, 3}}); c_FOREACH (i, cmap_pnt, pmap) printf(" (%d, %d: %d)", i.ref->first.x, i.ref->first.y, i.ref->second); puts(""); - c_FORLIST (i, cmap_str_raw, { + smap = c_make(cmap_str, { {"Hello, friend", "long time no see"}, - {"So long, friend", "see you around"}, - }) cmap_str_emplace(&smap, c_PAIR(i.ref)); + {"So long", "see you around"}, + }); - c_FORLIST (i, const char*, { + sset = c_make(cset_str, { "Hello, friend", "Nice to see you again", - "So long, friend", - }) cset_str_emplace(&sset, *i.ref); + "So long", + }); c_FOREACH (i, cset_str, sset) printf(" %s\n", cstr_str(i.ref)); diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 32634795..3f03ef0f 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -72,3 +72,56 @@ CTEST(cspan, slice2) { ASSERT_EQ(65112, sum); } } + + +#define i_type Tiles +#define i_val intspan3 +#include + +CTEST_FIXTURE(cspan_cube) { + cstack_int stack; + Tiles tiles; +}; + +CTEST_SETUP(cspan_cube) { + enum {TSIZE=4, CUBE=64, N=CUBE*CUBE*CUBE}; + + fix->stack = cstack_int_init(); + fix->tiles = Tiles_init(); + + cstack_int_reserve(&fix->stack, N); + c_FORRANGE (i, N) + cstack_int_push(&fix->stack, i+1); + + intspan3 ms3 = cspan_md(fix->stack.data, CUBE, CUBE, CUBE); + + c_FORRANGE (i, 0, ms3.dim[0], TSIZE) { + c_FORRANGE (j, 0, ms3.dim[1], TSIZE) { + c_FORRANGE (k, 0, ms3.dim[2], TSIZE) { + intspan3 tile = ms3; + cspan_slice(&tile, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); + Tiles_push(&fix->tiles, tile); + } + } + } +} + +// Optional teardown function for suite, called after every test in suite +CTEST_TEARDOWN(cspan_cube) { + cstack_int_drop(&fix->stack); + Tiles_drop(&fix->tiles); +} + + +CTEST_F(cspan_cube, slice3) { + intptr_t n = cstack_int_size(&fix->stack); + //printf("\ntiles: %zi, cells: %zi\n", Tiles_size(&fix->tiles), n); + + int64_t sum = 0; + // iterate each 3d tile in sequence + c_FOREACH (i, Tiles, fix->tiles) + c_FOREACH (t, intspan3, *i.ref) + sum += *t.ref; + + ASSERT_EQ(n*(n + 1)/2, sum); +} diff --git a/misc/tests/ctest.h b/misc/tests/ctest.h index 294a6ca2..6cfb507a 100644 --- a/misc/tests/ctest.h +++ b/misc/tests/ctest.h @@ -113,14 +113,14 @@ struct ctest { #ifdef __cplusplus #define CTEST_SETUP(sname) \ - template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + template <> void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_TEARDOWN(sname) \ - template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + template <> void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_FIXTURE(sname) \ - template void CTEST_IMPL_SETUP_FNAME(sname)(T* self) { } \ - template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* self) { } \ + template void CTEST_IMPL_SETUP_FNAME(sname)(T* fix) { } \ + template void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* fix) { } \ struct CTEST_IMPL_DATA_SNAME(sname) #define CTEST_IMPL_CTEST(sname, tname, tskip) \ @@ -130,23 +130,23 @@ struct ctest { #define CTEST_IMPL_CTEST_F(sname, tname, tskip) \ static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_SETUP_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname); \ static void (*CTEST_IMPL_TEARDOWN_TPNAME(sname, tname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname); \ CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_TPNAME(sname, tname), &CTEST_IMPL_TEARDOWN_TPNAME(sname, tname)); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #else #define CTEST_SETUP(sname) \ - static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_SETUP_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_SETUP_FNAME(sname); \ - static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_SETUP_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_TEARDOWN(sname) \ - static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ static void (*CTEST_IMPL_TEARDOWN_FPNAME(sname))(struct CTEST_IMPL_DATA_SNAME(sname)*) = &CTEST_IMPL_TEARDOWN_FNAME(sname); \ - static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_TEARDOWN_FNAME(sname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #define CTEST_FIXTURE(sname) \ struct CTEST_IMPL_DATA_SNAME(sname); \ @@ -161,9 +161,9 @@ struct ctest { #define CTEST_IMPL_CTEST_F(sname, tname, tskip) \ static struct CTEST_IMPL_DATA_SNAME(sname) CTEST_IMPL_DATA_TNAME(sname, tname); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self); \ + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix); \ CTEST_IMPL_STRUCT(sname, tname, tskip, &CTEST_IMPL_DATA_TNAME(sname, tname), &CTEST_IMPL_SETUP_FPNAME(sname), &CTEST_IMPL_TEARDOWN_FPNAME(sname)); \ - static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* self) + static void CTEST_IMPL_FNAME(sname, tname)(struct CTEST_IMPL_DATA_SNAME(sname)* fix) #endif @@ -186,12 +186,6 @@ void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const #define ASSERT_WSTREQ(exp, real) assert_wstr("==", exp, real, __FILE__, __LINE__) #define ASSERT_WSTRNE(exp, real) assert_wstr("!=", exp, real, __FILE__, __LINE__) -void assert_data(const unsigned char* exp, size_t expsize, - const unsigned char* real, size_t realsize, - const char* caller, int line); -#define ASSERT_DATA(exp, expsize, real, realsize) \ - assert_data(exp, expsize, real, realsize, __FILE__, __LINE__) - #define CTEST_FLT_EPSILON 1e-5 #define CTEST_DBL_EPSILON 1e-12 @@ -363,21 +357,6 @@ void assert_wstr(const char* cmp, const wchar_t *exp, const wchar_t *real, const } } -void assert_data(const unsigned char* exp, size_t expsize, - const unsigned char* real, size_t realsize, - const char* caller, int line) { - size_t i; - if (expsize != realsize) { - CTEST_ERR("%s:%d expected %" PRIuMAX " bytes, got %" PRIuMAX, caller, line, (uintmax_t) expsize, (uintmax_t) realsize); - } - for (i=0; iuse_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 232be16b..41ee1713 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -125,7 +125,7 @@ struct { uint64_t *data64; size_t _size; } typedef i_type; 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 void cbits_drop(cbits* self) { c_free(self->data64); } STC_INLINE size_t cbits_size(const cbits* self) { return self->_size; } STC_INLINE cbits* cbits_take(cbits* self, cbits other) { @@ -138,7 +138,7 @@ 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}; + cbits set = {(uint64_t *)memcpy(c_malloc(bytes), other.data64, bytes), other._size}; return set; } @@ -153,7 +153,7 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) { 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); + 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); @@ -175,13 +175,13 @@ STC_INLINE cbits cbits_move(cbits* self) { } STC_INLINE cbits cbits_with_size(const size_t size, const bool value) { - cbits set = {(uint64_t *)c_MALLOC(_cbits_bytes(size)), size}; + 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}; + cbits set = {(uint64_t *)c_malloc(_cbits_bytes(size)), size}; cbits_set_pattern(&set, pattern); return set; } diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 0b6c29ec..4f0a2976 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -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 7c25d397..39ece500 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -68,51 +68,44 @@ #ifdef __cplusplus #include - #define c_ALLOC(T) static_cast(c_MALLOC(sizeof(T))) - #define c_ALLOC_N(T, n) static_cast(c_MALLOC(sizeof(T)*(n))) + #define c_ALLOC(T) static_cast(c_malloc(c_sizeof(T))) + #define c_ALLOC_N(T, n) static_cast(c_malloc(c_sizeof(T)*(n))) #define c_NEW(T, ...) new (c_ALLOC(T)) T(__VA_ARGS__) #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_LITERAL(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) -#endif #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_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_LTu(a, b) ((size_t)(a) < (size_t)(b)) -#define c_u2i(u) ((intptr_t)((u) + 0*sizeof((u) == 1U))) -#define c_i2u(i) ((size_t)(i) + 0*sizeof((i) == 1)) -#define c_sizeof(x) (intptr_t)sizeof(x) +#define c_sizeof (intptr_t)sizeof +#define c_strlen(str) (intptr_t)strlen(str) +#define c_strncmp(d, s, ilen) strncmp(d, s, 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_memcmp(d, s, ilen) memcmp(d, s, c_i2u(ilen)) -#define c_strlen(str) (intptr_t)strlen(str) -#define c_strncmp(d, s, ilen) strncmp(d, s, 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)) @@ -131,12 +124,12 @@ /* Function macros and others */ #define c_make(C, ...) \ - C##_from_n((C##_raw[])__VA_ARGS__, sizeof((C##_raw[])__VA_ARGS__)/sizeof(C##_raw)) + 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__) @@ -147,9 +140,9 @@ typedef const char* crawstr; #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; @@ -166,15 +159,15 @@ 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--); @@ -191,7 +184,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, ; 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) @@ -211,7 +204,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #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 @@ -244,12 +237,12 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #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 { \ - size_t index = 0; \ + intptr_t index = 0; \ for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \ ++index; \ } while (0) #define c_find_if_5(it, C, start, end, pred) do { \ - size_t index = 0; \ + intptr_t index = 0; \ const C##_value* _endref = (end).ref; \ for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ ++index; \ @@ -258,7 +251,7 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle, #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 d17182b1..f648547b 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -229,7 +229,7 @@ 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)); + _cx_value* d = (_cx_value*)c_realloc(self->_base, self->_len*sizeof(i_key)); if (d) { self->_base = d; self->_cap = self->_len; @@ -242,7 +242,7 @@ STC_DEF void _cx_memb(_drop)(_cx_self* self) { if (self->_base) { _cx_memb(_clear)(self); - c_FREE(self->_base); + c_free(self->_base); } } @@ -250,7 +250,7 @@ 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)); + _cx_value* d = (_cx_value*)c_realloc(self->_base, cap*sizeof(i_key)); if (!d) return 0; self->_cap = cap; diff --git a/include/stc/clist.h b/include/stc/clist.h index fc4d8540..4eac2954 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -348,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* diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 49d7d159..f9cd6986 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -312,8 +312,8 @@ 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) { @@ -390,9 +390,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 *)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) @@ -416,7 +416,7 @@ _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), + (uint8_t *) c_calloc(_nbuckets + 1, 1), self->size, (i_size)_nbuckets, }; bool ok = m.table && m._hashx; @@ -432,8 +432,8 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { } c_swap(_cx_self, self, &m); } - c_FREE(m._hashx); - c_FREE(m.table); + c_free(m._hashx); + c_free(m.table); return ok; } diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 8bb70216..59419f16 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -55,7 +55,7 @@ STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_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*sizeof *d); return d ? (self->data = d, self->_cap = cap, true) : false; } @@ -79,7 +79,7 @@ STC_INLINE void _cx_memb(_clear)(_cx_self* self) { } 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) { return q->_len; } diff --git a/include/stc/csmap.h b/include/stc/csmap.h index fba08eba..b92d25d8 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -228,7 +228,7 @@ STC_DEF bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { if (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)*sizeof(_cx_node)); if (!nodes) return false; nodes[0] = c_LITERAL(_cx_node){{0, 0}, 0}; @@ -578,7 +578,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/cstack.h b/include/stc/cstack.h index 2a4d620d..69ea6f0c 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -59,12 +59,12 @@ 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}; + _cx_self out = {(_cx_value *) c_malloc(cap*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}; + _cx_self out = {(_cx_value *) c_malloc(size*sizeof null), size, size}; while (size) out.data[--size] = null; return out; } @@ -79,7 +79,7 @@ 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 } @@ -100,7 +100,7 @@ STC_INLINE size_t _cx_memb(_capacity)(const _cx_self* self) { STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_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*sizeof *t); if (t) { self->_cap = n, self->data = t; return true; } #endif return false; @@ -154,7 +154,7 @@ 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*sizeof(_cx_value)), v._len, v._len}; if (!out.data) out._cap = 0; else for (size_t i = 0; i < v._len; ++v.data) out.data[i++] = i_keyclone((*v.data)); diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 071e40e8..9f6415ac 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -62,7 +62,7 @@ enum { cstr_s_cap = sizeof(cstr_buf) - 2 }; #define cstr_l_size(s) ((s)->lon.size) #define cstr_l_set_size(s, len) ((s)->lon.data[(s)->lon.size = (len)] = 0) #define cstr_l_data(s) (s)->lon.data -#define cstr_l_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); @@ -471,7 +471,7 @@ STC_DEF char* _cstr_internal_move(cstr* self, const size_t pos1, const size_t po STC_DEF char* _cstr_init(cstr* self, const size_t len, const size_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,26 +485,26 @@ 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); 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) { 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); + char* data = (char *)c_malloc(cap + 1); const size_t len = cstr_s_size(self); memcpy(data, self->sml.data, cstr_s_cap + 1); self->lon.data = data; diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 2ff2c58a..83d94652 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -277,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) { 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*sizeof(i_key)); if (!d) return false; self->data = d; diff --git a/include/stc/priv/altnames.h b/include/stc/priv/altnames.h index 695d3ebc..024868c2 100644 --- a/include/stc/priv/altnames.h +++ b/include/stc/priv/altnames.h @@ -23,10 +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_arraylen c_ARRAYLEN #define c_forlist c_FORLIST #define c_forrange c_FORRANGE diff --git a/misc/archived/csmap.h b/misc/archived/csmap.h index 6f3ee98b..aa7fc85e 100644 --- a/misc/archived/csmap.h +++ b/misc/archived/csmap.h @@ -400,7 +400,7 @@ _cx_memb(_erase_r_)(_cx_node *tn, const _cx_rawkey* rkey, int *erased) { } else { /* unlink node */ tx = tn; tn = tn->link[tn->link[0]->level == 0]; - c_FREE(tx); + c_free(tx); } } if (tn->link[0]->level < tn->level - 1 || tn->link[1]->level < tn->level - 1) { @@ -492,7 +492,7 @@ _cx_memb(_drop_r_)(_cx_node* tn) { _cx_memb(_drop_r_)(tn->link[0]); _cx_memb(_drop_r_)(tn->link[1]); _cx_memb(_value_drop)(&tn->value); - c_FREE(tn); + c_free(tn); } } diff --git a/misc/archived/cstr.h b/misc/archived/cstr.h index f4e9dde2..9111fe6d 100644 --- a/misc/archived/cstr.h +++ b/misc/archived/cstr.h @@ -77,7 +77,7 @@ STC_INLINE size_t cstr_size(const cstr* self) { return _cstr_p(self)->size STC_INLINE size_t cstr_capacity(cstr s) { return _cstr_p(&s)->cap; } STC_INLINE bool cstr_empty(cstr s) { return _cstr_p(&s)->size == 0; } STC_INLINE void cstr_drop(cstr* self) - { if (_cstr_p(self)->cap) c_FREE(_cstr_p(self)); } + { if (_cstr_p(self)->cap) c_free(_cstr_p(self)); } STC_INLINE cstr cstr_clone(cstr s) { return cstr_from_n(s.str, _cstr_p(&s)->size); } STC_INLINE void cstr_clear(cstr* self) @@ -142,7 +142,7 @@ STC_INLINE char* cstr_append_uninit(cstr *self, size_t n) { STC_INLINE cstr* cstr_take(cstr* self, cstr s) { if (self->str != s.str && _cstr_p(self)->cap) - c_FREE(_cstr_p(self)); + c_free(_cstr_p(self)); self->str = s.str; return self; } @@ -202,7 +202,7 @@ cstr_reserve(cstr* self, const size_t cap) { cstr_priv *p = _cstr_p(self); const size_t oldcap = p->cap; if (cap > oldcap) { - p = (cstr_priv*) c_REALLOC(((oldcap != 0) & (p != &_cstr_nullrep)) ? p : NULL, _cstr_opt_mem(cap)); + p = (cstr_priv*) c_realloc(((oldcap != 0) & (p != &_cstr_nullrep)) ? p : NULL, _cstr_opt_mem(cap)); if (!p) return NULL; self->str = p->chr; if (oldcap == 0) self->str[p->size = 0] = '\0'; @@ -222,7 +222,7 @@ cstr_resize(cstr* self, const size_t len, const char fill) { STC_DEF cstr cstr_from_n(const char* str, const size_t n) { if (n == 0) return cstr_NULL; - cstr_priv* prv = (cstr_priv*) c_MALLOC(_cstr_opt_mem(n)); + cstr_priv* prv = (cstr_priv*) c_malloc(_cstr_opt_mem(n)); cstr s = {(char *) memcpy(prv->chr, str, n)}; s.str[prv->size = n] = '\0'; prv->cap = _cstr_opt_cap(n); @@ -313,11 +313,11 @@ STC_DEF void cstr_replace_at_sv(cstr* self, const size_t pos, size_t len, csview repl) { const size_t sz = cstr_size(self); if (len > sz - pos) len = sz - pos; - char buf[256], *xstr = repl.size > 256 ? c_MALLOC(repl.size) : buf; + char buf[256], *xstr = repl.size > 256 ? c_malloc(repl.size) : buf; memcpy(xstr, repl.str, repl.size); _cstr_internal_move(self, pos + len, pos + repl.size); memcpy(&self->str[pos], xstr, repl.size); - if (repl.size > 256) c_FREE(xstr); + if (repl.size > 256) c_free(xstr); } STC_DEF cstr diff --git a/misc/examples/rawptr_elements.c b/misc/examples/rawptr_elements.c index 05910c77..96a9ba86 100644 --- a/misc/examples/rawptr_elements.c +++ b/misc/examples/rawptr_elements.c @@ -13,7 +13,7 @@ typedef int64_t inttype; #define i_valfrom(raw) c_NEW(inttype, raw) #define i_valto(x) **x #define i_valclone(x) c_NEW(inttype, *x) -#define i_valdrop(x) c_FREE(*x) +#define i_valdrop(x) c_free(*x) #include // With cbox: diff --git a/misc/examples/shape.c b/misc/examples/shape.c index 75a5e174..9ce0b946 100644 --- a/misc/examples/shape.c +++ b/misc/examples/shape.c @@ -38,7 +38,7 @@ void Shape_delete(Shape* shape) { if (shape) { shape->api->drop(shape); - c_FREE(shape); + c_free(shape); } } diff --git a/src/cregex.c b/src/cregex.c index cf2f8eaf..8cffd077 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -549,7 +549,7 @@ _optimize(_Parser *par, _Reprog *pp) */ intptr_t ipp = (intptr_t)pp; size_t size = sizeof(_Reprog) + (size_t)(par->freep - pp->firstinst)*sizeof(_Reinst); - _Reprog *npp = (_Reprog *)c_REALLOC(pp, size); + _Reprog *npp = (_Reprog *)c_realloc(pp, size); ptrdiff_t diff = (intptr_t)npp - ipp; if ((npp == NULL) | (diff == 0)) @@ -844,10 +844,10 @@ _regcomp1(_Reprog *progp, _Parser *par, const char *s, int cflags) /* get memory for the program. estimated max usage */ const size_t instcap = 5 + 6*strlen(s); - _Reprog* pp = (_Reprog *)c_REALLOC(progp, sizeof(_Reprog) + instcap*sizeof(_Reinst)); + _Reprog* pp = (_Reprog *)c_realloc(progp, sizeof(_Reprog) + instcap*sizeof(_Reinst)); if (pp == NULL) { par->error = CREG_OUTOFMEMORY; - c_FREE(progp); + c_free(progp); return NULL; } pp->flags.icase = (cflags & CREG_C_ICASE) != 0; @@ -898,7 +898,7 @@ _regcomp1(_Reprog *progp, _Parser *par, const char *s, int cflags) pp->nsubids = (unsigned)par->cursubid; out: if (par->error) { - c_FREE(pp); + c_free(pp); pp = NULL; } return pp; @@ -1133,7 +1133,7 @@ _regexec2(const _Reprog *progp, /* program to run */ _Relist *relists; /* mark space */ - relists = (_Relist *)c_MALLOC(2 * _BIGLISTSIZE*sizeof(_Relist)); + relists = (_Relist *)c_malloc(2 * _BIGLISTSIZE*sizeof(_Relist)); if (relists == NULL) return -1; @@ -1143,7 +1143,7 @@ _regexec2(const _Reprog *progp, /* program to run */ j->reliste[1] = relists + 2*_BIGLISTSIZE - 2; rv = _regexec1(progp, bol, mp, ms, j, mflags); - c_FREE(relists); + c_free(relists); return rv; } @@ -1308,6 +1308,6 @@ cregex_replace_pattern_6(const char* pattern, const char* input, const char* rep void cregex_drop(cregex* self) { - c_FREE(self->prog); + c_free(self->prog); } #endif -- cgit v1.2.3 From b677a0c3950b8294ba6458e682a885351273ac08 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 31 Jan 2023 18:55:08 +0100 Subject: Converted all containers but the maps and examples to signed sizes and indices. --- include/stc/cbits.h | 92 +++++++++---------- include/stc/ccommon.h | 11 +-- include/stc/cdeq.h | 110 +++++++++++------------ include/stc/clist.h | 16 ++-- include/stc/cpque.h | 32 +++---- include/stc/cregex.h | 2 +- include/stc/cstack.h | 32 +++---- include/stc/cstr.h | 212 ++++++++++++++++++++++---------------------- include/stc/csview.h | 74 ++++++++-------- include/stc/cvec.h | 66 +++++++------- include/stc/forward.h | 28 +++--- include/stc/utf8.h | 24 ++--- misc/examples/demos.c | 6 +- misc/examples/new_pque.c | 2 +- misc/examples/stack.c | 2 +- misc/examples/sview_split.c | 2 +- src/cregex.c | 25 +++--- src/utf8code.c | 16 ++-- 18 files changed, 372 insertions(+), 380 deletions(-) (limited to 'include') diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 41ee1713..9f0afc9c 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -56,40 +56,40 @@ int main() { #include #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 (int)__builtin_popcountll(x);} #elif defined(_MSC_VER) && defined(_WIN64) #include - STC_INLINE uint64_t cpopcount64(uint64_t x) {return __popcnt64(x);} + STC_INLINE int cpopcount64(uint64_t x) {return __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_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 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); +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,13 +174,13 @@ STC_INLINE cbits cbits_move(cbits* self) { return tmp; } -STC_INLINE cbits cbits_with_size(const size_t size, const bool value) { +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) { +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; @@ -198,7 +198,7 @@ struct { uint64_t data64[(i_capacity - 1)/64 + 1]; } typedef i_type; 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/ccommon.h b/include/stc/ccommon.h index 39ece500..f0d157e3 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -31,12 +31,11 @@ #include #include "priv/altnames.h" +#define c_NPOS INTPTR_MAX #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 #if defined STC_NDEBUG || defined NDEBUG #define c_ASSERT(expr) (void)(expr) @@ -91,11 +90,13 @@ #define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ _tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0) #define c_sizeof (intptr_t)sizeof -#define c_strlen(str) (intptr_t)strlen(str) -#define c_strncmp(d, s, ilen) strncmp(d, s, c_i2u(ilen)) +#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_memcmp(d, s, ilen) memcmp(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)) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index f648547b..44c5e8d5 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -42,15 +42,15 @@ _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, size_t n) +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, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #if !defined _i_queue #if !defined i_no_emplace @@ -89,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; } @@ -100,7 +100,7 @@ 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; + intptr_t n = self->_len; return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } @@ -115,15 +115,15 @@ STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_t n) #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; } @@ -131,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 @@ -144,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 @@ -167,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 @@ -207,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) { @@ -228,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; @@ -246,11 +246,11 @@ _cx_memb(_drop)(_cx_self* self) { } } -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; @@ -260,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); } @@ -308,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; } @@ -317,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 @@ -362,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; } @@ -375,8 +375,8 @@ _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; + c_memmove(p1, p2, (end - p2)*c_sizeof *p1); + self->_len -= len; return c_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } @@ -384,7 +384,7 @@ _cx_memb(_erase_range_p)(_cx_self* self, _cx_value* p1, _cx_value* p2) { 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)); @@ -396,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 4eac2954..283c8774 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -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); @@ -134,11 +134,11 @@ STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw) #endif // !i_no_emplace 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, size_t n) +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, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, size_t n) { return true; } +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) @@ -150,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; @@ -414,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/cpque.h b/include/stc/cpque.h index 59419f16..21130864 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -41,19 +41,19 @@ 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_LITERAL(_cx_self){NULL}; } -STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +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, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { +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); return d ? (self->data = d, self->_cap = cap, true) : false; @@ -62,32 +62,32 @@ STC_INLINE bool _cx_memb(_reserve)(_cx_self* self, const size_t cap) { 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); } -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) @@ -117,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; @@ -128,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); } @@ -143,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); } @@ -155,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/cregex.h b/include/stc/cregex.h index 206c319d..c41fbb67 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -143,7 +143,7 @@ cstr cregex_replace_sv_6(const cregex* re, csview input, const char* replace, un #define cregex_replace_3(re, input, replace) cregex_replace_4(re, input, replace, ~0U) STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char* replace, unsigned count) { - csview sv = {input, strlen(input)}; + csview sv = {input, c_strlen(input)}; return cregex_replace_sv_4(re, sv, replace, count); } diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 69ea6f0c..97d5256c 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -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; } @@ -83,13 +83,13 @@ STC_INLINE void _cx_memb(_drop)(_cx_self* self) { #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,17 +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; @@ -136,15 +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 void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) +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, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } -STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx) +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 @@ -154,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; } diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 9f6415ac..f965487c 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -47,26 +47,26 @@ #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_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 **********************************/ @@ -74,19 +74,19 @@ STC_API char* _cstr_internal_move(cstr* self, size_t pos1, size_t pos2); #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) @@ -101,25 +101,25 @@ STC_INLINE csview cstr_sv(const cstr* 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); @@ -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); } @@ -375,33 +375,33 @@ STC_INLINE char* cstr_append_s(cstr* self, cstr s) { #define cstr_replace(...) c_MACRO_OVERLOAD(cstr_replace, __VA_ARGS__) #define cstr_replace_3(self, search, repl) cstr_replace_4(self, search, repl, ~0U) -STC_INLINE void cstr_replace_4(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)); +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_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,25 +451,25 @@ 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); cstr_l_set_size(self, len); @@ -488,13 +488,13 @@ STC_DEF void cstr_shrink_to_fit(cstr* self) { 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); } } -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); @@ -505,47 +505,47 @@ STC_DEF char* cstr_reserve(cstr* self, const size_t cap) { /* 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); + 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 4df735b3..a30672cd 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -34,41 +34,41 @@ #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_LITERAL(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; @@ -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 83d94652..11d145e6 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,9 +114,9 @@ _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, size_t n) +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, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE i_key _cx_memb(_value_clone)(_cx_value val) { return i_keyclone(val); } @@ -128,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; } @@ -142,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; @@ -162,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 @@ -175,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 @@ -188,17 +188,17 @@ _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; + intptr_t n = self->_len; return c_LITERAL(_cx_iter){n ? self->data : NULL, self->data + n}; } @@ -211,8 +211,8 @@ STC_INLINE void _cx_memb(_next)(_cx_iter* it) STC_INLINE _cx_iter _cx_memb(_advance)(_cx_iter it, intptr_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 @@ -281,9 +281,9 @@ _cx_memb(_drop)(_cx_self* self) { } 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; @@ -293,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; @@ -316,16 +316,16 @@ _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_LITERAL(_cx_iter){pos, self->data + self->_len}; @@ -334,19 +334,19 @@ _cx_memb(_insert_uninit)(_cx_self* self, _cx_value* pos, const size_t n) { 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_LITERAL(_cx_iter){p2 == end ? NULL : p1, end - len}; } @@ -362,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)); @@ -374,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)); diff --git a/include/stc/forward.h b/include/stc/forward.h index 60c18c37..dfb43a9f 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -23,7 +23,7 @@ #ifndef STC_FORWARD_H_INCLUDED #define STC_FORWARD_H_INCLUDED -#include +#include #define forward_carc(CX, VAL) _c_carc_types(CX, VAL) #define forward_carr2(CX, VAL) _c_carr2_types(CX, VAL) @@ -42,15 +42,17 @@ #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 +73,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 +82,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; \ @@ -188,20 +180,20 @@ typedef union { #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/utf8.h b/include/stc/utf8.h index ea661bb6..ce50af87 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -23,7 +23,7 @@ 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 uint32_t utf8_peek_off(const char *s, int offset); @@ -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); } @@ -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,29 +89,29 @@ 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 diff --git a/misc/examples/demos.c b/misc/examples/demos.c index c92f95b2..898c24cf 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -38,7 +38,7 @@ void vectordemo1() c_WITH (cvec_ix bignums = cvec_ix_with_capacity(100), cvec_ix_drop(&bignums)) { cvec_ix_reserve(&bignums, 100); - for (size_t i = 10; i <= 100; i += 10) + for (int i = 10; i <= 100; i += 10) cvec_ix_push(&bignums, i * i); printf("erase - %d: %" PRIu64 "\n", 3, bignums.data[3]); @@ -47,8 +47,8 @@ void vectordemo1() cvec_ix_pop(&bignums); // erase the last cvec_ix_erase_n(&bignums, 0, 1); // erase the first - for (size_t i = 0; i < cvec_ix_size(&bignums); ++i) { - printf("%" c_ZU ": %" PRIu64 "\n", i, bignums.data[i]); + for (int i = 0; i < cvec_ix_size(&bignums); ++i) { + printf("%d: %" PRIu64 "\n", i, bignums.data[i]); } } } diff --git a/misc/examples/new_pque.c b/misc/examples/new_pque.c index d127b592..b3d94c2f 100644 --- a/misc/examples/new_pque.c +++ b/misc/examples/new_pque.c @@ -48,7 +48,7 @@ int main() cpque_int_push(&ique, 123); cpque_int_push(&ique, 321); // print - for (size_t i=0; iexprp, cls[i].c, (size_t)cls[i].n)) { if (par->rune_type == TOK_IRUNE && (cls[i].r == UTF_ll || cls[i].r == UTF_lu)) @@ -908,8 +908,7 @@ out: static int _runematch(_Rune s, _Rune r) { - int inv = 0; - uint32_t n; + int inv = 0, n; switch (s) { case ASC_D: inv = 1; case ASC_d: return inv ^ (isdigit(r) != 0); case ASC_S: inv = 1; case ASC_s: return inv ^ (isspace(r) != 0); @@ -951,7 +950,7 @@ _runematch(_Rune s, _Rune r) case UTF_greek: case UTF_GREEK: case UTF_han: case UTF_HAN: case UTF_latin: case UTF_LATIN: - n = s - UTF_GRP; + n = (int)s - UTF_GRP; inv = n & 1; return inv ^ utf8_isgroup(n / 2, r); } @@ -1050,7 +1049,7 @@ _regexec1(const _Reprog *progp, /* program to run */ tlp->se.m[inst->r.subid].str = s; continue; case TOK_RBRA: - tlp->se.m[inst->r.subid].size = (size_t)(s - tlp->se.m[inst->r.subid].str); + tlp->se.m[inst->r.subid].size = (s - tlp->se.m[inst->r.subid].str); continue; case TOK_ANY: ok = (r != '\n'); @@ -1100,7 +1099,7 @@ _regexec1(const _Reprog *progp, /* program to run */ match = !(mflags & CREG_M_FULLMATCH) || ((s == j->eol || r == 0 || r == '\n') && (tlp->se.m[0].str == bol || tlp->se.m[0].str[-1] == '\n')); - tlp->se.m[0].size = (size_t)(s - tlp->se.m[0].str); + tlp->se.m[0].size = (s - tlp->se.m[0].str); if (mp != NULL) _renewmatch(mp, ms, &tlp->se, progp->nsubids); break; @@ -1199,7 +1198,7 @@ static void _build_subst(const char* replace, unsigned nmatch, const csview match[], bool (*mfun)(int, csview, cstr*), cstr* subst) { cstr_buf buf = cstr_buffer(subst); - size_t len = 0, cap = buf.cap; + intptr_t len = 0, cap = buf.cap; char* dst = buf.data; cstr mstr = cstr_NULL; @@ -1283,7 +1282,7 @@ cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigne while (count-- && cregex_find_sv(re, input, match) == CREG_OK) { _build_subst(replace, nmatch, match, mfun, &subst); - const size_t mpos = (size_t)(match[0].str - input.str); + const intptr_t mpos = (match[0].str - input.str); if (copy & (mpos > 0)) cstr_append_n(&out, input.str, mpos); cstr_append_s(&out, subst); input.str = match[0].str + match[0].size; @@ -1300,7 +1299,7 @@ cregex_replace_pattern_6(const char* pattern, const char* input, const char* rep cregex re = cregex_init(); if (cregex_compile(&re, pattern, crflags) != CREG_OK) assert(0); - csview sv = {input, strlen(input)}; + csview sv = {input, c_strlen(input)}; cstr out = cregex_replace_sv(&re, sv, replace, count, mfun, crflags); cregex_drop(&re); return out; diff --git a/src/utf8code.c b/src/utf8code.c index 3ad47941..351ddab5 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -53,7 +53,7 @@ uint32_t utf8_peek_off(const char* s, int pos) { return utf8_peek(s); } -bool utf8_valid_n(const char* s, size_t nbytes) { +bool utf8_valid_n(const char* s, intptr_t nbytes) { utf8_decode_t d = {.state=0}; while ((nbytes-- != 0) & (*s != 0)) utf8_decode(&d, (uint8_t)*s++); @@ -61,7 +61,7 @@ bool utf8_valid_n(const char* s, size_t nbytes) { } uint32_t utf8_casefold(uint32_t c) { - for (size_t i=0; i < casefold_len; ++i) { + for (int i=0; i < casefold_len; ++i) { const struct CaseMapping entry = casemappings[i]; if (c <= entry.c2) { if (c < entry.c1) return c; @@ -74,7 +74,7 @@ uint32_t utf8_casefold(uint32_t c) { } uint32_t utf8_tolower(uint32_t c) { - for (size_t i=0; i < sizeof upcase_ind/sizeof *upcase_ind; ++i) { + for (int i=0; i < (int)(sizeof upcase_ind/sizeof *upcase_ind); ++i) { const struct CaseMapping entry = casemappings[upcase_ind[i]]; if (c <= entry.c2) { if (c < entry.c1) return c; @@ -87,7 +87,7 @@ uint32_t utf8_tolower(uint32_t c) { } uint32_t utf8_toupper(uint32_t c) { - for (size_t i=0; i < sizeof lowcase_ind/sizeof *lowcase_ind; ++i) { + for (int i=0; i < (int)(sizeof lowcase_ind/sizeof *lowcase_ind); ++i) { const struct CaseMapping entry = casemappings[lowcase_ind[i]]; if (c <= entry.m2) { int d = entry.m2 - entry.c2; @@ -101,7 +101,7 @@ uint32_t utf8_toupper(uint32_t c) { int utf8_icmp_sv(const csview s1, const csview s2) { utf8_decode_t d1 = {.state=0}, d2 = {.state=0}; - size_t j1 = 0, j2 = 0; + intptr_t j1 = 0, j2 = 0; while ((j1 < s1.size) & (j2 < s2.size)) { do { utf8_decode(&d1, (uint8_t)s1.str[j1++]); } while (d1.state); do { utf8_decode(&d2, (uint8_t)s2.str[j2++]); } while (d2.state); @@ -142,7 +142,7 @@ bool utf8_isgroup(int group, uint32_t c) { bool utf8_isalpha(uint32_t c) { static int16_t groups[] = {U8G_Latin, U8G_Nl, U8G_Greek, U8G_Cyrillic, U8G_Han, U8G_Devanagari, U8G_Arabic}; - if (c < 128) return isalpha(c) != 0; + if (c < 128) return isalpha((int)c) != 0; for (unsigned j=0; j < c_ARRAYLEN(groups); ++j) if (utf8_isgroup(groups[j], c)) return true; @@ -150,13 +150,13 @@ bool utf8_isalpha(uint32_t c) { } bool utf8_iscased(uint32_t c) { - if (c < 128) return isalpha(c) != 0; + if (c < 128) return isalpha((int)c) != 0; return utf8_islower(c) || utf8_isupper(c) || utf8_isgroup(U8G_Lt, c); } bool utf8_isword(uint32_t c) { - if (c < 128) return (isalnum(c) != 0) | (c == '_'); + if (c < 128) return (isalnum((int)c) != 0) | (c == '_'); return utf8_isalpha(c) || utf8_isgroup(U8G_Nd, c) || utf8_isgroup(U8G_Pc, c); } -- cgit v1.2.3 From 6ce6ef3307e52db5813d3c8d6a2cba52df06daf8 Mon Sep 17 00:00:00 2001 From: Tyge Lovset Date: Wed, 1 Feb 2023 08:38:45 +0100 Subject: Massive update from unsigned sizes and indices to signed. --- README.md | 2 +- docs/cbits_api.md | 46 ++++++++++++++++---------------- docs/ccommon_api.md | 2 +- docs/cdeq_api.md | 20 +++++++------- docs/clist_api.md | 4 +-- docs/cmap_api.md | 16 +++++------ docs/cpque_api.md | 12 ++++----- docs/cqueue_api.md | 2 +- docs/crandom_api.md | 4 +-- docs/cset_api.md | 12 ++++----- docs/csmap_api.md | 8 +++--- docs/cspan_api.md | 22 +++++++-------- docs/csset_api.md | 8 +++--- docs/cstack_api.md | 16 +++++------ docs/cstr_api.md | 62 +++++++++++++++++++++---------------------- docs/csview_api.md | 40 ++++++++++++++-------------- docs/cvec_api.md | 28 +++++++++---------- include/stc/algo/csort.h | 4 +-- include/stc/ccommon.h | 9 +++---- include/stc/cdeq.h | 2 +- include/stc/cmap.h | 48 ++++++++++++++++----------------- include/stc/cpque.h | 2 +- include/stc/crandom.h | 2 +- include/stc/cregex.h | 14 +++++----- include/stc/csmap.h | 16 +++++------ include/stc/cstack.h | 2 +- include/stc/cstr.h | 2 +- include/stc/cvec.h | 2 +- include/stc/forward.h | 14 +++++----- include/stc/priv/template.h | 2 +- include/stc/utf8.h | 4 +-- misc/archived/cstr.h | 4 +-- misc/examples/astar.c | 4 +-- misc/examples/birthday.c | 6 ++--- misc/examples/bits.c | 12 ++++----- misc/examples/bits2.c | 4 +-- misc/examples/books.c | 2 +- misc/examples/complex.c | 2 +- misc/examples/csmap_erase.c | 6 ++--- misc/examples/csmap_find.c | 2 +- misc/examples/cstr_match.c | 6 ++--- misc/examples/demos.c | 4 +-- misc/examples/forfilter.c | 2 +- misc/examples/gauss1.c | 2 +- misc/examples/gauss2.c | 4 +-- misc/examples/make.sh | 2 +- misc/examples/new_queue.c | 10 +++---- misc/examples/prime.c | 18 ++++++------- misc/examples/priority.c | 6 ++--- misc/examples/queue.c | 6 ++--- misc/examples/random.c | 18 ++++++------- misc/examples/regex_match.c | 3 ++- misc/examples/sort.c | 8 +++--- misc/examples/sso_map.c | 2 +- misc/examples/sso_substr.c | 2 +- misc/examples/utf8replace_c.c | 7 ++--- src/cregex.c | 60 ++++++++++++++++++++--------------------- src/utf8code.c | 4 +-- 58 files changed, 313 insertions(+), 320 deletions(-) (limited to 'include') diff --git a/README.md b/README.md index 2ba88048..a4b3c76e 100644 --- a/README.md +++ b/README.md @@ -112,7 +112,7 @@ int main(void) FVec_push_back(&vec, 20.f); FVec_push_back(&vec, 30.f); - for (size_t i = 0; i < FVec_size(vec); ++i) + for (intptr_t i = 0; i < FVec_size(vec); ++i) printf(" %g", vec.data[i]); FVec_drop(&vec); // cleanup memory diff --git a/docs/cbits_api.md b/docs/cbits_api.md index b21611df..726fb68b 100644 --- a/docs/cbits_api.md +++ b/docs/cbits_api.md @@ -21,34 +21,34 @@ All cbits definitions and prototypes are available by including a single header ```c cbits cbits_init(void); cbits cbits_from(const char* str); -cbits cbits_with_size(size_t size, bool value); // size must be <= N if N is defined -cbits cbits_with_pattern(size_t size, uint64_t pattern); +cbits cbits_with_size(intptr_t size, bool value); // size must be <= N if N is defined +cbits cbits_with_pattern(intptr_t size, uint64_t pattern); cbits cbits_clone(cbits other); void cbits_clear(cbits* self); cbits* cbits_copy(cbits* self, const cbits* other); -void cbits_resize(cbits* self, size_t size, bool value); // only if i_len is not defined +void cbits_resize(cbits* self, intptr_t size, bool value); // only if i_len is not defined void cbits_drop(cbits* self); cbits* cbits_take(cbits* self, const cbits* other); // give other to self cbits cbits_move(cbits* self); // transfer self to caller -size_t cbits_size(const cbits* self); -size_t cbits_count(const cbits* self); // count number of bits set +intptr_t cbits_size(const cbits* self); +intptr_t cbits_count(const cbits* self); // count number of bits set -bool cbits_test(const cbits* self, size_t i); -bool cbits_at(const cbits* self, size_t i); // same as cbits_test() +bool cbits_test(const cbits* self, intptr_t i); +bool cbits_at(const cbits* self, intptr_t i); // same as cbits_test() bool cbits_subset_of(const cbits* self, const cbits* other); // is set a subset of other? bool cbits_disjoint(const cbits* self, const cbits* other); // no common bits -char* cbits_to_str(const cbits* self, char* str, size_t start, size_t stop); +char* cbits_to_str(const cbits* self, char* str, intptr_t start, intptr_t stop); -void cbits_set(cbits* self, size_t i); -void cbits_reset(cbits* self, size_t i); -void cbits_set_value(cbits* self, size_t i, bool value); +void cbits_set(cbits* self, intptr_t i); +void cbits_reset(cbits* self, intptr_t i); +void cbits_set_value(cbits* self, intptr_t i, bool value); void cbits_set_all(cbits* self, bool value); void cbits_set_pattern(cbits* self, uint64_t pattern); void cbits_flip_all(cbits* self); -void cbits_flip(cbits* self, size_t i); +void cbits_flip(cbits* self, intptr_t i); void cbits_intersect(cbits* self, const cbits* other); void cbits_union(cbits* self, const cbits* other); @@ -70,19 +70,19 @@ void cbits_xor(cbits* self, const cbits* other); // set #include #include -cbits sieveOfEratosthenes(size_t n) +cbits sieveOfEratosthenes(intptr_t n) { cbits bits = cbits_with_size(n>>1, true); - size_t q = (size_t) sqrt(n); + intptr_t q = (intptr_t) sqrt(n); - for (size_t i = 3; i <= q; i += 2) { - for (size_t j = i; j < n; j += 2) { + for (intptr_t i = 3; i <= q; i += 2) { + for (intptr_t j = i; j < n; j += 2) { if (cbits_test(&bits, j>>1)) { i = j; break; } } - for (size_t j = i*i; j < n; j += i*2) + for (intptr_t j = i*i; j < n; j += i*2) cbits_reset(&bits, j>>1); } return bits; @@ -90,19 +90,19 @@ cbits sieveOfEratosthenes(size_t n) int main(void) { - size_t n = 100000000; - printf("computing prime numbers up to %" c_ZU "\n", n); + intptr_t n = 100000000; + printf("computing prime numbers up to %" c_ZI "\n", n); clock_t t1 = clock(); cbits primes = sieveOfEratosthenes(n + 1); - size_t nprimes = cbits_count(&primes); + intptr_t nprimes = cbits_count(&primes); clock_t t2 = clock(); - printf("number of primes: %" c_ZU ", time: %f\n", nprimes, (float)(t2 - t1)/CLOCKS_PER_SEC); + printf("number of primes: %" c_ZI ", time: %f\n", nprimes, (float)(t2 - t1)/CLOCKS_PER_SEC); printf(" 2"); - for (size_t i = 3; i < 1000; i += 2) - if (cbits_test(&primes, i>>1)) printf(" %" c_ZU, i); + for (intptr_t i = 3; i < 1000; i += 2) + if (cbits_test(&primes, i>>1)) printf(" %" c_ZI, i); puts(""); cbits_drop(&primes); diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index 18edbeba..43f747ab 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -361,5 +361,5 @@ Memory allocator for the entire library. Macros can be overridden by the user. - **c_ARRAYLEN(array)**: Return number of elements in an array. array must not be a pointer! ```c int array[] = {1, 2, 3, 4}; -size_t n = c_ARRAYLEN(array); +intptr_t n = c_ARRAYLEN(array); ``` diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 584d382f..44a75fc2 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -27,21 +27,21 @@ See the c++ class [std::deque](https://en.cppreference.com/w/cpp/container/deque ```c cdeq_X cdeq_X_init(void); -cdeq_X cdeq_X_with_capacity(size_t size); +cdeq_X cdeq_X_with_capacity(intptr_t size); cdeq_X cdeq_X_clone(cdeq_X deq); void cdeq_X_clear(cdeq_X* self); void cdeq_X_copy(cdeq_X* self, const cdeq_X* other); cdeq_X_iter cdeq_X_copy_range(cdeq_X* self, i_val* pos, const i_val* p1, const i_val* p2); -bool cdeq_X_reserve(cdeq_X* self, size_t cap); +bool cdeq_X_reserve(cdeq_X* self, intptr_t cap); void cdeq_X_shrink_to_fit(cdeq_X* self); void cdeq_X_drop(cdeq_X* self); // destructor bool cdeq_X_empty(const cdeq_X* self); -size_t cdeq_X_size(const cdeq_X* self); -size_t cdeq_X_capacity(const cdeq_X* self); +intptr_t cdeq_X_size(const cdeq_X* self); +intptr_t cdeq_X_capacity(const cdeq_X* self); -const cdeq_X_value* cdeq_X_at(const cdeq_X* self, size_t idx); +const cdeq_X_value* cdeq_X_at(const cdeq_X* self, intptr_t idx); const cdeq_X_value* cdeq_X_get(const cdeq_X* self, i_valraw raw); // return NULL if not found cdeq_X_value* cdeq_X_get_mut(cdeq_X* self, i_valraw raw); // mutable get cdeq_X_iter cdeq_X_find(const cdeq_X* self, i_valraw raw); @@ -60,18 +60,18 @@ cdeq_X_value* cdeq_X_emplace_back(cdeq_X* self, i_valraw raw); cdeq_X_value* cdeq_X_emplace(cdeq_X* self, i_valraw raw); // alias for emplace_back() void cdeq_X_pop_back(cdeq_X* self); -cdeq_X_iter cdeq_X_insert(cdeq_X* self, size_t idx, i_val value); // move value -cdeq_X_iter cdeq_X_insert_n(cdeq_X* self, size_t idx, const i_val[] arr, size_t n); // move arr values +cdeq_X_iter cdeq_X_insert(cdeq_X* self, intptr_t idx, i_val value); // move value +cdeq_X_iter cdeq_X_insert_n(cdeq_X* self, intptr_t idx, const i_val[] arr, intptr_t n); // move arr values cdeq_X_iter cdeq_X_insert_at(cdeq_X* self, cdeq_X_iter it, i_val value); // move value cdeq_X_iter cdeq_X_insert_range(cdeq_X* self, i_val* pos, const i_val* p1, const i_val* p2); -cdeq_X_iter cdeq_X_emplace_n(cdeq_X* self, size_t idx, const i_valraw[] arr, size_t n); // clone values +cdeq_X_iter cdeq_X_emplace_n(cdeq_X* self, intptr_t idx, const i_valraw[] arr, intptr_t n); // clone values cdeq_X_iter cdeq_X_emplace_at(cdeq_X* self, cdeq_X_iter it, i_valraw raw); cdeq_X_iter cdeq_X_emplace_range(cdeq_X* self, i_val* pos, const i_valraw* p1, const i_valraw* p2); -cdeq_X_iter cdeq_X_erase_n(cdeq_X* self, size_t idx, size_t n); +cdeq_X_iter cdeq_X_erase_n(cdeq_X* self, intptr_t idx, intptr_t n); cdeq_X_iter cdeq_X_erase_at(cdeq_X* self, cdeq_X_iter it); cdeq_X_iter cdeq_X_erase_range(cdeq_X* self, cdeq_X_iter it1, cdeq_X_iter it2); cdeq_X_iter cdeq_X_erase_range_p(cdeq_X* self, i_val* p1, i_val* p2); @@ -83,7 +83,7 @@ void cdeq_X_sort_range(cdeq_X_iter i1, cdeq_X_iter i2, cdeq_X_iter cdeq_X_begin(const cdeq_X* self); cdeq_X_iter cdeq_X_end(const cdeq_X* self); void cdeq_X_next(cdeq_X_iter* it); -cdeq_X_iter cdeq_X_advance(cdeq_X_iter it, intptr_t n); +cdeq_X_iter cdeq_X_advance(cdeq_X_iter it, size_t n); cdeq_X_raw cdeq_X_value_toraw(cdeq_X_value* pval); cdeq_X_value cdeq_X_value_clone(cdeq_X_value val); diff --git a/docs/clist_api.md b/docs/clist_api.md index e97d7b5d..13c27308 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -48,7 +48,7 @@ void clist_X_copy(clist_X* self, const clist_X* other); void clist_X_drop(clist_X* self); // destructor bool clist_X_empty(const clist_X* list); -size_t clist_X_count(const clist_X* list); // size() in O(n) time +intptr_t clist_X_count(const clist_X* list); // size() in O(n) time clist_X_value* clist_X_back(const clist_X* self); clist_X_value* clist_X_front(const clist_X* self); @@ -67,7 +67,7 @@ clist_X_iter clist_X_emplace_at(clist_X* self, clist_X_iter it, i_valraw void clist_X_pop_front(clist_X* self); clist_X_iter clist_X_erase_at(clist_X* self, clist_X_iter it); // return iter after it clist_X_iter clist_X_erase_range(clist_X* self, clist_X_iter it1, clist_X_iter it2); -size_t clist_X_remove(clist_X* self, i_valraw raw); // removes all matches +intptr_t clist_X_remove(clist_X* self, i_valraw raw); // removes all matches clist_X clist_X_split_off(clist_X* self, clist_X_iter i1, clist_X_iter i2); // split off [i1, i2) clist_X_iter clist_X_splice(clist_X* self, clist_X_iter it, clist_X* other); // return updated valid it diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 3848e67e..7b8fbb8f 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -48,20 +48,20 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain ```c cmap_X cmap_X_init(void); -cmap_X cmap_X_with_capacity(size_t cap); +cmap_X cmap_X_with_capacity(intptr_t cap); cmap_X cmap_X_clone(cmap_x map); void cmap_X_clear(cmap_X* self); void cmap_X_copy(cmap_X* self, const cmap_X* other); -float cmap_X_max_load_factor(const cmap_X* self); // default: 0.85f -bool cmap_X_reserve(cmap_X* self, size_t size); +float cmap_X_max_load_factor(const cmap_X* self); // default: 0.85f +bool cmap_X_reserve(cmap_X* self, intptr_t size); void cmap_X_shrink_to_fit(cmap_X* self); void cmap_X_drop(cmap_X* self); // destructor -size_t cmap_X_size(const cmap_X* self); -size_t cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor +intptr_t cmap_X_size(const cmap_X* self); +intptr_t cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor bool cmap_X_empty(const cmap_X* self ); -size_t cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets +intptr_t cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets const cmap_X_mapped* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map cmap_X_mapped* cmap_X_at_mut(cmap_X* self, i_keyraw rkey); // mutable at @@ -77,7 +77,7 @@ cmap_X_result cmap_X_push(cmap_X* self, cmap_X_value entry); cmap_X_result cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped -size_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 +intptr_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 cmap_X_iter cmap_X_erase_at(cmap_X* self, cmap_X_iter it); // return iter after it void cmap_X_erase_entry(cmap_X* self, cmap_X_value* entry); @@ -93,7 +93,7 @@ Helpers: ```c uint64_t c_default_hash(const X *obj); // macro, calls cfasthash(obj, sizeof *obj) uint64_t cstrhash(const char *str); // string hash funcion, uses strlen() -uint64_t cfasthash(const void *data, size_t len); // base hash function +uint64_t cfasthash(const void *data, intptr_t len); // base hash function // equalto template parameter functions: bool c_default_eq(const i_keyraw* a, const i_keyraw* b); // *a == *b diff --git a/docs/cpque_api.md b/docs/cpque_api.md index 392d3149..48a5b29e 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -28,17 +28,17 @@ See the c++ class [std::priority_queue](https://en.cppreference.com/w/cpp/contai ```c cpque_X cpque_X_init(void); // create empty pri-queue. -cpque_X cpque_X_with_capacity(size_t cap); -cpque_X cpque_X_with_size(size_t size, i_val null); +cpque_X cpque_X_with_capacity(intptr_t cap); +cpque_X cpque_X_with_size(intptr_t size, i_val null); cpque_X cpque_X_clone(cpque_X pq); void cpque_X_clear(cpque_X* self); -bool cpque_X_reserve(cpque_X* self, size_t n); +bool cpque_X_reserve(cpque_X* self, intptr_t n); void cpque_X_shrink_to_fit(cpque_X* self); void cpque_X_copy(cpque_X* self, const cpque_X* other); void cpque_X_drop(cpque_X* self); // destructor -size_t cpque_X_size(const cpque_X* self); +intptr_t cpque_X_size(const cpque_X* self); bool cpque_X_empty(const cpque_X* self); i_val* cpque_X_top(const cpque_X* self); @@ -47,7 +47,7 @@ void cpque_X_push(cpque_X* self, i_val value); void cpque_X_emplace(cpque_X* self, i_valraw raw); // converts from raw void cpque_X_pop(cpque_X* self); -void cpque_X_erase_at(cpque_X* self, size_t idx); +void cpque_X_erase_at(cpque_X* self, intptr_t idx); i_val cpque_X_value_clone(i_val value); ``` @@ -71,7 +71,7 @@ i_val cpque_X_value_clone(i_val value); int main() { - size_t N = 10000000; + intptr_t N = 10000000; stc64_t rng = stc64_new(1234); stc64_uniform_t dist = stc64_uniform_new(0, N * 10); diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index 977fa855..212cdabe 100644 --- a/docs/cqueue_api.md +++ b/docs/cqueue_api.md @@ -32,7 +32,7 @@ void cqueue_X_clear(cqueue_X* self); void cqueue_X_copy(cqueue_X* self, const cqueue_X* other); void cqueue_X_drop(cqueue_X* self); // destructor -size_t cqueue_X_size(const cqueue_X* self); +intptr_t cqueue_X_size(const cqueue_X* self); bool cqueue_X_empty(const cqueue_X* self); cqueue_X_value* cqueue_X_front(const cqueue_X* self); cqueue_X_value* cqueue_X_back(const cqueue_X* self); diff --git a/docs/crandom_api.md b/docs/crandom_api.md index bd3bf848..7dced0eb 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -76,7 +76,7 @@ double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist); // Declare int -> int sorted map. Uses typetag 'i' for ints. #define i_key int -#define i_val size_t +#define i_val intptr_t #define i_tag i #include @@ -102,7 +102,7 @@ int main() // Print the gaussian bar chart cstr bar = cstr_init(); c_FOREACH (i, csmap_i, mhist) { - size_t n = (size_t) (i.ref->second * StdDev * Scale * 2.5 / N); + int n = (int)(i.ref->second * StdDev * Scale * 2.5 / N); if (n > 0) { cstr_resize(&bar, n, '*'); printf("%4d %s\n", i.ref->first, cstr_str(&bar)); diff --git a/docs/cset_api.md b/docs/cset_api.md index e1d08a87..2e80a49b 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -30,20 +30,20 @@ A **cset** is an associative container that contains a set of unique objects of ```c cset_X cset_X_init(void); -cset_X cset_X_with_capacity(size_t cap); +cset_X cset_X_with_capacity(intptr_t cap); cset_X cset_X_clone(cset_x set); void cset_X_clear(cset_X* self); void cset_X_copy(cset_X* self, const cset_X* other); float cset_X_max_load_factor(const cset_X* self); // default: 0.85 -bool cset_X_reserve(cset_X* self, size_t size); +bool cset_X_reserve(cset_X* self, intptr_t size); void cset_X_shrink_to_fit(cset_X* self); void cset_X_drop(cset_X* self); // destructor -size_t cset_X_size(const cset_X* self); // num. of allocated buckets -size_t cset_X_capacity(const cset_X* self); // buckets * max_load_factor +intptr_t cset_X_size(const cset_X* self); // num. of allocated buckets +intptr_t cset_X_capacity(const cset_X* self); // buckets * max_load_factor bool cset_X_empty(const cset_X* self); -size_t cset_X_bucket_count(const cset_X* self); +intptr_t cset_X_bucket_count(const cset_X* self); bool cset_X_contains(const cset_X* self, i_keyraw rkey); const cset_X_value* cset_X_get(const cset_X* self, i_keyraw rkey); // return NULL if not found @@ -54,7 +54,7 @@ cset_X_result cset_X_insert(cset_X* self, i_key key); cset_X_result cset_X_push(cset_X* self, i_key key); // alias for insert. cset_X_result cset_X_emplace(cset_X* self, i_keyraw rkey); -size_t cset_X_erase(cset_X* self, i_keyraw rkey); // return 0 or 1 +intptr_t cset_X_erase(cset_X* self, i_keyraw rkey); // return 0 or 1 cset_X_iter cset_X_erase_at(cset_X* self, cset_X_iter it); // return iter after it void cset_X_erase_entry(cset_X* self, cset_X_value* entry); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 687a6cab..d0a57bdb 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -42,8 +42,8 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo ```c csmap_X csmap_X_init(void); -csset_X csmap_X_with_capacity(size_t cap); -bool csmap_X_reserve(csmap_X* self, size_t cap); +csset_X csmap_X_with_capacity(intptr_t cap); +bool csmap_X_reserve(csmap_X* self, intptr_t cap); void csmap_X_shrink_to_fit(csmap_X* self); csmap_X csmap_X_clone(csmap_x map); @@ -51,7 +51,7 @@ void csmap_X_clear(csmap_X* self); void csmap_X_copy(csmap_X* self, const csmap_X* other); void csmap_X_drop(csmap_X* self); // destructor -size_t csmap_X_size(const csmap_X* self); +intptr_t csmap_X_size(const csmap_X* self); bool csmap_X_empty(const csmap_X* self); bool csmap_X_capacity(const csmap_X* self); @@ -74,7 +74,7 @@ csmap_X_result csmap_X_push(csmap_X* self, csmap_X_value entry); csmap_X_result csmap_X_emplace(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map csmap_X_result csmap_X_emplace_or_assign(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped -size_t csmap_X_erase(csmap_X* self, i_keyraw rkey); +intptr_t csmap_X_erase(csmap_X* self, i_keyraw rkey); csmap_X_iter csmap_X_erase_at(csmap_X* self, csmap_X_iter it); // returns iter after it csmap_X_iter csmap_X_erase_range(csmap_X* self, csmap_X_iter it1, csmap_X_iter it2); // returns updated it2 diff --git a/docs/cspan_api.md b/docs/cspan_api.md index c4cb7c16..8f6f695f 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -20,32 +20,32 @@ using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with Note that `cspan_md()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. ```c -SpanTypeN cspan_md(ValueType* data, size_t xdim, ...); // create a multi-dimensional cspan +SpanTypeN cspan_md(ValueType* data, intptr_t xdim, ...); // create a multi-dimensional cspan SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1d-dimensional cspan from values SpanType cspan_from(STCContainer* cnt); // create a 1d cspan from a compatible STC container SpanType cspan_from_array(ValueType array[]); // create a 1d cspan from a C array -size_t cspan_size(const SpanTypeN* self); // return number of elements +intptr_t cspan_size(const SpanTypeN* self); // return number of elements unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions -size_t cspan_index(const SpanTypeN* self, size_t x, ...); // index of element +intptr_t cspan_index(const SpanTypeN* self, intptr_t x, ...); // index of element -ValueType* cspan_at(SpanTypeN* self, size_t x, ...); // at(): num of args specifies rank of input span. +ValueType* cspan_at(SpanTypeN* self, intptr_t x, ...); // at(): num of args specifies rank of input span. ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); // return a subspan of lower rank: -SpanType cspan_submd2(SpanType2* self, size_t x); // return a 1d subspan from a 2d span. -SpanTypeN cspan_submd3(SpanType3* self, size_t x, ...); // return a 1d or 2d subspan from a 3d span. -SpanTypeN cspan_submd4(SpanType4* self, size_t x, ...); // number of args determines rank of output span. +SpanType cspan_submd2(SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. +SpanTypeN cspan_submd3(SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. +SpanTypeN cspan_submd4(SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. // return a sliced span of same rank: void cspan_slice(SpanTypeN* self, {x0,x1}, {y0,y1},...); // slice multidim span into a md subspan. // return a subspan of same rank. Like e.g. cspan_slice(&ms3, {offset, offset+count}, {0}, {0}); -SpanType cspan_subspan(const SpanType* self, size_t offset, size_t count); -SpanType2 cspan_subspan2(const SpanType2 self, size_t offset, size_t count); -SpanType3 cspan_subspan3(const SpanType3 self, size_t offset, size_t count); -SpanType4 cspan_subspan4(const SpanType4 self, size_t offset, size_t count); +SpanType cspan_subspan(const SpanType* self, intptr_t offset, intptr_t count); +SpanType2 cspan_subspan2(const SpanType2 self, intptr_t offset, intptr_t count); +SpanType3 cspan_subspan3(const SpanType3 self, intptr_t offset, intptr_t count); +SpanType4 cspan_subspan4(const SpanType4 self, intptr_t offset, intptr_t count); SpanTypeN_iter SpanType_begin(const SpanTypeN* self); SpanTypeN_iter SpanType_end(const SpanTypeN* self); diff --git a/docs/csset_api.md b/docs/csset_api.md index 6276f486..7e068909 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -28,8 +28,8 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo ```c csset_X csset_X_init(void); -csset_X csset_X_with_capacity(size_t cap); -bool csset_X_reserve(csset_X* self, size_t cap); +csset_X csset_X_with_capacity(intptr_t cap); +bool csset_X_reserve(csset_X* self, intptr_t cap); void csset_X_shrink_to_fit(csset_X* self); csset_X csset_X_clone(csset_x set); @@ -37,7 +37,7 @@ void csset_X_clear(csset_X* self); void csset_X_copy(csset_X* self, const csset_X* other); void csset_X_drop(csset_X* self); // destructor -size_t csset_X_size(const csset_X* self); +intptr_t csset_X_size(const csset_X* self); bool csset_X_empty(const csset_X* self); const csset_X_value* csset_X_get(const csset_X* self, i_keyraw rkey); // const get @@ -51,7 +51,7 @@ csset_X_result csset_X_insert(csset_X* self, i_key key); csset_X_result csset_X_push(csset_X* self, i_key key); // alias for insert() csset_X_result csset_X_emplace(csset_X* self, i_keyraw rkey); -size_t csset_X_erase(csset_X* self, i_keyraw rkey); +intptr_t csset_X_erase(csset_X* self, i_keyraw rkey); csset_X_iter csset_X_erase_at(csset_X* self, csset_X_iter it); // return iter after it csset_X_iter csset_X_erase_range(csset_X* self, csset_X_iter it1, csset_X_iter it2); // return updated it2 diff --git a/docs/cstack_api.md b/docs/cstack_api.md index e8423385..b1371f4e 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -26,24 +26,24 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack ```c cstack_X cstack_X_init(void); -cstack_X cstack_X_with_capacity(size_t cap); -cstack_X cstack_X_with_size(size_t size, i_val fill); +cstack_X cstack_X_with_capacity(intptr_t cap); +cstack_X cstack_X_with_size(intptr_t size, i_val fill); cstack_X cstack_X_clone(cstack_X st); void cstack_X_clear(cstack_X* self); -bool cstack_X_reserve(cstack_X* self, size_t n); +bool cstack_X_reserve(cstack_X* self, intptr_t n); void cstack_X_shrink_to_fit(cstack_X* self); -i_val* cstack_X_append_uninit(cstack_X* self, size_t n); +i_val* cstack_X_append_uninit(cstack_X* self, intptr_t n); void cstack_X_copy(cstack_X* self, const cstack_X* other); void cstack_X_drop(cstack_X* self); // destructor -size_t cstack_X_size(const cstack_X* self); -size_t cstack_X_capacity(const cstack_X* self); +intptr_t cstack_X_size(const cstack_X* self); +intptr_t cstack_X_capacity(const cstack_X* self); bool cstack_X_empty(const cstack_X* self); i_val* cstack_X_top(const cstack_X* self); -const i_val* cstack_X_at(const cstack_X* self, size_t idx); -i_val* cstack_X_at_mut(cstack_X* self, size_t idx); +const i_val* cstack_X_at(const cstack_X* self, intptr_t idx); +i_val* cstack_X_at_mut(cstack_X* self, intptr_t idx); i_val* cstack_X_push(cstack_X* self, i_val value); i_val* cstack_X_emplace(cstack_X* self, i_valraw raw); diff --git a/docs/cstr_api.md b/docs/cstr_api.md index 48999654..6bf5c723 100644 --- a/docs/cstr_api.md +++ b/docs/cstr_api.md @@ -21,10 +21,10 @@ All cstr definitions and prototypes are available by including a single header f cstr cstr_init(void); // constructor; same as cstr_NULL. cstr cstr_lit(const char literal_only[]); // cstr from literal; no strlen() call. cstr cstr_from(const char* str); // constructor using strlen() -cstr cstr_from_n(const char* str, size_t n); // constructor with n first bytes of str +cstr cstr_from_n(const char* str, intptr_t n); // constructor with n first bytes of str cstr cstr_from_sv(csview sv); // construct cstr from csview -cstr cstr_with_capacity(size_t cap); -cstr cstr_with_size(size_t len, char fill); // repeat fill len times +cstr cstr_with_capacity(intptr_t cap); +cstr cstr_with_size(intptr_t len, char fill); // repeat fill len times cstr cstr_from_fmt(const char* fmt, ...); // printf() formatting cstr cstr_clone(cstr s); @@ -37,49 +37,49 @@ char* cstr_data(cstr* self); // cast to csview cstr_sv(const cstr* self); // cast to string view cstr_buf cstr_buffer(cstr* self); // cast to mutable buffer (with capacity) -size_t cstr_size(const cstr* self); -size_t cstr_capacity(const cstr* self); +intptr_t cstr_size(const cstr* self); +intptr_t cstr_capacity(const cstr* self); bool cstr_empty(const cstr* self); -char* cstr_reserve(cstr* self, size_t capacity); // return pointer to buffer -void cstr_resize(cstr* self, size_t len, char fill); +char* cstr_reserve(cstr* self, intptr_t capacity); // return pointer to buffer +void cstr_resize(cstr* self, intptr_t len, char fill); void cstr_shrink_to_fit(cstr* self); void cstr_clear(cstr* self); char* cstr_assign(cstr* self, const char* str); -char* cstr_assign_n(cstr* self, const char* str, size_t n); // assign n first bytes of str +char* cstr_assign_n(cstr* self, const char* str, intptr_t n); // assign n first bytes of str char* cstr_assign_sv(cstr* self, csview sv); char* cstr_copy(cstr* self, cstr s); // copy-assign a cstr int cstr_printf(cstr* self, const char* fmt, ...); // source and target must not overlap. char* cstr_append(cstr* self, const char* str); -char* cstr_append_n(cstr* self, const char* str, size_t n); // append n first bytes of str +char* cstr_append_n(cstr* self, const char* str, intptr_t n); // append n first bytes of str char* cstr_append_sv(cstr* self, csview str); char* cstr_append_s(cstr* self, cstr str); int cstr_append_fmt(cstr* self, const char* fmt, ...); // printf() formatting -char* cstr_append_uninit(cstr* self, size_t len); // return ptr to start of uninited data +char* cstr_append_uninit(cstr* self, intptr_t len); // return ptr to start of uninited data void cstr_push(cstr* self, const char* chr); // append one utf8 char void cstr_pop(cstr* self); // pop one utf8 char -void cstr_insert(cstr* self, size_t pos, const char* ins); -void cstr_insert_sv(cstr* self, size_t pos, csview ins); -void cstr_insert_s(cstr* self, size_t pos, cstr ins); +void cstr_insert(cstr* self, intptr_t pos, const char* ins); +void cstr_insert_sv(cstr* self, intptr_t pos, csview ins); +void cstr_insert_s(cstr* self, intptr_t pos, cstr ins); -void cstr_erase(cstr* self, size_t pos, size_t len); // erase len bytes from pos +void cstr_erase(cstr* self, intptr_t pos, intptr_t len); // erase len bytes from pos void cstr_replace(cstr* self, const char* search, const char* repl, unsigned count = MAX_INT); cstr cstr_replace_sv(csview in, csview search, csview repl, unsigned count); -void cstr_replace_at(cstr* self, size_t pos, size_t len, const char* repl); // replace at a position -void cstr_replace_at_sv(cstr* self, size_t pos, size_t len, const csview repl); -void cstr_replace_at_s(cstr* self, size_t pos, size_t len, cstr repl); +void cstr_replace_at(cstr* self, intptr_t pos, intptr_t len, const char* repl); // replace at a position +void cstr_replace_at_sv(cstr* self, intptr_t pos, intptr_t len, const csview repl); +void cstr_replace_at_s(cstr* self, intptr_t pos, intptr_t len, cstr repl); bool cstr_equals(const cstr* self, const char* str); bool cstr_equals_sv(const cstr* self, csview sv); bool cstr_equals_s(const cstr* self, cstr s); -size_t cstr_find(const cstr* self, const char* search); -size_t cstr_find_at(const cstr* self, size_t pos, const char* search); // search from pos +intptr_t cstr_find(const cstr* self, const char* search); +intptr_t cstr_find_at(const cstr* self, intptr_t pos, const char* search); // search from pos bool cstr_contains(const cstr* self, const char* search); bool cstr_starts_with(const cstr* self, const char* str); @@ -96,13 +96,13 @@ bool cstr_getdelim(cstr *self, int delim, FILE *stream); // does no #### UTF8 methods ```c -size_t cstr_u8_size(const cstr* self); // number of utf8 codepoints -size_t cstr_u8_size_n(const cstr self, size_t nbytes); // utf8 size within n bytes -size_t cstr_u8_to_pos(const cstr* self, size_t u8idx); // byte pos offset at utf8 codepoint index -const char* cstr_u8_at(const cstr* self, size_t u8idx); // char* position at utf8 codepoint index -csview cstr_u8_chr(const cstr* self, size_t u8idx); // get utf8 character as a csview -void cstr_u8_replace_at(cstr* self, size_t bytepos, size_t u8len, csview repl); // replace u8len utf8 chars -void cstr_u8_erase(cstr* self, size_t bytepos, size_t u8len); // erase u8len codepoints from pos +intptr_t cstr_u8_size(const cstr* self); // number of utf8 codepoints +intptr_t cstr_u8_size_n(const cstr self, intptr_t nbytes); // utf8 size within n bytes +intptr_t cstr_u8_to_pos(const cstr* self, intptr_t u8idx); // byte pos offset at utf8 codepoint index +const char* cstr_u8_at(const cstr* self, intptr_t u8idx); // char* position at utf8 codepoint index +csview cstr_u8_chr(const cstr* self, intptr_t u8idx); // get utf8 character as a csview +void cstr_u8_replace_at(cstr* self, intptr_t bytepos, intptr_t u8len, csview repl); // replace u8len utf8 chars +void cstr_u8_erase(cstr* self, intptr_t bytepos, intptr_t u8len); // erase u8len codepoints from pos // iterate utf8 codepoints cstr_iter cstr_begin(const cstr* self); @@ -128,7 +128,7 @@ bool cstr_istarts_with(const cstr* self, const char* str); bool cstr_iends_with(const cstr* self, const char* str); // " ``` -Note that all methods with arguments `(..., const char* str, size_t n)`, `n` must be within the range of `str` length. +Note that all methods with arguments `(..., const char* str, intptr_t n)`, `n` must be within the range of `str` length. #### Helper methods: ```c @@ -136,7 +136,7 @@ int cstr_cmp(const cstr* s1, const cstr* s2); bool cstr_eq(const cstr* s1, const cstr* s2); bool cstr_hash(const cstr* self); -char* cstrnstrn(const char* str, const char* search, size_t slen, size_t nlen); +char* cstrnstrn(const char* str, const char* search, intptr_t slen, intptr_t nlen); ``` ## Types @@ -145,8 +145,8 @@ char* cstrnstrn(const char* str, const char* search, size_t slen, size_t |:----------------|:-------------------------------------------|:---------------------| | `cstr` | `struct { ... }` | The string type | | `cstr_value` | `char` | String element type | -| `csview` | `struct { const char *str; size_t size; }` | String view type | -| `cstr_buf` | `struct { char *data; size_t size, cap; }` | String buffer type | +| `csview` | `struct { const char *str; intptr_t size; }` | String view type | +| `cstr_buf` | `struct { char *data; intptr_t size, cap; }` | String buffer type | ## Constants and macros @@ -162,7 +162,7 @@ char* cstrnstrn(const char* str, const char* search, size_t slen, size_t int main() { c_AUTO (cstr, s0, s1, full_path) { s0 = cstr_lit("Initialization without using strlen()."); - printf("%s\nLength: %" c_ZU "\n\n", cstr_str(&s0), cstr_size(&s0)); + printf("%s\nLength: %" c_ZI "\n\n", cstr_str(&s0), cstr_size(&s0)); s1 = cstr_lit("one-nine-three-seven-five."); printf("%s\n", cstr_str(&s1)); diff --git a/docs/csview_api.md b/docs/csview_api.md index 4085a708..8064e4fd 100644 --- a/docs/csview_api.md +++ b/docs/csview_api.md @@ -27,31 +27,31 @@ All csview definitions and prototypes are available by including a single header ```c csview c_SV(const char literal_only[]); // construct from literal, no strlen() -csview c_SV(const char* str, size_t n); // construct from str and length n +csview c_SV(const char* str, intptr_t n); // construct from str and length n csview csview_lit(const char literal_only[]); // alias for c_SV(lit) csview csview_from(const char* str); // construct from const char* -csview csview_from_n(const char* str, size_t n); // alias for c_SV(str, n) +csview csview_from_n(const char* str, intptr_t n); // alias for c_SV(str, n) -size_t csview_size(csview sv); +intptr_t csview_size(csview sv); bool csview_empty(csview sv); void csview_clear(csview* self); bool csview_equals(csview sv, csview sv2); -size_t csview_find(csview sv, const char* str); -size_t csview_find_sv(csview sv, csview find); +intptr_t csview_find(csview sv, const char* str); +intptr_t csview_find_sv(csview sv, csview find); bool csview_contains(csview sv, const char* str); bool csview_starts_with(csview sv, const char* str); bool csview_ends_with(csview sv, const char* str); -csview csview_substr_ex(csview sv, intptr_t pos, size_t n); // negative pos count from end +csview csview_substr_ex(csview sv, intptr_t pos, intptr_t n); // negative pos count from end csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2); // negative p1, p2 count from end -csview csview_token(csview sv, const char* sep, size_t* start); // *start > sv.size after last token +csview csview_token(csview sv, const char* sep, intptr_t* start); // *start > sv.size after last token ``` #### UTF8 methods ```c -size_t csview_u8_size(csview sv); -csview csview_u8_substr(csview sv, size_t bytepos, size_t u8len); +intptr_t csview_u8_size(csview sv); +csview csview_u8_substr(csview sv, intptr_t bytepos, intptr_t u8len); bool csview_valid_utf8(csview sv); // requires linking with src/utf8code.c csview_iter csview_begin(const csview* self); @@ -60,14 +60,14 @@ void csview_next(csview_iter* it); // utf8 csview_iter csview_advance(csview_iter it, intptr_t n); // from utf8.h -size_t utf8_size(const char *s); -size_t utf8_size_n(const char *s, size_t nbytes); // number of UTF8 codepoints within n bytes -const char* utf8_at(const char *s, size_t index); // from UTF8 index to char* position -size_t utf8_pos(const char* s, size_t index); // from UTF8 index to byte index position +intptr_t utf8_size(const char *s); +intptr_t utf8_size_n(const char *s, intptr_t nbytes); // number of UTF8 codepoints within n bytes +const char* utf8_at(const char *s, intptr_t index); // from UTF8 index to char* position +intptr_t utf8_pos(const char* s, intptr_t index); // from UTF8 index to byte index position unsigned utf8_chr_size(const char* s); // UTF8 character size: 1-4 // implemented in src/utf8code.c: bool utf8_valid(const char* s); -bool utf8_valid_n(const char* s, size_t nbytes); +bool utf8_valid_n(const char* s, intptr_t nbytes); uint32_t utf8_decode(utf8_decode_t *d, uint8_t byte); // decode next byte to utf8, return state. unsigned utf8_encode(char *out, uint32_t codepoint); // encode unicode cp into out buffer uint32_t utf8_peek(const char* s); // codepoint value of character at s @@ -76,11 +76,11 @@ uint32_t utf8_peek_off(const char* s, int offset); // code #### Extended cstr methods ```c -csview cstr_substr(const cstr* self, size_t pos, size_t n); -csview cstr_substr_ex(const cstr* s, intptr_t pos, size_t n); // negative pos count from end -csview cstr_u8_substr(const cstr* self, size_t bytepos, size_t u8len); +csview cstr_substr(const cstr* self, intptr_t pos, intptr_t n); +csview cstr_substr_ex(const cstr* s, intptr_t pos, intptr_t n); // negative pos count from end +csview cstr_u8_substr(const cstr* self, intptr_t bytepos, intptr_t u8len); -csview cstr_slice(const cstr* self, size_t p1, size_t p2); +csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2); csview cstr_slice_ex(const cstr* s, intptr_t p, intptr_t q); // negative p or q count from end ``` #### Iterate tokens with *c_FORTOKEN*, *c_FORTOKEN_SV* @@ -103,7 +103,7 @@ uint64_t csview_hash(const csview* x); | Type name | Type definition | Used to represent... | |:----------------|:-------------------------------------------|:-------------------------| -| `csview` | `struct { const char *str; size_t size; }` | The string view type | +| `csview` | `struct { const char *str; intptr_t size; }` | The string view type | | `csview_value` | `char` | The string element type | | `csview_iter` | `struct { csview_value *ref; }` | UTF8 iterator | @@ -125,7 +125,7 @@ int main () // (quoting Alfred N. Whitehead) csview sv1 = cstr_substr(&str1, 3, 5); // "think" - size_t pos = cstr_find(&str1, "live"); // position of "live" in str1 + intptr_t pos = cstr_find(&str1, "live"); // position of "live" in str1 csview sv2 = cstr_substr(&str1, pos, 4); // get "live" csview sv3 = cstr_slice(&str1, -8, -1); // get "details" printf("%.*s %.*s %.*s\n", diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 08c404af..d67e80fd 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -31,26 +31,26 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect ```c cvec_X cvec_X_init(void); -cvec_X cvec_X_with_size(size_t size, i_val null); -cvec_X cvec_X_with_capacity(size_t size); +cvec_X cvec_X_with_size(intptr_t size, i_val null); +cvec_X cvec_X_with_capacity(intptr_t size); cvec_X cvec_X_clone(cvec_X vec); void cvec_X_clear(cvec_X* self); void cvec_X_copy(cvec_X* self, const cvec_X* other); cvec_X_iter cvec_X_copy_range(cvec_X* self, i_val* pos, const i_val* p1, const i_val* p2); -bool cvec_X_reserve(cvec_X* self, size_t cap); -bool cvec_X_resize(cvec_X* self, size_t size, i_val null); -cvec_X_iter cvec_X_insert_uninit(cvec_X* self, i_val* pos, size_t n); // return pos iter +bool cvec_X_reserve(cvec_X* self, intptr_t cap); +bool cvec_X_resize(cvec_X* self, intptr_t size, i_val null); +cvec_X_iter cvec_X_insert_uninit(cvec_X* self, i_val* pos, intptr_t n); // return pos iter void cvec_X_shrink_to_fit(cvec_X* self); void cvec_X_drop(cvec_X* self); // destructor bool cvec_X_empty(const cvec_X* self); -size_t cvec_X_size(const cvec_X* self); -size_t cvec_X_capacity(const cvec_X* self); +intptr_t cvec_X_size(const cvec_X* self); +intptr_t cvec_X_capacity(const cvec_X* self); -const cvec_X_value* cvec_X_at(const cvec_X* self, size_t idx); +const cvec_X_value* cvec_X_at(const cvec_X* self, intptr_t idx); const cvec_X_value* cvec_X_get(const cvec_X* self, i_valraw raw); // return NULL if not found -cvec_X_value* cvec_X_at_mut(cvec_X* self, size_t idx); +cvec_X_value* cvec_X_at_mut(cvec_X* self, intptr_t idx); cvec_X_value* cvec_X_get_mut(cvec_X* self, i_valraw raw); // find mutable value, return value ptr cvec_X_iter cvec_X_find(const cvec_X* self, i_valraw raw); cvec_X_iter cvec_X_find_in(cvec_X_iter i1, cvec_X_iter i2, i_valraw raw); // return cvec_X_end() if not found @@ -71,18 +71,18 @@ cvec_X_value* cvec_X_emplace_back(cvec_X* self, i_valraw raw); void cvec_X_pop(cvec_X* self); void cvec_X_pop_back(cvec_X* self); // alias for pop -cvec_X_iter cvec_X_insert(cvec_X* self, size_t idx, i_val value); // move value -cvec_X_iter cvec_X_insert_n(cvec_X* self, size_t idx, const i_val[] arr, size_t n); // move n values +cvec_X_iter cvec_X_insert(cvec_X* self, intptr_t idx, i_val value); // move value +cvec_X_iter cvec_X_insert_n(cvec_X* self, intptr_t idx, const i_val[] arr, intptr_t n); // move n values cvec_X_iter cvec_X_insert_at(cvec_X* self, cvec_X_iter it, i_val value); // move value cvec_X_iter cvec_X_insert_range(cvec_X* self, i_val* pos, const i_val* p1, const i_val* p2); -cvec_X_iter cvec_X_emplace_n(cvec_X* self, size_t idx, const i_valraw[] arr, size_t n); // clone values +cvec_X_iter cvec_X_emplace_n(cvec_X* self, intptr_t idx, const i_valraw[] arr, intptr_t n); // clone values cvec_X_iter cvec_X_emplace_at(cvec_X* self, cvec_X_iter it, i_valraw raw); cvec_X_iter cvec_X_emplace_range(cvec_X* self, i_val* pos, const i_valraw* p1, const i_valraw* p2); -cvec_X_iter cvec_X_erase_n(cvec_X* self, size_t idx, size_t n); +cvec_X_iter cvec_X_erase_n(cvec_X* self, intptr_t idx, intptr_t n); cvec_X_iter cvec_X_erase_at(cvec_X* self, cvec_X_iter it); cvec_X_iter cvec_X_erase_range(cvec_X* self, cvec_X_iter it1, cvec_X_iter it2); cvec_X_iter cvec_X_erase_range_p(cvec_X* self, i_val* p1, i_val* p2); @@ -94,7 +94,7 @@ void cvec_X_sort_range(cvec_X_iter i1, cvec_X_iter i2, cvec_X_iter cvec_X_begin(const cvec_X* self); cvec_X_iter cvec_X_end(const cvec_X* self); void cvec_X_next(cvec_X_iter* iter); -cvec_X_iter cvec_X_advance(cvec_X_iter it, intptr_t n); +cvec_X_iter cvec_X_advance(cvec_X_iter it, size_t n); cvec_X_raw cvec_X_value_toraw(cvec_X_value* pval); cvec_X_value cvec_X_value_clone(cvec_X_value val); diff --git a/include/stc/algo/csort.h b/include/stc/algo/csort.h index 9c9bcd5b..c8c41257 100644 --- a/include/stc/algo/csort.h +++ b/include/stc/algo/csort.h @@ -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 diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index f0d157e3..30184407 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -32,13 +32,10 @@ #include "priv/altnames.h" #define c_NPOS INTPTR_MAX -#if SIZE_MAX == UINT32_MAX - #define c_ZU PRIu32 -#elif SIZE_MAX == UINT64_MAX - #define c_ZU PRIu64 -#endif +#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 diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 44c5e8d5..56c0e867 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -110,7 +110,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) 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 diff --git a/include/stc/cmap.h b/include/stc/cmap.h index f9cd6986..f90d9a4a 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -53,7 +53,7 @@ int main(void) { #include "forward.h" #include #include -typedef struct { size_t idx; uint8_t hx; } chash_bucket_t; +typedef struct { intptr_t idx; uint8_t hx; } chash_bucket_t; #endif // CMAP_H_INCLUDED #ifndef _i_prefix @@ -96,13 +96,13 @@ 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)(intptr_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, intptr_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); @@ -111,10 +111,10 @@ 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 intptr_t _cx_memb(_size)(const _cx_self* map) { return map->size; } +STC_INLINE intptr_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } +STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* map) + { return (intptr_t)((float)map->bucket_count * (i_max_load_factor)); } STC_INLINE bool _cx_memb(_contains)(const _cx_self* self, _cx_rawkey rkey) { return self->size && self->_hashx[_cx_memb(_bucket_)(self, &rkey).idx]; } @@ -195,7 +195,7 @@ _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, size_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -208,7 +208,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) { #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) { @@ -238,7 +238,7 @@ _cx_memb(_advance)(_cx_iter it, size_t n) { STC_INLINE _cx_iter _cx_memb(_find)(const _cx_self* self, _cx_rawkey rkey) { - size_t idx; + intptr_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return c_LITERAL(_cx_iter){self->table + idx, self->table + self->bucket_count, @@ -248,7 +248,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; + intptr_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return self->table + idx; return NULL; @@ -258,12 +258,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 intptr_t _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 @@ -278,11 +278,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 intptr_t fastrange_1(uint64_t x, uint64_t n) + { return (intptr_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 intptr_t fastrange_2(uint64_t x, uint64_t n) + { return (intptr_t)(x & (n - 1)); } // n power of 2. STC_INLINE uint64_t next_power_of_2(uint64_t n) { n--; @@ -294,7 +294,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 intptr_t cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -319,7 +319,7 @@ STC_DEF void _cx_memb(_drop)(_cx_self* self) { 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 @@ -355,7 +355,7 @@ 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)}; + 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) { @@ -390,7 +390,7 @@ 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; } else @@ -404,7 +404,7 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { +_cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { const i_size _oldbuckets = self->bucket_count; if (_newcap != self->size && _newcap <= _oldbuckets) return true; @@ -424,7 +424,7 @@ _cx_memb(_reserve)(_cx_self* self, const size_t _newcap) { 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; @@ -450,7 +450,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 21130864..55df70ef 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -55,7 +55,7 @@ STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) 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; } diff --git a/include/stc/crandom.h b/include/stc/crandom.h index 5641d788..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. diff --git a/include/stc/cregex.h b/include/stc/cregex.h index c41fbb67..564fe157 100644 --- a/include/stc/cregex.h +++ b/include/stc/cregex.h @@ -105,7 +105,7 @@ STC_INLINE cregex cregex_from_2(const char* pattern, int cflags) { } /* number of capture groups in a regex pattern including full the match capture, 0 if regex is invalid */ -unsigned cregex_captures(const cregex* re); +int cregex_captures(const cregex* re); /* return CREG_OK, CREG_NOMATCH or CREG_MATCHERROR. */ #define cregex_find(...) c_MACRO_OVERLOAD(cregex_find, __VA_ARGS__) @@ -132,17 +132,17 @@ STC_INLINE bool cregex_is_match(const cregex* re, const char* input) /* 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, ~0U) + 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, unsigned count, +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); /* 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, ~0U) +#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, unsigned count) { +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); } @@ -150,10 +150,10 @@ STC_INLINE cstr cregex_replace_4(const cregex* re, const char* input, const char /* replace + compile RE pattern, and extra arguments */ #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, ~0U) + 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, unsigned count, +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 */ diff --git a/include/stc/csmap.h b/include/stc/csmap.h index b92d25d8..de948618 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -107,7 +107,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, intptr_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 +118,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 intptr_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } +STC_INLINE intptr_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 +130,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 intptr_t cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -225,10 +225,10 @@ _cx_memb(_init)(void) { } 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) 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_LITERAL(_cx_node){{0, 0}, 0}; @@ -294,7 +294,7 @@ _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, size_t n) { +STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) { while (n--) #if defined _i_isset && defined i_no_emplace _cx_memb(_insert)(self, *raw++); @@ -307,7 +307,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, size_t n) { #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, size_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #ifndef _i_isset diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 97d5256c..dd4686e9 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -185,7 +185,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) 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 f965487c..2966db81 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -374,7 +374,7 @@ STC_INLINE char* cstr_append_s(cstr* self, cstr s) { } #define cstr_replace(...) c_MACRO_OVERLOAD(cstr_replace, __VA_ARGS__) -#define cstr_replace_3(self, search, repl) cstr_replace_4(self, search, repl, ~0U) +#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)); diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 11d145e6..06664b4d 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -208,7 +208,7 @@ STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self) 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 intptr_t _cx_memb(_index)(const _cx_self* self, _cx_iter it) diff --git a/include/stc/forward.h b/include/stc/forward.h index dfb43a9f..594c76fa 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -26,17 +26,15 @@ #include #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_cmap_huge(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, intptr_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_cset_huge(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, intptr_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) diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 9634fe2c..4aa54bcc 100644 --- a/include/stc/priv/template.h +++ b/include/stc/priv/template.h @@ -47,7 +47,7 @@ #ifdef i_size #define _i_expandby 2 #else - #define i_size uint32_t + #define i_size int32_t #define _i_expandby 1 #endif diff --git a/include/stc/utf8.h b/include/stc/utf8.h index ce50af87..da738fc6 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -25,7 +25,7 @@ extern bool utf8_iscased(uint32_t c); extern bool utf8_isword(uint32_t c); 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) @@ -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); } diff --git a/misc/archived/cstr.h b/misc/archived/cstr.h index 9111fe6d..17baf52c 100644 --- a/misc/archived/cstr.h +++ b/misc/archived/cstr.h @@ -56,7 +56,7 @@ STC_API void cstr_resize(cstr* self, size_t len, char fill); STC_API cstr* cstr_assign_n(cstr* self, const char* str, size_t n); STC_API int cstr_printf(cstr* self, const char* fmt, ...); STC_API cstr* cstr_append_n(cstr* self, const char* str, size_t n); -STC_API cstr cstr_replace_sv(csview str, csview find, csview repl, unsigned count); +STC_API cstr cstr_replace_sv(csview str, csview find, csview repl, int count); STC_DEF void cstr_replace_at_sv(cstr* self, const size_t pos, size_t len, csview repl); STC_API void cstr_erase(cstr* self, size_t pos, size_t n); STC_API size_t cstr_find(const cstr* self, const char* needle); @@ -182,7 +182,7 @@ STC_INLINE uint64_t cstr_hash(const cstr *self) { return cfasthash(self->str, _cstr_p(self)->size); } -STC_INLINE void cstr_replace_ex(cstr* self, const char* find, const char* repl, unsigned count) { +STC_INLINE void cstr_replace_ex(cstr* self, const char* find, const char* repl, int count) { cstr_take(self, cstr_replace_sv(cstr_sv(self), c_SV(find, strlen(find)), c_SV(repl, strlen(repl)), count)); } diff --git a/misc/examples/astar.c b/misc/examples/astar.c index 828de8ce..ddc9de3a 100644 --- a/misc/examples/astar.c +++ b/misc/examples/astar.c @@ -37,7 +37,7 @@ point_equal(const point* a, const point* b) point point_from(const cstr* maze, const char* c, int width) { - int index = cstr_find(maze, c); + int index = (int)cstr_find(maze, c); return point_init(index % width, index / width, width); } @@ -156,7 +156,7 @@ main(void) "# # # # # # #\n" "#########################################################################\n"), cstr_drop(&maze)) { - int width = cstr_find(&maze, "\n") + 1; + int width = (int)cstr_find(&maze, "\n") + 1; c_WITH (cdeq_point path = astar(&maze, width), cdeq_point_drop(&path)) { c_FOREACH (it, cdeq_point, path) diff --git a/misc/examples/birthday.c b/misc/examples/birthday.c index be7f0457..fb887cd7 100644 --- a/misc/examples/birthday.c +++ b/misc/examples/birthday.c @@ -50,19 +50,19 @@ void test_distribution(void) uint64_t sum = 0; c_FOREACH (i, cmap_x, map) sum += i.ref->second; - sum /= map.size; + sum /= (uint64_t)map.size; c_FOREACH (i, cmap_x, map) { printf("%4" PRIu32 ": %" PRIu64 " - %" PRIu64 ": %11.8f\n", i.ref->first, i.ref->second, sum, - (1 - (double)i.ref->second / sum)); + (1.0 - (double)i.ref->second / (double)sum)); } } } int main() { - seed = time(NULL); + seed = (uint64_t)time(NULL); test_distribution(); test_repeats(); } diff --git a/misc/examples/bits.c b/misc/examples/bits.c index c50eac6e..03b74881 100644 --- a/misc/examples/bits.c +++ b/misc/examples/bits.c @@ -4,18 +4,18 @@ int main() { c_WITH (cbits set = cbits_with_size(23, true), cbits_drop(&set)) { - printf("count %" c_ZU ", %" c_ZU "\n", cbits_count(&set), cbits_size(&set)); + printf("count %" c_ZI ", %" c_ZI "\n", cbits_count(&set), cbits_size(&set)); cbits s1 = cbits_from("1110100110111"); char buf[256]; cbits_to_str(&s1, buf, 0, 255); - printf("buf: %s: %" c_ZU "\n", buf, cbits_count(&s1)); + printf("buf: %s: %" c_ZI "\n", buf, cbits_count(&s1)); cbits_drop(&s1); cbits_reset(&set, 9); cbits_resize(&set, 43, false); printf(" str: %s\n", cbits_to_str(&set, buf, 0, 255)); - printf("%4" c_ZU ": ", cbits_size(&set)); + printf("%4" c_ZI ": ", cbits_size(&set)); c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); @@ -25,12 +25,12 @@ int main() cbits_resize(&set, 93, false); cbits_resize(&set, 102, true); cbits_set_value(&set, 99, false); - printf("%4" c_ZU ": ", cbits_size(&set)); + printf("%4" c_ZI ": ", cbits_size(&set)); c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts("\nIterate:"); - printf("%4" c_ZU ": ", cbits_size(&set)); + printf("%4" c_ZI ": ", cbits_size(&set)); c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); @@ -52,7 +52,7 @@ int main() puts(""); cbits_set_all(&set, false); - printf("%4" c_ZU ": ", cbits_size(&set)); + printf("%4" c_ZI ": ", cbits_size(&set)); c_FORRANGE (i, cbits_size(&set)) printf("%d", cbits_test(&set, i)); puts(""); diff --git a/misc/examples/bits2.c b/misc/examples/bits2.c index 502e7268..9b1d1af7 100644 --- a/misc/examples/bits2.c +++ b/misc/examples/bits2.c @@ -9,10 +9,10 @@ int main() { Bits s1 = Bits_from("1110100110111"); - printf("size %" c_ZU "\n", Bits_size(&s1)); + printf("size %" c_ZI "\n", Bits_size(&s1)); char buf[256]; Bits_to_str(&s1, buf, 0, 256); - printf("buf: %s: count=%" c_ZU "\n", buf, Bits_count(&s1)); + printf("buf: %s: count=%" c_ZI "\n", buf, Bits_count(&s1)); Bits_reset(&s1, 8); printf(" s1: %s\n", Bits_to_str(&s1, buf, 0, 256)); diff --git a/misc/examples/books.c b/misc/examples/books.c index 96c7ff6e..4695941a 100644 --- a/misc/examples/books.c +++ b/misc/examples/books.c @@ -32,7 +32,7 @@ int main() // When collections store owned values (String), they can still be // queried using references (&str). if (cmap_str_contains(&book_reviews, "Les Misérables")) { - printf("We've got %" c_ZU " reviews, but Les Misérables ain't one.", + printf("We've got %" c_ZI " reviews, but Les Misérables ain't one.", cmap_str_size(&book_reviews)); } diff --git a/misc/examples/complex.c b/misc/examples/complex.c index 6fed1d4d..5c212554 100644 --- a/misc/examples/complex.c +++ b/misc/examples/complex.c @@ -37,7 +37,7 @@ int main() // Put in some data in the structures stack.data[3] = 3.1415927f; - printf("stack size: %" c_ZU "\n", FloatStack_size(&stack)); + printf("stack size: %" c_ZI "\n", FloatStack_size(&stack)); StackList list = StackList_init(); StackList_push_back(&list, stack); diff --git a/misc/examples/csmap_erase.c b/misc/examples/csmap_erase.c index bef00a17..a41a1ad1 100644 --- a/misc/examples/csmap_erase.c +++ b/misc/examples/csmap_erase.c @@ -12,7 +12,7 @@ void printmap(mymap m) { c_FOREACH (elem, mymap, m) printf(" [%d, %s]", elem.ref->first, cstr_str(&elem.ref->second)); - printf("\nsize() == %" c_ZU "\n\n", mymap_size(&m)); + printf("\nsize() == %" c_ZI "\n\n", mymap_size(&m)); } int main() @@ -75,9 +75,9 @@ int main() puts("Starting data of map m3 is:"); printmap(m3); // The 3rd member function removes elements with a given Key - size_t count = mymap_erase(&m3, 2); + int count = mymap_erase(&m3, 2); // The 3rd member function also returns the number of elements removed - printf("The number of elements removed from m3 is: %" c_ZU "\n", count); + printf("The number of elements removed from m3 is: %d\n", count); puts("After the element with a key of 2 is deleted, the map m3 is:"); printmap(m3); } diff --git a/misc/examples/csmap_find.c b/misc/examples/csmap_find.c index a61a47be..201c94e9 100644 --- a/misc/examples/csmap_find.c +++ b/misc/examples/csmap_find.c @@ -18,7 +18,7 @@ void print_elem(csmap_istr_raw p) { #define using_print_collection(CX) \ void print_collection_##CX(const CX* t) { \ - printf("%" c_ZU " elements: ", CX##_size(t)); \ + printf("%" c_ZI " elements: ", CX##_size(t)); \ \ c_FOREACH (p, CX, *t) { \ print_elem(CX##_value_toraw(p.ref)); \ diff --git a/misc/examples/cstr_match.c b/misc/examples/cstr_match.c index 116e5dd4..bfb0df6b 100644 --- a/misc/examples/cstr_match.c +++ b/misc/examples/cstr_match.c @@ -5,8 +5,8 @@ int main() { c_WITH (cstr ss = cstr_lit("The quick brown fox jumps over the lazy dog.JPG"), cstr_drop(&ss)) { - size_t pos = cstr_find_at(&ss, 0, "brown"); - printf("%" c_ZU " [%s]\n", pos, pos == c_NPOS ? "" : cstr_str(&ss) + pos); + intptr_t pos = cstr_find_at(&ss, 0, "brown"); + printf("%" c_ZI " [%s]\n", pos, pos == c_NPOS ? "" : cstr_str(&ss) + pos); printf("equals: %d\n", cstr_equals(&ss, "The quick brown fox jumps over the lazy dog.JPG")); printf("contains: %d\n", cstr_contains(&ss, "umps ove")); printf("starts_with: %d\n", cstr_starts_with(&ss, "The quick brown")); @@ -16,7 +16,7 @@ int main() cstr s1 = cstr_lit("hell😀 w😀rl🐨"); csview ch1 = cstr_u8_chr(&s1, 7); csview ch2 = cstr_u8_chr(&s1, 10); - printf("%s\nsize: %" c_ZU ", %" c_ZU "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1)); + printf("%s\nsize: %" c_ZI ", %" c_ZI "\n", cstr_str(&s1), cstr_u8_size(&s1), cstr_size(&s1)); printf("ch1: %.*s\n", c_ARGSV(ch1)); printf("ch2: %.*s\n", c_ARGSV(ch2)); } diff --git a/misc/examples/demos.c b/misc/examples/demos.c index 898c24cf..7070d860 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -175,11 +175,11 @@ void mapdemo3() cmap_str_iter it = cmap_str_find(&table, "Make"); c_FOREACH (i, cmap_str, table) printf("entry: %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); - printf("size %" c_ZU ": remove: Make: %s\n", cmap_str_size(&table), cstr_str(&it.ref->second)); + printf("size %" c_ZI ": remove: Make: %s\n", cmap_str_size(&table), cstr_str(&it.ref->second)); //cmap_str_erase(&table, "Make"); cmap_str_erase_at(&table, it); - printf("size %" c_ZU "\n", cmap_str_size(&table)); + printf("size %" c_ZI "\n", cmap_str_size(&table)); c_FOREACH (i, cmap_str, table) printf("entry: %s: %s\n", cstr_str(&i.ref->first), cstr_str(&i.ref->second)); cmap_str_drop(&table); // frees key and value cstrs, and hash table. diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c index fdc013a3..1c64c9e0 100644 --- a/misc/examples/forfilter.c +++ b/misc/examples/forfilter.c @@ -69,7 +69,7 @@ void demo2(void) , c_flt_skipwhile(x, *x.ref != 11) && *x.ref % 2 != 0 , c_flt_take(x, 5)) - IVec_push(&vector, *x.ref * *x.ref); + IVec_push(&vector, (int)(*x.ref * *x.ref)); c_FOREACH (x, IVec, vector) printf(" %d", *x.ref); puts(""); diff --git a/misc/examples/gauss1.c b/misc/examples/gauss1.c index db103945..6b06b4e8 100644 --- a/misc/examples/gauss1.c +++ b/misc/examples/gauss1.c @@ -24,7 +24,7 @@ int main() printf("Demo of gaussian / normal distribution of %d random samples\n", N); // Setup random engine with normal distribution. - uint64_t seed = time(NULL); + uint64_t seed = (uint64_t)time(NULL); stc64_t rng = stc64_new(seed); stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); diff --git a/misc/examples/gauss2.c b/misc/examples/gauss2.c index c2ed2e00..c627fc91 100644 --- a/misc/examples/gauss2.c +++ b/misc/examples/gauss2.c @@ -18,7 +18,7 @@ int main() printf("Demo of gaussian / normal distribution of %d random samples\n", N); // Setup random engine with normal distribution. - uint64_t seed = time(NULL); + uint64_t seed = (uint64_t)time(NULL); stc64_t rng = stc64_new(seed); stc64_normalf_t dist = stc64_normalf_new(Mean, StdDev); @@ -33,7 +33,7 @@ int main() // Print the gaussian bar chart c_AUTO (cstr, bar) c_FORPAIR (index, count, csmap_int, mhist) { - size_t n = (size_t) (*_.count * StdDev * Scale * 2.5 / (float)N); + int n = (int)((float)*_.count * StdDev * Scale * 2.5f / (float)N); if (n > 0) { cstr_resize(&bar, n, '*'); printf("%4d %s\n", *_.index, cstr_str(&bar)); diff --git a/misc/examples/make.sh b/misc/examples/make.sh index bd1392fc..0c4760a6 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -7,7 +7,7 @@ if [ "$(uname)" = 'Linux' ]; then fi #cc=gcc; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" -cc=gcc; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wsign-compare -Wwrite-strings" # -Wconversion +cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings" # -Wconversion #cc=tcc; cflags="-Wall -std=c99" #cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wsign-compare -Wwrite-strings" #cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -DSTC_CSTR_V1 -DSTC_CSMAP_V1" diff --git a/misc/examples/new_queue.c b/misc/examples/new_queue.c index 828387b5..5c25a229 100644 --- a/misc/examples/new_queue.c +++ b/misc/examples/new_queue.c @@ -22,25 +22,25 @@ int point_cmp(const Point* a, const Point* b) { int main() { int n = 50000000; - stc64_t rng = stc64_new(time(NULL)); + stc64_t rng = stc64_new((uint64_t)time(NULL)); stc64_uniform_t dist = stc64_uniform_new(0, n); c_AUTO (IQ, Q) { // Push 50'000'000 random numbers onto the queue. c_FORRANGE (n) - IQ_push(&Q, stc64_uniform(&rng, &dist)); + IQ_push(&Q, (int)stc64_uniform(&rng, &dist)); // Push or pop on the queue 50 million times - printf("befor: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q)); + printf("befor: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q)); c_FORRANGE (n) { - int r = stc64_uniform(&rng, &dist); + int r = (int)stc64_uniform(&rng, &dist); if (r & 3) IQ_push(&Q, r); else IQ_pop(&Q); } - printf("after: size %" c_ZU ", capacity %" c_ZU "\n", IQ_size(&Q), IQ_capacity(&Q)); + printf("after: size %" c_ZI ", capacity %" c_ZI "\n", IQ_size(&Q), IQ_capacity(&Q)); } } diff --git a/misc/examples/prime.c b/misc/examples/prime.c index 5c8d65d3..11c1f1c7 100644 --- a/misc/examples/prime.c +++ b/misc/examples/prime.c @@ -6,19 +6,19 @@ #include -cbits sieveOfEratosthenes(size_t n) +cbits sieveOfEratosthenes(intptr_t n) { cbits bits = cbits_with_size(n/2 + 1, true); - size_t q = (size_t) sqrt((double) n) + 1; - for (size_t i = 3; i < q; i += 2) { - size_t j = i; + intptr_t q = (intptr_t)sqrt((double) n) + 1; + for (intptr_t i = 3; i < q; i += 2) { + intptr_t j = i; for (; j < n; j += 2) { if (cbits_test(&bits, j>>1)) { i = j; break; } } - for (size_t j = i*i; j < n; j += i*2) + for (intptr_t j = i*i; j < n; j += i*2) cbits_reset(&bits, j>>1); } return bits; @@ -26,16 +26,16 @@ cbits sieveOfEratosthenes(size_t n) int main(void) { - size_t n = 1000000000; - printf("computing prime numbers up to %" c_ZU "\n", n); + intptr_t n = 1000000000; + printf("computing prime numbers up to %" c_ZI "\n", n); clock_t t1 = clock(); c_WITH (cbits primes = sieveOfEratosthenes(n + 1), cbits_drop(&primes)) { puts("done"); - size_t np = cbits_count(&primes); + intptr_t np = cbits_count(&primes); clock_t t2 = clock(); - printf("number of primes: %" c_ZU ", time: %f\n", np, (t2 - t1) / (float)CLOCKS_PER_SEC); + printf("number of primes: %" c_ZI ", time: %f\n", np, (float)(t2 - t1) / (float)CLOCKS_PER_SEC); puts("Show all the primes in the range [2, 1000):"); printf("2"); c_FORRANGE (i, 3, 1000, 2) diff --git a/misc/examples/priority.c b/misc/examples/priority.c index 22da6f60..f39c0634 100644 --- a/misc/examples/priority.c +++ b/misc/examples/priority.c @@ -9,13 +9,13 @@ #include int main() { - size_t N = 10000000; - stc64_t rng = stc64_new(time(NULL)); + intptr_t N = 10000000; + stc64_t rng = stc64_new((uint64_t)time(NULL)); stc64_uniform_t dist = stc64_uniform_new(0, N * 10); c_AUTO (cpque_i, heap) { // Push ten million random numbers to priority queue - printf("Push %" c_ZU " numbers\n", N); + printf("Push %" c_ZI " numbers\n", N); c_FORRANGE (N) cpque_i_push(&heap, stc64_uniform(&rng, &dist)); diff --git a/misc/examples/queue.c b/misc/examples/queue.c index 1d325fc6..ee537b58 100644 --- a/misc/examples/queue.c +++ b/misc/examples/queue.c @@ -15,17 +15,17 @@ int main() { { // Push ten million random numbers onto the queue. c_FORRANGE (n) - cqueue_i_push(&queue, stc64_uniform(&rng, &dist)); + cqueue_i_push(&queue, (int)stc64_uniform(&rng, &dist)); // Push or pop on the queue ten million times printf("%d\n", n); c_FORRANGE (n) { // forrange uses initial n only. - int r = stc64_uniform(&rng, &dist); + int r = (int)stc64_uniform(&rng, &dist); if (r & 1) ++n, cqueue_i_push(&queue, r); else --n, cqueue_i_pop(&queue); } - printf("%d, %" c_ZU "\n", n, cqueue_i_size(&queue)); + printf("%d, %" c_ZI "\n", n, cqueue_i_size(&queue)); } } diff --git a/misc/examples/random.c b/misc/examples/random.c index 82214924..fc4576dd 100644 --- a/misc/examples/random.c +++ b/misc/examples/random.c @@ -5,10 +5,10 @@ int main() { const size_t N = 1000000000; - const uint64_t seed = time(NULL), range = 1000000; + const uint64_t seed = (uint64_t)time(NULL), range = 1000000; stc64_t rng = stc64_new(seed); - uint64_t sum; + int64_t sum; clock_t diff, before; printf("Compare speed of full and unbiased ranged random numbers...\n"); @@ -18,8 +18,8 @@ int main() sum += (uint32_t)stc64_rand(&rng); } diff = clock() - before; - printf("full range\t\t: %f secs, %" c_ZU ", avg: %f\n", - (float)diff / CLOCKS_PER_SEC, N, (double)sum / N); + printf("full range\t\t: %f secs, %" c_ZI ", avg: %f\n", + (float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N); stc64_uniform_t dist1 = stc64_uniform_new(0, range); rng = stc64_new(seed); @@ -29,17 +29,17 @@ int main() sum += stc64_uniform(&rng, &dist1); // unbiased } diff = clock() - before; - printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZU ", avg: %f\n", - range, (float)diff/CLOCKS_PER_SEC, N, (double)sum / N); + printf("unbiased 0-%" PRIu64 "\t: %f secs, %" c_ZI ", avg: %f\n", + range, (float)diff/CLOCKS_PER_SEC, N, (float)sum / (float)N); sum = 0; rng = stc64_new(seed); before = clock(); c_FORRANGE (N) { - sum += stc64_rand(&rng) % (range + 1); // biased + sum += (int64_t)(stc64_rand(&rng) % (range + 1)); // biased } diff = clock() - before; - printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZU ", avg: %f\n", - range, (float)diff / CLOCKS_PER_SEC, N, (double)sum / N); + printf("biased 0-%" PRIu64 " \t: %f secs, %" c_ZI ", avg: %f\n", + range, (float)diff / CLOCKS_PER_SEC, N, (float)sum / (float)N); } diff --git a/misc/examples/regex_match.c b/misc/examples/regex_match.c index 93b83651..b7d6ed3a 100644 --- a/misc/examples/regex_match.c +++ b/misc/examples/regex_match.c @@ -1,6 +1,7 @@ #define i_extern #include #include + #define i_val float #include @@ -22,7 +23,7 @@ int main() // extract and convert all numbers in str to floats c_FORMATCH (i, &re, str) - cstack_float_push(&vec, atof(i.match[0].str)); + cstack_float_push(&vec, (float)atof(i.match[0].str)); c_FOREACH (i, cstack_float, vec) printf(" %g\n", *i.ref); diff --git a/misc/examples/sort.c b/misc/examples/sort.c index de94cede..65077143 100644 --- a/misc/examples/sort.c +++ b/misc/examples/sort.c @@ -14,7 +14,7 @@ #define fmt_Elem "%lld" -int testsort(csort_elm_value *a, size_t size, const char *desc) { +int testsort(csort_elm_value *a, long size, const char *desc) { clock_t t = clock(); #ifdef __cplusplus printf("std::sort: "); @@ -26,13 +26,13 @@ int testsort(csort_elm_value *a, size_t size, const char *desc) { t = clock() - t; printf("%s: %d elements sorted in %.2f ms\n", - desc, (int)size, t*1000.0/CLOCKS_PER_SEC); + desc, (int)size, (float)t*1000.0f/CLOCKS_PER_SEC); return 0; } int main(int argc, char *argv[]) { - size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000; - csort_elm_value *a = (csort_elm_value*)malloc(sizeof(*a) * size); + long i, size = argc > 1 ? strtol(argv[1], NULL, 0) : 10000000; + csort_elm_value *a = (csort_elm_value*)c_malloc(c_sizeof(*a) * size); if (a == NULL) return -1; for (i = 0; i < size; i++) diff --git a/misc/examples/sso_map.c b/misc/examples/sso_map.c index f88e5f79..0841a316 100644 --- a/misc/examples/sso_map.c +++ b/misc/examples/sso_map.c @@ -10,7 +10,7 @@ int main() cmap_str_emplace(&m, "Test long ", "This is a longer string"); c_FORPAIR (k, v, cmap_str, m) - printf("%s: '%s' Len=%" c_ZU ", Is long: %s\n", + printf("%s: '%s' Len=%" c_ZI ", Is long: %s\n", cstr_str(_.k), cstr_str(_.v), cstr_size(_.v), cstr_is_long(_.v) ? "true" : "false"); } diff --git a/misc/examples/sso_substr.c b/misc/examples/sso_substr.c index be372a8d..7e858ec4 100644 --- a/misc/examples/sso_substr.c +++ b/misc/examples/sso_substr.c @@ -6,7 +6,7 @@ int main () { cstr str = cstr_lit("We think in generalities, but we live in details."); csview sv1 = cstr_substr_ex(&str, 3, 5); // "think" - size_t pos = cstr_find(&str, "live"); // position of "live" + intptr_t pos = cstr_find(&str, "live"); // position of "live" csview sv2 = cstr_substr_ex(&str, pos, 4); // "live" csview sv3 = cstr_slice_ex(&str, -8, -1); // "details" printf("%.*s, %.*s, %.*s\n", c_ARGSV(sv1), c_ARGSV(sv2), c_ARGSV(sv3)); diff --git a/misc/examples/utf8replace_c.c b/misc/examples/utf8replace_c.c index 2d8d1921..3f98a2a6 100644 --- a/misc/examples/utf8replace_c.c +++ b/misc/examples/utf8replace_c.c @@ -16,11 +16,8 @@ int main() { c_FOREACH (c, cstr, hello) printf("%.*s,", c_ARGSV(c.u8.chr)); - //csview sv = c_SV("If you find the time, you will find the winner"); - //str = cstr_replace_sv(sv, c_SV("find"), c_SV("match"), 0); - - str = cstr_lit("If you find the time, you will find the winner"); - cstr_replace(&str, "find", "match"); + str = cstr_lit("scooby, dooby doo"); + cstr_replace(&str, "oo", "00"); printf("\n%s\n", cstr_str(&str)); } } diff --git a/src/cregex.c b/src/cregex.c index 1876e860..c17e1967 100644 --- a/src/cregex.c +++ b/src/cregex.c @@ -78,7 +78,7 @@ typedef struct _Reprog { _Reinst *startinst; /* start pc */ _Reflags flags; - unsigned nsubids; + int nsubids; _Reclass cclass[_NCLASS]; /* .data */ _Reinst firstinst[]; /* .text : originally 5 elements? */ } _Reprog; @@ -246,13 +246,13 @@ utfruneicase(const char *s, _Rune c) * save a new match in mp */ static void -_renewmatch(_Resub *mp, unsigned ms, _Resublist *sp, unsigned nsubids) +_renewmatch(_Resub *mp, int ms, _Resublist *sp, int nsubids) { if (mp==NULL || ms==0) return; if (mp[0].str == NULL || sp->m[0].str < mp[0].str || (sp->m[0].str == mp[0].str && sp->m[0].size > mp[0].size)) { - for (unsigned i=0; im[i]; } } @@ -265,7 +265,7 @@ _renewmatch(_Resub *mp, unsigned ms, _Resublist *sp, unsigned nsubids) static _Relist* _renewthread(_Relist *lp, /* _relist to add to */ _Reinst *ip, /* instruction to add */ - unsigned ms, + int ms, _Resublist *sep) /* pointers to subexpressions */ { _Relist *p; @@ -297,7 +297,7 @@ _renewthread(_Relist *lp, /* _relist to add to */ static _Relist* _renewemptythread(_Relist *lp, /* _relist to add to */ _Reinst *ip, /* instruction to add */ - unsigned ms, + int ms, const char *sp) /* pointers to subexpressions */ { _Relist *p; @@ -895,7 +895,7 @@ _regcomp1(_Reprog *progp, _Parser *par, const char *s, int cflags) pp->startinst = par->andp->first; pp = _optimize(par, pp); - pp->nsubids = (unsigned)par->cursubid; + pp->nsubids = par->cursubid; out: if (par->error) { c_free(pp); @@ -910,20 +910,20 @@ _runematch(_Rune s, _Rune r) { int inv = 0, n; switch (s) { - case ASC_D: inv = 1; case ASC_d: return inv ^ (isdigit(r) != 0); - case ASC_S: inv = 1; case ASC_s: return inv ^ (isspace(r) != 0); - case ASC_W: inv = 1; case ASC_w: return inv ^ ((isalnum(r) != 0) | (r == '_')); - case ASC_AL: inv = 1; case ASC_al: return inv ^ (isalpha(r) != 0); - case ASC_AN: inv = 1; case ASC_an: return inv ^ (isalnum(r) != 0); + case ASC_D: inv = 1; case ASC_d: return inv ^ (isdigit((int)r) != 0); + case ASC_S: inv = 1; case ASC_s: return inv ^ (isspace((int)r) != 0); + case ASC_W: inv = 1; case ASC_w: return inv ^ ((isalnum((int)r) != 0) | (r == '_')); + case ASC_AL: inv = 1; case ASC_al: return inv ^ (isalpha((int)r) != 0); + case ASC_AN: inv = 1; case ASC_an: return inv ^ (isalnum((int)r) != 0); case ASC_AS: return (r >= 128); case ASC_as: return (r < 128); case ASC_BL: inv = 1; case ASC_bl: return inv ^ ((r == ' ') | (r == '\t')); - case ASC_CT: inv = 1; case ASC_ct: return inv ^ (iscntrl(r) != 0); - case ASC_GR: inv = 1; case ASC_gr: return inv ^ (isgraph(r) != 0); - case ASC_PR: inv = 1; case ASC_pr: return inv ^ (isprint(r) != 0); - case ASC_PU: inv = 1; case ASC_pu: return inv ^ (ispunct(r) != 0); - case ASC_LO: inv = 1; case ASC_lo: return inv ^ (islower(r) != 0); - case ASC_UP: inv = 1; case ASC_up: return inv ^ (isupper(r) != 0); - case ASC_XD: inv = 1; case ASC_xd: return inv ^ (isxdigit(r) != 0); + case ASC_CT: inv = 1; case ASC_ct: return inv ^ (iscntrl((int)r) != 0); + case ASC_GR: inv = 1; case ASC_gr: return inv ^ (isgraph((int)r) != 0); + case ASC_PR: inv = 1; case ASC_pr: return inv ^ (isprint((int)r) != 0); + case ASC_PU: inv = 1; case ASC_pu: return inv ^ (ispunct((int)r) != 0); + case ASC_LO: inv = 1; case ASC_lo: return inv ^ (islower((int)r) != 0); + case ASC_UP: inv = 1; case ASC_up: return inv ^ (isupper((int)r) != 0); + case ASC_XD: inv = 1; case ASC_xd: return inv ^ (isxdigit((int)r) != 0); case UTF_AN: inv = 1; case UTF_an: return inv ^ utf8_isalnum(r); case UTF_BL: inv = 1; case UTF_bl: return inv ^ utf8_isblank(r); case UTF_SP: inv = 1; case UTF_sp: return inv ^ utf8_isspace(r); @@ -966,7 +966,7 @@ static int _regexec1(const _Reprog *progp, /* program to run */ const char *bol, /* string to run machine on */ _Resub *mp, /* subexpression elements */ - unsigned ms, /* number of elements at mp */ + int ms, /* number of elements at mp */ _Reljunk *j, int mflags ) @@ -979,7 +979,7 @@ _regexec1(const _Reprog *progp, /* program to run */ const char *s, *p; _Rune r, *rp, *ep; int n, checkstart, match = 0; - unsigned i; + int i; bool icase = progp->flags.icase; checkstart = j->starttype; @@ -1123,7 +1123,7 @@ static int _regexec2(const _Reprog *progp, /* program to run */ const char *bol, /* string to run machine on */ _Resub *mp, /* subexpression elements */ - unsigned ms, /* number of elements at mp */ + int ms, /* number of elements at mp */ _Reljunk *j, int mflags ) @@ -1149,7 +1149,7 @@ _regexec2(const _Reprog *progp, /* program to run */ static int _regexec(const _Reprog *progp, /* program to run */ const char *bol, /* string to run machine on */ - unsigned ms, /* number of elements at mp */ + int ms, /* number of elements at mp */ _Resub mp[], /* subexpression elements */ int mflags) { @@ -1195,7 +1195,7 @@ _regexec(const _Reprog *progp, /* program to run */ static void -_build_subst(const char* replace, unsigned nmatch, const csview match[], +_build_subst(const char* replace, int nmatch, const csview match[], bool (*mfun)(int, csview, cstr*), cstr* subst) { cstr_buf buf = cstr_buffer(subst); intptr_t len = 0, cap = buf.cap; @@ -1216,7 +1216,7 @@ _build_subst(const char* replace, unsigned nmatch, const csview match[], csview m = mfun && mfun(g, match[g], &mstr) ? cstr_sv(&mstr) : match[g]; if (len + m.size > cap) dst = cstr_reserve(subst, cap = cap*3/2 + m.size); - for (unsigned i = 0; i < m.size; ++i) + for (int i = 0; i < (int)m.size; ++i) dst[len++] = m.str[i]; } ++replace; @@ -1244,9 +1244,9 @@ cregex_compile_3(cregex *self, const char* pattern, int cflags) { return self->error = par.error; } -unsigned +int cregex_captures(const cregex* self) { - return self->prog ? 1U + self->prog->nsubids : 0U; + return self->prog ? 1 + self->prog->nsubids : 0; } int @@ -1271,13 +1271,13 @@ cregex_find_pattern_4(const char* pattern, const char* input, } cstr -cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigned count, +cregex_replace_sv_6(const cregex* re, csview input, const char* replace, int count, bool (*mfun)(int, csview, cstr*), int rflags) { cstr out = cstr_NULL; cstr subst = cstr_NULL; csview match[CREG_MAX_CAPTURES]; - unsigned nmatch = cregex_captures(re); - if (!count) count = ~0U; + int nmatch = cregex_captures(re); + if (!count) count = INT32_MAX; bool copy = !(rflags & CREG_R_STRIP); while (count-- && cregex_find_sv(re, input, match) == CREG_OK) { @@ -1294,7 +1294,7 @@ cregex_replace_sv_6(const cregex* re, csview input, const char* replace, unsigne } cstr -cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, unsigned count, +cregex_replace_pattern_6(const char* pattern, const char* input, const char* replace, int count, bool (*mfun)(int, csview, cstr*), int crflags) { cregex re = cregex_init(); if (cregex_compile(&re, pattern, crflags) != CREG_OK) diff --git a/src/utf8code.c b/src/utf8code.c index 351ddab5..a892f5fd 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -20,7 +20,7 @@ const uint8_t utf8_dtab[] = { 12,36,12,12,12,12,12,12,12,12,12,12, }; -unsigned utf8_encode(char *out, uint32_t c) +int utf8_encode(char *out, uint32_t c) { if (c < 0x80U) { out[0] = (char) c; @@ -143,7 +143,7 @@ bool utf8_isalpha(uint32_t c) { static int16_t groups[] = {U8G_Latin, U8G_Nl, U8G_Greek, U8G_Cyrillic, U8G_Han, U8G_Devanagari, U8G_Arabic}; if (c < 128) return isalpha((int)c) != 0; - for (unsigned j=0; j < c_ARRAYLEN(groups); ++j) + for (int j=0; j < c_ARRAYLEN(groups); ++j) if (utf8_isgroup(groups[j], c)) return true; return false; -- cgit v1.2.3 From 473a13c1c15b8c1e1edc79cff074c0dcf490fc1b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 1 Feb 2023 22:44:21 +0100 Subject: Fixed to allow int64_t sized maps. (defaults to 32-bit). --- docs/cmap_api.md | 14 ++++++------ docs/csmap_api.md | 10 ++++----- include/stc/cmap.h | 52 +++++++++++++++++++++++++-------------------- include/stc/csmap.h | 21 ++++++++++-------- include/stc/forward.h | 18 ++++++++-------- include/stc/priv/template.h | 7 ++---- 6 files changed, 64 insertions(+), 58 deletions(-) (limited to 'include') diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 8e5a53d9..0b91abc7 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -48,20 +48,20 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain ```c cmap_X cmap_X_init(void); -cmap_X cmap_X_with_capacity(intptr_t cap); +cmap_X cmap_X_with_capacity(int64_t cap); cmap_X cmap_X_clone(cmap_x map); void cmap_X_clear(cmap_X* self); void cmap_X_copy(cmap_X* self, const cmap_X* other); float cmap_X_max_load_factor(const cmap_X* self); // default: 0.85f -bool cmap_X_reserve(cmap_X* self, intptr_t size); +bool cmap_X_reserve(cmap_X* self, int64_t size); void cmap_X_shrink_to_fit(cmap_X* self); void cmap_X_drop(cmap_X* self); // destructor -intptr_t cmap_X_size(const cmap_X* self); -intptr_t cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor bool cmap_X_empty(const cmap_X* self ); -intptr_t cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets +int64_t cmap_X_size(const cmap_X* self); +int64_t cmap_X_capacity(const cmap_X* self); // buckets * max_load_factor +int64_t cmap_X_bucket_count(const cmap_X* self); // num. of allocated buckets const cmap_X_mapped* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map cmap_X_mapped* cmap_X_at_mut(cmap_X* self, i_keyraw rkey); // mutable at @@ -77,14 +77,14 @@ cmap_X_result cmap_X_push(cmap_X* self, cmap_X_value entry); cmap_X_result cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map cmap_X_result cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped -intptr_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 +int cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 cmap_X_iter cmap_X_erase_at(cmap_X* self, cmap_X_iter it); // return iter after it void cmap_X_erase_entry(cmap_X* self, cmap_X_value* entry); cmap_X_iter cmap_X_begin(const cmap_X* self); cmap_X_iter cmap_X_end(const cmap_X* self); void cmap_X_next(cmap_X_iter* it); -cmap_X_iter cmap_X_advance(cmap_X_iter it, size_t n); +cmap_X_iter cmap_X_advance(cmap_X_iter it, uint64_t n); cmap_X_value cmap_X_value_clone(cmap_X_value val); cmap_X_raw cmap_X_value_toraw(cmap_X_value* pval); diff --git a/docs/csmap_api.md b/docs/csmap_api.md index d0a57bdb..b090f737 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -42,8 +42,8 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo ```c csmap_X csmap_X_init(void); -csset_X csmap_X_with_capacity(intptr_t cap); -bool csmap_X_reserve(csmap_X* self, intptr_t cap); +csset_X csmap_X_with_capacity(int64_t cap); +bool csmap_X_reserve(csmap_X* self, int64_t cap); void csmap_X_shrink_to_fit(csmap_X* self); csmap_X csmap_X_clone(csmap_x map); @@ -51,9 +51,9 @@ void csmap_X_clear(csmap_X* self); void csmap_X_copy(csmap_X* self, const csmap_X* other); void csmap_X_drop(csmap_X* self); // destructor -intptr_t csmap_X_size(const csmap_X* self); bool csmap_X_empty(const csmap_X* self); -bool csmap_X_capacity(const csmap_X* self); +int64_t csmap_X_size(const csmap_X* self); +int64_t csmap_X_capacity(const csmap_X* self); const csmap_X_mapped* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map csmap_X_mapped* csmap_X_at_mut(csmap_X* self, i_keyraw rkey); // mutable at @@ -81,7 +81,7 @@ csmap_X_iter csmap_X_erase_range(csmap_X* self, csmap_X_iter it1, csmap csmap_X_iter csmap_X_begin(const csmap_X* self); csmap_X_iter csmap_X_end(const csmap_X* self); void csmap_X_next(csmap_X_iter* iter); -csmap_X_iter csmap_X_advance(csmap_X_iter it, size_t n); +csmap_X_iter csmap_X_advance(csmap_X_iter it, uint64_t n); csmap_X_value csmap_X_value_clone(csmap_X_value val); csmap_X_raw csmap_X_value_toraw(csmap_X_value* pval); diff --git a/include/stc/cmap.h b/include/stc/cmap.h index f90d9a4a..393df53f 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -53,7 +53,7 @@ int main(void) { #include "forward.h" #include #include -typedef struct { intptr_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 { intptr_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,13 +102,13 @@ typedef _i_SET_ONLY( i_keyraw ) i_valraw second; } ) _cx_raw; -STC_API _cx_self _cx_memb(_with_capacity)(intptr_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, intptr_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); @@ -111,10 +117,10 @@ 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 intptr_t _cx_memb(_size)(const _cx_self* map) { return map->size; } -STC_INLINE intptr_t _cx_memb(_bucket_count)(_cx_self* map) { return map->bucket_count; } -STC_INLINE intptr_t _cx_memb(_capacity)(const _cx_self* map) - { return (intptr_t)((float)map->bucket_count * (i_max_load_factor)); } +STC_INLINE 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]; } @@ -195,7 +201,7 @@ _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, intptr_t n) { +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++); @@ -208,7 +214,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, 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) { @@ -231,14 +237,14 @@ _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) { - intptr_t idx; + int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return c_LITERAL(_cx_iter){self->table + idx, self->table + self->bucket_count, @@ -248,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) { - intptr_t idx; + int64_t idx; if (self->size && self->_hashx[idx = _cx_memb(_bucket_)(self, &rkey).idx]) return self->table + idx; return NULL; @@ -258,7 +264,7 @@ STC_INLINE _cx_value* _cx_memb(_get_mut)(_cx_self* self, _cx_rawkey rkey) { return (_cx_value*)_cx_memb(_get)(self, rkey); } -STC_INLINE intptr_t +STC_INLINE int _cx_memb(_erase)(_cx_self* self, _cx_rawkey rkey) { if (self->size == 0) return 0; @@ -278,11 +284,11 @@ _cx_memb(_erase_at)(_cx_self* self, _cx_iter it) { #if defined(i_implement) #ifndef CMAP_H_INCLUDED -STC_INLINE intptr_t fastrange_1(uint64_t x, uint64_t n) - { return (intptr_t)((uint32_t)x*n >> 32); } // n < 2^32 +STC_INLINE int64_t fastrange_1(uint64_t x, uint64_t n) + { return (int64_t)((uint32_t)x*n >> 32); } // n < 2^32 -STC_INLINE intptr_t fastrange_2(uint64_t x, uint64_t n) - { return (intptr_t)(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--; @@ -294,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 intptr_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self h = {0}; _cx_memb(_reserve)(&h, cap); return h; @@ -354,7 +360,7 @@ 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; + 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]) { @@ -404,8 +410,8 @@ _cx_memb(_clone)(_cx_self m) { #endif STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const intptr_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; @@ -417,7 +423,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { _cx_self m = { c_ALLOC_N(_cx_value, _nbuckets), (uint8_t *) c_calloc(_nbuckets + 1, 1), - self->size, (i_size)_nbuckets, + self->size, _nbuckets, }; bool ok = m.table && m._hashx; if (ok) { /* Rehash: */ @@ -428,7 +434,7 @@ _cx_memb(_reserve)(_cx_self* self, const intptr_t _newcap) { _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); } diff --git a/include/stc/csmap.h b/include/stc/csmap.h index de948618..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, intptr_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 intptr_t _cx_memb(_size)(const _cx_self* cx) { return cx->size; } -STC_INLINE intptr_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 intptr_t cap) { +_cx_memb(_with_capacity)(const int64_t cap) { _cx_self tree = _cx_memb(_init)(); _cx_memb(_reserve)(&tree, cap); return tree; @@ -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,8 +228,8 @@ _cx_memb(_init)(void) { } STC_DEF bool -_cx_memb(_reserve)(_cx_self* self, const intptr_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)*c_sizeof(_cx_node)); if (!nodes) @@ -294,7 +297,7 @@ _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, intptr_t n) { +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++); @@ -307,7 +310,7 @@ STC_INLINE void _cx_memb(_put_n)(_cx_self* self, const _cx_raw* raw, intptr_t n) #endif } -STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, intptr_t n) +STC_INLINE _cx_self _cx_memb(_from_n)(const _cx_raw* raw, int64_t n) { _cx_self cx = {0}; _cx_memb(_put_n)(&cx, raw, n); return cx; } #ifndef _i_isset diff --git a/include/stc/forward.h b/include/stc/forward.h index 594c76fa..6103ba39 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -30,9 +30,9 @@ #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, int32_t, c_true, c_false) -#define forward_cmap_huge(CX, KEY, VAL) _c_chash_types(CX, KEY, VAL, intptr_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_cset_huge(CX, KEY) _c_chash_types(CX, cset, KEY, KEY, intptr_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) @@ -98,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 ) \ @@ -117,14 +117,14 @@ 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 SZ SELF##_sizet; \ typedef struct SELF##_node SELF##_node; \ \ typedef SET_ONLY( SELF##_key ) \ @@ -144,13 +144,13 @@ typedef union { \ typedef struct { \ SELF##_node *root; \ - SELF##_size_t size; \ + SELF##_sizet 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 ) \ @@ -166,12 +166,12 @@ 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 diff --git a/include/stc/priv/template.h b/include/stc/priv/template.h index 4aa54bcc..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 int32_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 || \ -- cgit v1.2.3 From feef5067e0c3f1cf113b4fa8b302ac1bfa249e68 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 3 Feb 2023 10:38:18 +0100 Subject: Update cspan, needs more testing. --- docs/cbits_api.md | 68 ++++++++++++++++++++++++------------------------ include/stc/cspan.h | 65 ++++++++++++++++++++++++++++++++++++++------- misc/examples/multidim.c | 2 +- misc/tests/cspan_test.c | 2 +- 4 files changed, 91 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/docs/cbits_api.md b/docs/cbits_api.md index f3967739..60586a5b 100644 --- a/docs/cbits_api.md +++ b/docs/cbits_api.md @@ -19,40 +19,40 @@ All cbits definitions and prototypes are available by including a single header ## Methods ```c -cbits cbits_init(void); -cbits cbits_from(const char* str); -cbits cbits_with_size(intptr_t size, bool value); // size must be <= N if N is defined -cbits cbits_with_pattern(intptr_t size, uint64_t pattern); -cbits cbits_clone(cbits other); - -void cbits_clear(cbits* self); -cbits* cbits_copy(cbits* self, const cbits* other); -void cbits_resize(cbits* self, intptr_t size, bool value); // only if i_len is not defined -void cbits_drop(cbits* self); - -cbits* cbits_take(cbits* self, const cbits* other); // give other to self -cbits cbits_move(cbits* self); // transfer self to caller - -intptr_t cbits_size(const cbits* self); -intptr_t cbits_count(const cbits* self); // count number of bits set - -bool cbits_test(const cbits* self, intptr_t i); -bool cbits_at(const cbits* self, intptr_t i); // same as cbits_test() -bool cbits_subset_of(const cbits* self, const cbits* other); // is set a subset of other? -bool cbits_disjoint(const cbits* self, const cbits* other); // no common bits -char* cbits_to_str(const cbits* self, char* str, intptr_t start, intptr_t stop); - -void cbits_set(cbits* self, intptr_t i); -void cbits_reset(cbits* self, intptr_t i); -void cbits_set_value(cbits* self, intptr_t i, bool value); -void cbits_set_all(cbits* self, bool value); -void cbits_set_pattern(cbits* self, uint64_t pattern); -void cbits_flip_all(cbits* self); -void cbits_flip(cbits* self, intptr_t i); - -void cbits_intersect(cbits* self, const cbits* other); -void cbits_union(cbits* self, const cbits* other); -void cbits_xor(cbits* self, const cbits* other); // set of disjoint bits +cbits cbits_init(void); +cbits cbits_from(const char* str); +cbits cbits_with_size(intptr_t size, bool value); // size must be <= N if N is defined +cbits cbits_with_pattern(intptr_t size, uint64_t pattern); +cbits cbits_clone(cbits other); + +void cbits_clear(cbits* self); +cbits* cbits_copy(cbits* self, const cbits* other); +void cbits_resize(cbits* self, intptr_t size, bool value); // only if i_len is not defined +void cbits_drop(cbits* self); + +cbits* cbits_take(cbits* self, const cbits* other); // give other to self +cbits cbits_move(cbits* self); // transfer self to caller + +intptr_t cbits_size(const cbits* self); +intptr_t cbits_count(const cbits* self); // count number of bits set + +bool cbits_test(const cbits* self, intptr_t i); +bool cbits_at(const cbits* self, intptr_t i); // same as cbits_test() +bool cbits_subset_of(const cbits* self, const cbits* other); // is set a subset of other? +bool cbits_disjoint(const cbits* self, const cbits* other); // no common bits +char* cbits_to_str(const cbits* self, char* str, intptr_t start, intptr_t stop); + +void cbits_set(cbits* self, intptr_t i); +void cbits_reset(cbits* self, intptr_t i); +void cbits_set_value(cbits* self, intptr_t i, bool value); +void cbits_set_all(cbits* self, bool value); +void cbits_set_pattern(cbits* self, uint64_t pattern); +void cbits_flip_all(cbits* self); +void cbits_flip(cbits* self, intptr_t i); + +void cbits_intersect(cbits* self, const cbits* other); +void cbits_union(cbits* self, const cbits* other); +void cbits_xor(cbits* self, const cbits* other); // set of disjoint bits ``` ## Types diff --git a/include/stc/cspan.h b/include/stc/cspan.h index a5f3fdc7..9e981009 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -114,10 +114,16 @@ typedef struct { int32_t d[5]; } cspan_idx5; #define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) #define cspan_rank(self) c_ARRAYLEN((self)->dim) -#define cspan_index(self, ...) \ + +#define cspan_idx(self, ...) \ c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__) +#define cspan_index(self, ...) \ + (_cspan_index(c_NUMARGS(__VA_ARGS__), (self)->dim, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ + c_static_assert(cspan_rank(self) == c_NUMARGS(__VA_ARGS__))) + #define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__)) +#define cspan_item(self, ...) ((self)->data + cspan_idx(self, __VA_ARGS__)) // same as cspan_at(), only for rank <= 5 #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) @@ -161,7 +167,7 @@ typedef struct { int32_t d[5]; } cspan_idx5; {.data=cspan_at(self, x, y, z, w, 0), .dim={(self)->dim[4]}} // cspan_slice: -// e.g.: cspan_slice(&ms3, {1,3}, {0}, {1,4}); +// e.g.: cspan_slice(&ms3, {1,3}, {0,-1}, {1,4}); #define cspan_slice(self, ...) \ ((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \ @@ -192,6 +198,18 @@ STC_INLINE intptr_t _cspan_i5(const int32_t dim[4], const cspan_idx4 stri, int32 return (intptr_t)stri.d[4]*(stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w) + v; } +STC_INLINE intptr_t _cspan_index(int rank, const int32_t dim[], const int32_t stri[], const int32_t a[]) { + intptr_t off = a[0]; + bool ok = c_LTu(a[0], dim[0]); + for (int i = 1; i < rank; ++i) { + off *= stri[i]; + off += a[i]; + ok &= c_LTu(a[i], dim[i]); + } + c_ASSERT(ok); + return off; +} + STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { intptr_t sz = dim[0]; while (rank-- > 1) sz *= dim[rank]; @@ -216,19 +234,46 @@ STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[], } STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], const int32_t a[][2]) { - int32_t t = a[0][1] ? a[0][1] : dim[0]; - c_ASSERT(!c_LTu(dim[0], t)); - dim[0] = t - a[0][0]; - - intptr_t off = a[0][0]; - for (int i = 1; i < rank; ++i) { + intptr_t off = 0; + bool ok = true; + for (int i = 0; i < rank; ++i) { off *= stri[i]; off += a[i][0]; - t = a[i][1] ? a[i][1] : dim[i]; - c_ASSERT(!c_LTu(dim[i], t)); + int32_t t; + switch (a[i][1]) { + case 0: t = a[i][0] + 1; break; + case -1: t = dim[i]; break; + default: t = a[i][1]; + } dim[i] = t - a[i][0]; + ok &= c_LTu(0, dim[i]); } + c_ASSERT(ok); return off; } +STC_INLINE intptr_t _cspan_subslice(int* orank, int32_t odim[], int32_t ostri[], + const int32_t dim[], const int32_t stri[], + int rank, const int32_t a[][2]) { + intptr_t off = a[0][0]; + intptr_t s = 1; + int i = 0, j = 0, ok = true; + for (; i < rank; ++i) { + off *= stri[i]; + off += a[i][0]; + int32_t t; + switch (a[i][1]) { + case 0: s *= stri[i]; continue; + case -1: t = dim[i]; break; + default: t = a[i][1]; break; + } + odim[j] = t - a[i][0]; + ostri[j] = s*stri[i]; + s = 1; ++j; + ok &= c_LTu(0, odim[0]); + } + *orank = j; + c_ASSERT(ok); + return off; +} #endif diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 3bc1feec..d0ddf839 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -30,7 +30,7 @@ int main() } puts("ss3 = ms3[:, 1:3, 1:3]"); ispan3 ss3 = ms3; - cspan_slice(&ss3, {0}, {1,3}, {1,3}); + cspan_slice(&ss3, {0,-1}, {1,3}, {1,3}); for (int i=0; i != ss3.dim[0]; i++) { for (int j=0; j != ss3.dim[1]; j++) { diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 26a662e6..43422f1c 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -32,7 +32,7 @@ CTEST(cspan, slice) { } intspan2 m2 = m1; - cspan_slice(&m2, {0}, {2,4}); + cspan_slice(&m2, {0,-1}, {2,4}); size_t sum2 = 0; for (size_t i = 0; i < m2.dim[0]; ++i) { -- cgit v1.2.3 From 5f4076de684da4507943a86e26dcaa5479cdf69b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 3 Feb 2023 17:48:43 +0100 Subject: Temporary commit. Will cleanup cspan and usage. --- CMakeLists.txt | 8 ++++---- include/stc/cspan.h | 30 ++++++++++++++++++++++-------- include/stc/forward.h | 28 ---------------------------- misc/examples/multidim.c | 4 +++- misc/tests/cspan_test.c | 2 +- 5 files changed, 30 insertions(+), 42 deletions(-) (limited to 'include') diff --git a/CMakeLists.txt b/CMakeLists.txt index 2f54a7f2..9369a070 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,10 +16,10 @@ if(BUILD_TESTING) foreach(file IN LISTS misc/examples) get_filename_component(name "${file}" NAME_WE) add_executable(${name} ${file}) - target_compile_options(${name} PRIVATE "-pthread") - if(CMAKE_THREAD_LIBS_INIT) - target_link_libraries(${name} PRIVATE "${CMAKE_THREAD_LIBS_INIT}") - endif() + target_compile_options(${name} PRIVATE "-pthread") + if(CMAKE_THREAD_LIBS_INIT) + target_link_libraries(${name} PRIVATE "${CMAKE_THREAD_LIBS_INIT}") + endif() target_link_libraries(${name} PRIVATE stc m) add_test(NAME ${name} COMMAND ${name}) endforeach() diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 9e981009..78c92520 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -74,6 +74,13 @@ int demo2() { STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ return (Self){.data=raw, .dim={(int32_t)n}}; \ } \ + STC_INLINE Self Self##_subslice_(Self##_value* v, const int32_t dim[], const int32_t stri[], \ + const int rank, const int32_t a[][2]) { \ + Self s = {.data=v}; int outrank; \ + s.data += _cspan_subslice(s.dim, s.stride.d, &outrank, dim, stri, rank, a); \ + c_ASSERT(outrank == RANK); \ + return s; \ + } \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ Self##_iter it = {.ref=self->data, .pos={0}, ._s=self}; \ return it; \ @@ -91,12 +98,14 @@ int demo2() { #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) -#define using_cspan5(Self, T) using_cspan4(Self, T); using_cspan_4(Self##4, T, 5) 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,c_END #define cspan_md(array, ...) \ {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} @@ -105,6 +114,11 @@ typedef struct { int32_t d[5]; } cspan_idx5; #define cspan_make(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} +#define cspan_subslice(OutSpan, parent, ...) \ + OutSpan##_subslice_((parent)->data, (parent)->dim, (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={(int32_t)(container)->_len}} @@ -170,10 +184,10 @@ typedef struct { int32_t d[5]; } cspan_idx5; // e.g.: cspan_slice(&ms3, {1,3}, {0,-1}, {1,4}); #define cspan_slice(self, ...) \ - ((void)((self)->data += _cspan_slice(cspan_rank(self), (self)->dim, (self)->stride.d, \ - (const int32_t[][2]){__VA_ARGS__}) + \ + ((void)((self)->data += _cspan_slice((self)->dim, (self)->stride.d, \ + cspan_rank(self), (const int32_t[][2]){__VA_ARGS__}) + \ c_static_assert(cspan_rank(self) == \ - sizeof((const int32_t[][2]){__VA_ARGS__})/8))) + sizeof((const int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2])))) // FUNCTIONS @@ -233,7 +247,7 @@ STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[], return off; } -STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], const int32_t a[][2]) { +STC_INLINE intptr_t _cspan_slice(int32_t dim[], const int32_t stri[], const int rank, const int32_t a[][2]) { intptr_t off = 0; bool ok = true; for (int i = 0; i < rank; ++i) { @@ -252,7 +266,7 @@ STC_INLINE intptr_t _cspan_slice(int rank, int32_t dim[], const int32_t stri[], return off; } -STC_INLINE intptr_t _cspan_subslice(int* orank, int32_t odim[], int32_t ostri[], +STC_INLINE intptr_t _cspan_subslice(int32_t odim[], int32_t ostri[], int* orank, const int32_t dim[], const int32_t stri[], int rank, const int32_t a[][2]) { intptr_t off = a[0][0]; @@ -270,10 +284,10 @@ STC_INLINE intptr_t _cspan_subslice(int* orank, int32_t odim[], int32_t ostri[], odim[j] = t - a[i][0]; ostri[j] = s*stri[i]; s = 1; ++j; - ok &= c_LTu(0, odim[0]); + ok &= c_LTu(0, odim[i]) & !c_LTu(dim[i], t); } *orank = j; - c_ASSERT(ok); + c_ASSERT(ok); /* input indices ok */ return off; } #endif diff --git a/include/stc/forward.h b/include/stc/forward.h index 6103ba39..d08b71b4 100644 --- a/include/stc/forward.h +++ b/include/stc/forward.h @@ -120,33 +120,6 @@ typedef union { 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##_sizet; \ - 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##_sizet size; \ - } SELF -#else #define _c_aatree_types(SELF, KEY, VAL, SZ, MAP_ONLY, SET_ONLY) \ typedef KEY SELF##_key; \ typedef VAL SELF##_mapped; \ @@ -173,7 +146,6 @@ typedef union { SELF##_node *nodes; \ SELF##_sizet root, disp, head, size, cap; \ } SELF -#endif #define _c_cstack_fixed(SELF, VAL, CAP) \ typedef VAL SELF##_value; \ diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index d0ddf839..49b24172 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -5,6 +5,7 @@ #include using_cspan3(ispan, int); +#define c_ALL 0,-1 int main() { @@ -30,7 +31,8 @@ int main() } puts("ss3 = ms3[:, 1:3, 1:3]"); ispan3 ss3 = ms3; - cspan_slice(&ss3, {0,-1}, {1,3}, {1,3}); + //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3}); + ss3 = cspan_subslice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3}); for (int i=0; i != ss3.dim[0]; i++) { for (int j=0; j != ss3.dim[1]; j++) { diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 43422f1c..cd6a1271 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -32,7 +32,7 @@ CTEST(cspan, slice) { } intspan2 m2 = m1; - cspan_slice(&m2, {0,-1}, {2,4}); + cspan_slice(&m2, {c_ALL}, {2,4}); size_t sum2 = 0; for (size_t i = 0; i < m2.dim[0]; ++i) { -- cgit v1.2.3 From dc2c5fa4f1b319988c622f6172926652cabfd1aa Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 3 Feb 2023 20:08:03 +0100 Subject: Feature completed cspan. --- docs/cspan_api.md | 17 +++---- include/stc/cspan.h | 120 +++++++++++++++-------------------------------- misc/examples/multidim.c | 3 +- misc/tests/cspan_test.c | 8 ++-- 4 files changed, 52 insertions(+), 96 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 70587f3e..e8c53965 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -35,18 +35,19 @@ ValueType* cspan_at(SpanTypeN* self, intptr_t x, ...); // at(): num ValueType* cspan_front(SpanTypeN* self); ValueType* cspan_back(SpanTypeN* self); + // general slicing: create a subspan. can do the same as submd() and subspan(). +SpanTypeN cspan_slice(T SpanTypeN, SpanTypeM* parent, {x0,x1}, {y0,y1}, ...); // {v} reduces rank. {v,-1} slice to end. + // return a subspan of lower rank: -SpanType cspan_submd2(SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. -SpanTypeN cspan_submd3(SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. -SpanTypeN cspan_submd4(SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. +SpanType cspan_submd2(SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. +SpanTypeN cspan_submd3(SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. +SpanTypeN cspan_submd4(SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. SpanTypeN cspan_submd5(SpanType5* self, intptr_t x, ...); -void cspan_slice(SpanTypeN* self, {x0,x1}, {y0,y1},...); // slice a span to make it a subspan of same rank - // return a subspan of same rank. Similar to e.g. cspan_slice(&ms3, {offset, offset+count}, {0}, {0}); -SpanType cspan_subspan(const SpanType* self, intptr_t offset, intptr_t count); -SpanType2 cspan_subspan2(const SpanType2 self, intptr_t offset, intptr_t count); -SpanType3 cspan_subspan3(const SpanType3 self, intptr_t offset, intptr_t count); +SpanType cspan_subspan(SpanType* self, intptr_t offset, intptr_t count); +SpanType2 cspan_subspan2(SpanType2 self, intptr_t offset, intptr_t count); +SpanType3 cspan_subspan3(SpanType3 self, intptr_t offset, intptr_t count); SpanTypeN_iter SpanType_begin(const SpanTypeN* self); SpanTypeN_iter SpanType_end(const SpanTypeN* self); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 78c92520..94415df7 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -74,10 +74,10 @@ int demo2() { STC_INLINE Self Self##_from_n(Self##_raw* raw, const intptr_t n) { \ return (Self){.data=raw, .dim={(int32_t)n}}; \ } \ - STC_INLINE Self Self##_subslice_(Self##_value* v, const int32_t dim[], const int32_t stri[], \ + STC_INLINE Self Self##_slice_(Self##_value* v, const int32_t dim[], const int32_t stri[], \ const int rank, const int32_t a[][2]) { \ Self s = {.data=v}; int outrank; \ - s.data += _cspan_subslice(s.dim, s.stride.d, &outrank, dim, stri, rank, a); \ + s.data += _cspan_slice(s.dim, s.stride.d, &outrank, dim, stri, rank, a); \ c_ASSERT(outrank == RANK); \ return s; \ } \ @@ -90,7 +90,7 @@ int demo2() { return it; \ } \ STC_INLINE void Self##_next(Self##_iter* it) { \ - it->ref += _cspan_next_##RANK(RANK, it->pos, it->_s->dim, it->_s->stride.d); \ + it->ref += _cspan_next##RANK(RANK, it->pos, it->_s->dim, it->_s->stride.d); \ if (it->pos[0] == it->_s->dim[0]) it->ref = NULL; \ } \ struct stc_nostruct @@ -104,8 +104,7 @@ 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,c_END +#define c_ALL 0,-1 #define cspan_md(array, ...) \ {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} @@ -114,10 +113,10 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_make(SpanType, ...) \ {.data=(SpanType##_value[])__VA_ARGS__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} -#define cspan_subslice(OutSpan, parent, ...) \ - OutSpan##_subslice_((parent)->data, (parent)->dim, (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__}) +#define cspan_slice(OutSpan, parent, ...) \ + OutSpan##_slice_((parent)->data, (parent)->dim, (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) \ @@ -129,29 +128,33 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) #define cspan_rank(self) c_ARRAYLEN((self)->dim) -#define cspan_idx(self, ...) \ - c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, (self)->stride, __VA_ARGS__) - -#define cspan_index(self, ...) \ - (_cspan_index(c_NUMARGS(__VA_ARGS__), (self)->dim, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ +#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)->dim, (self)->stride, __VA_ARGS__) +#define cspan_idx_5(self, ...) \ + (_cspan_idxN(c_NUMARGS(__VA_ARGS__), (self)->dim, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ c_static_assert(cspan_rank(self) == c_NUMARGS(__VA_ARGS__))) +#define cspan_idx_6 cspan_idx_5 #define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__)) -#define cspan_item(self, ...) ((self)->data + cspan_idx(self, __VA_ARGS__)) // same as cspan_at(), only for rank <= 5 #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) -// cspan_subspanN. for N > 3, use cspan_slice(&ms4, {offset, offset + count}, {0}, {0}, {0}); - +// cspan_subspanN. (N<5) Optimized, but same as e.g. cspan_slice(Span2, &ms4, {offset, offset + count}, {c_ALL}, {c_ALL}); #define cspan_subspan(self, offset, count) \ {.data=cspan_at(self, offset), .dim={count}} #define cspan_subspan2(self, offset, count) \ {.data=cspan_at(self, offset, 0), .dim={count, (self)->dim[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]}, .stride={(self)->stride}} +#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]}, \ + .stride={(self)->stride}} // cspan_submdN: return reduced rank - #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) \ @@ -169,50 +172,24 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_submd4_4(self, x, y, z) \ {.data=cspan_at(self, x, y, z, 0), .dim={(self)->dim[3]}} -#define cspan_submd5_2(self, x) \ - {.data=cspan_at(self, x, 0, 0, 0, 0), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3], (self)->dim[4]}, \ - .stride={.d={0, (self)->stride.d[2], (self)->stride.d[3], (self)->stride.d[4]}}} -#define cspan_submd5_3(self, x, y) \ - {.data=cspan_at(self, x, y, 0, 0, 0), .dim={(self)->dim[2], (self)->dim[3], (self)->dim[4]}, \ - .stride={.d={0, (self)->stride.d[3], (self)->stride.d[4]}}} -#define cspan_submd5_4(self, x, y, z) \ - {.data=cspan_at(self, x, y, z, 0, 0), .dim={(self)->dim[3], (self)->dim[4]}, .stride={.d={0, (self)->stride.d[4]}}} -#define cspan_submd5_5(self, x, y, z, w) \ - {.data=cspan_at(self, x, y, z, w, 0), .dim={(self)->dim[4]}} - -// cspan_slice: -// e.g.: cspan_slice(&ms3, {1,3}, {0,-1}, {1,4}); - -#define cspan_slice(self, ...) \ - ((void)((self)->data += _cspan_slice((self)->dim, (self)->stride.d, \ - cspan_rank(self), (const int32_t[][2]){__VA_ARGS__}) + \ - c_static_assert(cspan_rank(self) == \ - sizeof((const int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2])))) - // FUNCTIONS -STC_INLINE intptr_t _cspan_i1(const int32_t dim[1], const cspan_idx1 stri, int32_t x) +STC_INLINE intptr_t _cspan_idx1(const int32_t dim[1], const cspan_idx1 stri, int32_t x) { c_ASSERT(c_LTu(x, dim[0])); return x; } -STC_INLINE intptr_t _cspan_i2(const int32_t dim[2], const cspan_idx2 stri, int32_t x, int32_t y) +STC_INLINE intptr_t _cspan_idx2(const int32_t dim[2], const cspan_idx2 stri, int32_t x, int32_t y) { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1])); return (intptr_t)stri.d[1]*x + y; } -STC_INLINE intptr_t _cspan_i3(const int32_t dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { +STC_INLINE intptr_t _cspan_idx3(const int32_t dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2])); return (intptr_t)stri.d[2]*(stri.d[1]*x + y) + z; } -STC_INLINE intptr_t _cspan_i4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, - int32_t z, int32_t w) { +STC_INLINE intptr_t _cspan_idx4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, + int32_t z, int32_t w) { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3])); return (intptr_t)stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w; } -STC_INLINE intptr_t _cspan_i5(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, int32_t z, - int32_t w, int32_t v) { - c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3]) && c_LTu(v, dim[4])); - return (intptr_t)stri.d[4]*(stri.d[3]*(stri.d[2]*(stri.d[1]*x + y) + z) + w) + v; -} - -STC_INLINE intptr_t _cspan_index(int rank, const int32_t dim[], const int32_t stri[], const int32_t a[]) { +STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t dim[], const int32_t stri[], const int32_t a[]) { intptr_t off = a[0]; bool ok = c_LTu(a[0], dim[0]); for (int i = 1; i < rank; ++i) { @@ -230,12 +207,13 @@ STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { return sz; } -#define _cspan_next_1(r, pos, d, s) (++pos[0], 1) -#define _cspan_next_3 _cspan_next_2 -#define _cspan_next_4 _cspan_next_2 -#define _cspan_next_5 _cspan_next_2 +#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_next_2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) { +STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) { intptr_t off = 1, rs = 1; ++pos[rank - 1]; while (--rank && pos[rank] == dim[rank]) { @@ -247,30 +225,10 @@ STC_INLINE intptr_t _cspan_next_2(int rank, int32_t pos[], const int32_t dim[], return off; } -STC_INLINE intptr_t _cspan_slice(int32_t dim[], const int32_t stri[], const int rank, const int32_t a[][2]) { - intptr_t off = 0; - bool ok = true; - for (int i = 0; i < rank; ++i) { - off *= stri[i]; - off += a[i][0]; - int32_t t; - switch (a[i][1]) { - case 0: t = a[i][0] + 1; break; - case -1: t = dim[i]; break; - default: t = a[i][1]; - } - dim[i] = t - a[i][0]; - ok &= c_LTu(0, dim[i]); - } - c_ASSERT(ok); - return off; -} - -STC_INLINE intptr_t _cspan_subslice(int32_t odim[], int32_t ostri[], int* orank, - const int32_t dim[], const int32_t stri[], - int rank, const int32_t a[][2]) { - intptr_t off = a[0][0]; - intptr_t s = 1; +STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, + const int32_t dim[], const int32_t stri[], + int rank, const int32_t a[][2]) { + intptr_t off = 0, s = 1; int i = 0, j = 0, ok = true; for (; i < rank; ++i) { off *= stri[i]; @@ -283,11 +241,11 @@ STC_INLINE intptr_t _cspan_subslice(int32_t odim[], int32_t ostri[], int* orank, } odim[j] = t - a[i][0]; ostri[j] = s*stri[i]; + ok &= c_LTu(0, odim[j]) & !c_LTu(dim[i], t); s = 1; ++j; - ok &= c_LTu(0, odim[i]) & !c_LTu(dim[i], t); } *orank = j; - c_ASSERT(ok); /* input indices ok */ + c_ASSERT(ok); return off; } #endif diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 49b24172..49d7fbc4 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -5,7 +5,6 @@ #include using_cspan3(ispan, int); -#define c_ALL 0,-1 int main() { @@ -32,7 +31,7 @@ int main() puts("ss3 = ms3[:, 1:3, 1:3]"); ispan3 ss3 = ms3; //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3}); - ss3 = cspan_subslice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3}); + ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3}); for (int i=0; i != ss3.dim[0]; i++) { for (int j=0; j != ss3.dim[1]; j++) { diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index cd6a1271..044ce629 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -31,8 +31,7 @@ CTEST(cspan, slice) { } } - intspan2 m2 = m1; - cspan_slice(&m2, {c_ALL}, {2,4}); + intspan2 m2 = cspan_slice(intspan2, &m1, {c_ALL}, {2,4}); size_t sum2 = 0; for (size_t i = 0; i < m2.dim[0]; ++i) { @@ -54,7 +53,7 @@ CTEST(cspan, slice2) { cstack_int_push(&stack, i); intspan3 ms3 = cspan_md(stack.data, 10, 20, 30); - cspan_slice(&ms3, {1,4}, {3,7}, {20,24}); + ms3 = cspan_slice(intspan3, &ms3, {1,4}, {3,7}, {20,24}); size_t sum = 0; for (size_t i = 0; i < ms3.dim[0]; ++i) { @@ -98,8 +97,7 @@ CTEST_SETUP(cspan_cube) { c_FORRANGE (i, 0, ms3.dim[0], TSIZE) { c_FORRANGE (j, 0, ms3.dim[1], TSIZE) { c_FORRANGE (k, 0, ms3.dim[2], TSIZE) { - intspan3 tile = ms3; - cspan_slice(&tile, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); + intspan3 tile = cspan_slice(intspan3, &ms3, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); Tiles_push(&_self->tiles, tile); } } -- cgit v1.2.3 From cd806510c7c77c0618af58bd804e36f2bad6d219 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 3 Feb 2023 22:08:54 +0100 Subject: More cspan docs fixes. --- docs/cspan_api.md | 65 ++++++++++++++++++++++++++++------------------------- include/stc/cspan.h | 3 ++- 2 files changed, 37 insertions(+), 31 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index e8c53965..8321b1e5 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -31,23 +31,24 @@ intptr_t cspan_size(const SpanTypeN* self); // return nu unsigned cspan_rank(const SpanTypeN* self); // return number of dimensions intptr_t cspan_index(const SpanTypeN* self, intptr_t x, ..); // index of element -ValueType* cspan_at(SpanTypeN* self, intptr_t x, ...); // at(): num of args specifies rank of input span. -ValueType* cspan_front(SpanTypeN* self); -ValueType* cspan_back(SpanTypeN* self); +ValueType* cspan_at(const SpanTypeN* self, intptr_t x, ...); // at(): num of args specifies rank of input span. +ValueType* cspan_front(const SpanTypeN* self); +ValueType* cspan_back(const SpanTypeN* self); - // general slicing: create a subspan. can do the same as submd() and subspan(). -SpanTypeN cspan_slice(T SpanTypeN, SpanTypeM* parent, {x0,x1}, {y0,y1}, ...); // {v} reduces rank. {v,-1} slice to end. + // general slicing to create a subspan. + // {ind} reduces rank. {ind,c_END} slice to end. {c_ALL} take the full extent. +SpanTypeN cspan_slice(T SpanTypeN, const SpanTypeM* parent, {x0,x1}, {y0,y1}, ...); - // return a subspan of lower rank: -SpanType cspan_submd2(SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. -SpanTypeN cspan_submd3(SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. -SpanTypeN cspan_submd4(SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. -SpanTypeN cspan_submd5(SpanType5* self, intptr_t x, ...); + // create a subspan of lower rank. Like e.g. cspan_slice(Span2, &ms3, {x}, {c_ALL}, {c_ALL}); +SpanType cspan_submd2(const SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. +SpanTypeN cspan_submd3(const SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. +SpanTypeN cspan_submd4(const SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. +SpanTypeN cspan_submd5(const SpanType5* self, intptr_t x, ...); - // return a subspan of same rank. Similar to e.g. cspan_slice(&ms3, {offset, offset+count}, {0}, {0}); -SpanType cspan_subspan(SpanType* self, intptr_t offset, intptr_t count); -SpanType2 cspan_subspan2(SpanType2 self, intptr_t offset, intptr_t count); -SpanType3 cspan_subspan3(SpanType3 self, intptr_t offset, intptr_t count); + // create a subspan of same rank. Like e.g. cspan_slice(Span3, &ms3, {off, off+count}, {c_ALL}, {c_ALL}); +SpanType cspan_subspan(const SpanType* self, intptr_t offset, intptr_t count); +SpanType2 cspan_subspan2(const SpanType2 self, intptr_t offset, intptr_t count); +SpanType3 cspan_subspan3(const SpanType3 self, intptr_t offset, intptr_t count); SpanTypeN_iter SpanType_begin(const SpanTypeN* self); SpanTypeN_iter SpanType_end(const SpanTypeN* self); @@ -59,30 +60,34 @@ void SpanType_next(SpanTypeN_iter* it); |:------------------|:-----------------------------------------------|:---------------------| | SpanTypeN | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | | SpanTypeN`_value` | `ValueType` | The ValueType | -| SpanTypeN`_iter` | `struct { ValueType *ref; ... }` | Iterator type | +| `c_ALL` | `0,-1` | Full extent | +| `c_END` | `-1` | End of extent | ## Example 1 The *cspan_slice()* function is similar to pythons numpy multi-dimensional arrays slicing, e.g.: ```py import numpy as np -ms3 = np.array((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24), int) -ms3 = np.reshape(ms3, (2, 3, 4)) -ss3 = ms3[:, 1:3, 2:] -ss2 = ss3[1] +if __name__ == '__main__': + ms3 = np.array((1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24), int) -for i in range(ss2.shape[0]): - for j in range(ss2.shape[1]): - print(" {}".format(ss2[i, j]), end='') -print('') + ms3 = np.reshape(ms3, (2, 3, 4)) + ss3 = ms3[:, 1:3, 2:] + ss2 = ss3[1] + + for i in range(ss2.shape[0]): + for j in range(ss2.shape[1]): + print(" {}".format(ss2[i, j]), end='') + print('') + + for i in ss2.flat: + print(" {}".format(i), end='') -for i in ss2.flat: - print(" {}".format(i), end='') # 19 20 23 24 # 19 20 23 24 ``` -... can be done in C with STC: +... can be done in C with cspan: ```c #include #include @@ -91,8 +96,8 @@ using_cspan3(myspan, int); // define myspan, myspan2, myspan3. int main() { int arr[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}; - myspan3 ms3 = cspan_md(arr, 2, 3, 4), ss3 = ms3; - cspan_slice(&ss3, {0}, {1,3}, {2,}); + myspan3 ms3 = cspan_md(arr, 2, 3, 4); + myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {1,3}, {2,c_END}); myspan2 ss2 = cspan_submd3(&ss3, 1); c_FORRANGE (i, ss2.dim[0]) @@ -104,7 +109,7 @@ int main() { fmt_print(" {}", *i.ref); } ``` -... or (mostly) in C++23: +... and (almost) in C++23: ```c++ #include #include @@ -122,7 +127,7 @@ int main() { std::print(" {}", ss2[i, j]); std::println(); - // mdspan can't printed as a flat array, afaik. + // std::mdspan can't be iterated as a flat container! } ``` ## Example 2 diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 94415df7..315465db 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -104,7 +104,8 @@ 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_ALL 0,-1 +#define c_END -1 +#define c_ALL 0,c_END #define cspan_md(array, ...) \ {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} -- cgit v1.2.3 From 2f370b34484e63359f9fd2850b72d3d1d8dfc32a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 3 Feb 2023 22:37:59 +0100 Subject: Renamed dim to shape in cspan. Not 100% sure it will survive. --- docs/cspan_api.md | 12 +++---- include/stc/cspan.h | 94 ++++++++++++++++++++++++------------------------ misc/examples/multidim.c | 16 ++++----- misc/tests/cspan_test.c | 26 +++++++------- 4 files changed, 74 insertions(+), 74 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 8321b1e5..66a7b01b 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -58,7 +58,7 @@ void SpanType_next(SpanTypeN_iter* it); | Type name | Type definition | Used to represent... | |:------------------|:-----------------------------------------------|:---------------------| -| SpanTypeN | `struct { ValueType *data; uint32_t dim[N]; }` | SpanType with rank N | +| SpanTypeN | `struct { ValueType *data; uint32_t shape[N]; }` | SpanType with rank N | | SpanTypeN`_value` | `ValueType` | The ValueType | | `c_ALL` | `0,-1` | Full extent | | `c_END` | `-1` | End of extent | @@ -100,8 +100,8 @@ int main() { myspan3 ss3 = cspan_slice(myspan3, &ms3, {c_ALL}, {1,3}, {2,c_END}); myspan2 ss2 = cspan_submd3(&ss3, 1); - c_FORRANGE (i, ss2.dim[0]) - c_FORRANGE (j, ss2.dim[1]) + c_FORRANGE (i, ss2.shape[0]) + c_FORRANGE (j, ss2.shape[1]) fmt_print(" {}", *cspan_at(&ss2, i, j)); puts(""); @@ -163,9 +163,9 @@ int main() fmt_print("{} ", *i.ref); puts("\niterate span3 by dimensions:"); - c_FORRANGE (i, span3.dim[0]) { - c_FORRANGE (j, span3.dim[1]) { - c_FORRANGE (k, span3.dim[2]) + c_FORRANGE (i, span3.shape[0]) { + c_FORRANGE (j, span3.shape[1]) { + c_FORRANGE (k, span3.shape[2]) fmt_printf(" {:2}", *cspan_at(&span3, i, j, k)); printf(" |"); } diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 315465db..3ddc9742 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -32,8 +32,8 @@ int demo1() { float raw[4*5]; Span2f ms = cspan_md(raw, 4, 5); - for (int i=0; iref += _cspan_next##RANK(RANK, it->pos, it->_s->dim, it->_s->stride.d); \ - if (it->pos[0] == it->_s->dim[0]) it->ref = NULL; \ + 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 @@ -108,35 +108,35 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define c_ALL 0,c_END #define cspan_md(array, ...) \ - {.data=array, .dim={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} + {.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__, .dim={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} + {.data=(SpanType##_value[])__VA_ARGS__, .shape={sizeof((SpanType##_value[])__VA_ARGS__)/sizeof(SpanType##_value)}} #define cspan_slice(OutSpan, parent, ...) \ - OutSpan##_slice_((parent)->data, (parent)->dim, (parent)->stride.d, cspan_rank(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={(int32_t)(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)}} + {.data=(array) + c_static_assert(sizeof(array) != sizeof(void*)), .shape={c_ARRAYLEN(array)}} -#define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self)) -#define cspan_rank(self) c_ARRAYLEN((self)->dim) +#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)->dim, (self)->stride, __VA_ARGS__) + c_PASTE(_cspan_idx, c_NUMARGS(__VA_ARGS__))((self)->shape, (self)->stride, __VA_ARGS__) #define cspan_idx_5(self, ...) \ - (_cspan_idxN(c_NUMARGS(__VA_ARGS__), (self)->dim, (self)->stride.d, (int32_t[]){__VA_ARGS__}) + \ + (_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__))) #define cspan_idx_6 cspan_idx_5 @@ -146,65 +146,65 @@ typedef struct { int32_t d[6]; } cspan_idx6; // cspan_subspanN. (N<5) Optimized, but same as e.g. cspan_slice(Span2, &ms4, {offset, offset + 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]}, .stride={(self)->stride}} + {.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]}, .stride={(self)->stride}} + {.data=cspan_at(self, offset, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride={(self)->stride}} #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]}, \ + {.data=cspan_at(self, offset, 0, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2], (self)->shape[3]}, \ .stride={(self)->stride}} // cspan_submdN: return reduced rank #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), .dim={(self)->dim[1]}} + {.data=cspan_at(self, x, 0), .shape={(self)->shape[1]}} #define cspan_submd3_2(self, x) \ - {.data=cspan_at(self, x, 0, 0), .dim={(self)->dim[1], (self)->dim[2]}, \ + {.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), .dim={(self)->dim[2]}} + {.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), .dim={(self)->dim[1], (self)->dim[2], (self)->dim[3]}, \ + {.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), .dim={(self)->dim[2], (self)->dim[3]}, .stride={.d={0, (self)->stride.d[3]}}} + {.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), .dim={(self)->dim[3]}} + {.data=cspan_at(self, x, y, z, 0), .shape={(self)->shape[3]}} // FUNCTIONS -STC_INLINE intptr_t _cspan_idx1(const int32_t dim[1], const cspan_idx1 stri, int32_t x) - { c_ASSERT(c_LTu(x, dim[0])); return x; } +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 dim[2], const cspan_idx2 stri, int32_t x, int32_t y) - { c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1])); return (intptr_t)stri.d[1]*x + y; } +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 dim[3], const cspan_idx3 stri, int32_t x, int32_t y, int32_t z) { - c_ASSERT(c_LTu(x, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2])); +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 intptr_t _cspan_idx4(const int32_t dim[4], const cspan_idx4 stri, int32_t x, int32_t y, +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, dim[0]) && c_LTu(y, dim[1]) && c_LTu(z, dim[2]) && c_LTu(w, dim[3])); + 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 intptr_t _cspan_idxN(int rank, const int32_t dim[], const int32_t stri[], const int32_t a[]) { +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], dim[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], dim[i]); + ok &= c_LTu(a[i], shape[i]); } c_ASSERT(ok); return off; } -STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { - intptr_t sz = dim[0]; - while (rank-- > 1) sz *= dim[rank]; +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; } @@ -214,12 +214,12 @@ STC_INLINE intptr_t _cspan_size(const int32_t dim[], int rank) { #define _cspan_next5 _cspan_next2 #define _cspan_next6 _cspan_next2 -STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t dim[], const int32_t stride[]) { +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] == dim[rank]) { + while (--rank && pos[rank] == shape[rank]) { pos[rank] = 0, ++pos[rank - 1]; - const intptr_t ds = rs*dim[rank]; + const intptr_t ds = rs*shape[rank]; rs *= stride[rank]; off += rs - ds; } @@ -227,7 +227,7 @@ STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t dim[], c } STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, - const int32_t dim[], const int32_t stri[], + const int32_t shape[], const int32_t stri[], int rank, const int32_t a[][2]) { intptr_t off = 0, s = 1; int i = 0, j = 0, ok = true; @@ -237,12 +237,12 @@ STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, int32_t t; switch (a[i][1]) { case 0: s *= stri[i]; continue; - case -1: t = dim[i]; break; + 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(dim[i], t); + ok &= c_LTu(0, odim[j]) & !c_LTu(shape[i], t); s = 1; ++j; } *orank = j; diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 49d7fbc4..e61959ca 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -19,9 +19,9 @@ int main() ispan3 ms3 = cspan_md(v.data, 2, 3, 4); puts("ms3:"); - for (int i=0; i != ms3.dim[0]; i++) { - for (int j=0; j != ms3.dim[1]; j++) { - for (int k=0; k != ms3.dim[2]; k++) { + for (int i=0; i != ms3.shape[0]; i++) { + for (int j=0; j != ms3.shape[1]; j++) { + for (int k=0; k != ms3.shape[2]; k++) { printf(" %2d", *cspan_at(&ms3, i, j, k)); } puts(""); @@ -33,9 +33,9 @@ int main() //cspan_slice(&ss3, {c_ALL}, {1,3}, {1,3}); ss3 = cspan_slice(ispan3, &ms3, {c_ALL}, {1,3}, {1,3}); - for (int i=0; i != ss3.dim[0]; i++) { - for (int j=0; j != ss3.dim[1]; j++) { - for (int k=0; k != ss3.dim[2]; k++) { + for (int i=0; i != ss3.shape[0]; i++) { + for (int j=0; j != ss3.shape[1]; j++) { + for (int k=0; k != ss3.shape[2]; k++) { printf(" %2d", *cspan_at(&ss3, i, j, k)); } puts(""); @@ -51,8 +51,8 @@ int main() ispan2 ms2 = cspan_submd3(&ms3, 0); // write data using 2D view - for (int i=0; i != ms2.dim[0]; i++) - for (int j=0; j != ms2.dim[1]; j++) + for (int i=0; i != ms2.shape[0]; i++) + for (int j=0; j != ms2.shape[1]; j++) *cspan_at(&ms2, i, j) = i*1000 + j; puts("\nview data as 1D view:"); diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c index 044ce629..9e33467c 100644 --- a/misc/tests/cspan_test.c +++ b/misc/tests/cspan_test.c @@ -9,11 +9,11 @@ CTEST(cspan, subdim) { int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; intspan3 m = cspan_md(array, 2, 2, 3); - for (size_t i = 0; i < m.dim[0]; ++i) { + for (size_t i = 0; i < m.shape[0]; ++i) { intspan2 sub_i = cspan_submd3(&m, i); - for (size_t j = 0; j < m.dim[1]; ++j) { + for (size_t j = 0; j < m.shape[1]; ++j) { intspan sub_i_j = cspan_submd2(&sub_i, j); - for (size_t k = 0; k < m.dim[2]; ++k) { + for (size_t k = 0; k < m.shape[2]; ++k) { ASSERT_EQ(*cspan_at(&sub_i_j, k), *cspan_at(&m, i, j, k)); } } @@ -25,8 +25,8 @@ CTEST(cspan, slice) { intspan2 m1 = cspan_md(array, 3, 4); size_t sum1 = 0; - for (size_t i = 0; i < m1.dim[0]; ++i) { - for (size_t j = 0; j < m1.dim[1]; ++j) { + for (size_t i = 0; i < m1.shape[0]; ++i) { + for (size_t j = 0; j < m1.shape[1]; ++j) { sum1 += *cspan_at(&m1, i, j); } } @@ -34,8 +34,8 @@ CTEST(cspan, slice) { intspan2 m2 = cspan_slice(intspan2, &m1, {c_ALL}, {2,4}); size_t sum2 = 0; - for (size_t i = 0; i < m2.dim[0]; ++i) { - for (size_t j = 0; j < m2.dim[1]; ++j) { + for (size_t i = 0; i < m2.shape[0]; ++i) { + for (size_t j = 0; j < m2.shape[1]; ++j) { sum2 += *cspan_at(&m2, i, j); } } @@ -56,9 +56,9 @@ CTEST(cspan, slice2) { ms3 = cspan_slice(intspan3, &ms3, {1,4}, {3,7}, {20,24}); size_t sum = 0; - for (size_t i = 0; i < ms3.dim[0]; ++i) { - for (size_t j = 0; j < ms3.dim[1]; ++j) { - for (size_t k = 0; k < ms3.dim[2]; ++k) { + for (size_t i = 0; i < ms3.shape[0]; ++i) { + for (size_t j = 0; j < ms3.shape[1]; ++j) { + for (size_t k = 0; k < ms3.shape[2]; ++k) { sum += *cspan_at(&ms3, i, j, k); } } @@ -94,9 +94,9 @@ CTEST_SETUP(cspan_cube) { intspan3 ms3 = cspan_md(_self->stack.data, CUBE, CUBE, CUBE); - c_FORRANGE (i, 0, ms3.dim[0], TSIZE) { - c_FORRANGE (j, 0, ms3.dim[1], TSIZE) { - c_FORRANGE (k, 0, ms3.dim[2], TSIZE) { + c_FORRANGE (i, 0, ms3.shape[0], TSIZE) { + c_FORRANGE (j, 0, ms3.shape[1], TSIZE) { + c_FORRANGE (k, 0, ms3.shape[2], TSIZE) { intspan3 tile = cspan_slice(intspan3, &ms3, {i, i + TSIZE}, {j, j + TSIZE}, {k, k + TSIZE}); Tiles_push(&_self->tiles, tile); } -- cgit v1.2.3 From 51961138b16fbebacd76ce465fb979830d0f9701 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 4 Feb 2023 00:14:45 +0100 Subject: Minor docs update. --- docs/cspan_api.md | 6 ++---- include/stc/cspan.h | 7 ++----- 2 files changed, 4 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 66a7b01b..616b090e 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -16,7 +16,6 @@ using_cspan(SpanTypeN, ValueType, Rank); // define multi-dimensional span with R using_cspan2(S, ValueType); // define span types S, S2 with ranks 1, 2. using_cspan3(S, ValueType); // define span types S, S2, S3 with ranks 1, 2, 3. using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. -using_cspan5(S, ValueType); // define span types S, S2, .., S5 with ranks 1, 2, 3, 4, 5. ``` ## Methods Note that `cspan_md()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type @@ -35,15 +34,14 @@ ValueType* cspan_at(const SpanTypeN* self, intptr_t x, ...); // at(): num ValueType* cspan_front(const SpanTypeN* self); ValueType* cspan_back(const SpanTypeN* self); - // general slicing to create a subspan. - // {ind} reduces rank. {ind,c_END} slice to end. {c_ALL} take the full extent. + // general index slicing to create a subspan. + // {x} reduces rank. {x,c_END} slice to end. {c_ALL} take the full extent. SpanTypeN cspan_slice(T SpanTypeN, const SpanTypeM* parent, {x0,x1}, {y0,y1}, ...); // create a subspan of lower rank. Like e.g. cspan_slice(Span2, &ms3, {x}, {c_ALL}, {c_ALL}); SpanType cspan_submd2(const SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. SpanTypeN cspan_submd3(const SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. SpanTypeN cspan_submd4(const SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. -SpanTypeN cspan_submd5(const SpanType5* self, intptr_t x, ...); // create a subspan of same rank. Like e.g. cspan_slice(Span3, &ms3, {off, off+count}, {c_ALL}, {c_ALL}); SpanType cspan_subspan(const SpanType* self, intptr_t offset, intptr_t count); diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 3ddc9742..bee67863 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -134,10 +134,10 @@ typedef struct { int32_t d[6]; } cspan_idx6; #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__) + 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__))) + 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__)) @@ -151,9 +151,6 @@ typedef struct { int32_t d[6]; } cspan_idx6; {.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), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride={(self)->stride}} -#define cspan_subspan4(self, offset, count) \ - {.data=cspan_at(self, offset, 0, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2], (self)->shape[3]}, \ - .stride={(self)->stride}} // cspan_submdN: return reduced rank #define cspan_submd4(...) c_MACRO_OVERLOAD(cspan_submd4, __VA_ARGS__) -- cgit v1.2.3 From 0aecdd8a1f9d212630acb6dca3ed8bebefb09139 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 4 Feb 2023 00:22:50 +0100 Subject: Removed a warning. --- include/stc/cspan.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stc/cspan.h b/include/stc/cspan.h index bee67863..30046898 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -226,12 +226,12 @@ STC_INLINE intptr_t _cspan_next2(int rank, int32_t pos[], const int32_t shape[], 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, s = 1; + 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]; - int32_t t; switch (a[i][1]) { case 0: s *= stri[i]; continue; case -1: t = shape[i]; break; -- cgit v1.2.3 From 626b893b4d9e048c27571bfa28352914f2cd2bbc Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 4 Feb 2023 16:40:39 +0100 Subject: More cspan docs updates. --- README.md | 11 +++--- docs/cspan_api.md | 107 +++++++++++++++++++++++++++------------------------- include/stc/cspan.h | 8 ++-- 3 files changed, 66 insertions(+), 60 deletions(-) (limited to 'include') diff --git a/README.md b/README.md index 63308051..a9d177e1 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,22 @@ STC - Smart Template Containers for C ===================================== -News: Version 4.1 RC (Feb 2023) +News: Version 4.1 RC2 (Feb 2023) ------------------------------------------------ Major changes: - Signed sizes and indices for all containers (no more mixing unsigned/signed bugs). -- A new exciting [**cspan**](docs/cspan_api.md) single/multi-dimensional array view. +- A new exciting [**cspan**](docs/cspan_api.md) single/multi-dimensional array view (with numpy-like slicing). - Updates on cregex with several new unicode character classes. - Uppercase flow-control macros (ccommon.h). Lowercase macros are [still supported](include/stc/priv/altnames.h). +- Some API changes in cregex and cstr. - [See detailed changes for version 4](#version-4). Introduction ------------ STC is a *modern*, *templated*, *user-friendly*, *type-safe*, *very fast* and *compact* container library for C99. -The API is fairly similar to c++ STL, but a bit more uniform across the containers and takes -inspiration from Rust and Python as well. It is an advantage to know how these containers work in other languages, like -Java, C# or C++, but it's not required. +The API has similarities with c++ STL, but is more uniform across the containers and takes inspiration from Rust +and Python as well. It is an advantage to know how these containers work in other languages, like Java, C# or C++, +but it's not required. This library allows you to manage both trivial to very complex data in a wide variety of containers without the need for boilerplate code. You may specify element-cloning, -comparison, -destruction and diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 616b090e..650260a2 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -38,12 +38,12 @@ ValueType* cspan_back(const SpanTypeN* self); // {x} reduces rank. {x,c_END} slice to end. {c_ALL} take the full extent. SpanTypeN cspan_slice(T SpanTypeN, const SpanTypeM* parent, {x0,x1}, {y0,y1}, ...); - // create a subspan of lower rank. Like e.g. cspan_slice(Span2, &ms3, {x}, {c_ALL}, {c_ALL}); + // create a subspan of lower rank. Like e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL}); SpanType cspan_submd2(const SpanType2* self, intptr_t x); // return a 1d subspan from a 2d span. SpanTypeN cspan_submd3(const SpanType3* self, intptr_t x, ...); // return a 1d or 2d subspan from a 3d span. SpanTypeN cspan_submd4(const SpanType4* self, intptr_t x, ...); // number of args determines rank of output span. - // create a subspan of same rank. Like e.g. cspan_slice(Span3, &ms3, {off, off+count}, {c_ALL}, {c_ALL}); + // create a subspan of same rank. Like e.g. cspan_slice(Span3, &ms3, {off,off+count}, {c_ALL}, {c_ALL}); SpanType cspan_subspan(const SpanType* self, intptr_t offset, intptr_t count); SpanType2 cspan_subspan2(const SpanType2 self, intptr_t offset, intptr_t count); SpanType3 cspan_subspan3(const SpanType3 self, intptr_t offset, intptr_t count); @@ -132,63 +132,68 @@ int main() { ```c #include #include -#define i_val float -#include -using_cspan3(Span, float); // Shorthand to define span types Span, Span2, and Span3. +using_cspan3(Span, int); // Shorthand to define Span, Span2, and Span3 int main() { - int xd = 6, yd = 4, zd = 3; - c_AUTO (cstack_float, vec) - { - c_FORRANGE (i, xd*yd*zd) - cstack_float_push(&vec, i); - - // define "span3[xd][yd][zd]" - Span3 span3 = cspan_md(vec.data, xd, yd, zd); - *cspan_at(&span3, 4, 3, 2) = 3.14159f; - fmt_print("index: {}", cspan_index(&span3, 4, 3, 2)); - - Span span1 = cspan_submd3(&span3, 4, 3); - printf("\niterate span1: "); - c_FOREACH (i, Span, span1) - fmt_print("{} ", *i.ref); - - Span2 span2 = cspan_submd3(&span3, 4); - printf("\niterate span2: "); - c_FOREACH (i, Span2, span2) - fmt_print("{} ", *i.ref); - - puts("\niterate span3 by dimensions:"); - c_FORRANGE (i, span3.shape[0]) { - c_FORRANGE (j, span3.shape[1]) { - c_FORRANGE (k, span3.shape[2]) - fmt_printf(" {:2}", *cspan_at(&span3, i, j, k)); - printf(" |"); - } - puts(""); - } + // c_make() can create any STC container/span from an initializer list: + Span span = c_make(Span, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, + 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}); + // create a 3d cspan: + Span3 span3 = cspan_md(span.data, 2, 4, 3); + + // reduce rank: (i.e. span3[1]) + Span2 span2 = cspan_submd3(&span3, 1); - fmt_println("{}", *cspan_at(&span3, 4, 3, 2)); - fmt_println("{}", *cspan_at(&span2, 3, 2)); - fmt_println("{}", *cspan_at(&span1, 2)); + puts("\niterate span2 flat:"); + c_FOREACH (i, Span2, span2) + fmt_print(" {}", *i.ref); + puts(""); + + // slice without reducing rank: + Span3 ss3 = cspan_slice(Span3, &span3, {c_ALL}, {3,4}, {c_ALL}); + + puts("\niterate ss3 by dimensions:"); + c_FORRANGE (i, ss3.shape[0]) { + c_FORRANGE (j, ss3.shape[1]) { + c_FORRANGE (k, ss3.shape[2]) + fmt_print(" {:2}", *cspan_at(&ss3, i, j, k)); + fmt_print(" |"); + } + puts(""); } + // slice and reduce rank: + Span2 ss2 = cspan_slice(Span2, &span3, {c_ALL}, {3}, {c_ALL}); + + puts("\niterate ss2 by dimensions:"); + c_FORRANGE (i, ss2.shape[0]) { + c_FORRANGE (j, ss2.shape[1]) { + fmt_print(" {:2}", *cspan_at(&ss2, i, j)); + fmt_print(" |"); + } + puts(""); + } + + puts("\niterate ss2 flat:"); + c_FOREACH (i, Span2, ss2) + fmt_print(" {:2}", *i.ref); + puts(""); } ``` Output: ``` -index: 59 -iterate span1: 57 58 3.14159 -iterate span2: 48 49 50 51 52 53 54 55 56 57 58 3.14159 -iterate span3 by dimensions: - 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11 | - 12 13 14 | 15 16 17 | 18 19 20 | 21 22 23 | - 24 25 26 | 27 28 29 | 30 31 32 | 33 34 35 | - 36 37 38 | 39 40 41 | 42 43 44 | 45 46 47 | - 48 49 50 | 51 52 53 | 54 55 56 | 57 58 3.14159 | - 60 61 62 | 63 64 65 | 66 67 68 | 69 70 71 | -3.14159 -3.14159 -3.14159 +iterate span2 flat: + 13 14 15 16 17 18 19 20 21 22 23 24 + +iterate ss3 by dimensions: + 10 11 12 | + 22 23 24 | + +iterate ss2 by dimensions: + 10 | 11 | 12 | + 22 | 23 | 24 | + +iterate ss2 flat: + 10 11 12 22 23 24 ``` diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 30046898..68a13522 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -105,7 +105,7 @@ 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,c_END +#define c_ALL 0,-1 #define cspan_md(array, ...) \ {.data=array, .shape={__VA_ARGS__}, .stride={.d={__VA_ARGS__}}} @@ -144,7 +144,7 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_front(self) ((self)->data) #define cspan_back(self) ((self)->data + cspan_size(self) - 1) -// cspan_subspanN. (N<5) Optimized, but same as e.g. cspan_slice(Span2, &ms4, {offset, offset + count}, {c_ALL}, {c_ALL}); +// 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), .shape={count}} #define cspan_subspan2(self, offset, count) \ @@ -152,7 +152,7 @@ typedef struct { int32_t d[6]; } cspan_idx6; #define cspan_subspan3(self, offset, count) \ {.data=cspan_at(self, offset, 0, 0), .shape={count, (self)->shape[1], (self)->shape[2]}, .stride={(self)->stride}} -// cspan_submdN: return reduced rank +// 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) \ @@ -233,7 +233,7 @@ STC_INLINE intptr_t _cspan_slice(int32_t odim[], int32_t ostri[], int* orank, off *= stri[i]; off += a[i][0]; switch (a[i][1]) { - case 0: s *= stri[i]; continue; + 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; } -- cgit v1.2.3 From d2ff84c53aa9bd3857fdf22dcf7cd9398a4780be Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 4 Feb 2023 23:24:54 +0100 Subject: Fix in ccommon.h to make MSVC work again (without -std:c11) + small adjustments. --- docs/cspan_api.md | 4 ++-- include/stc/cbits.h | 4 ++-- include/stc/ccommon.h | 2 +- misc/examples/make.sh | 12 ++++++------ misc/tests/ctest.h | 12 ++++++++---- 5 files changed, 19 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/docs/cspan_api.md b/docs/cspan_api.md index 650260a2..d0463b0a 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -18,8 +18,7 @@ using_cspan3(S, ValueType); // define span types S, S2, S3 with ran using_cspan4(S, ValueType); // define span types S, S2, S3, S4 with ranks 1, 2, 3, 4. ``` ## Methods -Note that `cspan_md()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type -on assignment, but not on initialization of a span variable. All functions are type-safe, and arguments are side-effect safe, except for SpanType arg. which must not have side-effects. +All functions are type-safe, and index arguments are side-effect safe. ```c SpanTypeN cspan_md(ValueType* data, intptr_t xdim, ...); // create a multi-dimensional cspan SpanType cspan_make(T SpanType, {v1, v2, ...}); // make a 1d-dimensional cspan from values @@ -129,6 +128,7 @@ int main() { } ``` ## Example 2 +Slicing cspan without and with reducing the rank (like numpy array slicing): ```c #include #include diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 9f0afc9c..577ecb13 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -60,10 +60,10 @@ int main() { #define _cbits_bytes(n) (_cbits_words(n) * c_sizeof(uint64_t)) #if defined(__GNUC__) || defined(__clang__) - STC_INLINE int cpopcount64(uint64_t x) {return (int)__builtin_popcountll(x);} + STC_INLINE int cpopcount64(uint64_t x) {return __builtin_popcountll(x);} #elif defined(_MSC_VER) && defined(_WIN64) #include - STC_INLINE int cpopcount64(uint64_t x) {return __popcnt64(x);} + STC_INLINE int cpopcount64(uint64_t x) {return (int)__popcnt64(x);} #else STC_INLINE int cpopcount64(uint64_t x) { /* http://en.wikipedia.org/wiki/Hamming_weight */ x -= (x >> 1) & 0x5555555555555555; diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 30184407..b8ea6316 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -52,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__ diff --git a/misc/examples/make.sh b/misc/examples/make.sh index 0c4760a6..19a53880 100755 --- a/misc/examples/make.sh +++ b/misc/examples/make.sh @@ -6,14 +6,14 @@ if [ "$(uname)" = 'Linux' ]; then oflag='-o ' fi +cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings" #cc=gcc; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall $sanitize" -cc=gcc; cflags="-s -O2 -std=c99 -Wconversion -Wpedantic -Wall -Wsign-compare -Wwrite-strings" # -Wconversion #cc=tcc; cflags="-Wall -std=c99" #cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -Wsign-compare -Wwrite-strings" #cc=clang; cflags="-s -O2 -std=c99 -Werror -Wfatal-errors -Wpedantic -Wall -DSTC_CSTR_V1 -DSTC_CSMAP_V1" #cc=gcc; cflags="-x c++ -s -O2 -Wall -std=c++20" #cc=g++; cflags="-x c++ -s -O2 -Wall" -#cc=cl; cflags="-O2 -nologo -W2 -MD" +#cc=cl; cflags="-O2 -nologo -W3 -MD" #cc=cl; cflags="-nologo -TP" #cc=cl; cflags="-nologo -std:c11" @@ -40,13 +40,13 @@ fi if [ $run = 0 ] ; then for i in *.c ; do - echo $comp -I../include $i $clibs $oflag$(basename $i .c).exe - $comp -I../include $i $clibs $oflag$(basename $i .c).exe + echo $comp -I../../include $i $clibs $oflag$(basename $i .c).exe + $comp -I../../include $i $clibs $oflag$(basename $i .c).exe done else for i in *.c ; do - echo $comp -I../include $i $clibs - $comp -I../include $i $clibs + echo $comp -I../../include $i $clibs + $comp -I../../include $i $clibs if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi if [ -f ./a.exe ]; then ./a.exe; fi if [ -f ./a.out ]; then ./a.out; fi diff --git a/misc/tests/ctest.h b/misc/tests/ctest.h index 4d42216c..373cda0e 100644 --- a/misc/tests/ctest.h +++ b/misc/tests/ctest.h @@ -93,8 +93,10 @@ struct ctest { #ifdef __APPLE__ #define CTEST_IMPL_SECTION __attribute__ ((used, section ("__DATA, .ctest"), aligned(1))) -#else +#elif !defined _MSC_VER #define CTEST_IMPL_SECTION __attribute__ ((used, section (".ctest"), aligned(1))) +#else +#define CTEST_IMPL_SECTION #endif #define CTEST_IMPL_STRUCT(sname, tname, tskip, tdata, tsetup, tteardown) \ @@ -445,7 +447,7 @@ static void sighandler(int signum) const char msg_nocolor[] = "[SIGSEGV: Segmentation fault]\n"; const char* msg = color_output ? msg_color : msg_nocolor; - intptr_t n = write(STDOUT_FILENO, msg, (unsigned int)strlen(msg)); + intptr_t n = write(1, msg, (unsigned int)strlen(msg)); (void)n; /* "Unregister" the signal handler and send the signal back to the process * so it can terminate as expected */ @@ -457,8 +459,10 @@ static void sighandler(int signum) #endif int ctest_main(int argc, const char *argv[]); - -__attribute__((no_sanitize_address)) int ctest_main(int argc, const char *argv[]) +#ifdef __GNUC__ +__attribute__((no_sanitize_address)) +#endif +int ctest_main(int argc, const char *argv[]) { static int total = 0; static int num_ok = 0; -- cgit v1.2.3