aboutsummaryrefslogtreecommitdiff
path: root/hash_table.h
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-12-29 12:12:26 +0800
committerMistivia <i@mistivia.com>2025-12-29 12:12:26 +0800
commit0074174afc3105701212c6f0922d64d8b266782a (patch)
tree865c2290bb22b71ee01f5abcdb6ddd47ae9ea89f /hash_table.h
parentb96da8398fdfb34e3ee35d00060291154481453c (diff)
type alias for hash table
Diffstat (limited to 'hash_table.h')
-rw-r--r--hash_table.h76
1 files changed, 40 insertions, 36 deletions
diff --git a/hash_table.h b/hash_table.h
index 24814e4..4d52063 100644
--- a/hash_table.h
+++ b/hash_table.h
@@ -16,71 +16,73 @@ struct hash_table {
};
typedef struct hash_table HashTable;
-#define HASH_TABLE_DEF(K, V) \
+#define HASH_TABLE_DEF_AS(K, V, A) \
typedef struct { \
K key; \
V val; \
- } K##2##V##HashTableEntry; \
- typedef K##2##V##HashTableEntry *K##2##V##HashTableIter; \
+ } A##Entry_; \
+ typedef A##Entry_ *A##Iter; \
typedef struct { \
HashTable ht; \
- } K##2##V##HashTable; \
- void K##2##V##HashTable_init(K##2##V##HashTable *self); \
- K##2##V##HashTable K##2##V##HashTable_create(); \
- bool K##2##V##HashTable_insert(K##2##V##HashTable *self, K key, V value); \
- void K##2##V##HashTable_remove(K##2##V##HashTable *ht, K##2##V##HashTableIter iter); \
- V* K##2##V##HashTable_get(K##2##V##HashTable *self, K key); \
- K##2##V##HashTableIter K##2##V##HashTable_find(K##2##V##HashTable *self, K key); \
- K##2##V##HashTableIter K##2##V##HashTable_begin(K##2##V##HashTable *self); \
- K##2##V##HashTableIter K##2##V##HashTable_next(K##2##V##HashTable *self, K##2##V##HashTableIter iter); \
- void K##2##V##HashTable_free(K##2##V##HashTable *self); \
- K##2##V##HashTable K##2##V##HashTable_move(K##2##V##HashTable *self); \
+ } A; \
+ void A##_init(A *self); \
+ A A##_create(); \
+ bool A##_insert(A *self, K key, V value); \
+ void A##_remove(A *ht, A##Iter iter); \
+ V* A##_get(A *self, K key); \
+ A##Iter A##_find(A *self, K key); \
+ A##Iter A##_begin(A *self); \
+ A##Iter A##_next(A *self, A##Iter iter); \
+ void A##_free(A *self); \
+ A A##_move(A *self);
-#define HASH_TABLE_IMPL(K, V) \
- static uint64_t K##2##V##HashTable_hash(void *vk) { \
+#define HASH_TABLE_DEF(K, V) HASH_TABLE_DEF_AS(K, V, K##2##V##HashTable)
+
+#define HASH_TABLE_IMPL_AS(K, V, A) \
+ static uint64_t A##_hash(void *vk) { \
K *k = vk; \
return K##_hash(*k); \
} \
- static bool K##2##V##HashTable_eq(void *vk1, void *vk2) { \
+ static bool A##_eq(void *vk1, void *vk2) { \
K *k1 = vk1, *k2 = vk2; \
return K##_eq(*k1, *k2); \
} \
- void K##2##V##HashTable_init(K##2##V##HashTable *self) { \
- init_hash_table(&self->ht, sizeof(K##2##V##HashTableEntry), 16); \
+ void A##_init(A *self) { \
+ init_hash_table(&self->ht, sizeof(A##Entry_), 16); \
} \
- K##2##V##HashTable K##2##V##HashTable_create() { \
- K##2##V##HashTable self; \
- K##2##V##HashTable_init(&self); \
+ A A##_create() { \
+ A self; \
+ A##_init(&self); \
return self; \
} \
- bool K##2##V##HashTable_insert(K##2##V##HashTable *self, K key, V value) { \
- K##2##V##HashTableEntry entry; \
+ bool A##_insert(A *self, K key, V value) { \
+ A##Entry_ entry; \
entry.key = key; \
entry.val = value; \
- return hash_table_insert(&self->ht, &entry, K##2##V##HashTable_hash, K##2##V##HashTable_eq); \
+ return hash_table_insert(&self->ht, &entry, A##_hash, A##_eq); \
} \
- K##2##V##HashTableIter K##2##V##HashTable_find(K##2##V##HashTable *self, K key) { \
- return hash_table_find(&self->ht, &key, K##2##V##HashTable_hash, K##2##V##HashTable_eq); \
+ A##Iter A##_find(A *self, K key) { \
+ return hash_table_find(&self->ht, &key, A##_hash, A##_eq); \
} \
- V* K##2##V##HashTable_get(K##2##V##HashTable *self, K key) { \
- K##2##V##HashTableEntry* entry = hash_table_find(&self->ht, &key, K##2##V##HashTable_hash, K##2##V##HashTable_eq); \
+ V* A##_get(A *self, K key) { \
+ A##Entry_* entry = hash_table_find(&self->ht, &key, A##_hash, A##_eq); \
if (entry == NULL) return NULL; \
return &(entry->val); \
} \
- void K##2##V##HashTable_remove(K##2##V##HashTable *self, K##2##V##HashTableIter iter) { \
+ void A##_remove(A *self, A##Iter iter) { \
hash_table_remove(&self->ht, iter); \
} \
- K##2##V##HashTableIter K##2##V##HashTable_begin(K##2##V##HashTable *self) { \
+ A##Iter A##_begin(A *self) { \
return hash_table_begin(&self->ht); \
} \
- K##2##V##HashTableIter K##2##V##HashTable_next(K##2##V##HashTable *self, K##2##V##HashTableIter iter) { \
+ A##Iter A##_next(A *self, A##Iter iter) { \
return hash_table_next(&self->ht, iter); \
} \
- void K##2##V##HashTable_free(K##2##V##HashTable *self) { \
+ void A##_free(A *self) { \
destroy_hash_table(&self->ht); \
} \
- K##2##V##HashTable K##2##V##HashTable_move(K##2##V##HashTable *self) { \
- K##2##V##HashTable dup; \
+ A A##_move(A *self) { \
+ A dup; \
dup.ht = self->ht; \
self->ht.buf = NULL; \
self->ht.flagbuf = NULL; \
@@ -88,7 +90,9 @@ typedef struct hash_table HashTable;
self->ht.cap = 0; \
self->ht.taken = 0; \
return dup; \
- } \
+ }
+
+#define HASH_TABLE_IMPL(K, V) HASH_TABLE_IMPL_AS(K, V, K##2##V##HashTable)
HASH_TABLE_DEF(String, Int);
HASH_TABLE_DEF(String, String);