From fbfb57d46e4d042c2c5ae1c5a56dad00660d3e3b Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 21 Sep 2021 20:49:22 +0200 Subject: Added copy(self, other) function to all containers. Fixed some docs. --- include/stc/carr2.h | 5 +++++ include/stc/carr3.h | 5 +++++ include/stc/cbits.h | 4 ++-- include/stc/ccommon.h | 2 +- include/stc/cdeq.h | 4 ++++ include/stc/clist.h | 6 ++++++ include/stc/cmap.h | 5 +++++ include/stc/cpque.h | 5 +++++ include/stc/csmap.h | 6 ++++++ include/stc/csptr.h | 16 +++++++++++++--- include/stc/cstack.h | 5 +++++ include/stc/cvec.h | 6 ++++++ 12 files changed, 63 insertions(+), 6 deletions(-) (limited to 'include/stc') diff --git a/include/stc/carr2.h b/include/stc/carr2.h index 12b61876..d2f1e729 100644 --- a/include/stc/carr2.h +++ b/include/stc/carr2.h @@ -79,6 +79,11 @@ STC_INLINE cx_value_t *cx_memb(_data)(Self* self) STC_INLINE cx_value_t *cx_memb(_at)(Self* self, size_t x, size_t y) { return *self->data + self->ydim*x + y; } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { return c_make(cx_iter_t){*self->data}; } diff --git a/include/stc/carr3.h b/include/stc/carr3.h index 1e11b03e..45b57acc 100644 --- a/include/stc/carr3.h +++ b/include/stc/carr3.h @@ -81,6 +81,11 @@ STC_INLINE cx_value_t* cx_memb(_data)(Self* self) STC_INLINE cx_value_t* cx_memb(_at)(Self* self, size_t x, size_t y, size_t z) { return **self->data + self->zdim*(self->ydim*x + y) + z; } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { return c_make(cx_iter_t){**self->data}; } diff --git a/include/stc/cbits.h b/include/stc/cbits.h index 9bf28d44..2843f7d0 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -67,7 +67,7 @@ STC_API cbits cbits_from_str(const char* str); STC_API char* cbits_to_str(cbits set, char* str, size_t start, intptr_t stop); STC_API cbits cbits_clone(cbits other); STC_API void cbits_resize(cbits* self, size_t size, bool value); -STC_API cbits* cbits_assign(cbits* self, cbits other); +STC_API cbits* cbits_copy(cbits* self, cbits other); STC_API size_t cbits_count(cbits set); STC_API bool cbits_subset_of(cbits set, cbits other); STC_API bool cbits_disjoint(cbits set, cbits other); @@ -160,7 +160,7 @@ STC_INLINE void cbits_xor(cbits *self, cbits other) { #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -STC_DEF cbits* cbits_assign(cbits* self, cbits other) { +STC_DEF cbits* cbits_copy(cbits* self, cbits other) { if (self->data64 == other.data64) return self; if (self->size != other.size) return cbits_take(self, cbits_clone(other)); memcpy(self->data64, other.data64, ((other.size + 63) >> 6)*8); diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index a97828af..69afb2bd 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -103,7 +103,7 @@ #define c_rawstr_compare(x, y) strcmp(*(x), *(y)) #define c_rawstr_equals(x, y) (strcmp(*(x), *(y)) == 0) #define c_rawstr_hash(p, dummy) c_default_hash(*(p), strlen(*(p))) -#define c_strhash(s) c_default_hash(s, strlen(s)) +#define c_strhash(s) c_default_hash(s, strlen(s)) #define c_no_clone(x) (assert(!"c_no_clone() called"), x) #define c_default_fromraw(x) (x) diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 8fe81845..5dec0f3c 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -67,6 +67,10 @@ STC_INLINE i_val cx_memb(_value_fromraw)(i_valraw raw) { return i_valfrom STC_INLINE i_valraw cx_memb(_value_toraw)(cx_value_t* pval) { return i_valto(pval); } STC_INLINE i_val cx_memb(_value_clone)(i_val val) { return i_valfrom(i_valto(&val)); } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); + } STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw) { return cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE void cx_memb(_pop_front)(Self* self) diff --git a/include/stc/clist.h b/include/stc/clist.h index 0b5cd35c..e123200f 100644 --- a/include/stc/clist.h +++ b/include/stc/clist.h @@ -128,6 +128,12 @@ STC_INLINE cx_iter_t cx_memb(_emplace)(Self* self, cx_iter_t it, i_valraw raw STC_INLINE cx_value_t* cx_memb(_front)(const Self* self) { return &self->last->next->value; } STC_INLINE cx_value_t* cx_memb(_back)(const Self* self) { return &self->last->value; } +STC_INLINE void +cx_memb(_copy)(Self *self, Self other) { + if (self->last == other.last) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE cx_iter_t cx_memb(_iter)(const Self* self, cx_node_t* prev) { return c_make(cx_iter_t){&self->last, prev, &prev->next->value}; diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 096a7045..763764b7 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -272,6 +272,11 @@ STC_DEF void cx_memb(_clear)(Self* self) { memset(self->_hashx, 0, self->bucket_count); } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->table == other.table) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + cx_MAP_ONLY( STC_DEF cx_result_t cx_memb(_insert_or_assign)(Self* self, i_key _key, i_val _mapped) { diff --git a/include/stc/cpque.h b/include/stc/cpque.h index de509710..6819948f 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -60,6 +60,11 @@ STC_INLINE void cx_memb(_clear)(Self* self) { STC_INLINE void cx_memb(_del)(Self* self) { cx_memb(_clear)(self); c_free(self->data); } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE size_t cx_memb(_size)(Self q) { return q.size; } diff --git a/include/stc/csmap.h b/include/stc/csmap.h index cc7eeb49..204a6bb3 100644 --- a/include/stc/csmap.h +++ b/include/stc/csmap.h @@ -124,6 +124,12 @@ cx_memb(_with_capacity)(size_t size) { return tree; } +STC_INLINE void +cx_memb(_copy)(Self *self, Self other) { + if (self->nodes == other.nodes) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE cx_rawvalue_t cx_memb(_value_toraw)(cx_value_t* val) { return cx_SET_ONLY( i_keyto(val) ) diff --git a/include/stc/csptr.h b/include/stc/csptr.h index d1a0cfe6..11dfaa5a 100644 --- a/include/stc/csptr.h +++ b/include/stc/csptr.h @@ -80,6 +80,13 @@ typedef long atomic_count_t; #endif #include "template.h" +#ifdef i_nonatomic + #define cx_increment(v) (++*(v)) + #define cx_decrement(v) (--*(v)) +#else + #define cx_increment(v) c_atomic_increment(v) + #define cx_decrement(v) c_atomic_decrement(v) +#endif #ifndef i_fwd cx_deftypes(_c_csptr_types, Self, i_val); #endif @@ -109,7 +116,7 @@ cx_memb(_make)(cx_value_t val) { STC_INLINE Self cx_memb(_clone)(Self ptr) { - if (ptr.use_count) c_atomic_increment(ptr.use_count); + if (ptr.use_count) cx_increment(ptr.use_count); return ptr; } @@ -122,7 +129,7 @@ cx_memb(_move)(Self* self) { STC_INLINE void cx_memb(_del)(Self* self) { - if (self->use_count && c_atomic_decrement(self->use_count) == 0) { + if (self->use_count && cx_decrement(self->use_count) == 0) { i_valdel(self->get); if (self->get != &((cx_csptr_rep*)self->use_count)->val) c_free(self->get); @@ -154,7 +161,7 @@ STC_INLINE cx_value_t* cx_memb(_copy)(Self* self, Self ptr) { cx_memb(_del)(self); *self = ptr; - if (self->use_count) c_atomic_increment(self->use_count); + if (self->use_count) cx_increment(self->use_count); return self->get; } @@ -164,4 +171,7 @@ cx_memb(_compare)(const Self* x, const Self* y) { } #undef cx_csptr_rep +#undef cx_increment +#undef cx_decrement +#undef i_nonatomic #include "template.h" \ No newline at end of file diff --git a/include/stc/cstack.h b/include/stc/cstack.h index f7c16421..7543fb4f 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -86,6 +86,11 @@ STC_INLINE Self cx_memb(_clone)(Self v) { return out; } +STC_INLINE void cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE void cx_memb(_emplace_items)(Self *self, const cx_rawvalue_t arr[], size_t n) { for (size_t i = 0; i < n; ++i) cx_memb(_push)(self, i_valfrom(arr[i])); } diff --git a/include/stc/cvec.h b/include/stc/cvec.h index 03e04290..3dc7b4c1 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -137,6 +137,12 @@ cx_memb(_shrink_to_fit)(Self *self) { cx_memb(_del)(self); *self = cx; } +STC_INLINE void +cx_memb(_copy)(Self *self, Self other) { + if (self->data == other.data) return; + cx_memb(_del)(self); *self = cx_memb(_clone)(other); +} + STC_INLINE cx_iter_t cx_memb(_insert)(Self* self, size_t idx, i_val value) { return cx_memb(_insert_range_p)(self, self->data + idx, &value, &value + 1, false); -- cgit v1.2.3