summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-07-22 17:41:00 +0200
committerTyge Løvset <[email protected]>2020-07-22 17:41:00 +0200
commit392b2bf5c17f9e54a39a4a98c5a96d20e87c08a2 (patch)
treea11607d10b8bae14b25bb1e3f920c3dbeed83994
parent1591c0696586f91b426d023982b5cb1834aff557 (diff)
downloadSTC-modified-392b2bf5c17f9e54a39a4a98c5a96d20e87c08a2.tar.gz
STC-modified-392b2bf5c17f9e54a39a4a98c5a96d20e87c08a2.zip
Fixed cmap_xx_clear()
-rw-r--r--examples/benchmark.c2
-rw-r--r--examples/demos.c2
-rw-r--r--examples/inits.c5
-rw-r--r--stc/cmap.h37
4 files changed, 24 insertions, 22 deletions
diff --git a/examples/benchmark.c b/examples/benchmark.c
index 62a6cb52..3bcd3d5c 100644
--- a/examples/benchmark.c
+++ b/examples/benchmark.c
@@ -38,7 +38,7 @@ sfc64_random_t rng;
#define CMAP_FIND(tag, key) (cmap_##tag##_find(map, key) != NULL)
#define CMAP_SIZE(tag) cmap_size(map)
#define CMAP_BUCKETS(tag) cmap_bucketCount(map)
-#define CMAP_CLEAR(tag) cmap_##tag##_destroy(&map)
+#define CMAP_CLEAR(tag) cmap_##tag##_clear(&map)
#define KMAP_SETUP(tag, Key, Value) khash_t(ii)* map = kh_init(ii); khiter_t ki; int ret
#define KMAP_PUT(tag, key, val) (*(ki = kh_put(ii, map, key, &ret), map->vals[ki] = val, &map->vals[ki]))
diff --git a/examples/demos.c b/examples/demos.c
index 89918d33..9eec136a 100644
--- a/examples/demos.c
+++ b/examples/demos.c
@@ -160,6 +160,8 @@ void mapdemo3()
cmap_ss_put(&table, "Make", cstr_make("my"));
cmap_ss_put(&table, "Sunny", cstr_make("day"));
CMapEntry_ss *e = cmap_ss_find(&table, "Make");
+ c_foreach (i, cmap_ss, table)
+ printf("entry: %s: %s\n", i.item->key.str, i.item->value.str);
printf("size %zu: remove: Make: %s\n", cmap_size(table), e->value.str);
cmap_ss_erase(&table, "Make");
//cmap_ss_eraseEntry(&table, e);
diff --git a/examples/inits.c b/examples/inits.c
index 0848ef80..eb0968b4 100644
--- a/examples/inits.c
+++ b/examples/inits.c
@@ -9,11 +9,12 @@ declare_CMap_str(si, int);
int main(void) {
int year = 2020;
- CMap_ms ms = cmap_ms_from((CMapInput_ms[]) {
+ CMapInput_ms ini[] = {
100, cstr_make("Hello"),
110, cstr_make("World"),
120, cstr_from("Howdy, -%d-", year),
- }, 3);
+ };
+ CMap_ms ms = cmap_ms_from(ini, 3);
c_foreach (i, cmap_ms, ms)
printf("%d: %s\n", i.item->key, i.item->value.str);
diff --git a/stc/cmap.h b/stc/cmap.h
index d9a3ed36..a11ccc40 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -50,6 +50,7 @@ int main(void) {
#define CMAP__H__
#include <stdlib.h>
+#include <string.h>
#include "cdefs.h"
#define cmap_init {NULL, NULL, 0, 0, 0.85f, 0.15f}
@@ -137,7 +138,7 @@ ctype##entry_##tag##_destroy(CType##Entry_##tag* e) { \
keyDestroy(&e->key); \
OPT_1_##ctype(valueDestroy(&e->value);) \
} \
-typedef struct { \
+typedef const struct { \
RawKey key; \
OPT_1_##ctype(Value value;) \
} CType##Input_##tag; \
@@ -165,8 +166,10 @@ STC_API void \
ctype##_##tag##_destroy(CType##_##tag* self); \
STC_API void \
ctype##_##tag##_clear(CType##_##tag* self); \
-STC_API void \
-ctype##_##tag##_setLoadFactors(CType##_##tag* self, float maxLoadFactor, float shrinkLimitFactor); \
+STC_INLINE void \
+ctype##_##tag##_setLoadFactors(CType##_##tag* self, float max, float shrink) { \
+ self->maxLoadFactor = max; self->shrinkLimitFactor = shrink; \
+} \
STC_API CType##Entry_##tag* \
ctype##_##tag##_find(const CType##_##tag* self, CType##RawKey_##tag rawKey); \
STC_API CType##Entry_##tag* /* similar to c++ std::map.insert_or_assign(): */ \
@@ -210,27 +213,23 @@ ctype##_##tag##_from(const CType##Input_##tag in[], size_t size) { \
return hh; \
} \
\
-STC_API void \
-ctype##_##tag##_destroy(CType##_##tag* self) { \
- if (self->size) { \
- size_t cap = self->buckets; \
- CType##Entry_##tag* e = self->table, *end = e + cap; \
- uint8_t *hashx = self->_hashx; \
- for (; e != end; ++e) if (*hashx++) ctype##entry_##tag##_destroy(e); \
- } \
+STC_INLINE void ctype##_##tag##_wipe_(CType##_##tag* self) { \
+ if (self->size == 0) return; \
+ CType##Entry_##tag* e = self->table, *end = e + self->buckets; \
+ uint8_t *hx = self->_hashx; \
+ for (; e != end; ++e) if (*hx++) ctype##entry_##tag##_destroy(e); \
+} \
+ \
+STC_API void ctype##_##tag##_destroy(CType##_##tag* self) { \
+ ctype##_##tag##_wipe_(self); \
free(self->_hashx); \
free(self->table); \
} \
\
STC_API void ctype##_##tag##_clear(CType##_##tag* self) { \
- ctype##_##tag##_destroy(self); \
- self->buckets = self->size = 0; \
-} \
- \
-STC_API void \
-ctype##_##tag##_setLoadFactors(CType##_##tag* self, float maxLoadFactor, float shrinkLimitFactor) { \
- self->maxLoadFactor = maxLoadFactor; \
- self->shrinkLimitFactor = shrinkLimitFactor; \
+ ctype##_##tag##_wipe_(self); \
+ self->size = 0; \
+ memset(self->_hashx, 0, self->buckets); \
} \
\
STC_API size_t \