diff options
| author | Tyge Løvset <[email protected]> | 2020-03-24 19:27:26 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2020-03-24 19:27:26 +0100 |
| commit | 1086f8075de19eb62fe3407a01bc2c2f353a2f93 (patch) | |
| tree | c9e1a728bb7f387efd9df25de413d131e44e1a53 | |
| parent | 17261b08b01ac56bfd19733f8ff55629a9738382 (diff) | |
| download | STC-modified-1086f8075de19eb62fe3407a01bc2c2f353a2f93.tar.gz STC-modified-1086f8075de19eb62fe3407a01bc2c2f353a2f93.zip | |
Add files via upload
Fix
| -rw-r--r-- | c_lib/cdefs.h | 3 | ||||
| -rw-r--r-- | c_lib/cmap.h | 30 | ||||
| -rw-r--r-- | c_lib/cstring.h | 5 | ||||
| -rw-r--r-- | c_lib/cvector.h | 12 |
4 files changed, 24 insertions, 26 deletions
diff --git a/c_lib/cdefs.h b/c_lib/cdefs.h index f0210987..86a457e6 100644 --- a/c_lib/cdefs.h +++ b/c_lib/cdefs.h @@ -46,8 +46,7 @@ #define c_defaultInitRaw(x) (x)
#define c_defaultGetRaw(x) (x)
-#define c_defaultCompare(x, y) (*(x) < *(y) ? -1 : *(x) > *(y) ? 1 : 0)
-#define c_defaultEquals(x, y) (*(x) == *(y))
+#define c_defaultCompare(x, y) (*(x) == *(y) ? 0 : *(x) < *(y) ? -1 : 1)
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 ef44e2ce..8ebba47c 100644 --- a/c_lib/cmap.h +++ b/c_lib/cmap.h @@ -56,10 +56,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_defaultEquals, c_defaultDestroy)
+ declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, c_defaultCompare, c_defaultDestroy)
-#define declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, keyEquals, keyDestroy) \
- declare_CMap_10(tag, Key, Value, valueDestroy, keyHash, keyEquals, keyDestroy, \
+#define declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, keyCompare, keyDestroy) \
+ declare_CMap_10(tag, Key, Value, valueDestroy, keyHash, keyCompare, keyDestroy, \
Key, c_defaultGetRaw, c_defaultInitRaw)
@@ -70,15 +70,16 @@ 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, strcmp, cstring_destroy, \
- const char*, cstring_getRaw, cstring_make)
+ declare_CMap_10(tag, CString, Value, valueDestroy, cstring_hashRaw, cstring_compareRaw, cstring_destroy, \
+ const char* const, cstring_getRaw, cstring_make)
// CMap full:
-#define declare_CMap_10(tag, Key, Value, valueDestroy, keyHashRaw, keyEquals, keyDestroy, \
- KeyRaw, keyGetRaw, keyInitRaw) \
+#define declare_CMap_10(tag, Key, Value, valueDestroy, keyHashRaw, keyCompareRaw, keyDestroy, \
+ RawKey, keyGetRaw, keyInitRaw) \
declare_CMapEntry(tag, Key, Value, valueDestroy, keyDestroy); \
declare_CVector_4(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy, cmapentry_noCompare); \
+ typedef RawKey cmap_##tag##_rawkey_t; \
\
typedef struct CMap_##tag { \
CVector_map_##tag _table; \
@@ -128,26 +129,26 @@ static inline void cmap_##tag##_setShrinkLimitFactor(CMap_##tag* self, double li cmap_##tag##_reserve(self, (size_t) (cmap_size(*self) * 1.2 / limit)); \
} \
\
-static inline size_t cmap_##tag##_bucket(CMap_##tag* self, KeyRaw* rawKey, uint32_t* hxPtr) { \
- uint32_t hash = keyHashRaw(rawKey, sizeof(KeyRaw)), hx = (hash & cmapentry_HASH) | cmapentry_USED; \
+static inline size_t cmap_##tag##_bucket(CMap_##tag* self, const cmap_##tag##_rawkey_t* rawKey, uint32_t* hxPtr) { \
+ uint32_t hash = keyHashRaw(rawKey, sizeof(cmap_##tag##_rawkey_t)), hx = (hash & cmapentry_HASH) | cmapentry_USED; \
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 || !keyEquals(keyGetRaw(&slot[idx].key), rawKey))) { \
+ while (slot[idx].hashx && (slot[idx].hashx != hx || keyCompareRaw(keyGetRaw(&slot[idx].key), rawKey) != 0)) { \
if (++idx == cap) idx = 0; \
} \
*hxPtr = hx; \
return idx; \
} \
\
-static inline CMapEntry_##tag* cmap_##tag##_get(CMap_##tag map, KeyRaw rawKey) { \
+static inline CMapEntry_##tag* cmap_##tag##_get(CMap_##tag map, cmap_##tag##_rawkey_t rawKey) { \
if (cmap_size(map) == 0) return NULL; \
uint32_t hx; \
size_t idx = cmap_##tag##_bucket(&map, &rawKey, &hx); \
return map._table.data[idx].hashx ? &map._table.data[idx] : NULL; \
} \
\
-static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, KeyRaw rawKey, Value value) { \
+static inline CMapEntry_##tag* cmap_##tag##_put(CMap_##tag* self, cmap_##tag##_rawkey_t rawKey, Value value) { \
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)); \
@@ -180,7 +181,7 @@ static inline size_t cmap_##tag##_reserve(CMap_##tag* self, size_t size) { \ return newcap; \
} \
\
-static inline bool cmap_##tag##_erase(CMap_##tag* self, KeyRaw rawKey) { \
+static inline bool cmap_##tag##_erase(CMap_##tag* self, cmap_##tag##_rawkey_t rawKey) { \
if (cmap_size(*self) == 0) \
return false; \
size_t cap = cvector_capacity(self->_table); \
@@ -195,7 +196,7 @@ static inline bool cmap_##tag##_erase(CMap_##tag* self, KeyRaw rawKey) { \ if (++j == cap) j = 0; /* j %= cap; is slow */ \
if (! slot[j].hashx) \
break; \
- k = c_reduce(keyHashRaw(keyGetRaw(&slot[j].key), sizeof(KeyRaw)), cap); \
+ k = c_reduce(keyHashRaw(keyGetRaw(&slot[j].key), sizeof(cmap_##tag##_rawkey_t)), cap); \
if ((j < i) ^ (k <= i) ^ (k > j)) /* is k outside (i, j]? */ \
slot[i] = slot[j], i = j; \
} while (true); \
@@ -222,7 +223,6 @@ static inline cmap_##tag##_iter_t cmap_##tag##_end(CMap_##tag map) { \ cmap_##tag##_iter_t it = {end, end}; \
return it; \
} \
- \
typedef Key cmap_##tag##_key_t; \
typedef Value cmap_##tag##_value_t
diff --git a/c_lib/cstring.h b/c_lib/cstring.h index 1bcd3d61..869c7d5a 100644 --- a/c_lib/cstring.h +++ b/c_lib/cstring.h @@ -241,8 +241,9 @@ static inline CString cstring_temp(const char* str) { // CVector / CMap API functions:
-#define cstring_getRaw(x) ((x)->str)
-static inline uint32_t cstring_hashRaw(const char** str, size_t sz_ignored) { return c_defaultHash(*str, strlen(*str)); }
+#define cstring_getRaw(x) (&(x)->str)
+#define cstring_compareRaw(x, y) strcmp(*(x), *(y))
+static inline uint32_t cstring_hashRaw(const char* const* str, size_t ignored) { return c_defaultHash(*str, strlen(*str)); }
#endif
diff --git a/c_lib/cvector.h b/c_lib/cvector.h index 7cb1e082..aafa7c7f 100644 --- a/c_lib/cvector.h +++ b/c_lib/cvector.h @@ -41,7 +41,7 @@ #define declare_CVector_4(tag, Value, valueDestroy, valueCompare) \
declare_CVector_6(tag, Value, valueDestroy, valueCompare, Value, c_defaultGetRaw)
#define declare_CVector_string(tag) \
- declare_CVector_6(tag, CString, cstring_destroy, strcmp, const char*, cstring_getRaw)
+ declare_CVector_6(tag, CString, cstring_destroy, cstring_compareRaw, const char*, cstring_getRaw)
#define declare_CVector_6(tag, Value, valueDestroy, valueCompare, ValueRaw, valueGetRaw) \
typedef struct CVector_##tag { \
@@ -111,20 +111,18 @@ static inline void cvector_##tag##_erase(CVector_##tag* self, size_t pos, size_t } \
\
static inline int cvector_##tag##_sortCompare(const void* x, const void* y) { \
- return valueCompare(valueGetRaw((const Value *) x), \
- valueGetRaw((const Value *) y)); \
+ return valueCompare(valueGetRaw((const Value *) x), valueGetRaw((const Value *) y)); \
} \
\
static inline void cvector_##tag##_sort(CVector_##tag* self) { \
size_t len = cvector_size(*self); \
if (len) qsort(self->data, len, sizeof(Value), cvector_##tag##_sortCompare); \
} \
-\
-static inline size_t cvector_##tag##_find(CVector_##tag cv, Value value) { \
+ \
+static inline size_t cvector_##tag##_find(CVector_##tag cv, ValueRaw rawValue) { \
size_t n = cvector_size(cv); \
for (size_t i = 0; i < n; ++i) { \
- if (valueCompare(valueGetRaw(&cv.data[i]), valueGetRaw(&value)) == 0) \
- return i; \
+ if (valueCompare(valueGetRaw(&cv.data[i]), &rawValue) == 0) return i; \
} \
return c_npos; \
} \
|
