summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-03-19 09:42:54 +0100
committerGitHub <[email protected]>2020-03-19 09:42:54 +0100
commitffef6a1c3bb46256e40e5613653abf480e7ee73c (patch)
treea55af5c73a43b012dc45a5b2f177985148809007
parentb1f9a5d615507600d190bcb1c2b8f33a13e46da5 (diff)
downloadSTC-modified-ffef6a1c3bb46256e40e5613653abf480e7ee73c.tar.gz
STC-modified-ffef6a1c3bb46256e40e5613653abf480e7ee73c.zip
Added sort
Changed compare function API
-rw-r--r--c_lib/cdefs.h1
-rw-r--r--c_lib/cmap.h23
-rw-r--r--c_lib/cstring.h1
-rw-r--r--c_lib/cvector.h24
4 files changed, 32 insertions, 17 deletions
diff --git a/c_lib/cdefs.h b/c_lib/cdefs.h
index 598dc863..f9de4878 100644
--- a/c_lib/cdefs.h
+++ b/c_lib/cdefs.h
@@ -46,6 +46,7 @@
#define c_defaultInitRaw(x) (x)
#define c_defaultGetRaw(x) (x)
+#define c_defaultCompare(x, y) ((x) < (y) ? -1 : (x) > (y) ? 1 : 0)
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 218ef0e3..85b56f79 100644
--- a/c_lib/cmap.h
+++ b/c_lib/cmap.h
@@ -47,18 +47,21 @@ static inline void cmapentry_##tag##_destroy(struct CMapEntry_##tag* e) { \
} \
typedef struct CMapEntry_##tag CMapEntry_##tag
+#define cmapentry_noCompare(x, y) (0)
+
// CMap:
#define declare_CMap(...) c_MACRO_OVERLOAD(declare_CMap, __VA_ARGS__)
#define declare_CMap_3(tag, Key, Value) \
- declare_CMap_4(tag, Key, Value, c_defaultDestroy)
+ declare_CMap_5(tag, Key, Value, c_defaultDestroy, c_defaultHash)
-#define declare_CMap_4(tag, Key, Value, valueDestroy) \
- declare_CMap_7(tag, Key, Value, valueDestroy, memcmp, c_defaultHash, c_defaultDestroy)
+#define declare_CMap_5(tag, Key, Value, valueDestroy, keyHash) \
+ declare_CMap_7(tag, Key, Value, valueDestroy, keyHash, c_defaultCompare, c_defaultDestroy)
-#define declare_CMap_7(tag, Key, Value, valueDestroy, keyCompare, keyHash, keyDestroy) \
- declare_CMap_10(tag, Key, Value, valueDestroy, keyCompare, keyHash, keyDestroy, Key, c_defaultGetRaw, c_defaultInitRaw)
+#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)
// CMap<CString, Value>:
@@ -68,15 +71,15 @@ typedef struct CMapEntry_##tag CMapEntry_##tag
declare_CMap_stringkey_3(tag, Value, c_defaultDestroy)
#define declare_CMap_stringkey_3(tag, Value, valueDestroy) \
- declare_CMap_10(tag, CString, Value, valueDestroy, cstring_compareRaw, cstring_hashRaw, cstring_destroy, \
+ declare_CMap_10(tag, CString, Value, valueDestroy, cstring_hashRaw, strcmp, cstring_destroy, \
const char*, cstring_getRaw, cstring_make)
// CMap full:
-#define declare_CMap_10(tag, Key, Value, valueDestroy, keyCompareRaw, keyHashRaw, keyDestroy, \
+#define declare_CMap_10(tag, Key, Value, valueDestroy, keyHashRaw, keyCompareRaw, keyDestroy, \
KeyRaw, keyGetRaw, keyInitRaw) \
declare_CMapEntry(tag, Key, Value, valueDestroy, keyDestroy); \
- declare_CVector_3(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy); \
+ declare_CVector_4(map_##tag, CMapEntry_##tag, cmapentry_##tag##_destroy, cmapentry_noCompare); \
\
typedef struct CMap_##tag { \
CVector_map_##tag _table; \
@@ -125,7 +128,8 @@ static inline size_t cmap_##tag##_bucket(CMap_##tag* self, KeyRaw rawKey, uint32
uint32_t hash = keyHashRaw(&rawKey, sizeof(KeyRaw)); \
size_t cap = cvector_capacity(self->_table); \
size_t idx = c_reduce(hash, cap); \
- while (self->_table.data[idx].used && (self->_table.data[idx].hash != hash || keyCompareRaw(&self->_table.data[idx].key, &rawKey, sizeof(Key)) != 0)) {\
+ CMapEntry_##tag* slot = self->_table.data; \
+ while (slot[idx].used && (slot[idx].hash != hash || keyCompareRaw(keyGetRaw(slot[idx].key), rawKey) != 0)) { \
if (++idx == cap) idx = 0; \
} \
*h = hash; \
@@ -221,5 +225,4 @@ static inline cmap_##tag##_iter_t cmap_##tag##_end(CMap_##tag map) { \
typedef Key cmap_##tag##_key_t; \
typedef Value cmap_##tag##_value_t
-
#endif
diff --git a/c_lib/cstring.h b/c_lib/cstring.h
index 08e6c4c6..e40721fc 100644
--- a/c_lib/cstring.h
+++ b/c_lib/cstring.h
@@ -238,7 +238,6 @@ static inline char* cstring_splitNext(const char* delimiters) {
#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)); }
-static inline int cstring_compareRaw(CString* self, const char** str, size_t sz_ignored) { return strcmp(self->str, *str); }
#endif
diff --git a/c_lib/cvector.h b/c_lib/cvector.h
index 3e6208c1..15e43289 100644
--- a/c_lib/cvector.h
+++ b/c_lib/cvector.h
@@ -37,13 +37,13 @@
#define declare_CVector_2(tag, Value) \
declare_CVector_3(tag, Value, c_defaultDestroy)
#define declare_CVector_3(tag, Value, valueDestroy) \
- declare_CVector_5(tag, Value, valueDestroy, memcmp, Value)
+ declare_CVector_4(tag, Value, valueDestroy, c_defaultCompare)
#define declare_CVector_4(tag, Value, valueDestroy, valueCompare) \
- declare_CVector_5(tag, Value, valueDestroy, valueCompare, Value)
+ declare_CVector_6(tag, Value, valueDestroy, valueCompare, Value, c_defaultGetRaw)
#define declare_CVector_string(tag) \
- declare_CVector_5(tag, CString, cstring_destroy, cstring_compareRaw, const char*)
+ declare_CVector_6(tag, CString, cstring_destroy, strcmp, const char*, cstring_getRaw)
-#define declare_CVector_5(tag, Value, valueDestroy, valueCompareRaw, ValueRaw) \
+#define declare_CVector_6(tag, Value, valueDestroy, valueCompare, ValueRaw, valueGetRaw) \
typedef struct CVector_##tag { \
Value* data; \
} CVector_##tag; \
@@ -110,11 +110,23 @@ 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) { \
+ ValueRaw a = valueGetRaw(*(const Value *) x); \
+ ValueRaw b = valueGetRaw(*(const Value *) y); \
+ return valueCompare(a, b); \
+} \
+ \
+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, ValueRaw rawValue) { \
size_t n = cvector_size(cv); \
- for (size_t i = 0; i < n; ++i) \
- if (valueCompareRaw(&cv.data[i], &rawValue, sizeof(Value)) == 0) return i; \
+ for (size_t i = 0; i < n; ++i) { \
+ ValueRaw a = valueGetRaw(cv.data[i]); \
+ if (valueCompare(a, rawValue) == 0) return i; \
+ } \
return c_npos; \
} \
\