summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-02-06 21:30:21 +0100
committerTyge Løvset <[email protected]>2021-02-06 21:30:21 +0100
commit1f0785734d0126cff9525ee516fda0e9db8afbea (patch)
tree6a5181585b9aaba7dc754784f7ec02bc4950d3b9
parent8b3ee061718a11827466c9e376bf91a06ad23689 (diff)
downloadSTC-modified-1f0785734d0126cff9525ee516fda0e9db8afbea.tar.gz
STC-modified-1f0785734d0126cff9525ee516fda0e9db8afbea.zip
Some API changes in cmap. Will do for csmap as well.
-rw-r--r--examples/phonebook.c12
-rw-r--r--stc/ccommon.h8
-rw-r--r--stc/cmap.h74
-rw-r--r--stc/csmap.h2
4 files changed, 48 insertions, 48 deletions
diff --git a/examples/phonebook.c b/examples/phonebook.c
index 0f781334..395716bc 100644
--- a/examples/phonebook.c
+++ b/examples/phonebook.c
@@ -26,6 +26,7 @@
#include <stc/cstr.h>
using_cmap_str();
+using_cset_str();
void print_phone_book(cmap_str phone_book)
{
@@ -35,6 +36,11 @@ void print_phone_book(cmap_str phone_book)
int main(int argc, char **argv)
{
+ c_static_assert(3 == 3, "hello");
+
+ c_init (cset_str, names, {"Hello", "Cool", "True"});
+ c_foreach (i, cset_str, names) printf("set: %s\n", i.ref->str);
+
bool erased;
c_init (cmap_str, phone_book, {
{"Lilia Friedman", "(892) 670-4739"},
@@ -46,8 +52,8 @@ int main(int argc, char **argv)
printf("Phone book:\n");
print_phone_book(phone_book);
- c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1880"));
- c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1990"));
+ cmap_str_emplace(&phone_book, "Zak Byers", "(551) 396-1880");
+ cmap_str_emplace(&phone_book, "Zak Byers", "(551) 396-1990");
printf("\nPhone book after adding Zak Byers:\n");
print_phone_book(phone_book);
@@ -61,7 +67,7 @@ int main(int argc, char **argv)
printf("\nPhone book after erasing Tariq and Elliott:\n");
print_phone_book(phone_book);
- cmap_str_insert_or_assign(&phone_book, "Zak Byers", "(555) 396-188");
+ cmap_str_put(&phone_book, "Zak Byers", "(555) 396-188");
printf("\nPhone book after update phone of Zak Byers:\n");
print_phone_book(phone_book);
diff --git a/stc/ccommon.h b/stc/ccommon.h
index acf6e1e6..fd108208 100644
--- a/stc/ccommon.h
+++ b/stc/ccommon.h
@@ -61,7 +61,7 @@
#define _c_OVERLOAD_SELECT(NAME, NUM) _c_CAT( NAME ## _, NUM)
#define c_MACRO_OVERLOAD(NAME, ...) _c_OVERLOAD_SELECT(NAME, _c_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__)
-#define c_static_assert(cond) typedef char _static_assert_[(cond) ? 1 : -1]
+#define c_static_assert(cond, ...) typedef char _static_assert_[(cond) ? 1 : -1]
#define c_container_of(ptr, type, member) \
((type *)((char *)(ptr) - offsetof(type, member)))
@@ -124,12 +124,6 @@
ctype##_push_n(self, __arr, sizeof __arr/sizeof *__arr); \
} while (0)
-/* For cmap_X and csmap_X only: */
-#define c_try_emplace(self, ctype, rkey, mapped) do { \
- ctype##_result_t __r = ctype##_insert_key(self, rkey); \
- if (__r.second) __r.first->second = mapped; \
-} while (0)
-
#define c_del(ctype, ...) do { \
ctype##_t* __arr[] = {__VA_ARGS__}; \
for (size_t __i=0; __i<sizeof __arr/sizeof *__arr; ++__i) \
diff --git a/stc/cmap.h b/stc/cmap.h
index 6c8ee8ad..78f9468f 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -149,11 +149,6 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t;
C##_##X##_mapped_t second;} ) \
C##_##X##_value_t; \
\
- STC_INLINE void \
- C##_##X##_entry_del(C##_##X##_value_t* e) { \
- keyDel(KEY_REF_##C(e)); \
- MAP_ONLY_##C(mappedDel(&e->second);) \
- } \
typedef SET_ONLY_##C( C##_##X##_rawkey_t ) \
MAP_ONLY_##C( struct {C##_##X##_rawkey_t first; \
C##_##X##_rawmapped_t second;} ) \
@@ -221,49 +216,54 @@ typedef struct {size_t idx; uint32_t hx;} cmap_bucket_t, cset_bucket_t;
C##_##X##_contains(const C##_##X* self, RawKey rkey); \
\
STC_API C##_##X##_result_t \
- C##_##X##_insert_key(C##_##X* self, RawKey rkey); \
+ C##_##X##_insert_entry_(C##_##X* self, RawKey rkey); \
STC_API C##_bucket_t \
C##_##X##_bucket(const C##_##X* self, const C##_##X##_rawkey_t* rkeyptr); \
\
STC_INLINE C##_##X##_result_t \
C##_##X##_emplace(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- MAP_ONLY_##C( if (res.second) res.first->second = mappedFromRaw(rmapped); ) \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \
+ if (res.second) { \
+ *KEY_REF_##C(res.first) = keyFromRaw(rkey); \
+ MAP_ONLY_##C(res.first->second = mappedFromRaw(rmapped);) \
+ } \
return res; \
} \
STC_INLINE C##_##X##_result_t \
- C##_##X##_insert(C##_##X* self, C##_##X##_rawvalue_t raw) { \
- return SET_ONLY_##C( C##_##X##_insert_key(self, raw) ) \
- MAP_ONLY_##C( C##_##X##_emplace(self, raw.first, raw.second) ); \
+ C##_##X##_put(C##_##X* self, RawKey rkey MAP_ONLY_##C(, RawMapped rmapped)) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, rkey); \
+ if (res.second) *KEY_REF_##C(res.first) = keyFromRaw(rkey); \
+ MAP_ONLY_##C( else mappedDel(&res.first->second); \
+ res.first->second = mappedFromRaw(rmapped); ) \
+ return res; \
} \
STC_INLINE void \
C##_##X##_push_n(C##_##X* self, const C##_##X##_rawvalue_t arr[], size_t n) { \
- for (size_t i=0; i<n; ++i) C##_##X##_insert(self, arr[i]); \
+ for (size_t i=0; i<n; ++i) SET_ONLY_##C( C##_##X##_emplace(self, arr[i]); ) \
+ MAP_ONLY_##C( C##_##X##_put(self, arr[i].first, arr[i].second); ) \
} \
\
- MAP_ONLY_##C( \
STC_INLINE C##_##X##_result_t \
- C##_##X##_put(C##_##X* self, RawKey rkey, RawMapped rmapped) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- if (!res.second) mappedDel(&res.first->second); \
- res.first->second = mappedFromRaw(rmapped); return res; \
- } \
- STC_INLINE C##_##X##_result_t \
- C##_##X##_insert_or_assign(C##_##X* self, RawKey rkey, RawMapped rmapped) { \
- return C##_##X##_put(self, rkey, rmapped); \
- } \
- STC_INLINE C##_##X##_result_t \
- C##_##X##_put_mapped(C##_##X* self, RawKey rkey, Mapped mapped) { \
- C##_##X##_result_t res = C##_##X##_insert_key(self, rkey); \
- if (!res.second) mappedDel(&res.first->second); \
- res.first->second = mapped; return res; \
+ C##_##X##_insert(C##_##X* self, C##_##X##_value_t val) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(KEY_REF_##C(&val))); \
+ if (res.second) *res.first = val; else C##_##X##_value_del(&val); \
+ return res; \
} \
- STC_INLINE C##_##X##_mapped_t* \
- C##_##X##_at(const C##_##X* self, RawKey rkey) { \
- C##_bucket_t b = C##_##X##_bucket(self, &rkey); \
- assert(self->_hashx[b.idx]); \
- return &self->table[b.idx].second; \
- }) \
+\
+ MAP_ONLY_##C( \
+ STC_INLINE C##_##X##_result_t \
+ C##_##X##_insert_or_assign(C##_##X* self, Key key, Mapped mapped) { \
+ C##_##X##_result_t res = C##_##X##_insert_entry_(self, keyToRaw(&key)); \
+ if (res.second) res.first->first = key; else keyDel(&key); \
+ mappedDel(&res.first->second); res.first->second = mapped; \
+ return res; \
+ } \
+ STC_INLINE C##_##X##_mapped_t* \
+ C##_##X##_at(const C##_##X* self, RawKey rkey) { \
+ C##_bucket_t b = C##_##X##_bucket(self, &rkey); \
+ assert(self->_hashx[b.idx]); \
+ return &self->table[b.idx].second; \
+ }) \
\
STC_INLINE C##_##X##_iter_t \
C##_##X##_begin(C##_##X* self) { \
@@ -325,7 +325,7 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
if (self->size == 0) return; \
C##_##X##_value_t* e = self->table, *end = e + self->bucket_count; \
uint8_t *hx = self->_hashx; \
- for (; e != end; ++e) if (*hx++) C##_##X##_entry_del(e); \
+ for (; e != end; ++e) if (*hx++) C##_##X##_value_del(e); \
} \
\
STC_DEF void C##_##X##_del(C##_##X* self) { \
@@ -364,7 +364,6 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
if (*(it._hx = self->_hashx+b.idx)) it.ref = self->table+b.idx; \
return it; \
} \
-\
STC_DEF bool \
C##_##X##_contains(const C##_##X* self, RawKey rkey) { \
return self->size && self->_hashx[C##_##X##_bucket(self, &rkey).idx]; \
@@ -375,12 +374,11 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
C##_##X##_reserve(self, 5 + self->size * 3 / 2); \
} \
STC_DEF C##_##X##_result_t \
- C##_##X##_insert_key(C##_##X* self, RawKey rkey) { \
+ C##_##X##_insert_entry_(C##_##X* self, RawKey rkey) { \
C##_##X##_reserve_expand_(self); \
C##_bucket_t b = C##_##X##_bucket(self, &rkey); \
C##_##X##_result_t res = {&self->table[b.idx], !self->_hashx[b.idx]}; \
if (res.second) { \
- *KEY_REF_##C(res.first) = keyFromRaw(rkey); \
self->_hashx[b.idx] = (uint8_t) b.hx; \
++self->size; \
} \
@@ -431,7 +429,7 @@ enum {chash_HASH = 0x7f, chash_USED = 0x80};
size_t i = chash_entry_index(*self, val), j = i, k, cap = self->bucket_count; \
C##_##X##_value_t* slot = self->table; \
uint8_t* hashx = self->_hashx; \
- C##_##X##_entry_del(&slot[i]); \
+ C##_##X##_value_del(&slot[i]); \
do { /* delete without leaving tombstone */ \
if (++j == cap) j = 0; \
if (! hashx[j]) \
diff --git a/stc/csmap.h b/stc/csmap.h
index 881d9070..fb2d4b2b 100644
--- a/stc/csmap.h
+++ b/stc/csmap.h
@@ -188,6 +188,8 @@ struct csmap_rep { size_t root, disp, head, size, cap; void* data[]; };
C##_##X##_empty(C##_##X tree) {return _csmap_rep(&tree)->size == 0;} \
STC_INLINE size_t \
C##_##X##_size(C##_##X tree) {return _csmap_rep(&tree)->size;} \
+ STC_INLINE size_t \
+ C##_##X##_capacity(C##_##X tree) {return _csmap_rep(&tree)->cap;} \
STC_API void \
C##_##X##_del(C##_##X* self); \
STC_INLINE void \