diff options
| author | Tyge Løvset <[email protected]> | 2021-09-21 20:49:22 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-09-21 20:49:22 +0200 |
| commit | fbfb57d46e4d042c2c5ae1c5a56dad00660d3e3b (patch) | |
| tree | 31dea2b7af4298d211b99e50b903be5f39ddebfb | |
| parent | 32a3454eb463f86d7ace7b29c2e1574530499afc (diff) | |
| download | STC-modified-fbfb57d46e4d042c2c5ae1c5a56dad00660d3e3b.tar.gz STC-modified-fbfb57d46e4d042c2c5ae1c5a56dad00660d3e3b.zip | |
Added copy(self, other) function to all containers. Fixed some docs.
| -rw-r--r-- | docs/carray_api.md | 4 | ||||
| -rw-r--r-- | docs/cbits_api.md | 2 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 19 | ||||
| -rw-r--r-- | docs/clist_api.md | 15 | ||||
| -rw-r--r-- | docs/cmap_api.md | 54 | ||||
| -rw-r--r-- | docs/cpque_api.md | 3 | ||||
| -rw-r--r-- | docs/cqueue_api.md | 3 | ||||
| -rw-r--r-- | docs/cset_api.md | 19 | ||||
| -rw-r--r-- | docs/csmap_api.md | 35 | ||||
| -rw-r--r-- | docs/csptr_api.md | 15 | ||||
| -rw-r--r-- | docs/csset_api.md | 23 | ||||
| -rw-r--r-- | docs/cstack_api.md | 3 | ||||
| -rw-r--r-- | docs/cvec_api.md | 21 | ||||
| -rw-r--r-- | examples/runall.sh | 14 | ||||
| -rw-r--r-- | examples/sharedptr.c | 1 | ||||
| -rw-r--r-- | include/stc/carr2.h | 5 | ||||
| -rw-r--r-- | include/stc/carr3.h | 5 | ||||
| -rw-r--r-- | include/stc/cbits.h | 4 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 2 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 4 | ||||
| -rw-r--r-- | include/stc/clist.h | 6 | ||||
| -rw-r--r-- | include/stc/cmap.h | 5 | ||||
| -rw-r--r-- | include/stc/cpque.h | 5 | ||||
| -rw-r--r-- | include/stc/csmap.h | 6 | ||||
| -rw-r--r-- | include/stc/csptr.h | 16 | ||||
| -rw-r--r-- | include/stc/cstack.h | 5 | ||||
| -rw-r--r-- | include/stc/cvec.h | 6 |
27 files changed, 187 insertions, 113 deletions
diff --git a/docs/carray_api.md b/docs/carray_api.md index ce3a2ded..f74ee157 100644 --- a/docs/carray_api.md +++ b/docs/carray_api.md @@ -16,7 +16,7 @@ See the c++ class [boost::multi_array](https://www.boost.org/doc/libs/release/li #include <stc/carr2.h> // or <stc/carr3.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -26,6 +26,7 @@ carr2_X carr2_X_init(size_t xdim, size_t ydim); carr2_X carr2_X_with_values(size_t xdim, size_t ydim, Value val); carr2_X carr2_X_with_storage(size_t xdim, size_t ydim, Value* array); carr2_X carr2_X_clone(carr2_X arr); +void carr2_X_copy(carr2_X* self, carr2_X other); Value* carr2_X_release(carr2_X* self); // release storage (not freed) void carr2_X_del(carr2_X* self); @@ -43,6 +44,7 @@ carr3_X carr3_X_init(size_t xdim, size_t ydim, size_t zdim); carr3_X carr3_X_with_values(size_t xdim, size_t ydim, size_t zdim, Value val); carr3_X carr3_X_with_storage(size_t xdim, size_t ydim, size_t zdim, Value* array); carr3_X carr3_X_clone(carr3_X arr); +void carr3_X_copy(carr3_X* self, carr3_X other); Value* carr3_X_release(carr3_X* self); // release storage (not freed) void carr3_X_del(carr3_X* self); diff --git a/docs/cbits_api.md b/docs/cbits_api.md index 4d1c6bcf..4264b1d4 100644 --- a/docs/cbits_api.md +++ b/docs/cbits_api.md @@ -24,10 +24,10 @@ cbits cbits_from_str(const char* str); cbits cbits_clone(cbits other); void cbits_clear(cbits* self); +cbits* cbits_copy(cbits* self, cbits other); void cbits_resize(cbits* self, size_t size, bool value); void cbits_del(cbits* self); -cbits* cbits_assign(cbits* self, cbits other); cbits* cbits_take(cbits* self, cbits other); // give other to self cbits cbits_move(cbits* self); // transfer self to caller diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index de385d8b..486a5758 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -17,7 +17,7 @@ See the c++ class [std::deque](https://en.cppreference.com/w/cpp/container/deque #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cdeq.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -27,6 +27,7 @@ cdeq_X cdeq_X_with_capacity(size_t size); cdeq_X cdeq_X_clone(cdeq_X deq); void cdeq_X_clear(cdeq_X* self); +void cdeq_X_copy(cdeq_X* self, cdeq_X other); void cdeq_X_shrink_to_fit(cdeq_X* self); void cdeq_X_reserve(cdeq_X* self, size_t cap); void cdeq_X_swap(cdeq_X* a, cdeq_X* b); @@ -42,20 +43,20 @@ cdeq_X_value_t* cdeq_X_front(const cdeq_X* self); cdeq_X_value_t* cdeq_X_back(const cdeq_X* self); void cdeq_X_push_front(cdeq_X* self, Value value); -void cdeq_X_emplace_front(cdeq_X* self, RawValue raw); +void cdeq_X_emplace_front(cdeq_X* self, i_valraw raw); void cdeq_X_pop_front(cdeq_X* self); void cdeq_X_push_back(cdeq_X* self, Value value); -void cdeq_X_emplace_back(cdeq_X* self, RawValue raw); +void cdeq_X_emplace_back(cdeq_X* self, i_valraw raw); void cdeq_X_pop_back(cdeq_X* self); cdeq_X_iter_t cdeq_X_insert(cdeq_X* self, size_t idx, Value value); // move value cdeq_X_iter_t cdeq_X_insert_n(cdeq_X* self, size_t idx, const Value[] arr, size_t n); // move arr values cdeq_X_iter_t cdeq_X_insert_at(cdeq_X* self, cdeq_X_iter_t it, Value value); // move value -cdeq_X_iter_t cdeq_X_emplace(cdeq_X* self, size_t idx, RawValue raw); -cdeq_X_iter_t cdeq_X_emplace_n(cdeq_X* self, size_t idx, const RawValue[] arr, size_t n); -cdeq_X_iter_t cdeq_X_emplace_at(cdeq_X* self, cdeq_X_iter_t it, RawValue raw); +cdeq_X_iter_t cdeq_X_emplace(cdeq_X* self, size_t idx, i_valraw raw); +cdeq_X_iter_t cdeq_X_emplace_n(cdeq_X* self, size_t idx, const i_valraw[] arr, size_t n); +cdeq_X_iter_t cdeq_X_emplace_at(cdeq_X* self, cdeq_X_iter_t it, i_valraw raw); cdeq_X_iter_t cdeq_X_emplace_range(cdeq_X* self, cdeq_X_iter_t it, cdeq_X_iter_t it1, cdeq_X_iter_t it2); // will clone cdeq_X_iter_t cdeq_X_emplace_range_p(cdeq_X* self, Value* pos, @@ -66,9 +67,9 @@ cdeq_X_iter_t cdeq_X_erase_n(cdeq_X* self, size_t idx, size_t n); cdeq_X_iter_t cdeq_X_erase_at(cdeq_X* self, cdeq_X_iter_t it); cdeq_X_iter_t cdeq_X_erase_range(cdeq_X* self, cdeq_X_iter_t it1, cdeq_X_iter_t it2); -cdeq_X_iter_t cdeq_X_find(const cdeq_X* self, RawValue raw); -cdeq_X_iter_t cdeq_X_find_in(cdeq_X_iter_t i1, cdeq_X_iter_t i2, RawValue raw); -cdeq_X_value_t* cdeq_X_get(const cdeq_X* self, RawValue raw); // returns NULL if not found +cdeq_X_iter_t cdeq_X_find(const cdeq_X* self, i_valraw raw); +cdeq_X_iter_t cdeq_X_find_in(cdeq_X_iter_t i1, cdeq_X_iter_t i2, i_valraw raw); +cdeq_X_value_t* cdeq_X_get(const cdeq_X* self, i_valraw raw); // returns NULL if not found void cdeq_X_sort(cdeq_X* self); void cdeq_X_sort_range(cdeq_X_iter_t i1, cdeq_X_iter_t i2, diff --git a/docs/clist_api.md b/docs/clist_api.md index 343deb48..b9cf9d99 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -32,7 +32,7 @@ See the c++ class [std::list](https://en.cppreference.com/w/cpp/container/list) #include <stc/clist.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -41,6 +41,7 @@ clist_X clist_X_init(void); clist_X clist_X_clone(clist_X list); void clist_X_clear(clist_X* self); +void clist_X_copy(clist_X* self, clist_X other); void clist_X_del(clist_X* self); // destructor bool clist_X_empty(clist_X list); @@ -50,27 +51,27 @@ clist_X_value_t* clist_X_front(const clist_X* self); clist_X_value_t* clist_X_back(const clist_X* self); void clist_X_push_front(clist_X* self, Value value); -void clist_X_emplace_front(clist_X* self, RawValue raw); +void clist_X_emplace_front(clist_X* self, i_valraw raw); void clist_X_pop_front(clist_X* self); void clist_X_push_back(clist_X* self, Value value); // note: no pop_back(). -void clist_X_emplace_back(clist_X* self, RawValue raw); +void clist_X_emplace_back(clist_X* self, i_valraw raw); void clist_X_emplace_items(clist_X *self, const clist_X_rawvalue_t arr[], size_t n); clist_X_iter_t clist_X_insert(clist_X* self, clist_X_iter_t it, Value value); // return iter to new elem -clist_X_iter_t clist_X_emplace(clist_X* self, clist_X_iter_t it, RawValue raw); +clist_X_iter_t clist_X_emplace(clist_X* self, clist_X_iter_t it, i_valraw raw); clist_X_iter_t clist_X_erase_at(clist_X* self, clist_X_iter_t it); // return iter after it clist_X_iter_t clist_X_erase_range(clist_X* self, clist_X_iter_t it1, clist_X_iter_t it2); -size_t clist_X_remove(clist_X* self, RawValue raw); // removes matching elements +size_t clist_X_remove(clist_X* self, i_valraw raw); // removes matching elements clist_X clist_X_split_off(clist_X* self, clist_X_iter_t i1, clist_X_iter_t i2); // split off [i1, i2) clist_X_iter_t clist_X_splice(clist_X* self, clist_X_iter_t it, clist_X* other); // return updated valid it clist_X_iter_t clist_X_splice_range(clist_X* self, clist_X_iter_t it, // return updated valid it clist_X* other, clist_X_iter_t it1, clist_X_iter_t it2); -clist_X_iter_t clist_X_find(const clist_X* self, RawValue raw); -clist_X_iter_t clist_X_find_in(clist_X_iter_t it1, clist_X_iter_t it2, RawValue raw); +clist_X_iter_t clist_X_find(const clist_X* self, i_valraw raw); +clist_X_iter_t clist_X_find_in(clist_X_iter_t it1, clist_X_iter_t it2, i_valraw raw); void clist_X_sort(clist_X* self); diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 4b73345c..401e3cec 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -32,7 +32,7 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cmap.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -42,32 +42,33 @@ cmap_X cmap_X_with_capacity(size_t cap); cmap_X cmap_X_clone(cmap_x map); void cmap_X_clear(cmap_X* self); -void cmap_X_max_load_factor(cmap_X* self, float max_load); // default: 0.85 +void cmap_X_copy(cmap_X* self, cmap_X other); +void cmap_X_max_load_factor(cmap_X* self, float max_load); // default: 0.85 void cmap_X_reserve(cmap_X* self, size_t size); void cmap_X_shrink_to_fit(cmap_X* self); void cmap_X_swap(cmap_X* a, cmap_X* b); -void cmap_X_del(cmap_X* self); // destructor +void cmap_X_del(cmap_X* self); // destructor bool cmap_X_empty(cmap_X map); size_t cmap_X_size(cmap_X map); -size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor -size_t cmap_X_bucket_count(cmap_X map); // num. of allocated buckets +size_t cmap_X_capacity(cmap_X map); // buckets * max_load_factor +size_t cmap_X_bucket_count(cmap_X map); // num. of allocated buckets -bool cmap_X_contains(const cmap_X* self, RawKey rkey); -cmap_X_mapped_t* cmap_X_at(const cmap_X* self, RawKey rkey); // rkey must be in map. -cmap_X_value_t* cmap_X_get(const cmap_X* self, RawKey rkey); // return NULL if not found -cmap_X_iter_t cmap_X_find(const cmap_X* self, RawKey rkey); +bool cmap_X_contains(const cmap_X* self, i_keyraw rkey); +cmap_X_mapped_t* cmap_X_at(const cmap_X* self, i_keyraw rkey); // rkey must be in map. +cmap_X_value_t* cmap_X_get(const cmap_X* self, i_keyraw rkey); // return NULL if not found +cmap_X_iter_t cmap_X_find(const cmap_X* self, i_keyraw rkey); -cmap_X_result_t cmap_X_insert(cmap_X* self, Key key, Mapped mapped); // no change if key in map -cmap_X_result_t cmap_X_insert_or_assign(cmap_X* self, Key key, Mapped mapped); // always update mapped -cmap_X_result_t cmap_X_put(cmap_X* self, Key key, Mapped mapped); // alias for insert_or_assign +cmap_X_result_t cmap_X_insert(cmap_X* self, i_key key, i_val mapped); // no change if key in map +cmap_X_result_t cmap_X_insert_or_assign(cmap_X* self, i_key key, i_val mapped); // always update mapped +cmap_X_result_t cmap_X_put(cmap_X* self, i_key key, i_val mapped); // alias for insert_or_assign -cmap_X_result_t cmap_X_emplace(cmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map -cmap_X_result_t cmap_X_emplace_or_assign(cmap_X* self, RawKey rkey, RawMapped rmapped); // always update rmapped +cmap_X_result_t cmap_X_emplace(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map +cmap_X_result_t cmap_X_emplace_or_assign(cmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped void cmap_X_emplace_items(cmap_X* self, const cmap_X_rawvalue_t arr[], size_t n); -size_t cmap_X_erase(cmap_X* self, RawKey rkey); // return 0 or 1 -cmap_X_iter_t cmap_X_erase_at(cmap_X* self, cmap_X_iter_t it); // return iter after it +size_t cmap_X_erase(cmap_X* self, i_keyraw rkey); // return 0 or 1 +cmap_X_iter_t cmap_X_erase_at(cmap_X* self, cmap_X_iter_t it); // return iter after it void cmap_X_erase_entry(cmap_X* self, cmap_X_value_t* entry); cmap_X_iter_t cmap_X_begin(const cmap_X* self); @@ -77,21 +78,24 @@ void cmap_X_next(cmap_X_iter_t* it); cmap_X_value_t cmap_X_value_clone(cmap_X_value_t val); cmap_X_rawvalue_t cmap_X_value_toraw(cmap_X_value_t* pval); ``` +Helpers: ```c +uint64_t c_strhash(const char *str); // utility function + int c_rawstr_compare(const char* const* a, const char* const* b); bool c_rawstr_equals(const char* const* a, const char* const* b); uint64_t c_rawstr_hash(const char* const* strp, ...); -uint64_t c_default_hash(const void *data, size_t len); // key any trivial type -uint64_t c_default_hash32(const void* data, size_t is4); // key one 32bit int -uint64_t c_default_hash64(const void* data, size_t is8); // key one 64bit int -int c_default_equals(const RawKey* a, const RawKey* b); // the == operator -int c_memcmp_equals(const RawKey* a, const RawKey* b); // uses memcmp +uint64_t c_default_hash(const void *data, size_t len); // key any trivial type +uint64_t c_default_hash32(const void* data, size_t is4); // key one 32bit int +uint64_t c_default_hash64(const void* data, size_t is8); // key one 64bit int +int c_default_equals(const i_keyraw* a, const i_keyraw* b); // the == operator +int c_memcmp_equals(const i_keyraw* a, const i_keyraw* b); // uses memcmp Type c_no_clone(Type val); -Type c_default_fromraw(Type val); // plain copy +Type c_default_fromraw(Type val); // plain copy Type c_default_toraw(Type* val); -void c_default_del(Type* val); // does nothing +void c_default_del(Type* val); // does nothing ``` ## Types @@ -101,9 +105,9 @@ void c_default_del(Type* val); // doe | `cmap_X` | `struct { ... }` | The cmap type | | `cmap_X_rawkey_t` | `i_keyraw` | The raw key type | | `cmap_X_rawmapped_t` | `i_valraw` | The raw mapped type | -| `cmap_X_rawvalue_t` | `struct { i_keyraw first; i_valraw second; }` | RawKey + RawMapped type | +| `cmap_X_rawvalue_t` | `struct { i_keyraw first; i_valraw second; }` | i_keyraw + i_valraw type | | `cmap_X_key_t` | `i_key` | The key type | -| `cmap_X_mapped_t` | `i_valraw` | The mapped type | +| `cmap_X_mapped_t` | `i_val` | The mapped type | | `cmap_X_value_t` | `struct { const i_key first; i_val second; }` | The value: key is immutable | | `cmap_X_result_t` | `struct { cmap_X_value_t *ref; bool inserted; }`| Result of insert/put/emplace | | `cmap_X_iter_t` | `struct { cmap_X_value_t *ref; ... }` | Iterator type | diff --git a/docs/cpque_api.md b/docs/cpque_api.md index fd2c334e..0a4f7412 100644 --- a/docs/cpque_api.md +++ b/docs/cpque_api.md @@ -17,7 +17,7 @@ See the c++ class [std::priority_queue](https://en.cppreference.com/w/cpp/contai #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cpque.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -26,6 +26,7 @@ cpque_X cpque_X_init(void); cpque_X cpque_X_clone(cpque_X pq); void cpque_X_clear(cpque_X* self); +void cpque_X_copy(cpque_X* self, cpque_X other); void cpque_X_make_heap(cpque_X* self); void cpque_X_del(cpque_X* self); // destructor diff --git a/docs/cqueue_api.md b/docs/cqueue_api.md index ff2052e0..c6db0411 100644 --- a/docs/cqueue_api.md +++ b/docs/cqueue_api.md @@ -16,7 +16,7 @@ See the c++ class [std::queue](https://en.cppreference.com/w/cpp/container/queue #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cqueue.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -26,6 +26,7 @@ cqueue_X cqueue_X_init(void); cqueue_X cqueue_X_clone(cqueue_X q); void cqueue_X_clear(cqueue_X* self); +void cqueue_X_copy(cqueue_X* self, cqueue_X other); void cqueue_X_del(cqueue_X* self); // destructor size_t cqueue_X_size(cqueue_X q); diff --git a/docs/cset_api.md b/docs/cset_api.md index 99d7187e..22e65a5e 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -1,7 +1,7 @@ # STC [cset](../include/stc/cset.h): Unordered Set

-A **cset** is an associative container that contains a set of unique objects of type Key. Search, insertion, and removal have average constant-time complexity. See the c++ class
+A **cset** is an associative container that contains a set of unique objects of type i_key. Search, insertion, and removal have average constant-time complexity. See the c++ class
[std::unordered_set](https://en.cppreference.com/w/cpp/container/unordered_set) for a functional description.
## Header file and declaration
@@ -18,7 +18,7 @@ A **cset** is an associative container that contains a set of unique objects of #define i_keydel // destroy key func - defaults to empty destruct
#include <stc/cset.h>
```
-`X` should be replaced by the value of ***i_tag*** in all of the following documentation.
+`X` should be replaced by the value of `i_tag` in all of the following documentation.
## Methods
@@ -28,6 +28,7 @@ cset_X cset_X_with_capacity(size_t cap); cset_X cset_X_clone(cset_x set);
void cset_X_clear(cset_X* self);
+void cset_X_copy(cset_X* self, cset_X other);
void cset_X_max_load_factor(cset_X* self, float max_load); // default: 0.85
void cset_X_reserve(cset_X* self, size_t size);
void cset_X_shrink_to_fit(cset_X* self);
@@ -39,15 +40,15 @@ size_t cset_X_size(cset_X set); size_t cset_X_capacity(cset_X set); // buckets * max_load_factor
size_t cset_X_bucket_count(cset_X set);
-bool cset_X_contains(const cset_X* self, RawKey rkey);
-cset_X_value_t* cset_X_get(const cset_X* self, RawKey rkey); // return NULL if not found
-cset_X_iter_t cset_X_find(const cset_X* self, RawKey rkey);
+bool cset_X_contains(const cset_X* self, i_keyraw rkey);
+cset_X_value_t* cset_X_get(const cset_X* self, i_keyraw rkey); // return NULL if not found
+cset_X_iter_t cset_X_find(const cset_X* self, i_keyraw rkey);
-cset_X_result_t cset_X_insert(cset_X* self, Key key);
-cset_X_result_t cset_X_emplace(cset_X* self, RawKey rkey);
-void cset_X_emplace_items(cset_X* self, const RawKey arr[], size_t n);
+cset_X_result_t cset_X_insert(cset_X* self, i_key key);
+cset_X_result_t cset_X_emplace(cset_X* self, i_keyraw rkey);
+void cset_X_emplace_items(cset_X* self, const i_keyraw arr[], size_t n);
-size_t cset_X_erase(cset_X* self, RawKey rkey); // return 0 or 1
+size_t cset_X_erase(cset_X* self, i_keyraw rkey); // return 0 or 1
cset_X_iter_t cset_X_erase_at(cset_X* self, cset_X_iter_t it); // return iter after it
void cset_X_erase_entry(cset_X* self, cset_X_value_t* entry);
diff --git a/docs/csmap_api.md b/docs/csmap_api.md index 704a1685..1a7ed78c 100644 --- a/docs/csmap_api.md +++ b/docs/csmap_api.md @@ -29,7 +29,7 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo #define i_valdel // destroy value func - defaults to empty destruct #include <stc/csmap.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -38,30 +38,31 @@ csmap_X csmap_X_init(void); csmap_X csmap_X_clone(csmap_x map); void csmap_X_clear(csmap_X* self); +void csmap_X_copy(csmap_X* self, csmap_X other); void csmap_X_swap(csmap_X* a, csmap_X* b); -void csmap_X_del(csmap_X* self); // destructor +void csmap_X_del(csmap_X* self); // destructor bool csmap_X_empty(csmap_X map); size_t csmap_X_size(csmap_X map); -bool csmap_X_contains(const csmap_X* self, RawKey rkey); -csmap_X_mapped_t* csmap_X_at(const csmap_X* self, RawKey rkey); // rkey must be in map. -csmap_X_value_t* csmap_X_get(const csmap_X* self, RawKey rkey); // return NULL if not found -csmap_X_iter_t csmap_X_lower_bound(const csmap_X* self, RawKey rkey); // find closest entry >= rkey -csmap_X_iter_t csmap_X_find(const csmap_X* self, RawKey rkey); -csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out); // return NULL if not found +bool csmap_X_contains(const csmap_X* self, i_keyraw rkey); +csmap_X_mapped_t* csmap_X_at(const csmap_X* self, i_keyraw rkey); // rkey must be in map. +csmap_X_value_t* csmap_X_get(const csmap_X* self, i_keyraw rkey); // return NULL if not found +csmap_X_iter_t csmap_X_lower_bound(const csmap_X* self, i_keyraw rkey); // find closest entry >= rkey +csmap_X_iter_t csmap_X_find(const csmap_X* self, i_keyraw rkey); +csmap_X_value_t* csmap_X_find_it(const csmap_X* self, i_keyraw rkey, csmap_X_iter_t* out); // return NULL if not found -csmap_X_result_t csmap_X_insert(csmap_X* self, Key key, Mapped mapped); // no change if key in map -csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, Key key, Mapped mapped); // always update mapped -csmap_X_result_t csmap_X_put(csmap_X* self, Key key, Mapped mapped); // same as insert_or_assign() +csmap_X_result_t csmap_X_insert(csmap_X* self, i_key key, i_val mapped); // no change if key in map +csmap_X_result_t csmap_X_insert_or_assign(csmap_X* self, i_key key, i_val mapped); // always update mapped +csmap_X_result_t csmap_X_put(csmap_X* self, i_key key, i_val mapped); // same as insert_or_assign() -csmap_X_result_t csmap_X_emplace(csmap_X* self, RawKey rkey, RawMapped rmapped); // no change if rkey in map -csmap_X_result_t csmap_X_emplace_or_assign(csmap_X* self, RawKey rkey, RawMapped rmapped); // always update rmapped +csmap_X_result_t csmap_X_emplace(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map +csmap_X_result_t csmap_X_emplace_or_assign(csmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped void csmap_X_emplace_items(csmap_X* self, const csmap_X_rawvalue_t arr[], size_t n); -size_t csmap_X_erase(csmap_X* self, RawKey rkey); -csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t it); // returns iter after it -csmap_X_iter_t csmap_X_erase_range(csmap_X* self, csmap_X_iter_t it1, csmap_X_iter_t it2); // returns updated it2 +size_t csmap_X_erase(csmap_X* self, i_keyraw rkey); +csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t it); // returns iter after it +csmap_X_iter_t csmap_X_erase_range(csmap_X* self, csmap_X_iter_t it1, csmap_X_iter_t it2); // returns updated it2 csmap_X_iter_t csmap_X_begin(const csmap_X* self); csmap_X_iter_t csmap_X_end(const csmap_X* self); @@ -78,7 +79,7 @@ csmap_X_rawvalue_t csmap_X_value_toraw(csmap_X_value_t* pval); | `csmap_X` | `struct { ... }` | The csmap type | | `csmap_X_rawkey_t` | `i_keyraw` | The raw key type | | `csmap_X_rawmapped_t` | `i_valraw` | The raw mapped type | -| `csmap_X_rawvalue_t` | `struct { i_keyraw first; i_valraw second; }` | RawKey+RawMapped type | +| `csmap_X_rawvalue_t` | `struct { i_keyraw first; i_valraw second; }` | i_keyraw+i_valraw type | | `csmap_X_key_t` | `i_key` | The key type | | `csmap_X_mapped_t` | `i_val` | The mapped type | | `csmap_X_value_t` | `struct { const i_key first; i_val second; }` | The value: key is immutable | diff --git a/docs/csptr_api.md b/docs/csptr_api.md index ef908691..95c4f93c 100644 --- a/docs/csptr_api.md +++ b/docs/csptr_api.md @@ -2,10 +2,7 @@ **csptr** is a smart pointer that retains shared ownership of an object through a pointer. Several **csptr** objects may own the same object. The object is destroyed and its memory -deallocated when either of the following happens: - -- the last remaining **csptr** owning the object is destroyed with *csptr_X_del()*; -- the last remaining **csptr** owning the object is assigned another pointer via *csptr_X_clone()*, *csptr_X_move()* or by *csptr_X_reset()*. +deallocated when the last remaining **csptr** owning the object is destroyed with *csptr_X_del()*; The object is destroyed using *csptr_X_del()* or a custom deleter that is supplied to **csptr** in the using-statement. @@ -27,19 +24,20 @@ See the c++ classes [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/s #define i_valdel // destroy value func - defaults to empty destruct #include <stc/csptr.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. When declaring a container with shared pointers, define the `i_val_csptr` with the csptr's `i_tag`. See example below. ## Methods -The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on the ***i_cmp*** and ***i_valdel*** macros specified. +The *csptr_X_compare()*, *csptr_X_del()* methods are defined based on the `i_cmp` and `i_valdel` macros specified. -Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. For shared pointers stored in containers, define ***i_val_csptr*** to the shared pointers tag instead of a ***i_val*** macro. See example below. +Use *csptr_X_clone(p)* when sharing ownership of the pointed-to object. For shared pointers stored in containers, define `i_val_csptr` to the shared pointers tag instead of a `i_val` macro. See example below. ```c csptr_X csptr_X_init(); // empty constructor csptr_X csptr_X_make(Value val); // make_shared constructor, fast csptr_X csptr_X_from(Value* p); // construct from raw pointer csptr_X csptr_X_clone(csptr_X ptr); // clone shared (increase use count) csptr_X csptr_X_move(csptr_X* self); // fast transfer ownership to another sptr. +csptr_X_value_t* csptr_X_copy(csptr_X* self, csptr_X other); // copy shared (increase use count) void csptr_X_del(csptr_X* self); // destruct (decrease use count, free at 0) long csptr_X_use_count(csptr_X ptr); @@ -47,7 +45,6 @@ long csptr_X_use_count(csptr_X ptr); void csptr_X_reset(csptr_X* self); csptr_X_value_t* csptr_X_reset_make(csptr_X* self, Value val); // assign new sptr with value csptr_X_value_t* csptr_X_reset_with(csptr_X* self, Value* p); // slower than reset_make(). -csptr_X_value_t* csptr_X_copy(csptr_X* self, CX other); // copy shared (increase use count) int csptr_X_compare(const csptr_X* x, const csptr_X* y); bool csptr_X_equals(const csptr_X* x, const csptr_X* y); @@ -79,7 +76,7 @@ void int_del(int* x) { #include <stc/csset.h> // define a sorted set of csptr_int #define i_val_csptr int -#include <stc/cvec.h> +#include <stc/cvec.h> // define a sorted vector of csptr_int int main() { diff --git a/docs/csset_api.md b/docs/csset_api.md index f15d3a25..2d6c6486 100644 --- a/docs/csset_api.md +++ b/docs/csset_api.md @@ -1,7 +1,7 @@ # STC [csset](../include/stc/csset.h): Sorted Set

-A **csset** is an associative container that contains a sorted set of unique objects of type *Key*. Sorting is done using the key comparison function *keyCompare*. Search, removal, and insertion operations have logarithmic complexity. **csset** is implemented as an AA-tree.
+A **csset** is an associative container that contains a sorted set of unique objects of type *i_key*. Sorting is done using the key comparison function *keyCompare*. Search, removal, and insertion operations have logarithmic complexity. **csset** is implemented as an AA-tree.
See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) for a functional description.
@@ -17,7 +17,7 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo #define i_keydel // destroy key func - defaults to empty destruct
#include <stc/csset.h>
```
-`X` should be replaced by the value of ***i_tag*** in all of the following documentation.
+`X` should be replaced by the value of `i_tag` in all of the following documentation.
## Methods
@@ -26,23 +26,24 @@ csset_X csset_X_init(void); csset_X csset_X_clone(csset_x set);
void csset_X_clear(csset_X* self);
+void csset_X_copy(csset_X* self, csset_X other);
void csset_X_swap(csset_X* a, csset_X* b);
void csset_X_del(csset_X* self); // destructor
bool csset_X_empty(csset_X set);
size_t csset_X_size(csset_X set);
-bool csset_X_contains(const csset_X* self, RawKey rkey);
-csset_X_value_t* csset_X_get(const csset_X* self, RawKey rkey); // return NULL if not found
-csset_X_iter_t csset_X_lower_bound(const csset_X* self, RawKey rkey); // find closest entry >= rkey
-csset_X_iter_t csset_X_find(const csset_X* self, RawKey rkey);
-csset_X_value_t* csset_X_find_it(const csset_X* self, RawKey rkey, csset_X_iter_t* out); // return NULL if not found
+bool csset_X_contains(const csset_X* self, i_keyraw rkey);
+csset_X_value_t* csset_X_get(const csset_X* self, i_keyraw rkey); // return NULL if not found
+csset_X_iter_t csset_X_lower_bound(const csset_X* self, i_keyraw rkey); // find closest entry >= rkey
+csset_X_iter_t csset_X_find(const csset_X* self, i_keyraw rkey);
+csset_X_value_t* csset_X_find_it(const csset_X* self, i_keyraw rkey, csset_X_iter_t* out); // return NULL if not found
-csset_X_result_t csset_X_insert(csset_X* self, Key key);
-csset_X_result_t csset_X_emplace(csset_X* self, RawKey rkey);
-void csset_X_emplace_items(csset_X* self, const RawKey arr[], size_t n);
+csset_X_result_t csset_X_insert(csset_X* self, i_key key);
+csset_X_result_t csset_X_emplace(csset_X* self, i_keyraw rkey);
+void csset_X_emplace_items(csset_X* self, const i_keyraw arr[], size_t n);
-size_t csset_X_erase(csset_X* self, RawKey rkey);
+size_t csset_X_erase(csset_X* self, i_keyraw rkey);
csset_X_iter_t csset_X_erase_at(csset_X* self, csset_X_iter_t it); // return iter after it
csset_X_iter_t csset_X_erase_range(csset_X* self, csset_X_iter_t it1, csset_X_iter_t it2); // return updated it2
diff --git a/docs/cstack_api.md b/docs/cstack_api.md index 7cdfe019..cd230508 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -17,7 +17,7 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cstack.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -26,6 +26,7 @@ cstack_X cstack_X_init(void); cstack_X cstack_X_clone(cstack_X st); void cstack_X_clear(cstack_X* self); +void cstack_X_copy(cstack_X* self, cstack_X other); void cstack_X_del(cstack_X* self); // destructor size_t cstack_X_size(cstack_X st); diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 3e42b704..9d45dcd8 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -21,7 +21,7 @@ See the c++ class [std::vector](https://en.cppreference.com/w/cpp/container/vect #define i_valdel // destroy value func - defaults to empty destruct #include <stc/cvec.h> ``` -`X` should be replaced by the value of ***i_tag*** in all of the following documentation. +`X` should be replaced by the value of `i_tag` in all of the following documentation. ## Methods @@ -32,6 +32,7 @@ cvec_X cvec_X_with_capacity(size_t size); cvec_X cvec_X_clone(cvec_X vec); void cvec_X_clear(cvec_X* self); +void cvec_X_copy(cvec_X* self, cvec_X other); void cvec_X_shrink_to_fit(cvec_X* self); void cvec_X_reserve(cvec_X* self, size_t cap); void cvec_X_resize(cvec_X* self, size_t size, Value fill); @@ -47,16 +48,16 @@ cvec_X_value_t* cvec_X_front(const cvec_X* self); cvec_X_value_t* cvec_X_back(const cvec_X* self); void cvec_X_push_back(cvec_X* self, Value value); -void cvec_X_emplace_back(cvec_X* self, RawValue raw); +void cvec_X_emplace_back(cvec_X* self, i_valraw raw); void cvec_X_pop_back(cvec_X* self); cvec_X_iter_t cvec_X_insert(cvec_X* self, size_t idx, Value value); // move value cvec_X_iter_t cvec_X_insert_n(cvec_X* self, size_t idx, const Value[] arr, size_t n); // move arr values cvec_X_iter_t cvec_X_insert_at(cvec_X* self, cvec_X_iter_t it, Value value); // move value -cvec_X_iter_t cvec_X_emplace(cvec_X* self, size_t idx, RawValue raw); -cvec_X_iter_t cvec_X_emplace_n(cvec_X* self, size_t idx, const RawValue[] arr, size_t n); -cvec_X_iter_t cvec_X_emplace_at(cvec_X* self, cvec_X_iter_t it, RawValue raw); +cvec_X_iter_t cvec_X_emplace(cvec_X* self, size_t idx, i_valraw raw); +cvec_X_iter_t cvec_X_emplace_n(cvec_X* self, size_t idx, const i_valraw[] arr, size_t n); +cvec_X_iter_t cvec_X_emplace_at(cvec_X* self, cvec_X_iter_t it, i_valraw raw); cvec_X_iter_t cvec_X_emplace_range(cvec_X* self, cvec_X_iter_t it, cvec_X_iter_t it1, cvec_X_iter_t it2); // will clone cvec_X_iter_t cvec_X_emplace_range_p(cvec_X* self, Value* pos, @@ -67,11 +68,11 @@ cvec_X_iter_t cvec_X_erase_n(cvec_X* self, size_t idx, size_t n); cvec_X_iter_t cvec_X_erase_at(cvec_X* self, cvec_X_iter_t it); cvec_X_iter_t cvec_X_erase_range(cvec_X* self, cvec_X_iter_t it1, cvec_X_iter_t it2); -cvec_X_iter_t cvec_X_find(const cvec_X* self, RawValue raw); -cvec_X_iter_t cvec_X_find_in(cvec_X_iter_t i1, cvec_X_iter_t i2, RawValue raw); -cvec_X_value_t* cvec_X_get(const cvec_X* self, RawValue raw); // return NULL if not found -cvec_X_iter_t cvec_X_bsearch(const cvec_X* self, RawValue raw); -cvec_X_iter_t cvec_X_bsearch_in(cvec_X_iter_t i1, cvec_X_iter_t i2, RawValue raw); +cvec_X_iter_t cvec_X_find(const cvec_X* self, i_valraw raw); +cvec_X_iter_t cvec_X_find_in(cvec_X_iter_t i1, cvec_X_iter_t i2, i_valraw raw); +cvec_X_value_t* cvec_X_get(const cvec_X* self, i_valraw raw); // return NULL if not found +cvec_X_iter_t cvec_X_bsearch(const cvec_X* self, i_valraw raw); +cvec_X_iter_t cvec_X_bsearch_in(cvec_X_iter_t i1, cvec_X_iter_t i2, i_valraw raw); void cvec_X_sort(cvec_X* self); void cvec_X_sort_range(cvec_X_iter_t i1, cvec_X_iter_t i2, diff --git a/examples/runall.sh b/examples/runall.sh index 71b1c383..cec626a6 100644 --- a/examples/runall.sh +++ b/examples/runall.sh @@ -1,11 +1,15 @@ -#!/bin/sh +#!/bin/bash cc='gcc -std=c99 -pedantic' #cc='clang' #cc='clang -c -DSTC_HEADER' #cc='cl -nologo' #cc='cl -nologo -TP' #cc='cl -nologo -std:c11' +libs='' run=0 +if [ -z "$OS" ]; then + libs='-lm' +fi if [ "$1" == '-h' -o "$1" == '--help' ]; then echo usage: runall.sh [-run] [compiler + options] exit @@ -19,13 +23,13 @@ if [ ! -z "$1" ] ; then fi if [ $run = 0 ] ; then for i in *.c ; do - echo $cc -I../include $i - $cc -I../include $i + echo $cc -I../include $i $libs + $cc -I../include $i $libs done else for i in *.c ; do - echo $cc -I../include $i - $cc -I../include $i + echo $cc -I../include $i $libs + $cc -I../include $i $libs if [ -f $(basename -s .c $i).exe ]; then ./$(basename -s .c $i).exe; fi if [ -f ./a.exe ]; then ./a.exe; fi if [ -f ./a.out ]; then ./a.out; fi diff --git a/examples/sharedptr.c b/examples/sharedptr.c index 4d28e1c1..f1861929 100644 --- a/examples/sharedptr.c +++ b/examples/sharedptr.c @@ -6,6 +6,7 @@ void int_del(int* x) { #define i_val int
#define i_valdel int_del // optional func to show elements destroyed
+#define i_nonatomic // only for single thread; faster.
#include <stc/csptr.h> // define csptr_int shared pointers
#define i_key_csptr int // refer to csptr_int definition above
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);
|
