aboutsummaryrefslogtreecommitdiff
path: root/src/hmap.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/hmap.c')
-rw-r--r--src/hmap.c160
1 files changed, 80 insertions, 80 deletions
diff --git a/src/hmap.c b/src/hmap.c
index fb58a88..8acecd2 100644
--- a/src/hmap.c
+++ b/src/hmap.c
@@ -8,28 +8,28 @@
#if SIZE_MAX == 0xFFFFFFFF
/* If size_t is 32bit */
-static const size_t fnv_prime = 16777619u;
+static const size_t fnv_prime = 16777619u;
static const size_t fnv_offsetb = 2166136261u;
#elif SIZE_MAX == 0xFFFFFFFFFFFFFFFF
/* If size_t is 64bit */
-static const size_t fnv_prime = 1099511628211u;
+static const size_t fnv_prime = 1099511628211u;
static const size_t fnv_offsetb = 14695981039346656037u;
#else
/* If size_t is 128bit. Maybe this is overdoing it? */
-static const size_t fnv_prime = 309485009821345068724781371u;
+static const size_t fnv_prime = 309485009821345068724781371u;
static const size_t fnv_offsetb = 144066263297769815596495629667062367629u;
#endif
struct hnode {
- struct slice key;
- void *value;
- struct hnode *next;
+ struct slice key;
+ void *value;
+ struct hnode *next;
};
struct hmap_iter {
- struct hmap *map;
- size_t index;
- size_t count;
+ struct hmap *map;
+ size_t index;
+ size_t count;
struct hnode *cur;
};
@@ -38,7 +38,7 @@ static size_t
hash_slice(const struct slice *slice)
{
size_t hash = fnv_offsetb;
- size_t i = slice->start;
+ size_t i = slice->start;
while (i < slice->end) {
hash ^= slice->str[i];
hash *= fnv_prime;
@@ -52,8 +52,8 @@ struct hmap *
hmap_new_with_cap(size_t cap)
{
struct hmap *hm = malloc(sizeof *hm);
- hm->cap = cap;
- hm->size = 0;
+ hm->cap = cap;
+ hm->size = 0;
if (hm == NULL) return NULL;
hm->buckets = calloc(cap, sizeof hm->buckets);
if (hm->buckets == NULL) {
@@ -67,43 +67,43 @@ hmap_new_with_cap(size_t cap)
void *
hmap_sets(struct hmap *hm, struct slice key, void *value)
{
- int pos = hash_slice(&key) % hm->cap;
- struct hnode *head = hm->buckets[pos];
- struct hnode *node = head;
- void *old_value = NULL;
-
- while (node) {
- if (slice_cmp(&node->key, &key) == 0) {
- old_value = node->value;
- node->value = value;
- return old_value;
- }
- node = node->next;
- }
-
- node = malloc(sizeof *node);
- node->key = key;
- node->value = value;
- node->next = head;
- hm->buckets[pos] = node;
- hm->size++;
- return old_value;
+ int pos = hash_slice(&key) % hm->cap;
+ struct hnode *head = hm->buckets[pos];
+ struct hnode *node = head;
+ void *old_value = NULL;
+
+ while (node) {
+ if (slice_cmp(&node->key, &key) == 0) {
+ old_value = node->value;
+ node->value = value;
+ return old_value;
+ }
+ node = node->next;
+ }
+
+ node = malloc(sizeof *node);
+ node->key = key;
+ node->value = value;
+ node->next = head;
+ hm->buckets[pos] = node;
+ hm->size++;
+ return old_value;
}
void *
hmap_gets(struct hmap *hm, const struct slice *key)
{
- size_t pos = hash_slice(key) % hm->cap;
- struct hnode *node = hm->buckets[pos];
- while (node != NULL) {
- if (slice_cmp(&node->key, key) == 0) {
- return node->value;
- }
-
- node = node->next;
- }
+ size_t pos = hash_slice(key) % hm->cap;
+ struct hnode *node = hm->buckets[pos];
+ while (node != NULL) {
+ if (slice_cmp(&node->key, key) == 0) {
+ return node->value;
+ }
+
+ node = node->next;
+ }
- return NULL;
+ return NULL;
}
void *
@@ -116,29 +116,29 @@ hmap_get(struct hmap *hm, const char *k)
void *
hmap_removes(struct hmap *hm, const struct slice *key)
{
- int pos = hash_slice(key) % hm->cap;
- struct hnode *node = hm->buckets[pos];
- struct hnode *prev = NULL;
- void *old_value;
-
- while (node) {
- if (slice_cmp(&node->key, key) == 0) {
- if (prev) {
- prev->next = node->next;
- } else {
- hm->buckets[pos] = node->next;
- }
- old_value = node->value;
- free(node);
- hm->size--;
- return old_value;
- }
-
- prev = node;
- node = node->next;
- }
-
- return NULL;
+ int pos = hash_slice(key) % hm->cap;
+ struct hnode *node = hm->buckets[pos];
+ struct hnode *prev = NULL;
+ void *old_value;
+
+ while (node) {
+ if (slice_cmp(&node->key, key) == 0) {
+ if (prev) {
+ prev->next = node->next;
+ } else {
+ hm->buckets[pos] = node->next;
+ }
+ old_value = node->value;
+ free(node);
+ hm->size--;
+ return old_value;
+ }
+
+ prev = node;
+ node = node->next;
+ }
+
+ return NULL;
}
void *
@@ -148,16 +148,16 @@ hmap_remove(struct hmap *hm, const char *k)
return hmap_removes(hm, &key);
}
-#define HMAP_WALK(hm, ...) \
- struct hnode *node; \
- struct hnode *next; \
+#define HMAP_WALK(hm, ...) \
+ struct hnode *node; \
+ struct hnode *next; \
for (size_t i = 0; i < hm->cap; i++) { \
- node = hm->buckets[i]; \
- while (node) { \
- next = node->next; \
- __VA_ARGS__; \
- node = next; \
- } \
+ node = hm->buckets[i]; \
+ while (node) { \
+ next = node->next; \
+ __VA_ARGS__; \
+ node = next; \
+ } \
}
void
@@ -170,10 +170,10 @@ struct hmap_iter *
hmap_iter_new(struct hmap *hm)
{
struct hmap_iter *iter = malloc(sizeof(*iter));
- iter->map = hm;
- iter->index = 0;
- iter->count = hm->size;
- iter->cur = NULL;
+ iter->map = hm;
+ iter->index = 0;
+ iter->count = hm->size;
+ iter->cur = NULL;
return iter;
}
@@ -190,7 +190,7 @@ hmap_iter_next(struct hmap_iter *iter, const struct slice **key, void **value)
} else {
iter->cur = iter->cur->next;
}
- *key = &iter->cur->key;
+ *key = &iter->cur->key;
*value = iter->cur->value;
iter->count--;