diff options
| author | Tyge Løvset <[email protected]> | 2020-03-25 11:40:30 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-25 11:40:30 +0100 |
| commit | 533cb7a1099645b7d2c1c1bc91a22cba75d34735 (patch) | |
| tree | 9c1703d6cca9d4b8a849fc2a63365c7730803b89 | |
| parent | 2c228b239cc317651c0506396b1209af2cfc1406 (diff) | |
| download | STC-modified-533cb7a1099645b7d2c1c1bc91a22cba75d34735.tar.gz STC-modified-533cb7a1099645b7d2c1c1bc91a22cba75d34735.zip | |
Add files via upload
| -rw-r--r-- | c_lib/cdefs.h | 1 | ||||
| -rw-r--r-- | c_lib/cmap.h | 38 | ||||
| -rw-r--r-- | c_lib/cstring.h | 1 |
3 files changed, 32 insertions, 8 deletions
diff --git a/c_lib/cdefs.h b/c_lib/cdefs.h index 86a457e6..4c76cefd 100644 --- a/c_lib/cdefs.h +++ b/c_lib/cdefs.h @@ -47,6 +47,7 @@ #define c_defaultInitRaw(x) (x)
#define c_defaultGetRaw(x) (x)
#define c_defaultCompare(x, y) (*(x) == *(y) ? 0 : *(x) < *(y) ? -1 : 1)
+#define c_defaultEquals(x, y) (*(x) == *(y))
static inline void c_defaultDestroy(void* value) {}
#define c_foreach(it, ctag, con) \
diff --git a/c_lib/cmap.h b/c_lib/cmap.h index 852006fb..c685a096 100644 --- a/c_lib/cmap.h +++ b/c_lib/cmap.h @@ -38,6 +38,10 @@ struct CMapEntry_##tag { \ uint16_t hashx; \
}; \
\
+static inline struct CMapEntry_##tag cmapentry_##tag##_make(Key key, Value value) { \
+ struct CMapEntry_##tag e = {key, value, 0}; \
+ return e; \
+} \
static inline void cmapentry_##tag##_destroy(struct CMapEntry_##tag* e) { \
keyDestroy(&e->key); \
valueDestroy(&e->value); \
@@ -56,10 +60,10 @@ enum {cmapentry_HASH=0x7fff, cmapentry_USED=0x8000}; declare_CMap_5(tag, Key, Value, c_defaultDestroy, c_defaultHash)
#define declare_CMap_5(tag, Key, Value, valueDestroy, keyHash) \
- declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, c_defaultCompare, c_defaultDestroy)
+ declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, c_defaultEquals, c_defaultDestroy)
-#define declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, keyCompare, keyDestroy) \
- declare_CMap_10(tag, Key, Value, valueDestroy, keyHash, keyCompare, keyDestroy, \
+#define declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, keyEquals, keyDestroy) \
+ declare_CMap_10(tag, Key, Value, valueDestroy, keyHash, keyEquals, keyDestroy, \
Key, c_defaultGetRaw, c_defaultInitRaw)
@@ -70,12 +74,12 @@ enum {cmapentry_HASH=0x7fff, cmapentry_USED=0x8000}; declare_CMap_stringkey_3(tag, Value, c_defaultDestroy)
#define declare_CMap_stringkey_3(tag, Value, valueDestroy) \
- declare_CMap_10(tag, CString, Value, valueDestroy, cstring_hashRaw, cstring_compareRaw, cstring_destroy, \
+ declare_CMap_10(tag, CString, Value, valueDestroy, cstring_hashRaw, cstring_equalsRaw, cstring_destroy, \
const char* const, cstring_getRaw, cstring_make)
// CMap full:
-#define declare_CMap_10(tag, Key, Value, valueDestroy, keyHashRaw, keyCompareRaw, keyDestroy, \
+#define declare_CMap_10(tag, Key, Value, valueDestroy, keyHashRaw, keyEqualsRaw, keyDestroy, \
RawKey, keyGetRaw, keyInitRaw) \
declare_CMapEntry(tag, Key, Value, valueDestroy, keyDestroy); \
declare_CVector_4(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy, cmapentry_noCompare); \
@@ -134,7 +138,7 @@ static inline size_t cmap_##tag##_bucket(CMap_##tag* self, cmap_##tag##_rawkey_t size_t cap = cvector_capacity(self->_table); \
size_t idx = c_reduce(hash, cap); \
CMapEntry_##tag* slot = self->_table.data; \
- while (slot[idx].hashx && (slot[idx].hashx != hx || keyCompareRaw(keyGetRaw(&slot[idx].key), rawKey) != 0)) { \
+ while (slot[idx].hashx && (slot[idx].hashx != hx || !keyEqualsRaw(keyGetRaw(&slot[idx].key), rawKey))) { \
if (++idx == cap) idx = 0; \
} \
*hxPtr = hx; \
@@ -148,10 +152,14 @@ static inline CMapEntry_##tag* cmap_##tag##_get(CMap_##tag map, cmap_##tag##_raw return map._table.data[idx].hashx ? &map._table.data[idx] : NULL; \
} \
\
-static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, cmap_##tag##_rawkey_t rawKey, Value value) { \
+static inline void cmap_##tag##_expand(CMap_##tag* self) { \
size_t cap = cvector_capacity(self->_table); \
if (cmap_size(*self) + 1 >= cap * self->maxLoadPercent * 0.01) \
- cap = cmap_##tag##_reserve(self, (size_t) 7 + (1.6 * cap)); \
+ cmap_##tag##_reserve(self, (size_t) 7 + (1.6 * cap)); \
+} \
+ \
+static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, cmap_##tag##_rawkey_t rawKey, Value value) { \
+ cmap_##tag##_expand(self); \
uint32_t hx; \
size_t idx = cmap_##tag##_bucket(self, &rawKey, &hx); \
CMapEntry_##tag* e = &self->_table.data[idx]; \
@@ -164,6 +172,20 @@ static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, cmap_##tag##_r return e; \
} \
\
+static inline CMapEntry_##tag* cmap_##tag##_insert(CMap_##tag* self, CMapEntry_##tag entry) { \
+ cmap_##tag##_expand(self); \
+ uint32_t hx; \
+ size_t idx = cmap_##tag##_bucket(self, keyGetRaw(&entry.key), &hx); \
+ CMapEntry_##tag* e = &self->_table.data[idx]; \
+ if (! e->hashx) { \
+ e->key = entry.key; \
+ e->hashx = hx; \
+ ++self->_size; \
+ } \
+ e->value = entry.value; \
+ return e; \
+} \
+ \
static inline size_t cmap_##tag##_reserve(CMap_##tag* self, size_t size) { \
size_t oldcap = cvector_capacity(self->_table), newcap = 1 + (size / 2) * 2; \
if (cmap_size(*self) >= newcap * self->maxLoadPercent * 0.01) return oldcap; \
diff --git a/c_lib/cstring.h b/c_lib/cstring.h index 869c7d5a..92cb1555 100644 --- a/c_lib/cstring.h +++ b/c_lib/cstring.h @@ -243,6 +243,7 @@ static inline CString cstring_temp(const char* str) { #define cstring_getRaw(x) (&(x)->str)
#define cstring_compareRaw(x, y) strcmp(*(x), *(y))
+#define cstring_equalsRaw(x, y) (strcmp(*(x), *(y)) == 0)
static inline uint32_t cstring_hashRaw(const char* const* str, size_t ignored) { return c_defaultHash(*str, strlen(*str)); }
|
