diff options
| author | Tyge Løvset <[email protected]> | 2020-03-04 16:53:40 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-04 16:53:40 +0100 |
| commit | 07c3e6d107e987d401f2c119c1ccc77699d32e9a (patch) | |
| tree | b46aa8e72e6377803ad7e4789ad61625668b0abd | |
| parent | a4cc759fbe132b8a642421a49ffca7937feea5c0 (diff) | |
| download | STC-modified-07c3e6d107e987d401f2c119c1ccc77699d32e9a.tar.gz STC-modified-07c3e6d107e987d401f2c119c1ccc77699d32e9a.zip | |
Added untouched to map Entry: can be inspected to see if a put updated a value or not.
| -rw-r--r-- | cmap.h | 35 |
1 files changed, 20 insertions, 15 deletions
@@ -14,7 +14,7 @@ // CMapEntry:
#define declare_CMapEntry_5(tag, Key, Value, keyDestroy, valueDestroy) \
-typedef struct CMapEntry(tag) { Key key; Value value; short _used; } CMapEntry(tag); \
+typedef struct CMapEntry(tag) { Key key; Value value; short untouched, _used; } CMapEntry(tag); \
typedef struct CMapIter(tag) { CMapEntry(tag) *item, *_end; } CMapIter(tag); \
\
static inline void cmapentry_##tag##_destroy(CMapEntry(tag)* p) { \
@@ -73,8 +73,12 @@ static inline void cmap_##tag##_clear(CMap(tag)* self) { \ *self = cm; \
} \
\
+static inline void cmap_##tag##_swap(CMap(tag)* a, CMap(tag)* b) { \
+ cvector__map##tag##_swap(&a->_vec, &b->_vec); \
+ _cdef_swap(cvector_size_t, a->_size, b->_size); \
+} \
\
-static inline cvector_size_t _cmap_##tag##_index(CMap(tag) cm, KeyRaw rawKey) { \
+static inline cvector_size_t _cmap_##tag##_findIndex(CMap(tag) cm, KeyRaw rawKey) { \
cvector_size_t cap = cvector_capacity(cm._vec); \
cvector_size_t idx = keyHasher(&rawKey, sizeof(Key)) % cap, first = idx; \
FIBONACCI_DECL; \
@@ -85,34 +89,25 @@ static inline cvector_size_t _cmap_##tag##_index(CMap(tag) cm, KeyRaw rawKey) { \
static inline CMapEntry(tag)* cmap_##tag##_get(CMap(tag) cm, KeyRaw rawKey) { \
if (cm._size == 0) return NULL; \
- cvector_size_t idx = _cmap_##tag##_index(cm, rawKey); \
+ cvector_size_t idx = _cmap_##tag##_findIndex(cm, rawKey); \
return cm._vec.data[idx]._used ? &cm._vec.data[idx] : NULL; \
} \
\
-static inline int cmap_##tag##_erase(CMap(tag)* self, KeyRaw rawKey) { \
- CMapEntry(tag)* entryPtr = cmap_##tag##_get(*self, rawKey); \
- if (entryPtr) { \
- cmapentry_##tag##_destroy(entryPtr); \
- --self->_size; \
- return 1; \
- } \
- return 0; \
-} \
- \
static inline cvector_size_t cmap_##tag##_rehash(CMap(tag)* self); /* predeclared */ \
\
static inline CMapEntry(tag)* cmap_##tag##_put(CMap(tag)* self, KeyRaw rawKey, Value value) { \
cvector_size_t cap = cvector_capacity(self->_vec); \
if (self->_size >= cap * 8 / 10) \
cap = cmap_##tag##_rehash(self); \
- cvector_size_t idx = _cmap_##tag##_index(*self, rawKey); \
+ cvector_size_t idx = _cmap_##tag##_findIndex(*self, rawKey); \
CMapEntry(tag)* e = &self->_vec.data[idx]; \
+ e->value = value; \
+ e->untouched = !e->_used; \
if (!e->_used) { \
e->key = keyInit(rawKey); \
e->_used = 1; \
++self->_size; \
} \
- e->value = value; \
return e; \
} \
\
@@ -130,6 +125,16 @@ static inline cvector_size_t cmap_##tag##_rehash(CMap(tag)* self) { \ return newcap; \
} \
\
+static inline int cmap_##tag##_erase(CMap(tag)* self, KeyRaw rawKey) { \
+ CMapEntry(tag)* entryPtr = cmap_##tag##_get(*self, rawKey); \
+ if (entryPtr) { \
+ cmapentry_##tag##_destroy(entryPtr); \
+ --self->_size; \
+ return 1; \
+ } \
+ return 0; \
+} \
+ \
\
static inline CMapIter(tag) cmap_##tag##_begin(CMap(tag) map) { \
CMapIter(tag) null = {NULL, NULL}; \
|
