diff options
| -rw-r--r-- | docs/cspan_api.md | 12 | ||||
| -rw-r--r-- | include/stc/carc.h | 2 | ||||
| -rw-r--r-- | include/stc/cbits.h | 4 | ||||
| -rw-r--r-- | include/stc/cbox.h | 4 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 6 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 8 | ||||
| -rw-r--r-- | include/stc/clist.h | 6 | ||||
| -rw-r--r-- | include/stc/cmap.h | 8 | ||||
| -rw-r--r-- | include/stc/cpque.h | 2 | ||||
| -rw-r--r-- | include/stc/crandom.h | 4 | ||||
| -rw-r--r-- | include/stc/csmap.h | 4 | ||||
| -rw-r--r-- | include/stc/cspan.h | 62 | ||||
| -rw-r--r-- | include/stc/cstack.h | 4 | ||||
| -rw-r--r-- | include/stc/cstr.h | 16 | ||||
| -rw-r--r-- | include/stc/csview.h | 8 | ||||
| -rw-r--r-- | include/stc/cvec.h | 25 | ||||
| -rw-r--r-- | misc/archived/csmap.h | 4 | ||||
| -rw-r--r-- | misc/archived/cstr.h | 4 | ||||
| -rw-r--r-- | misc/examples/csmap_find.c | 6 | ||||
| -rw-r--r-- | misc/examples/multidim.c | 24 | ||||
| -rw-r--r-- | misc/examples/printspan.c | 2 | ||||
| -rw-r--r-- | misc/examples/vikings.c | 4 | ||||
| -rw-r--r-- | misc/tests/cspan_test.c | 21 | ||||
| -rw-r--r-- | misc/tests/ctest.h | 11 |
24 files changed, 142 insertions, 109 deletions
diff --git a/docs/cspan_api.md b/docs/cspan_api.md index d1532d7b..6da2acca 100644 --- a/docs/cspan_api.md +++ b/docs/cspan_api.md @@ -10,12 +10,12 @@ See the c++ classes [std::span](https://en.cppreference.com/w/cpp/container/span ```c #include <stc/cspan.h> -use_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: -use_cspan2(S, ValueType); // define span types S, S2 with ranks 1, 2. -use_cspan3(S, ValueType); // define span types S, S2, S3 with ranks 1, 2, 3. -use_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_multidim()`, `cmake_from*()`, `cspan_atN()`, `and cspan_subspanN()` require a (safe) cast to its span-type @@ -66,7 +66,7 @@ void SpanType_next(SpanTypeN_iter* it); #define i_val float #include <stc/cstack.h> -use_cspan3(Span, float); // Shorthand to define span types Span, Span2, and Span3. +using_cspan3(Span, float); // Shorthand to define span types Span, Span2, and Span3. int main() { 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<i_key>(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<T*>(c_MALLOC(sizeof(T))) #define c_ALLOC_N(T, n) static_cast<T*>(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/cspan.h b/include/stc/cspan.h index 03307288..fabefc06 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -25,8 +25,8 @@ #include <stdio.h> #include <stc/cspan.h> #include <stc/algo/filter.h> -use_cspan(Span2f, float, 2); -use_cspan(Intspan, int, 1); +using_cspan(Span2f, float, 2); +using_cspan(Intspan, int, 1); int demo1() { float raw[4*5]; @@ -60,10 +60,14 @@ int demo2() { #include "ccommon.h" -#define use_cspan(Self, T, RANK) \ +#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}}; \ @@ -81,14 +85,16 @@ int demo2() { { if (++it->ref == it->end) it->ref = NULL; } \ struct stc_nostruct -#define use_cspan2(Self, T) use_cspan(Self, T, 1); use_cspan(Self##2, T, 2) -#define use_cspan3(Self, T) use_cspan2(Self, T); use_cspan(Self##3, T, 3) -#define use_cspan4(Self, T) use_cspan3(Self, T); use_cspan(Self##4, T, 4) - -#define cspan_rank_ok(self, rank) c_static_assert(cspan_rank(self) == rank) +#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 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/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..2ff2c58a 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 @@ -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 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/csmap_find.c b/misc/examples/csmap_find.c index a94348f8..a61a47be 100644 --- a/misc/examples/csmap_find.c +++ b/misc/examples/csmap_find.c @@ -16,7 +16,7 @@ void print_elem(csmap_istr_raw p) { printf("(%d, %s) ", p.first, p.second); } -#define use_print_collection(CX) \ +#define using_print_collection(CX) \ void print_collection_##CX(const CX* t) { \ printf("%" c_ZU " elements: ", CX##_size(t)); \ \ @@ -26,8 +26,8 @@ void print_elem(csmap_istr_raw p) { puts(""); \ } -use_print_collection(csmap_istr) -use_print_collection(cvec_istr) +using_print_collection(csmap_istr) +using_print_collection(cvec_istr) void findit(csmap_istr c, csmap_istr_key val) { diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c index 37360516..d69a04fb 100644 --- a/misc/examples/multidim.c +++ b/misc/examples/multidim.c @@ -4,20 +4,32 @@ #include <stc/cspan.h> #include <stdio.h> -use_cspan3(ispan, int); +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/examples/printspan.c b/misc/examples/printspan.c index a27d8bbd..82b54367 100644 --- a/misc/examples/printspan.c +++ b/misc/examples/printspan.c @@ -12,7 +12,7 @@ #include <stc/cset.h> #include <stc/cspan.h> -use_cspan(intspan, int, 1); +using_cspan(intspan, int, 1); void printMe(intspan container) { printf("%d:", (int)cspan_size(&container)); 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: diff --git a/misc/tests/cspan_test.c b/misc/tests/cspan_test.c new file mode 100644 index 00000000..8d6d47c2 --- /dev/null +++ b/misc/tests/cspan_test.c @@ -0,0 +1,21 @@ +#include <stdio.h> +#include <stc/cspan.h> +#include "ctest.h" + +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); + + for (size_t i = 0; i < m.dim[0]; ++i) { + intspan2 sub_i = cspan_at3(&m, i); + for (size_t j = 0; j < m.dim[1]; ++j) { + intspan sub_i_j = cspan_at2(&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)); + } + } + } +} 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 <typename T> void CTEST_IMPL_SETUP_FNAME(sname)(T* data) { } \ - template <typename T> void CTEST_IMPL_TEARDOWN_FNAME(sname)(T* data) { } \ + template <typename T> void CTEST_IMPL_SETUP_FNAME(sname)(T* self) { } \ + template <typename T> 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, |
