-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashmap.h
45 lines (33 loc) · 1 KB
/
hashmap.h
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
#pragma once
#include <stdint.h>
#include "array.h"
#define HASHMAP_BASE_SIZE 8
// From CPython
#define USABLE_FRACTION(n) (((n) << 1)/3)
#define ESTIMATE_SIZE(n) (((n)*3+1) >> 1)
#define E_HASHMAP_KEY_NOT_FOUND 1
#define E_HASHMAP_FULL 2
#define E_HASHMAP_CANNOT_SET_VALUE 3
#define E_HASHMAP_CANNOT_RESIZE 4
typedef struct {
char *key;
int value;
int is_deleted;
unsigned long hash;
} hashmap_item;
typedef struct {
uint32_t count;
array *values; // array of hashmap_item
} hashmap;
hashmap *hashmap_init();
void hashmap_free(hashmap *hm);
uint32_t hashmap_len(hashmap *hm);
int hashmap_ensure_size(hashmap *hm);
int hashmap_resize(hashmap *hm);
array *hashmap_values(hashmap *hm);
int hashmap_set(hashmap*, char *key, int value);
hashmap_item *hashmap_get(hashmap *hm, char *key);
int hashmap_delete(hashmap *hm, char *key);
int _hashmap_find_index(hashmap *hm, char *key, unsigned long hash);
int _hashmap_find_empty_index(hashmap *hm, unsigned long hash);
unsigned long hash(char *s);