From c52b96019b66bbff1ac41d8a1333092a13dbd45a Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 2 Jun 2021 15:24:14 +0200 Subject: - Backported csmap_v1.h (with malloced nodes) to match csmap. - Updated csmap/cmap to separate out types in order to later have incomplete values support. - Added incomplete value support to csptr. I question the need because recursive structures should not use shared_ptr. - Added c_forinitdel() macro, support up to 3 variables. --- include/stc/ccommon.h | 10 +++++++++- include/stc/cmap.h | 41 +++++++++++++++++++++++----------------- include/stc/csmap.h | 52 +++++++++++++++++++++++++++++---------------------- include/stc/csptr.h | 12 +++++++++--- include/stc/cvec.h | 3 --- 5 files changed, 72 insertions(+), 46 deletions(-) (limited to 'include') diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index a650d6d8..ba00659e 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -67,6 +67,7 @@ ((type *)((char *)(ptr) - offsetof(type, member))) #define c_struct(S) typedef struct S S; struct S +#define c_arg(...) __VA_ARGS__ #if __cplusplus #define c_new(T) static_cast(c_malloc(sizeof(T))) #define c_new_n(T, n) static_cast(c_malloc(sizeof(T)*(n))) @@ -104,6 +105,9 @@ #define c_default_del(ptr) ((void) (ptr)) +#define c_true(...) __VA_ARGS__ +#define c_false(...) + /* Generic algorithms */ #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) @@ -124,10 +128,14 @@ ; (i <= _c_end) == (0 < _c_inc); i += _c_inc) #define c_forvar(declvar, ...) for (declvar, *_c_ii = NULL; !_c_ii; ++_c_ii, __VA_ARGS__) -#define c_arg(...) __VA_ARGS__ #define c_forscope(start, end) for (int _c_ii = (start, 0); !_c_ii; ++_c_ii, end) #define c_fordefer(...) for (int _c_ii = 0; !_c_ii; ++_c_ii, __VA_ARGS__) +#define c_forinitdel(...) c_MACRO_OVERLOAD(c_forinitdel, __VA_ARGS__) +#define c_forinitdel_2(CX, a) c_forvar(CX a = CX##_init(), CX##_del(&a)) +#define c_forinitdel_3(CX, a, b) c_forvar(c_arg(CX a = CX##_init(), b = CX##_init()), CX##_del(&b), CX##_del(&a)) +#define c_forinitdel_4(CX, a, b, c) c_forvar(c_arg(CX a = CX##_init(), b = CX##_init(), c = CX##_init()), CX##_del(&c), CX##_del(&b), CX##_del(&a)) + #define c_forbuffer(b, type, n) c_forbuffer_N(b, type, n, 256) #define c_forbuffer_N(b, type, n, BYTES) \ for (type _c_b[((BYTES) - 1) / sizeof(type) + 1], \ diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 8c226aa2..7db8ec8c 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -146,41 +146,48 @@ STC_INLINE uint64_t c_default_hash32(const void* data, size_t ignored) STC_INLINE uint64_t c_default_hash64(const void* data, size_t ignored) {return *(const uint64_t *)data * 0xc6a4a7935bd1e99d;} -#define _c_using_chash(CX, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ - mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ - keyDel, keyFromRaw, keyToRaw, RawKey) \ +#define _c_chash_types(CX, C, Key, Mapped) \ typedef Key CX##_key_t; \ typedef Mapped CX##_mapped_t; \ - typedef RawKey CX##_rawkey_t; \ - typedef RawMapped CX##_rawmapped_t; \ typedef CMAP_SIZE_T CX##_size_t; \ \ typedef SET_ONLY_##C( const CX##_key_t ) \ - MAP_ONLY_##C( struct {const CX##_key_t first; \ - Mapped second;} ) \ + MAP_ONLY_##C( struct CX##_value_t ) \ CX##_value_t; \ -\ - typedef SET_ONLY_##C( RawKey ) \ - MAP_ONLY_##C( struct {RawKey first; \ - RawMapped second;} ) \ - CX##_rawvalue_t; \ \ typedef struct { \ CX##_value_t *ref; \ bool inserted; \ } CX##_result_t; \ +\ + typedef struct { \ + CX##_value_t *ref; \ + uint8_t* _hx; \ + } CX##_iter_t; \ \ typedef struct { \ CX##_value_t* table; \ uint8_t* _hashx; \ CX##_size_t size, bucket_count; \ float max_load_factor; \ - } CX; \ + } CX + +#define _c_using_chash(CX, C, Key, Mapped, keyEqualsRaw, keyHashRaw, \ + mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ + keyDel, keyFromRaw, keyToRaw, RawKey) \ + _c_chash_types(CX, C, Key, Mapped); \ \ - typedef struct { \ - CX##_value_t *ref; \ - uint8_t* _hx; \ - } CX##_iter_t; \ + MAP_ONLY_##C( struct CX##_value_t { \ + const CX##_key_t first; \ + CX##_mapped_t second; \ + }; ) \ +\ + typedef RawKey CX##_rawkey_t; \ + typedef RawMapped CX##_rawmapped_t; \ + typedef SET_ONLY_##C( RawKey ) \ + MAP_ONLY_##C( struct { RawKey first; \ + RawMapped second; } ) \ + CX##_rawvalue_t; \ \ STC_API CX CX##_with_capacity(size_t cap); \ STC_API CX CX##_clone(CX map); \ diff --git a/include/stc/csmap.h b/include/stc/csmap.h index 336280bf..4f6780fa 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -133,40 +133,21 @@ int main(void) { struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; #define _csmap_rep(self) c_container_of((self)->nodes, struct csmap_rep, nodes) - -#define _c_using_aatree(CX, C, Key, Mapped, keyCompareRaw, \ - mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ - keyDel, keyFromRaw, keyToRaw, RawKey) \ +#define _c_aatree_types(CX, C, Key, Mapped) \ typedef Key CX##_key_t; \ typedef Mapped CX##_mapped_t; \ - typedef RawKey CX##_rawkey_t; \ - typedef RawMapped CX##_rawmapped_t; \ typedef CSMAP_SIZE_T CX##_size_t; \ \ typedef SET_ONLY_##C( const CX##_key_t ) \ - MAP_ONLY_##C( struct {const CX##_key_t first; \ - CX##_mapped_t second;} ) \ + MAP_ONLY_##C( struct CX##_value_t ) \ CX##_value_t; \ -\ - typedef SET_ONLY_##C( RawKey ) \ - MAP_ONLY_##C( struct {RawKey first; \ - RawMapped second;} ) \ - CX##_rawvalue_t; \ \ typedef struct { \ CX##_value_t *ref; \ bool inserted; \ } CX##_result_t; \ \ - typedef struct CX##_node { \ - CX##_size_t link[2]; \ - int8_t level; \ - CX##_value_t value; \ - } CX##_node_t; \ -\ - typedef struct { \ - CX##_node_t *nodes; \ - } CX; \ + typedef struct CX##_node_t CX##_node_t; \ \ typedef struct { \ CX##_value_t *ref; \ @@ -174,6 +155,33 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; int _top; \ CX##_size_t _tn, _st[36]; \ } CX##_iter_t; \ +\ + typedef struct { \ + CX##_node_t *nodes; \ + } CX + +#define _c_using_aatree(CX, C, Key, Mapped, keyCompareRaw, \ + mappedDel, mappedFromRaw, mappedToRaw, RawMapped, \ + keyDel, keyFromRaw, keyToRaw, RawKey) \ + _c_aatree_types(CX, C, Key, Mapped); \ +\ + MAP_ONLY_##C( struct CX##_value_t { \ + const CX##_key_t first; \ + CX##_mapped_t second; \ + }; ) \ +\ + struct CX##_node_t { \ + CX##_size_t link[2]; \ + int8_t level; \ + CX##_value_t value; \ + }; \ +\ + typedef RawKey CX##_rawkey_t; \ + typedef RawMapped CX##_rawmapped_t; \ + typedef SET_ONLY_##C( RawKey ) \ + MAP_ONLY_##C( struct { RawKey first; \ + RawMapped second; } ) \ + CX##_rawvalue_t; \ \ STC_API CX CX##_init(void); \ STC_API CX CX##_clone(CX tree); \ diff --git a/include/stc/csptr.h b/include/stc/csptr.h index 66ae5240..6e682360 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -91,16 +91,22 @@ typedef long atomic_count_t; #define using_csptr_3(X, Value, valueCompare) \ using_csptr_4(X, Value, valueCompare, c_default_del) #define using_csptr_4(X, Value, valueCompare, valueDel) \ - _c_using_csptr(csptr_##X, Value, valueCompare, valueDel) + _c_using_csptr(csptr_##X, Value, valueCompare, valueDel, c_true) +#define using_csptr_5(X, Value, valueCompare, valueDel, defineTypes) \ + _c_using_csptr(csptr_##X, Value, valueCompare, valueDel, defineTypes) +#define forward_csptr(X, Value) _csptr_types(csptr_##X, Value) -#define _c_using_csptr(CX, Value, valueCompare, valueDel) \ +#define _csptr_types(CX, Value) \ typedef Value CX##_value_t; \ \ typedef struct { \ CX##_value_t* get; \ atomic_count_t* use_count; \ - } CX; \ + } CX + +#define _c_using_csptr(CX, Value, valueCompare, valueDel, defineTypes) \ + defineTypes( _csptr_types(CX, Value); ) \ \ STC_INLINE CX \ CX##_from(CX##_value_t* p) { \ diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 02537081..26287aeb 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -50,9 +50,6 @@ struct cvec_rep { size_t size, cap; void* data[]; }; #define _cvec_rep(self) c_container_of((self)->data, struct cvec_rep, data) -#define c_true(...) __VA_ARGS__ -#define c_false(...) - #define _cvec_types(CX, Value) \ typedef Value CX##_value_t; \ typedef struct { CX##_value_t *ref; } CX##_iter_t; \ -- cgit v1.2.3