1 | /* |
---|
2 | * hashmap.h |
---|
3 | * Copyright (c) 2009 Vedant Kumar |
---|
4 | * |
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
6 | * of this software and associated documentation files (the "Software"), to deal |
---|
7 | * in the Software without restriction, including without limitation the rights |
---|
8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
9 | * copies of the Software, and to permit persons to whom the Software is |
---|
10 | * furnished to do so, subject to the following conditions: |
---|
11 | * |
---|
12 | * The above copyright notice and this permission notice shall be included in |
---|
13 | * all copies or substantial portions of the Software. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
21 | * THE SOFTWARE. |
---|
22 | */ |
---|
23 | |
---|
24 | #pragma once |
---|
25 | |
---|
26 | #define HMAP_PRESET_SIZE 2 << 6 // use a power of 2 for faster array access |
---|
27 | #define HMAP_GROWTH_RATE 2 |
---|
28 | /* #define HMAP_MAKE_HASHFN */ |
---|
29 | /* #define HMAP_THREAD_SAFE // build with -lrt */ |
---|
30 | /* #define HMAP_DESTRUCTORS */ |
---|
31 | |
---|
32 | #ifdef HMAP_THREAD_SAFE |
---|
33 | #include <semaphore.h> |
---|
34 | #endif |
---|
35 | |
---|
36 | #include <stdint.h> |
---|
37 | #include <stdlib.h> |
---|
38 | #include <stdbool.h> |
---|
39 | |
---|
40 | typedef void* key; |
---|
41 | typedef void* val; |
---|
42 | typedef struct key_val_map key_val_map; |
---|
43 | typedef struct hashmap hashmap; |
---|
44 | |
---|
45 | hashmap* mk_hmap(uint32_t (*hash_fn)(key), |
---|
46 | bool (*eq_fn)(key, key) |
---|
47 | #ifdef HMAP_DESTRUCTORS |
---|
48 | , void (*del_fn)(val) |
---|
49 | #endif |
---|
50 | ); |
---|
51 | |
---|
52 | void free_hmap(hashmap*); |
---|
53 | |
---|
54 | bool __hmap_add(hashmap* hmap, key in, val out); |
---|
55 | #define hmap_add(hmap, in, out) __hmap_add(hmap, (key) in, (val) out) |
---|
56 | |
---|
57 | val __hmap_get(hashmap* hmap, key in); |
---|
58 | #define hmap_get(hmap, obj) __hmap_get(hmap, (val) obj) |
---|