diff options
| author | Tyge Løvset <[email protected]> | 2020-03-10 09:43:22 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-10 09:43:22 +0100 |
| commit | 1f93cd63a5e2f13d5954c7519c7869ba6c870427 (patch) | |
| tree | 00c196044614262a76107b15177639bb2492378a | |
| parent | 4d237ccc0afa8f2fa655b6b885bae025e0d81993 (diff) | |
| download | STC-modified-1f93cd63a5e2f13d5954c7519c7869ba6c870427.tar.gz STC-modified-1f93cd63a5e2f13d5954c7519c7869ba6c870427.zip | |
Fix map
Swaps inuse buckets with tombstones if possibe when used
| -rw-r--r-- | cdef.h | 4 | ||||
| -rw-r--r-- | cmap.h | 13 | ||||
| -rw-r--r-- | cstring.h | 8 |
3 files changed, 15 insertions, 10 deletions
@@ -37,8 +37,8 @@ // #define foo_1(X) foo_2(X, 100)
// #define foo_2(X, Y) X + Y
-#define _cdef_max_alloca (1000)
-#define _cdef_swap(T, x, y) { T __t = x; x = y; y = __t; }
+#define cdef_max_alloca (1000)
+#define cdef_swap(T, x, y) { T __t = x; x = y; y = __t; }
#define cdef_initRaw(x) (x)
#define cdef_getRaw(x) (x)
@@ -109,7 +109,7 @@ static inline void cmap_##tag##_clear(CMap_##tag* self) { \ \
static inline void cmap_##tag##_swap(CMap_##tag* a, CMap_##tag* b) { \
cvector_map_##tag##_swap(&a->_vec, &b->_vec); \
- _cdef_swap(size_t, a->_size, b->_size); \
+ cdef_swap(size_t, a->_size, b->_size); \
} \
\
static inline void cmap_##tag##_setMaxLoadFactor(CMap_##tag* self, float fac) { \
@@ -126,10 +126,15 @@ static inline size_t cmap_##tag##_bucket(CMap_##tag cm, KeyRaw rawKey) { \ do { \
switch (state) { \
case CMapEntry_VACANT: \
- return erased_idx == cap ? idx : erased_idx; \
+ return erased_idx != cap ? erased_idx : idx; \
case CMapEntry_INUSE: \
- if (keyCompare(&cm._vec.data[idx].key, &rawKey, sizeof(Key)) == 0) return idx; \
- break; \
+ if (keyCompare(&cm._vec.data[idx].key, &rawKey, sizeof(Key)) != 0) \
+ break; \
+ if (erased_idx != cap) { \
+ cdef_swap(CMapEntry_##tag, cm._vec.data[erased_idx], cm._vec.data[idx]); \
+ return erased_idx; \
+ } \
+ return idx; \
case CMapEntry_ERASED: \
if (erased_idx == cap) erased_idx = idx; \
break; \
@@ -138,10 +138,10 @@ static inline void _cstring_internalMove(CString* self, size_t pos1, size_t pos2 }
static inline void cstring_insertN(CString* self, size_t pos, const char* str, size_t n) {
- char* xstr = (char *) memcpy(n > _cdef_max_alloca ? malloc(n) : alloca(n), str, n);
+ char* xstr = (char *) memcpy(n > cdef_max_alloca ? malloc(n) : alloca(n), str, n);
_cstring_internalMove(self, pos, pos + n);
memcpy(&self->str[pos], xstr, n);
- if (n > _cdef_max_alloca) free(xstr);
+ if (n > cdef_max_alloca) free(xstr);
}
static inline void cstring_insert(CString* self, size_t pos, const char* str) {
@@ -161,10 +161,10 @@ static inline size_t cstring_findN(CString cs, size_t pos, const char* needle, s static inline size_t cstring_replaceN(CString* self, size_t pos, const char* s1, size_t n1, const char* s2, size_t n2) {
size_t pos2 = cstring_findN(*self, pos, s1, n1);
if (pos2 == cstring_npos) return cstring_npos;
- char* xs2 = (char *) memcpy(n2 > _cdef_max_alloca ? malloc(n2) : alloca(n2), s2, n2);
+ char* xs2 = (char *) memcpy(n2 > cdef_max_alloca ? malloc(n2) : alloca(n2), s2, n2);
_cstring_internalMove(self, pos2 + n1, pos2 + n2);
memcpy(&self->str[pos2], xs2, n2);
- if (n2 > _cdef_max_alloca) free(xs2);
+ if (n2 > cdef_max_alloca) free(xs2);
return pos2;
}
|
