agendafs

A filesystem for your calendar.

git clone git://mccd.space/agendafs

hashmap.h (1931B)

      1 // Taken from https://github.com/prismz/hashmap
      2 //
      3 #ifndef HASHMAP_H
      4 #define HASHMAP_H
      5 
      6 #include "arena.h"
      7 #include <stdint.h>
      8 #include <stdio.h>
      9 #include <stdlib.h>
     10 #include <string.h>
     11 
     12 #define HM_CALLOC_FUNC calloc
     13 #define HM_STRDUP_FUNC strdup
     14 #define HM_EXIT_ON_ALLOC_FAIL 1
     15 #define HM_HASHMAP_MAX_LOAD 0.70f
     16 #define HM_RESIZE_SCALE_FACTOR 2
     17 #define HM_DEFAULT_HASHMAP_SIZE 16
     18 
     19 /* enabling this will print something for
     20  * each internal action, not recommended */
     21 // #define HM_DEBUG
     22 
     23 /* if enabled this will not trust the hash function.
     24  * It will search every item in the hashmap (after trying
     25  * the hashing method). */
     26 // #define HM_DONTTRUSTHASH
     27 
     28 /*
     29  * if using structs as values, this conveniently
     30  * typecasts your struct's free function
     31  * so that it can be passed to new_hashmap_item()
     32  */
     33 #define hashmap_item_free_func(a) (void (*)(void *)) a
     34 
     35 /* https://create.stephan-brumme.com/fnv-hash */
     36 #define HM_FNV_PRIME 0x01000193 //   16777619
     37 #define HM_FNV_SEED 0x811C9DC5	// 2166136261
     38 typedef void (*hashmap_iter_callback)(const char *key, void *value,
     39 				      void *user_data);
     40 
     41 struct bucket;
     42 struct bucket {
     43 	uint32_t hash;
     44 	char *key;
     45 	void *val;
     46 
     47 	/* collisions are stored as linked lists */
     48 	struct bucket *next;
     49 };
     50 
     51 struct hashmap {
     52 	struct bucket **buckets;
     53 	size_t n_buckets;
     54 	size_t capacity;
     55 	void (*val_free_func)(void *);
     56 };
     57 
     58 struct hashmap *
     59 hashmap_new(void (*val_free_func)(void *));
     60 void
     61 hashmap_free(struct hashmap *hm);
     62 int
     63 hashmap_resize(struct hashmap *hm);
     64 int
     65 hashmap_insert(struct hashmap *hm, const char *key, void *val);
     66 void *
     67 hashmap_get(struct hashmap *hm, const char *key);
     68 int
     69 hashmap_remove(struct hashmap *hm, const char *key);
     70 void
     71 hashmap_print(struct hashmap *hm);
     72 char **
     73 hashmap_get_keys(struct hashmap *hm, size_t *n_keys);
     74 void
     75 hashmap_free_keys(char **keys, size_t n_keys);
     76 struct hashmap *
     77 rhashmap_new(arena *region, void (*val_free_func)(void *));
     78 #endif /* HASHMAP_H */