diff options
| author | tylov <[email protected]> | 2023-07-12 19:39:59 +0200 |
|---|---|---|
| committer | tylov <[email protected]> | 2023-07-12 21:01:46 +0200 |
| commit | ebe5abc29d51c643520301e42124365477f44957 (patch) | |
| tree | f1188f5c649f4244c384484ae584894280396272 /include | |
| parent | 715a02ba8155de2f7d446e8d7d2ae305c27996b9 (diff) | |
| download | STC-modified-ebe5abc29d51c643520301e42124365477f44957.tar.gz STC-modified-ebe5abc29d51c643520301e42124365477f44957.zip | |
Changed docs and examples to use i_key* template parameters instead of i_val* for all non-maps.
Renamed c_ASSERT() to c_assert() and added optional message parameter to c_static_assert().
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/algo/sort.h | 21 | ||||
| -rw-r--r-- | include/stc/cbits.h | 8 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 14 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 4 | ||||
| -rw-r--r-- | include/stc/clist.h | 2 | ||||
| -rw-r--r-- | include/stc/cmap.h | 2 | ||||
| -rw-r--r-- | include/stc/cpque.h | 2 | ||||
| -rw-r--r-- | include/stc/cqueue.h | 4 | ||||
| -rw-r--r-- | include/stc/cspan.h | 28 | ||||
| -rw-r--r-- | include/stc/cstack.h | 8 | ||||
| -rw-r--r-- | include/stc/cvec.h | 8 | ||||
| -rw-r--r-- | include/stc/extend.h | 4 |
12 files changed, 56 insertions, 49 deletions
diff --git a/include/stc/algo/sort.h b/include/stc/algo/sort.h index 8365ccc5..01e7d521 100644 --- a/include/stc/algo/sort.h +++ b/include/stc/algo/sort.h @@ -22,13 +22,13 @@ */ /* Generic Quicksort in C, performs as fast as c++ std::sort(). template params: -#define i_val - value type [required] +#define i_key - value type [required] #define i_less - less function. default: *x < *y -#define i_type name - define {{name}}_sort_n(), else {{i_val}}array_sort_n(). +#define i_type name - define {{name}}_sort_n(), else {{i_key}}array_sort_n(). // ex1: #include <stdio.h> -#define i_val int +#define i_key int #include <stc/algo/sort.h> int main() { @@ -42,7 +42,7 @@ int main() { } // ex2: -#define i_val int +#define i_key int #define i_type IDeq #define i_more // retain input template params to be reused by sort.h #include <stc/cdeq.h> @@ -62,13 +62,16 @@ int main() { */ #include "../ccommon.h" +#if !defined i_key && defined i_val + #define i_key i_val +#endif #ifndef i_type #define i_at(arr, idx) (&arr[idx]) #ifndef i_tag - #define i_tag i_val + #define i_tag i_key #endif #define i_type c_PASTE(i_tag, array) - typedef i_val i_type; + typedef i_key i_type; #endif #ifndef i_at #define i_at(arr, idx) _cx_MEMB(_at_mut)(arr, idx) @@ -78,7 +81,7 @@ int main() { static inline void _cx_MEMB(_insertsort_ij)(_cx_Self* arr, intptr_t lo, intptr_t hi) { for (intptr_t j = lo, i = lo + 1; i <= hi; j = i, ++i) { - i_val key = *i_at(arr, i); + i_key key = *i_at(arr, i); while (j >= 0 && (i_less((&key), i_at(arr, j)))) { *i_at(arr, j + 1) = *i_at(arr, j); --j; @@ -90,14 +93,14 @@ static inline void _cx_MEMB(_insertsort_ij)(_cx_Self* arr, intptr_t lo, intptr_t static inline void _cx_MEMB(_sort_ij)(_cx_Self* arr, intptr_t lo, intptr_t hi) { intptr_t i = lo, j; while (lo < hi) { - i_val pivot = *i_at(arr, lo + (hi - lo)*7/16); + i_key pivot = *i_at(arr, lo + (hi - lo)*7/16); j = hi; while (i <= j) { while (i_less(i_at(arr, i), (&pivot))) ++i; while (i_less((&pivot), i_at(arr, j))) --j; if (i <= j) { - c_swap(i_val, i_at(arr, i), i_at(arr, j)); + c_swap(i_key, i_at(arr, i), i_at(arr, j)); ++i; --j; } } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 9463c82c..66bc6354 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -90,7 +90,7 @@ STC_INLINE _llong _cbits_count(const uint64_t* set, const _llong sz) { STC_INLINE char* _cbits_to_str(const uint64_t* set, const _llong sz, char* out, _llong start, _llong stop) { if (stop > sz) stop = sz; - assert(start <= stop); + c_assert(start <= stop); c_memset(out, '0', stop - start); for (_llong i = start; i < stop; ++i) @@ -122,7 +122,7 @@ STC_INLINE bool _cbits_disjoint(const uint64_t* set, const uint64_t* other, cons #if !defined i_capacity // DYNAMIC SIZE BITARRAY -#define _i_assert(x) assert(x) +#define _i_assert(x) c_assert(x) #define i_type cbits typedef struct { uint64_t *data64; _llong _size; } i_type; @@ -216,13 +216,13 @@ 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 _llong size, const bool value) { - assert(size <= i_capacity); + c_assert(size <= i_capacity); i_type set; _i_memb(_set_all)(&set, value); return set; } STC_INLINE i_type _i_memb(_with_pattern)(const _llong size, const uint64_t pattern) { - assert(size <= i_capacity); + c_assert(size <= i_capacity); i_type set; _i_memb(_set_pattern)(&set, pattern); return set; } diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index d6da8734..efbebdc3 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -34,11 +34,6 @@ typedef long long _llong; #define c_NPOS INTPTR_MAX #define c_ZI PRIiPTR #define c_ZU PRIuPTR -#if defined STC_NDEBUG || defined NDEBUG - #define c_ASSERT(expr) (void)(0) -#else - #define c_ASSERT(expr) assert(expr) -#endif #if defined(_MSC_VER) #pragma warning(disable: 4116 4996) // unnamed type definition in parentheses @@ -80,7 +75,14 @@ typedef long long _llong; #define c_free(p) free(p) #define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); free(_tp); } while (0) -#define c_static_assert(b) ((int)(0*sizeof(int[(b) ? 1 : -1]))) +#define c_static_assert(...) c_MACRO_OVERLOAD(c_static_assert, __VA_ARGS__) +#define c_static_assert_1(b) ((int)(0*sizeof(int[(b) ? 1 : -1]))) +#define c_static_assert_2(b, m) c_static_assert_1(b) +#if defined STC_NDEBUG || defined NDEBUG + #define c_assert(expr) ((void)0) +#else + #define c_assert(expr) assert(expr) +#endif #define c_container_of(p, C, m) ((C*)((char*)(1 ? (p) : &((C*)0)->m) - offsetof(C, m))) #define c_const_cast(T, p) ((T)(p) + 0*sizeof((T)0 == (p))) #define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \ diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index bac40f90..056ef005 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -51,13 +51,13 @@ _cx_MEMB(_push_back)(_cx_Self* self, _cx_value val) STC_INLINE void _cx_MEMB(_pop_back)(_cx_Self* self) { - assert(!_cx_MEMB(_empty)(self)); + c_assert(!_cx_MEMB(_empty)(self)); self->end = (self->end - 1) & self->capmask; i_keydrop((self->data + self->end)); } STC_INLINE _cx_value _cx_MEMB(_pull_back)(_cx_Self* self) { // move back out of deq - assert(!_cx_MEMB(_empty)(self)); + c_assert(!_cx_MEMB(_empty)(self)); self->end = (self->end - 1) & self->capmask; return self->data[self->end]; } diff --git a/include/stc/clist.h b/include/stc/clist.h index 38358d73..9cc1bb39 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -145,7 +145,7 @@ 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) { return _cx_MEMB(_push_back)(self, value); } STC_INLINE void _cx_MEMB(_pop_front)(_cx_Self* self) - { assert(!_cx_MEMB(_empty)(self)); _cx_MEMB(_erase_after_node)(self, self->last); } + { c_assert(!_cx_MEMB(_empty)(self)); _cx_MEMB(_erase_after_node)(self, self->last); } 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 _cx_raw _cx_MEMB(_value_toraw)(const _cx_value* pval) { return i_keyto(pval); } diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 21e7b933..513a8b93 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -117,7 +117,7 @@ STC_INLINE bool _cx_MEMB(_contains)(const _cx_Self* self, _cx_keyraw rke STC_INLINE const _cx_mapped* _cx_MEMB(_at)(const _cx_Self* self, _cx_keyraw rkey) { chash_bucket b = _cx_MEMB(_bucket_)(self, &rkey); - assert(b.found); + c_assert(b.found); return &self->data[b.idx].second; } STC_INLINE _cx_mapped* diff --git a/include/stc/cpque.h b/include/stc/cpque.h index cfe027cc..ca51eeff 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -89,7 +89,7 @@ STC_INLINE const _cx_value* _cx_MEMB(_top)(const _cx_Self* self) { return &self->data[0]; } STC_INLINE void _cx_MEMB(_pop)(_cx_Self* self) - { assert(!_cx_MEMB(_empty)(self)); _cx_MEMB(_erase_at)(self, 0); } + { c_assert(!_cx_MEMB(_empty)(self)); _cx_MEMB(_erase_at)(self, 0); } #if !defined i_no_clone STC_API _cx_Self _cx_MEMB(_clone)(_cx_Self q); diff --git a/include/stc/cqueue.h b/include/stc/cqueue.h index e9f1b877..5d38ca89 100644 --- a/include/stc/cqueue.h +++ b/include/stc/cqueue.h @@ -88,13 +88,13 @@ STC_INLINE _cx_value* _cx_MEMB(_back)(const _cx_Self* self) { return self->data + ((self->end - 1) & self->capmask); } STC_INLINE void _cx_MEMB(_pop)(_cx_Self* self) { // pop_front - assert(!_cx_MEMB(_empty)(self)); + c_assert(!_cx_MEMB(_empty)(self)); i_keydrop((self->data + self->start)); self->start = (self->start + 1) & self->capmask; } STC_INLINE _cx_value _cx_MEMB(_pull)(_cx_Self* self) { // move front out of queue - assert(!_cx_MEMB(_empty)(self)); + c_assert(!_cx_MEMB(_empty)(self)); intptr_t s = self->start; self->start = (s + 1) & self->capmask; return self->data[s]; diff --git a/include/stc/cspan.h b/include/stc/cspan.h index 89986d6f..4d091395 100644 --- a/include/stc/cspan.h +++ b/include/stc/cspan.h @@ -84,7 +84,7 @@ int demo2() { const int rank, const int32_t a[][2]) { \ Self s = {.data=v}; int outrank; \ s.data += _cspan_slice(s.shape, s.stride.d, &outrank, shape, stri, rank, a); \ - c_ASSERT(outrank == RANK); \ + c_assert(outrank == RANK); \ return s; \ } \ STC_INLINE Self##_iter Self##_begin(const Self* self) { \ @@ -145,7 +145,6 @@ using_cspan_tuple(7); using_cspan_tuple(8); #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_submd(): Reduce rank (N <= 4) Optimized, same as e.g. cspan_slice(Span2, &ms4, {x}, {y}, {c_ALL}, {c_ALL}); #define cspan_submd2(OutSpan, self, ...) _cspan_submdN(OutSpan, 2, self, __VA_ARGS__) #define cspan_submd3(OutSpan, self, ...) _cspan_submdN(OutSpan, 3, self, __VA_ARGS__) @@ -172,23 +171,22 @@ using_cspan_tuple(7); using_cspan_tuple(8); #define _cspan_submd4_5(ok, self, x, y, z) \ {.data=cspan_at(self, x, y, z, 0) + ok, .shape={(self)->shape[3]}, .stride={.d={(self)->stride.d[3]}}} -#define cspan_md(array, ...) cspan_md_order('C', array, __VA_ARGS__) -#define cspan_md_left(array, ...) cspan_md_order('F', array, __VA_ARGS__) -#define cspan_md_order(order, array, ...) \ +#define cspan_md(array, ...) cspan_md_ordered('C', array, __VA_ARGS__) +#define cspan_md_left(array, ...) cspan_md_ordered('F', array, __VA_ARGS__) +#define cspan_md_ordered(order, array, ...) \ {.data=array, .shape={__VA_ARGS__}, \ .stride=*(c_PASTE(cspan_tuple, c_NUMARGS(__VA_ARGS__))*)_cspan_shape2stride(order, ((int32_t[]){__VA_ARGS__}), c_NUMARGS(__VA_ARGS__))} #define cspan_transpose(self) \ _cspan_transpose((self)->shape, (self)->stride.d, cspan_rank(self)) - // General slicing function; #define cspan_slice(OutSpan, parent, ...) \ OutSpan##_slice_((parent)->data, (parent)->shape, (parent)->stride.d, cspan_rank(parent) + \ c_static_assert(cspan_rank(parent) == sizeof((int32_t[][2]){__VA_ARGS__})/sizeof(int32_t[2])), \ (const int32_t[][2]){__VA_ARGS__}) -// ----------- private definitions ------------ +/* ------------------- PRIVAT DEFINITIONS ------------------- */ // cspan_index() helpers: #define cspan_idx_1 cspan_idx_3 @@ -215,19 +213,19 @@ STC_INLINE void _cspan_transpose(int32_t shape[], int32_t stride[], int rank) { } STC_INLINE intptr_t _cspan_idx1(const int32_t shape[1], const cspan_tuple1 stri, int32_t x) - { c_ASSERT(c_LTu(x, shape[0])); return x; } + { c_assert(c_LTu(x, shape[0])); return (intptr_t)stri.d[0]*x; } STC_INLINE intptr_t _cspan_idx2(const int32_t shape[2], const cspan_tuple2 stri, int32_t x, int32_t y) - { c_ASSERT(c_LTu(x, shape[0]) && c_LTu(y, shape[1])); return (intptr_t)stri.d[0]*x + stri.d[1]*y; } + { c_assert(c_LTu(x, shape[0]) && c_LTu(y, shape[1])); return (intptr_t)stri.d[0]*x + stri.d[1]*y; } STC_INLINE intptr_t _cspan_idx3(const int32_t shape[3], const cspan_tuple3 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])); + c_assert(c_LTu(x, shape[0]) && c_LTu(y, shape[1]) && c_LTu(z, shape[2])); return (intptr_t)stri.d[0]*x + stri.d[1]*y + stri.d[2]*z; } STC_INLINE intptr_t _cspan_idxN(int rank, const int32_t shape[], const int32_t stride[], const int32_t a[]) { intptr_t off = 0; while (rank--) { - c_ASSERT(c_LTu(a[rank], shape[rank])); + c_assert(c_LTu(a[rank], shape[rank])); off += stride[rank]*a[rank]; } return off; @@ -239,6 +237,8 @@ STC_API intptr_t _cspan_next2(int32_t pos[], const int32_t shape[], const int32_ #define _cspan_next4 _cspan_next2 #define _cspan_next5 _cspan_next2 #define _cspan_next6 _cspan_next2 +#define _cspan_next7 _cspan_next2 +#define _cspan_next8 _cspan_next2 STC_API intptr_t _cspan_slice(int32_t oshape[], int32_t ostride[], int* orank, const int32_t shape[], const int32_t stride[], @@ -247,7 +247,7 @@ STC_API intptr_t _cspan_slice(int32_t oshape[], int32_t ostride[], int* orank, STC_API int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank); #endif // STC_CSPAN_H_INCLUDED -/* -------------------------- IMPLEMENTATION ------------------------- */ +/* --------------------- IMPLEMENTATION --------------------- */ #if defined(i_implement) || defined(i_static) STC_DEF int32_t* _cspan_shape2stride(char order, int32_t shape[], int rank) { @@ -283,13 +283,13 @@ STC_DEF intptr_t _cspan_slice(int32_t oshape[], int32_t ostride[], int* orank, for (; i < rank; ++i) { off += stride[i]*a[i][0]; switch (a[i][1]) { - case 0: c_ASSERT(c_LTu(a[i][0], shape[i])); continue; + case 0: c_assert(c_LTu(a[i][0], shape[i])); continue; case -1: end = shape[i]; break; default: end = a[i][1]; } oshape[oi] = end - a[i][0]; ostride[oi] = stride[i]; - c_ASSERT(c_LTu(0, oshape[oi]) & !c_LTu(shape[i], end)); + c_assert(c_LTu(0, oshape[oi]) & !c_LTu(shape[i], end)); ++oi; } *orank = oi; diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 24ec2d5f..f8640ed1 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -129,10 +129,10 @@ STC_INLINE _cx_value* _cx_MEMB(_push)(_cx_Self* self, _cx_value val) { } STC_INLINE void _cx_MEMB(_pop)(_cx_Self* self) - { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } + { c_assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } STC_INLINE _cx_value _cx_MEMB(_pull)(_cx_Self* self) - { assert(self->_len); return self->data[--self->_len]; } + { c_assert(self->_len); return self->data[--self->_len]; } 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++)); } @@ -141,9 +141,9 @@ STC_INLINE _cx_Self _cx_MEMB(_from_n)(const _cx_raw* raw, intptr_t n) { _cx_Self cx = {0}; _cx_MEMB(_put_n)(&cx, raw, n); return cx; } STC_INLINE const _cx_value* _cx_MEMB(_at)(const _cx_Self* self, intptr_t idx) - { assert(idx < self->_len); return self->data + idx; } + { c_assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_value* _cx_MEMB(_at_mut)(_cx_Self* self, intptr_t idx) - { assert(idx < self->_len); return self->data + idx; } + { c_assert(idx < self->_len); return self->data + idx; } #if !defined i_no_emplace STC_INLINE _cx_value* _cx_MEMB(_emplace)(_cx_Self* self, _cx_raw raw) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index e1d34365..9b95306e 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -133,9 +133,9 @@ STC_INLINE _cx_value* _cx_MEMB(_front)(const _cx_Self* self) { return self->da STC_INLINE _cx_value* _cx_MEMB(_back)(const _cx_Self* self) { return self->data + self->_len - 1; } STC_INLINE void _cx_MEMB(_pop)(_cx_Self* self) - { assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } + { c_assert(self->_len); _cx_value* p = &self->data[--self->_len]; i_keydrop(p); } STC_INLINE _cx_value _cx_MEMB(_pull)(_cx_Self* self) - { assert(self->_len); return self->data[--self->_len]; } + { c_assert(self->_len); return self->data[--self->_len]; } STC_INLINE _cx_value* _cx_MEMB(_push_back)(_cx_Self* self, i_key value) { return _cx_MEMB(_push)(self, value); } STC_INLINE void _cx_MEMB(_pop_back)(_cx_Self* self) { _cx_MEMB(_pop)(self); } @@ -182,11 +182,11 @@ _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 intptr_t idx) { - assert(idx < self->_len); return self->data + idx; + c_assert(idx < self->_len); return self->data + idx; } STC_INLINE _cx_value* _cx_MEMB(_at_mut)(_cx_Self* self, const intptr_t idx) { - assert(idx < self->_len); return self->data + idx; + c_assert(idx < self->_len); return self->data + idx; } diff --git a/include/stc/extend.h b/include/stc/extend.h index c0a00ff8..52d59414 100644 --- a/include/stc/extend.h +++ b/include/stc/extend.h @@ -43,8 +43,10 @@ #define _i_val i_val #endif -#ifdef _i_key +#if defined _i_key && defined _i_val c_PASTE(forward_, i_base)(i_type, _i_key, _i_val); +#elif defined _i_key + c_PASTE(forward_, i_base)(i_type, _i_key); #else c_PASTE(forward_, i_base)(i_type, _i_val); #endif |
