From 5f80d1cd671d8d03ae1100df2df015a130f75bed Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sat, 10 Apr 2021 20:07:47 +0200 Subject: A few internal members renamed. --- stc/cbits.h | 57 ++++++++++++++++++++++++++++++--------------------------- stc/cdeq.h | 33 +++++++++++++++------------------ stc/clist.h | 16 ++++++++-------- stc/csmap.h | 20 ++++++++++---------- stc/cvec.h | 23 ++++++++++------------- 5 files changed, 73 insertions(+), 76 deletions(-) diff --git a/stc/cbits.h b/stc/cbits.h index 5a0e11aa..0da46f11 100644 --- a/stc/cbits.h +++ b/stc/cbits.h @@ -55,7 +55,10 @@ int main() { #include #include "ccommon.h" -typedef struct cbits { uint64_t* _arr; size_t size; } cbits_t, cbits; +typedef struct cbits { + uint64_t *at64; + size_t size; +} cbits, cbits_t; STC_API cbits_t cbits_with_size(size_t size, bool value); STC_API cbits_t cbits_with_values(size_t size, uint64_t pattern); @@ -70,74 +73,74 @@ STC_API bool cbits_disjoint(cbits_t set, cbits_t other); STC_INLINE cbits_t cbits_init() { cbits_t set = {NULL, 0}; return set; } STC_INLINE void cbits_clear(cbits_t* self) { self->size = 0; } -STC_INLINE void cbits_del(cbits_t* self) { c_free(self->_arr); } +STC_INLINE void cbits_del(cbits_t* self) { c_free(self->at64); } STC_INLINE size_t cbits_size(cbits_t set) { return set.size; } STC_INLINE cbits_t* cbits_take(cbits_t* self, cbits_t other) { - if (self->_arr != other._arr) {cbits_del(self); *self = other;} + if (self->at64 != other.at64) {cbits_del(self); *self = other;} return self; } STC_INLINE cbits_t cbits_move(cbits_t* self) { - cbits_t tmp = *self; self->_arr = NULL, self->size = 0; + cbits_t tmp = *self; self->at64 = NULL, self->size = 0; return tmp; } STC_INLINE bool cbits_test(cbits_t set, size_t i) { - return (set._arr[i >> 6] & (1ull << (i & 63))) != 0; + return (set.at64[i >> 6] & (1ull << (i & 63))) != 0; } STC_INLINE bool cbits_at(cbits_t set, size_t i) { - return (set._arr[i >> 6] & (1ull << (i & 63))) != 0; + return (set.at64[i >> 6] & (1ull << (i & 63))) != 0; } STC_INLINE void cbits_set(cbits_t *self, size_t i) { - self->_arr[i >> 6] |= 1ull << (i & 63); + self->at64[i >> 6] |= 1ull << (i & 63); } STC_INLINE void cbits_reset(cbits_t *self, size_t i) { - self->_arr[i >> 6] &= ~(1ull << (i & 63)); + self->at64[i >> 6] &= ~(1ull << (i & 63)); } STC_INLINE void cbits_set_value(cbits_t *self, size_t i, bool value) { - self->_arr[i >> 6] ^= (-(uint64_t)value ^ self->_arr[i >> 6]) & 1ull << (i & 63); + self->at64[i >> 6] ^= (-(uint64_t)value ^ self->at64[i >> 6]) & 1ull << (i & 63); } STC_INLINE void cbits_flip(cbits_t *self, size_t i) { - self->_arr[i >> 6] ^= 1ull << (i & 63); + self->at64[i >> 6] ^= 1ull << (i & 63); } STC_INLINE void cbits_set_all(cbits_t *self, bool value) { - memset(self->_arr, -(int)value, ((self->size + 63) >> 6) * 8); + memset(self->at64, -(int)value, ((self->size + 63) >> 6) * 8); } STC_INLINE void cbits_set_values(cbits_t *self, uint64_t pattern) { size_t n = (self->size + 63) >> 6; - for (size_t i=0; i_arr[i] = pattern; + for (size_t i=0; iat64[i] = pattern; } STC_INLINE void cbits_flip_all(cbits_t *self) { size_t n = (self->size + 63) >> 6; - for (size_t i=0; i_arr[i] ^= ~0ull; + for (size_t i=0; iat64[i] ^= ~0ull; } /* Intersection */ STC_INLINE void cbits_intersect(cbits_t *self, cbits_t other) { assert(self->size == other.size); size_t n = (self->size + 63) >> 6; - for (size_t i=0; i_arr[i] &= other._arr[i]; + for (size_t i=0; iat64[i] &= other.at64[i]; } /* Union */ STC_INLINE void cbits_union(cbits_t *self, cbits_t other) { assert(self->size == other.size); size_t n = (self->size + 63) >> 6; - for (size_t i=0; i_arr[i] |= other._arr[i]; + for (size_t i=0; iat64[i] |= other.at64[i]; } /* Exclusive disjunction */ STC_INLINE void cbits_xor(cbits_t *self, cbits_t other) { assert(self->size == other.size); size_t n = (self->size + 63) >> 6; - for (size_t i=0; i_arr[i] ^= other._arr[i]; + for (size_t i=0; iat64[i] ^= other.at64[i]; } #if defined(__GNUC__) || defined(__clang__) @@ -157,21 +160,21 @@ STC_INLINE void cbits_xor(cbits_t *self, cbits_t other) { #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) STC_DEF cbits_t* cbits_assign(cbits_t* self, cbits_t other) { - if (self->_arr == other._arr) return self; + if (self->at64 == other.at64) return self; if (self->size != other.size) return cbits_take(self, cbits_clone(other)); - memcpy(self->_arr, other._arr, ((other.size + 63) >> 6)*8); + memcpy(self->at64, other.at64, ((other.size + 63) >> 6)*8); return self; } STC_DEF void cbits_resize(cbits_t* self, size_t size, bool value) { size_t new_n = (size + 63) >> 6, osize = self->size, old_n = (osize + 63) >> 6; - self->_arr = (uint64_t *) c_realloc(self->_arr, new_n * 8); + self->at64 = (uint64_t *) c_realloc(self->at64, new_n * 8); self->size = size; if (new_n >= old_n) { - memset(self->_arr + old_n, -(int)value, (new_n - old_n) * 8); + memset(self->at64 + old_n, -(int)value, (new_n - old_n) * 8); if (old_n > 0) { uint64_t m = (1ull << (osize & 63)) - 1; /* mask */ - value ? (self->_arr[old_n - 1] |= ~m) : (self->_arr[old_n - 1] &= m); + value ? (self->at64[old_n - 1] |= ~m) : (self->at64[old_n - 1] &= m); } } } @@ -199,13 +202,13 @@ STC_DEF char* cbits_to_str(cbits_t set, char* out, size_t start, intptr_t stop) } STC_DEF cbits_t cbits_clone(cbits_t other) { size_t bytes = ((other.size + 63) >> 6) * 8; - cbits_t set = {(uint64_t *) memcpy(c_malloc(bytes), other._arr, bytes), other.size}; + cbits_t set = {(uint64_t *) memcpy(c_malloc(bytes), other.at64, bytes), other.size}; return set; } STC_DEF size_t cbits_count(cbits_t s) { size_t count = 0, n = s.size >> 6; - for (size_t i = 0; i < n; ++i) count += cpopcount64(s._arr[i]); - if (s.size & 63) count += cpopcount64(s._arr[n] & ((1ull << (s.size & 63)) - 1)); + for (size_t i = 0; i < n; ++i) count += cpopcount64(s.at64[i]); + if (s.size & 63) count += cpopcount64(s.at64[n] & ((1ull << (s.size & 63)) - 1)); return count; } @@ -213,13 +216,13 @@ STC_DEF size_t cbits_count(cbits_t s) { assert(s.size == other.size); \ size_t n = s.size >> 6; \ for (size_t i = 0; i < n; ++i) \ - if ((s._arr[i] OPR other._arr[i]) != x) \ + if ((s.at64[i] OPR other.at64[i]) != x) \ return false; \ if (!(s.size & 63)) return true; \ uint64_t i = n, m = (1ull << (s.size & 63)) - 1; \ - return ((s._arr[i] OPR other._arr[i]) & m) == (x & m) + return ((s.at64[i] OPR other.at64[i]) & m) == (x & m) -STC_DEF bool cbits_subset_of(cbits_t s, cbits_t other) { _cbits_SETOP(|, s._arr[i]); } +STC_DEF bool cbits_subset_of(cbits_t s, cbits_t other) { _cbits_SETOP(|, s.at64[i]); } STC_DEF bool cbits_disjoint(cbits_t s, cbits_t other) { _cbits_SETOP(&, 0); } #endif diff --git a/stc/cdeq.h b/stc/cdeq.h index b6447eda..3e013078 100644 --- a/stc/cdeq.h +++ b/stc/cdeq.h @@ -41,20 +41,13 @@ #define using_cdeq_str() \ _c_using_cdeq(cdeq_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) -#define typedefs_cdeq(CX, Value, RawValue) \ - typedef Value CX##_value_t; \ - typedef RawValue CX##_rawvalue_t; \ - typedef struct { CX##_value_t *ref; } CX##_iter_t; \ - typedef struct { \ - CX##_value_t *base, *data; \ - } CX - -struct cdeq_rep { size_t size, cap; void* base[]; }; -#define cdeq_rep_(self) c_container_of((self)->base, struct cdeq_rep, base) -typedef int (*c_cmp_fn)(const void*, const void*); #define _c_using_cdeq(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ - typedefs_cdeq(CX, Value, RawValue); \ +\ + typedef Value CX##_value_t; \ + typedef RawValue CX##_rawvalue_t; \ + typedef struct {CX##_value_t *ref; } CX##_iter_t; \ + typedef struct {CX##_value_t *_base, *data;} CX; \ \ STC_API CX CX##_init(void); \ STC_API CX CX##_clone(CX deq); \ @@ -185,12 +178,16 @@ typedef int (*c_cmp_fn)(const void*, const void*); _c_implement_cdeq(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ struct stc_trailing_semicolon +struct cdeq_rep { size_t size, cap; void* base[]; }; +#define cdeq_rep_(self) c_container_of((self)->_base, struct cdeq_rep, base) +typedef int (*c_cmp_fn)(const void*, const void*); + /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) static struct cdeq_rep _cdeq_inits = {0, 0}; -#define _cdeq_nfront(self) ((self)->data - (self)->base) +#define _cdeq_nfront(self) ((self)->data - (self)->_base) static inline double _minf(double x, double y) {return x < y ? x : y;} static inline double _maxf(double x, double y) {return x > y ? x : y;} @@ -230,7 +227,7 @@ static inline double _maxf(double x, double y) {return x > y ? x : y;} CX##_expand_(CX* self, size_t n, bool at_front) { \ struct cdeq_rep* rep = cdeq_rep_(self); \ size_t len = rep->size, cap = rep->cap; \ - size_t nfront = self->data - self->base, nback = cap - (nfront + len); \ + size_t nfront = self->data - self->_base, nback = cap - (nfront + len); \ if (at_front && nfront >= n || !at_front && nback >= n) \ return; \ if ((len + n)*1.3 > cap) { \ @@ -238,15 +235,15 @@ static inline double _maxf(double x, double y) {return x > y ? x : y;} rep = (struct cdeq_rep*) c_realloc(rep->cap ? rep : NULL, \ sizeof(struct cdeq_rep) + cap*sizeof(Value)); \ rep->size = len, rep->cap = cap; \ - self->base = (CX##_value_t *) rep->base; \ - self->data = self->base + nfront; \ + self->_base = (CX##_value_t *) rep->base; \ + self->data = self->_base + nfront; \ CX##_expand_(self, n, at_front); \ return; \ } \ size_t unused = cap - (len + n); \ size_t pos = at_front ? _maxf(unused*0.5, (float) unused - nback) + n \ : _minf(unused*0.5, nfront); \ - self->data = (CX##_value_t *) memmove(self->base + pos, self->data, len*sizeof(Value)); \ + self->data = (CX##_value_t *) memmove(self->_base + pos, self->data, len*sizeof(Value)); \ } \ \ STC_DEF void \ @@ -260,7 +257,7 @@ static inline double _maxf(double x, double y) {return x > y ? x : y;} \ STC_DEF void \ CX##_push_front(CX* self, Value value) { \ - if (self->data == self->base) \ + if (self->data == self->_base) \ CX##_expand_(self, 1, true); \ *--self->data = value; \ ++cdeq_rep_(self)->size; \ diff --git a/stc/clist.h b/stc/clist.h index 61ac6ad2..fd484bad 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -74,24 +74,20 @@ typedef Value CX##_value_t; \ \ typedef struct CX##_node { \ - struct CX##_node* next; \ + struct CX##_node *next; \ CX##_value_t value; \ } CX##_node_t; \ \ typedef struct { \ - CX##_node_t* last; \ + CX##_node_t *last; \ } CX; \ \ typedef struct { \ - CX##_node_t* const *_last, *_prev; \ - CX##_value_t* ref; \ + CX##_node_t *const*_last, *_prev; \ + CX##_value_t *ref; \ } CX##_iter_t -_c_using_clist_types(clist_VOID, int); -STC_API size_t _clist_size(const clist_VOID* self); -#define _clist_node(CX, vp) c_container_of(vp, CX##_node_t, value) - #define _c_using_clist(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ \ _c_using_clist_types(CX, Value); \ @@ -171,6 +167,10 @@ STC_API size_t _clist_size(const clist_VOID* self); _c_implement_clist(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ struct stc_trailing_semicolon +_c_using_clist_types(clist_VOID, int); +STC_API size_t _clist_size(const clist_VOID* self); +#define _clist_node(CX, vp) c_container_of(vp, CX##_node_t, value) + /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) diff --git a/stc/csmap.h b/stc/csmap.h index 8694f6a7..732d3713 100644 --- a/stc/csmap.h +++ b/stc/csmap.h @@ -124,15 +124,6 @@ int main(void) { cstr_del, cstr_from, cstr_c_str, const char*, \ keyDel, keyFromRaw, keyToRaw, RawKey) -#define SET_ONLY_csmap_(...) -#define MAP_ONLY_csmap_(...) __VA_ARGS__ -#define KEY_REF_csmap_(vp) (&(vp)->first) -#ifndef CSMAP_SIZE_T -#define CSMAP_SIZE_T uint32_t -#endif -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, \ @@ -165,7 +156,7 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; } CX##_node_t; \ \ typedef struct { \ - CX##_node_t* nodes; \ + CX##_node_t *nodes; \ } CX; \ \ typedef struct { \ @@ -296,6 +287,15 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* nodes[]; }; keyDel, keyFromRaw, keyToRaw, RawKey) \ struct stc_trailing_semicolon +#define SET_ONLY_csmap_(...) +#define MAP_ONLY_csmap_(...) __VA_ARGS__ +#define KEY_REF_csmap_(vp) (&(vp)->first) +#ifndef CSMAP_SIZE_T +#define CSMAP_SIZE_T uint32_t +#endif +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) + /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) diff --git a/stc/cvec.h b/stc/cvec.h index 59bfd889..ffc66037 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -41,20 +41,13 @@ #define using_cvec_str() \ _c_using_cvec(cvec_str, cstr_t, cstr_compare_raw, cstr_del, cstr_from, cstr_c_str, const char*) -#define typedefs_cvec(CX, Value, RawValue) \ - typedef Value CX##_value_t; \ - typedef RawValue CX##_rawvalue_t; \ - typedef struct { CX##_value_t *ref; } CX##_iter_t; \ - typedef struct { \ - CX##_value_t* data; \ - } CX -struct cvec_rep { size_t size, cap; void* data[]; }; -#define _cvec_rep(self) c_container_of((self)->data, struct cvec_rep, data) -typedef int (*c_cmp_fn)(const void*, const void*); - #define _c_using_cvec(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ - typedefs_cvec(CX, Value, RawValue); \ +\ + typedef Value CX##_value_t; \ + typedef RawValue CX##_rawvalue_t; \ + typedef struct {CX##_value_t *ref;} CX##_iter_t; \ + typedef struct {CX##_value_t *data;} CX; \ \ STC_API CX CX##_init(void); \ STC_API CX CX##_clone(CX vec); \ @@ -190,6 +183,10 @@ typedef int (*c_cmp_fn)(const void*, const void*); _c_implement_cvec(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) \ struct stc_trailing_semicolon +struct cvec_rep { size_t size, cap; void* data[]; }; +#define _cvec_rep(self) c_container_of((self)->data, struct cvec_rep, data) +typedef int (*c_cmp_fn)(const void*, const void*); + /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) @@ -324,4 +321,4 @@ static struct cvec_rep _cvec_inits = {0, 0}; #define _c_implement_cvec(CX, Value, valueCompareRaw, valueDel, valueFromRaw, valueToRaw, RawValue) #endif -#endif +#endif \ No newline at end of file -- cgit v1.2.3