From 1f93cd63a5e2f13d5954c7519c7869ba6c870427 Mon Sep 17 00:00:00 2001 From: Tyge Løvset <60263450+tylo-work@users.noreply.github.com> Date: Tue, 10 Mar 2020 09:43:22 +0100 Subject: Fix map Swaps inuse buckets with tombstones if possibe when used --- cdef.h | 4 ++-- cmap.h | 13 +++++++++---- cstring.h | 8 ++++---- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/cdef.h b/cdef.h index 3282f9ac..865436a0 100644 --- a/cdef.h +++ b/cdef.h @@ -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) diff --git a/cmap.h b/cmap.h index d55cb317..5ec158ec 100644 --- a/cmap.h +++ b/cmap.h @@ -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; \ diff --git a/cstring.h b/cstring.h index 02dd3446..f877847c 100644 --- a/cstring.h +++ b/cstring.h @@ -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; } -- cgit v1.2.3