diff options
| author | Tyge Løvset <[email protected]> | 2021-01-04 17:15:46 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-01-04 17:15:46 +0100 |
| commit | 8505d9caf2280c0b7486fef9e018befe9d9fcb96 (patch) | |
| tree | ce24dc3825d43f44840e86fb9d8c188c59cf2db6 | |
| parent | e76652eda0c8e2008fd500adfcf4210e261f0583 (diff) | |
| download | STC-modified-8505d9caf2280c0b7486fef9e018befe9d9fcb96.tar.gz STC-modified-8505d9caf2280c0b7486fef9e018befe9d9fcb96.zip | |
Added value_clone() method to containers.
| -rw-r--r-- | examples/words.c | 4 | ||||
| -rw-r--r-- | stc/ccommon.h | 10 | ||||
| -rw-r--r-- | stc/cdeq.h | 32 | ||||
| -rw-r--r-- | stc/clist.h | 16 | ||||
| -rw-r--r-- | stc/cmap.h | 12 | ||||
| -rw-r--r-- | stc/cpque.h | 6 | ||||
| -rw-r--r-- | stc/cqueue.h | 6 | ||||
| -rw-r--r-- | stc/cstack.h | 6 | ||||
| -rw-r--r-- | stc/cvec.h | 28 |
9 files changed, 71 insertions, 49 deletions
diff --git a/examples/words.c b/examples/words.c index c8e17deb..5f24a944 100644 --- a/examples/words.c +++ b/examples/words.c @@ -11,7 +11,7 @@ using_cmap_strkey(si, int); int main1() { - c_defcnt (clist_str, lwords, { + c_defcon (clist_str, lwords, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); @@ -21,7 +21,7 @@ int main1() printf("%s\n", w.ref->str); puts(""); - c_defcnt (cvec_str, words, { + c_defcon (cvec_str, words, { "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" }); diff --git a/stc/ccommon.h b/stc/ccommon.h index d595c0eb..aae3cdd2 100644 --- a/stc/ccommon.h +++ b/stc/ccommon.h @@ -120,8 +120,14 @@ ctype##_push_n(self, __arr, sizeof __arr/sizeof *__arr); \
} while (0)
-#define c_defcnt(ctype, cnt, ...) \
- ctype cnt = ctype##_init(); c_push_items(&cnt, ctype, __VA_ARGS__)
+#define c_defcon(ctype, c, ...) \
+ ctype c = ctype##_init(); c_push_items(&c, ctype, __VA_ARGS__)
+
+#define c_convert(ctype1, c1, ctype2, c2, put) do { \
+ ctype2* __c2 = &(c2); \
+ c_foreach_3 (__i, ctype1, c1) \
+ ctype2##_##put(__c2, ctype2##_value_clone(*__i.ref)); \
+} while (0)
#define c_del(ctype, ...) do { \
ctype##_t* __arr[] = {__VA_ARGS__}; \
@@ -60,7 +60,9 @@ STC_INLINE size_t \
cdeq_##X##_capacity(cdeq_##X deq) {return deq.base ? _cdeq_capacity(&deq) : 0;} \
STC_INLINE Value \
- cdeq_##X##_value_from_raw(RawValue rawValue) {return valueFromRaw(rawValue);} \
+ cdeq_##X##_value_from_raw(RawValue raw) {return valueFromRaw(raw);} \
+ STC_INLINE cdeq_##X##_value_t \
+ cdeq_##X##_value_clone(cdeq_##X##_value_t val) {return valueFromRaw(valueToRaw(&val));} \
STC_INLINE void \
cdeq_##X##_clear(cdeq_##X* self); \
STC_API void \
@@ -98,8 +100,8 @@ STC_API void \
cdeq_##X##_push_back(cdeq_##X* self, Value value); \
STC_INLINE void \
- cdeq_##X##_emplace_back(cdeq_##X* self, RawValue rawValue) { \
- cdeq_##X##_push_back(self, valueFromRaw(rawValue)); \
+ cdeq_##X##_emplace_back(cdeq_##X* self, RawValue raw) { \
+ cdeq_##X##_push_back(self, valueFromRaw(raw)); \
} \
STC_INLINE void \
cdeq_##X##_pop_back(cdeq_##X* self) { \
@@ -109,8 +111,8 @@ STC_INLINE void \
cdeq_##X##_push_front(cdeq_##X* self, Value value); \
STC_INLINE void \
- cdeq_##X##_emplace_front(cdeq_##X* self, RawValue rawValue) { \
- cdeq_##X##_push_front(self, valueFromRaw(rawValue)); \
+ cdeq_##X##_emplace_front(cdeq_##X* self, RawValue raw) { \
+ cdeq_##X##_push_front(self, valueFromRaw(raw)); \
} \
STC_INLINE void \
cdeq_##X##_pop_front(cdeq_##X* self) { \
@@ -134,12 +136,12 @@ return cdeq_##X##_insert_range_p(self, self->data + idx, &value, &value + 1); \
} \
STC_INLINE cdeq_##X##_iter_t \
- cdeq_##X##_emplace(cdeq_##X* self, cdeq_##X##_iter_t pos, RawValue rawValue) { \
- return cdeq_##X##_insert(self, pos, valueFromRaw(rawValue)); \
+ cdeq_##X##_emplace(cdeq_##X* self, cdeq_##X##_iter_t pos, RawValue raw) { \
+ return cdeq_##X##_insert(self, pos, valueFromRaw(raw)); \
} \
STC_INLINE cdeq_##X##_iter_t \
- cdeq_##X##_emplace_at(cdeq_##X* self, size_t idx, RawValue rawValue) { \
- return cdeq_##X##_insert_at(self, idx, valueFromRaw(rawValue)); \
+ cdeq_##X##_emplace_at(cdeq_##X* self, size_t idx, RawValue raw) { \
+ return cdeq_##X##_insert_at(self, idx, valueFromRaw(raw)); \
} \
\
STC_API cdeq_##X##_iter_t \
@@ -159,9 +161,9 @@ } \
\
STC_API cdeq_##X##_iter_t \
- cdeq_##X##_find(const cdeq_##X* self, RawValue rawValue); \
+ cdeq_##X##_find(const cdeq_##X* self, RawValue raw); \
STC_API cdeq_##X##_iter_t \
- cdeq_##X##_find_in_range(const cdeq_##X* self, cdeq_##X##_iter_t first, cdeq_##X##_iter_t finish, RawValue rawValue); \
+ cdeq_##X##_find_in_range(const cdeq_##X* self, cdeq_##X##_iter_t first, cdeq_##X##_iter_t finish, RawValue raw); \
\
STC_INLINE cdeq_##X##_value_t* \
cdeq_##X##_front(cdeq_##X* self) {return self->data;} \
@@ -314,16 +316,16 @@ } \
\
STC_DEF cdeq_##X##_iter_t \
- cdeq_##X##_find_in_range(const cdeq_##X* self, cdeq_##X##_iter_t first, cdeq_##X##_iter_t finish, RawValue rawValue) { \
+ cdeq_##X##_find_in_range(const cdeq_##X* self, cdeq_##X##_iter_t first, cdeq_##X##_iter_t finish, RawValue raw) { \
for (; first.ref != finish.ref; cdeq_##X##_next(&first)) { \
RawValue r = valueToRaw(first.ref); \
- if (valueCompareRaw(&r, &rawValue) == 0) return first; \
+ if (valueCompareRaw(&r, &raw) == 0) return first; \
} \
return cdeq_##X##_end(self); \
} \
STC_DEF cdeq_##X##_iter_t \
- cdeq_##X##_find(const cdeq_##X* self, RawValue rawValue) { \
- return cdeq_##X##_find_in_range(self, cdeq_##X##_begin(self), cdeq_##X##_end(self), rawValue); \
+ cdeq_##X##_find(const cdeq_##X* self, RawValue raw) { \
+ return cdeq_##X##_find_in_range(self, cdeq_##X##_begin(self), cdeq_##X##_end(self), raw); \
} \
\
STC_DEF int \
diff --git a/stc/clist.h b/stc/clist.h index 5c5a5db5..e2374213 100644 --- a/stc/clist.h +++ b/stc/clist.h @@ -111,7 +111,9 @@ STC_API size_t _clist_size(const clist_void* self); STC_INLINE size_t \
clist_##X##_size(clist_##X ls) {return _clist_size((const clist_void*) &ls);} \
STC_INLINE Value \
- clist_##X##_value_from_raw(RawValue rawValue) {return valueFromRaw(rawValue);} \
+ clist_##X##_value_from_raw(RawValue raw) {return valueFromRaw(raw);} \
+ STC_INLINE clist_##X##_value_t \
+ clist_##X##_value_clone(clist_##X##_value_t val) {return valueFromRaw(valueToRaw(&val));} \
\
STC_API void \
clist_##X##_del(clist_##X* self); \
@@ -125,14 +127,14 @@ STC_API size_t _clist_size(const clist_void* self); STC_API void \
clist_##X##_push_back(clist_##X* self, Value value); \
STC_INLINE void \
- clist_##X##_emplace_back(clist_##X* self, RawValue rawValue) { \
- clist_##X##_push_back(self, valueFromRaw(rawValue)); \
+ clist_##X##_emplace_back(clist_##X* self, RawValue raw) { \
+ clist_##X##_push_back(self, valueFromRaw(raw)); \
} \
STC_API void \
clist_##X##_push_front(clist_##X* self, Value value); \
STC_INLINE void \
- clist_##X##_emplace_front(clist_##X* self, RawValue rawValue) { \
- clist_##X##_push_front(self, valueFromRaw(rawValue)); \
+ clist_##X##_emplace_front(clist_##X* self, RawValue raw) { \
+ clist_##X##_push_front(self, valueFromRaw(raw)); \
} \
\
STC_API clist_##X##_node_t* \
@@ -171,8 +173,8 @@ STC_API size_t _clist_size(const clist_void* self); STC_API clist_##X##_iter_t \
clist_##X##_insert_after(clist_##X* self, clist_##X##_iter_t pos, Value value); \
STC_INLINE clist_##X##_iter_t \
- clist_##X##_emplace_after(clist_##X* self, clist_##X##_iter_t pos, RawValue rawValue) { \
- return clist_##X##_insert_after(self, pos, valueFromRaw(rawValue)); \
+ clist_##X##_emplace_after(clist_##X* self, clist_##X##_iter_t pos, RawValue raw) { \
+ return clist_##X##_insert_after(self, pos, valueFromRaw(raw)); \
} \
STC_API clist_##X##_iter_t \
clist_##X##_erase_after(clist_##X* self, clist_##X##_iter_t pos); \
@@ -199,6 +199,12 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; ctype##_##X##_empty(ctype##_##X m) {return m.size == 0;} \
STC_INLINE size_t \
ctype##_##X##_size(ctype##_##X m) {return (size_t) m.size;} \
+ STC_INLINE ctype##_##X##_value_t \
+ ctype##_##X##_value_clone(ctype##_##X##_value_t val) { \
+ KEY_REF_##ctype(&val) = keyFromRaw(keyToRaw(&KEY_REF_##ctype(&val))); \
+ CMAP_ONLY_##ctype( val.second = mappedFromRaw(mappedToRaw(&val.second)); ) \
+ return val; \
+ } \
STC_INLINE size_t \
ctype##_##X##_bucket_count(ctype##_##X m) {return (size_t) m.bucket_count;} \
STC_INLINE size_t \
@@ -397,10 +403,8 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t; m.size, m.bucket_count, m.max_load_factor, m.shrink_limit_factor \
}; \
ctype##_##X##_value_t *e = m.table, *end = e + m.bucket_count, *dst = clone.table; \
- for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst) if (*hx) { \
- KEY_REF_##ctype(dst) = keyFromRaw(keyToRaw(&KEY_REF_##ctype(e))); \
- CMAP_ONLY_##ctype( dst->second = mappedFromRaw(mappedToRaw(&e->second)); ) \
- } \
+ for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst) \
+ if (*hx) *dst = ctype##_##X##_value_clone(*e); \
return clone; \
} \
\
diff --git a/stc/cpque.h b/stc/cpque.h index 6f3dc201..da2ad690 100644 --- a/stc/cpque.h +++ b/stc/cpque.h @@ -59,6 +59,8 @@ cpque_##X##_init(void) {return ctype##_init();} \
STC_INLINE cpque_##X \
cpque_##X##_clone(cpque_##X pq) {return ctype##_clone(pq);} \
+ STC_INLINE cpque_##X##_value_t \
+ cpque_##X##_value_clone(cpque_##X##_value_t val) {return ctype##_value_clone(val);} \
STC_INLINE size_t \
cpque_##X##_size(cpque_##X pq) {return ctype##_size(pq);} \
STC_INLINE bool \
@@ -76,8 +78,8 @@ STC_API void \
cpque_##X##_push(cpque_##X* self, cpque_##X##_value_t value); \
STC_INLINE void \
- cpque_##X##_emplace(cpque_##X* self, cpque_##X##_rawvalue_t rawValue) { \
- cpque_##X##_push(self, ctype##_value_from_raw(rawValue)); \
+ cpque_##X##_emplace(cpque_##X* self, cpque_##X##_rawvalue_t raw) { \
+ cpque_##X##_push(self, ctype##_value_from_raw(raw)); \
} \
STC_API void \
cpque_##X##_push_n(cpque_##X *self, const cpque_##X##_input_t in[], size_t size); \
diff --git a/stc/cqueue.h b/stc/cqueue.h index cb4b0563..0d349a26 100644 --- a/stc/cqueue.h +++ b/stc/cqueue.h @@ -66,6 +66,8 @@ cqueue_##X##_init(void) {return ctype##_init();} \
STC_INLINE cqueue_##X \
cqueue_##X##_clone(cqueue_##X q) {return ctype##_clone(q);} \
+ STC_INLINE cqueue_##X##_value_t \
+ cqueue_##X##_value_clone(cqueue_##X##_value_t val) {return ctype##_value_clone(val);} \
STC_INLINE void \
cqueue_##X##_del(cqueue_##X* self) {ctype##_del(self);} \
STC_INLINE size_t \
@@ -83,8 +85,8 @@ ctype##_push_back(self, value); \
} \
STC_INLINE void \
- cqueue_##X##_emplace(cqueue_##X* self, cqueue_##X##_rawvalue_t rawValue) { \
- ctype##_emplace_back(self, rawValue); \
+ cqueue_##X##_emplace(cqueue_##X* self, cqueue_##X##_rawvalue_t raw) { \
+ ctype##_emplace_back(self, raw); \
} \
STC_INLINE void \
cqueue_##X##_push_n(cqueue_##X *self, const cqueue_##X##_input_t in[], size_t size) { \
diff --git a/stc/cstack.h b/stc/cstack.h index 45abd596..7bf9cfe3 100644 --- a/stc/cstack.h +++ b/stc/cstack.h @@ -55,6 +55,8 @@ cstack_##X##_init(void) {return ctype##_init();} \
STC_INLINE cstack_##X \
cstack_##X##_clone(cstack_##X st) {return ctype##_clone(st);} \
+ STC_INLINE cstack_##X##_value_t \
+ cstack_##X##_value_clone(cstack_##X##_value_t val) {return ctype##_value_clone(val);} \
STC_INLINE void \
cstack_##X##_del(cstack_##X* self) {ctype##_del(self);} \
STC_INLINE size_t \
@@ -70,8 +72,8 @@ ctype##_push_back(self, value); \
} \
STC_INLINE void \
- cstack_##X##_emplace(cstack_##X* self, cstack_##X##_rawvalue_t rawValue) { \
- ctype##_emplace_back(self, rawValue); \
+ cstack_##X##_emplace(cstack_##X* self, cstack_##X##_rawvalue_t raw) { \
+ ctype##_emplace_back(self, raw); \
} \
STC_INLINE void \
cstack_##X##_push_n(cstack_##X *self, const cstack_##X##_input_t in[], size_t size) { \
@@ -62,7 +62,9 @@ STC_INLINE bool \
cvec_##X##_empty(cvec_##X vec) {return !cvec_##X##_size(vec);} \
STC_INLINE Value \
- cvec_##X##_value_from_raw(RawValue rawValue) {return valueFromRaw(rawValue);} \
+ cvec_##X##_value_from_raw(RawValue raw) {return valueFromRaw(raw);} \
+ STC_INLINE cvec_##X##_value_t \
+ cvec_##X##_value_clone(cvec_##X##_value_t val) {return valueFromRaw(valueToRaw(&val));} \
STC_INLINE void \
cvec_##X##_clear(cvec_##X* self); \
STC_API void \
@@ -99,8 +101,8 @@ STC_API void \
cvec_##X##_push_back(cvec_##X* self, Value value); \
STC_INLINE void \
- cvec_##X##_emplace_back(cvec_##X* self, RawValue rawValue) { \
- cvec_##X##_push_back(self, valueFromRaw(rawValue)); \
+ cvec_##X##_emplace_back(cvec_##X* self, RawValue raw) { \
+ cvec_##X##_push_back(self, valueFromRaw(raw)); \
} \
STC_INLINE void \
cvec_##X##_pop_back(cvec_##X* self) { \
@@ -123,12 +125,12 @@ return cvec_##X##_insert_range_p(self, self->data + idx, &value, &value + 1); \
} \
STC_INLINE cvec_##X##_iter_t \
- cvec_##X##_emplace(cvec_##X* self, cvec_##X##_iter_t pos, RawValue rawValue) { \
- return cvec_##X##_insert(self, pos, valueFromRaw(rawValue)); \
+ cvec_##X##_emplace(cvec_##X* self, cvec_##X##_iter_t pos, RawValue raw) { \
+ return cvec_##X##_insert(self, pos, valueFromRaw(raw)); \
} \
STC_INLINE cvec_##X##_iter_t \
- cvec_##X##_emplace_at(cvec_##X* self, size_t idx, RawValue rawValue) { \
- return cvec_##X##_insert_at(self, idx, valueFromRaw(rawValue)); \
+ cvec_##X##_emplace_at(cvec_##X* self, size_t idx, RawValue raw) { \
+ return cvec_##X##_insert_at(self, idx, valueFromRaw(raw)); \
} \
\
STC_API cvec_##X##_iter_t \
@@ -148,9 +150,9 @@ } \
\
STC_API cvec_##X##_iter_t \
- cvec_##X##_find(const cvec_##X* self, RawValue rawValue); \
+ cvec_##X##_find(const cvec_##X* self, RawValue raw); \
STC_API cvec_##X##_iter_t \
- cvec_##X##_find_in_range(const cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish, RawValue rawValue); \
+ cvec_##X##_find_in_range(const cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish, RawValue raw); \
\
STC_INLINE cvec_##X##_value_t* \
cvec_##X##_front(cvec_##X* self) {return self->data;} \
@@ -281,16 +283,16 @@ } \
\
STC_DEF cvec_##X##_iter_t \
- cvec_##X##_find_in_range(const cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish, RawValue rawValue) { \
+ cvec_##X##_find_in_range(const cvec_##X* self, cvec_##X##_iter_t first, cvec_##X##_iter_t finish, RawValue raw) { \
for (; first.ref != finish.ref; cvec_##X##_next(&first)) { \
RawValue r = valueToRaw(first.ref); \
- if (valueCompareRaw(&r, &rawValue) == 0) return first; \
+ if (valueCompareRaw(&r, &raw) == 0) return first; \
} \
return cvec_##X##_end(self); \
} \
STC_DEF cvec_##X##_iter_t \
- cvec_##X##_find(const cvec_##X* self, RawValue rawValue) { \
- return cvec_##X##_find_in_range(self, cvec_##X##_begin(self), cvec_##X##_end(self), rawValue); \
+ cvec_##X##_find(const cvec_##X* self, RawValue raw) { \
+ return cvec_##X##_find_in_range(self, cvec_##X##_begin(self), cvec_##X##_end(self), raw); \
} \
\
STC_DEF int \
|
