From 6e65e30c6e3c2bc271d3885e749ceb0289bbd9cf Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 22 Dec 2021 08:49:19 +0100 Subject: Changed the c_apply() and c_apply_pair() to one new c_apply() API. Added c_pair(v) for convenience. --- include/stc/cbox.h | 33 ++++++++++++++++++++------------- include/stc/ccommon.h | 28 +++++++++++++++++++++------- include/stc/csptr.h | 36 ++++++++++++++++++++---------------- include/stc/template.h | 20 ++++++++++++-------- 4 files changed, 73 insertions(+), 44 deletions(-) (limited to 'include') diff --git a/include/stc/cbox.h b/include/stc/cbox.h index bcd9bd8c..37687134 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -86,10 +86,15 @@ STC_INLINE _cx_self _cx_memb(_from_ptr)(i_val* p) { return c_make(_cx_self){p}; } STC_INLINE _cx_self -_cx_memb(_new)(i_val val) { +_cx_memb(_from)(i_val val) { return c_make(_cx_self){c_new(i_val, val)}; } +STC_INLINE i_val +_cx_memb(_toraw)(const _cx_self* self) { + return *self->get; +} + // destructor STC_INLINE void _cx_memb(_drop)(_cx_self* self) { @@ -109,16 +114,18 @@ _cx_memb(_reset)(_cx_self* self) { // take ownership of val STC_INLINE void -_cx_memb(_reset_new)(_cx_self* self, i_val val) { +_cx_memb(_reset_from)(_cx_self* self, i_val val) { if (self->get) { i_valdrop(self->get); *self->get = val; } else self->get = c_new(i_val, val); } -#if !c_option(c_no_clone) +#if !c_option(c_no_clone) + //#ifndef _i_valraw_default STC_INLINE _cx_self - _cx_memb(_from)(i_valraw raw) { + _cx_memb(_new)(i_valraw raw) { return c_make(_cx_self){c_new(i_val, i_valfrom(raw))}; } + //#endif STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) { @@ -141,29 +148,29 @@ _cx_memb(_take)(_cx_self* self, _cx_self other) { } STC_INLINE uint64_t -_cx_memb(_hash)(const _cx_self* self, size_t n) { +_cx_memb(_value_hash)(const _cx_value* x, size_t n) { #if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX - return c_hash64(&self->get, 8); + return c_hash64(&x, 8); #elif c_option(c_no_cmp) - return c_hash32(&self->get, 4); + return c_hash32(&x, 4); #else - i_valraw rx = i_valto(self->get); + i_valraw rx = i_valto(x); return i_hash(&rx, sizeof rx); #endif } STC_INLINE int -_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) - return c_default_cmp(&x->get, &y->get); + return c_default_cmp(&x, &y); #else - i_valraw rx = i_valto(x->get), ry = i_valto(x->get); + i_valraw rx = i_valto(x), ry = i_valto(x); return i_cmp(&rx, &ry); #endif } STC_INLINE bool -_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) { - return !_cx_memb(_cmp)(x, y); +_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { + return !_cx_memb(_value_cmp)(x, y); } #include "template.h" diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 9cbe24a4..e11fa3be 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -189,23 +189,37 @@ STC_INLINE uint64_t c_default_hash(const void* key, size_t len) { *b = (n)*sizeof *b > (BYTES) ? c_alloc_n(type, n) : _c_b \ ; b; b != _c_b ? c_free(b) : (void)0, b = NULL) -#define c_apply(C, method, cx, ...) do { \ +#define c_apply(v, method, T, ...) do { \ + const T _c_arr[] = __VA_ARGS__; \ + for (size_t index = 0; index < c_arraylen(_c_arr); ++index) \ + { const T v = _c_arr[index]; method; } \ +} while (0) +#define c_apply_arr(v, method, T, arr, n) do { \ + const T* _c_arr = arr; size_t _n = n; \ + for (size_t index = 0; index < _n; ++index) \ + { const T v = _c_arr[index]; method; } \ +} while (0) +#define c_apply_it(v, method, C, ...) do { \ + size_t index = 0; \ + c_foreach (_it, C, __VA_ARGS__) \ + { const C##_value v = *_it.ref; method; ++index; } \ +} while (0) +#define c_pair(v) (v).first, (v).second + +// [deprecated] +#define c_apply_OLD(C, method, cx, ...) do { \ const C##_rawvalue _c_arr[] = __VA_ARGS__; \ C* _c_cx = cx; \ for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \ C##_##method(_c_cx, _c_arr[_c_i]); \ } while (0) -#define c_apply_pair(C, method, cx, ...) do { \ +// [deprecated] +#define c_apply_pair_OLD(C, method, cx, ...) do { \ const C##_rawvalue _c_arr[] = __VA_ARGS__; \ C* _c_cx = cx; \ for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \ C##_##method(_c_cx, _c_arr[_c_i].first, _c_arr[_c_i].second); \ } while (0) -#define c_apply_n(C, method, cx, arr, n) do { \ - C* _c_cx = cx; \ - for (const C##_rawvalue *_c_i = arr, *_c_end = _c_i+(n); _c_i != _c_end; ++_c_i) \ - C##_##method(_c_cx, *_c_i); \ -} while (0) #define c_drop(C, ...) do { \ C* _c_arr[] = {__VA_ARGS__}; \ diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 92ed05ef..4e9b4687 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -104,13 +104,17 @@ _cx_memb(_from_ptr)(_cx_value* p) { } STC_INLINE _cx_self -_cx_memb(_new)(i_val val) { +_cx_memb(_from)(i_val val) { _cx_self ptr; _cx_csptr_rep *rep = c_alloc(_cx_csptr_rep); *(ptr.use_count = &rep->counter) = 1; *(ptr.get = &rep->value) = val; return ptr; } +STC_INLINE i_val _cx_memb(_toraw)(const _cx_self* self) { + return *self->get; +} + STC_INLINE _cx_self _cx_memb(_move)(_cx_self* self) { _cx_self ptr = *self; @@ -135,15 +139,15 @@ _cx_memb(_reset)(_cx_self* self) { } STC_INLINE void -_cx_memb(_reset_new)(_cx_self* self, i_val val) { +_cx_memb(_reset_from)(_cx_self* self, i_val val) { _cx_memb(_drop)(self); - *self = _cx_memb(_new)(val); + *self = _cx_memb(_from)(val); } -#if !c_option(c_no_clone) - STC_INLINE _cx_self _cx_memb(_from)(i_valraw raw) { - return _cx_memb(_new)(i_valfrom(raw)); -} +#if !c_option(c_no_clone) && !defined _i_valraw_default + STC_INLINE _cx_self _cx_memb(_new)(i_valraw raw) { + return _cx_memb(_from)(i_valfrom(raw)); + } #endif // does not use i_valfrom, so we can bypass c_no_clone @@ -167,30 +171,30 @@ _cx_memb(_take)(_cx_self* self, _cx_self ptr) { } STC_INLINE uint64_t -_cx_memb(_hash)(const _cx_self* self, size_t n) { +_cx_memb(_value_hash)(const _cx_value* x, size_t n) { #if c_option(c_no_cmp) && UINTPTR_MAX == UINT64_MAX - return c_hash64(&self->get, 8); + return c_hash64(&x, 8); #elif c_option(c_no_cmp) - return c_hash32(&self->get, 4); + return c_hash32(&x, 4); #else - i_valraw rx = i_valto(self->get); + i_valraw rx = i_valto(x); return i_hash(&rx, sizeof rx); #endif } STC_INLINE int -_cx_memb(_cmp)(const _cx_self* x, const _cx_self* y) { +_cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { #if c_option(c_no_cmp) - return c_default_cmp(&x->get, &y->get); + return c_default_cmp(&x, &y); #else - i_valraw rx = i_valto(x->get), ry = i_valto(x->get); + i_valraw rx = i_valto(x), ry = i_valto(x); return i_cmp(&rx, &ry); #endif } STC_INLINE bool -_cx_memb(_eq)(const _cx_self* x, const _cx_self* y) { - return !_cx_memb(_cmp)(x, y); +_cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { + return !_cx_memb(_value_cmp)(x, y); } #undef _i_atomic_inc #undef _i_atomic_dec_and_test diff --git a/include/stc/template.h b/include/stc/template.h index 83a362d7..675baa47 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -40,14 +40,6 @@ #define _cx_size _cx_memb(_size_t) #endif -#if defined i_key_csptr || defined i_key_ref // [deprecated] - #define i_key_bind i_key_csptr - #error "i_key_csptr/ref no longer supported: use new name i_key_bind" -#endif -#if defined i_val_csptr || defined i_val_ref // [deprecated] - #define i_val_bind i_val_csptr - #error "i_val_sptr/ref no longer supported: use new name i_val_bind" -#endif #if defined i_cnt || defined i_equ // [deprecated] #define i_type i_cnt #error "i_cnt and i_equ no longer supported: use new name i_type / i_eq" @@ -69,6 +61,9 @@ #ifndef i_tag #define i_tag str #endif +#elif defined i_key_ref + #define i_key_bind i_key_ref + #define i_keyraw c_PASTE(i_key_ref, _value) #endif #ifdef i_key_bind @@ -125,6 +120,9 @@ #if !defined i_tag && !defined i_key #define i_tag str #endif +#elif defined i_val_ref + #define i_val_bind i_val_ref + #define i_valraw c_PASTE(i_val_ref, _value) #endif #ifdef i_val_bind @@ -167,6 +165,7 @@ #define i_keyfrom c_default_clone #endif #ifndef i_keyraw + #define _i_keyraw_default #define i_keyraw i_key #define i_keyto c_default_toraw #endif @@ -192,6 +191,7 @@ #define i_valfrom c_default_clone #endif #ifndef i_valraw + #define _i_valraw_default #define i_valraw i_val #define i_valto c_default_toraw #endif @@ -219,6 +219,7 @@ #undef i_val #undef i_val_str +#undef i_val_ref #undef i_val_bind #undef i_valraw #undef i_valfrom @@ -227,6 +228,7 @@ #undef i_key #undef i_key_str +#undef i_key_ref #undef i_key_bind #undef i_keyraw #undef i_keyfrom @@ -234,6 +236,8 @@ #undef i_keydrop #undef _i_prefix +#undef _i_valraw_default +#undef _i_keyraw_default #undef _i_has_internal_clone #undef _i_template #endif -- cgit v1.2.3