aboutsummaryrefslogtreecommitdiff
path: root/src/include/hash_table.h
blob: 55136ba18f5fab1a52c487f9ffa4b655621e9494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#ifndef HASH_TABLE_H
#define HASH_TABLE_H
#include "list.h"
#include <stdlib.h>

#define DEFAULT_HT_SIZE 500

typedef struct PAIR_STRUCT {
  union {
    char *key;
    int ikey;
  };
  void *v;
} pair_t;

typedef struct {
  list_t **buckets;
  size_t size;
  bool is_map;
} ht_t;

pair_t *init_pair(char *key, void *value);

void *bucket_get(list_t *b, char *key);

void *bucket_pop(list_t *b, char *key);

void bucket_free(void *x, void (*freefunc)(void *));

pair_t *init_long_pair(unsigned long key, void *value);

void *bucket_get_by_long(list_t *b, unsigned long key);

void *bucket_pop_by_long(list_t *b, unsigned long key);

void bucket_free_with_long(void *x, void (*freefunc)(void *));

ht_t *init_ht(size_t size);

ht_t *init_map(size_t size);

void map_insert(ht_t *ht, unsigned long key, void *value);

void *map_pop(ht_t *ht, unsigned long key);

void *map_get(ht_t *ht, unsigned long key);

void map_free(void *x);

void ht_insert(ht_t *ht, char *key, void *value);

/*! @brief returns the old value, mutates ht state with new value */
void *ht_modify(ht_t *ht, char *key, void *newval);

/*! @brief overwrites old value with new value while freeing the old */
void ht_overwrite(ht_t *ht, char *key, void *newval, void (*freefunc)(void *));

void *ht_pop(ht_t *ht, char *key);

void *ht_get(ht_t *ht, char *key);

void ht_free(void *x, void (*freefunc)(void *));

/* djb2 hash function */
unsigned long hash(char *key);
#endif