|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2011 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 |
|
33 #include "map.h" |
|
34 |
|
35 hashmap_t* hashmap_new(size_t size) { |
|
36 hashmap_t *map = malloc(sizeof(hashmap_t)); |
|
37 map->map = calloc(size, sizeof(sdlist_t)); |
|
38 map->len = size; |
|
39 |
|
40 return map; |
|
41 } |
|
42 |
|
43 void hashmap_free(hashmap_t *map) { |
|
44 for(int i=0;i<map->len;i++) { |
|
45 sdlist_t *list = map->map[0]; |
|
46 while(list != NULL) { |
|
47 hashmap_elm_t *elm = (hashmap_elm_t*)list->data; |
|
48 free(elm->key.ptr); |
|
49 sdlist_t *l = list; |
|
50 list = list->next; |
|
51 free(elm); |
|
52 free(l); |
|
53 } |
|
54 } |
|
55 free(map->map); |
|
56 free(map); |
|
57 } |
|
58 |
|
59 |
|
60 void hashmap_put(hashmap_t *map, sstr_t key, void *value) { |
|
61 int hash = hashmap_hash(key.ptr, key.length); |
|
62 |
|
63 sdlist_t *list = map->map[hash%map->len]; |
|
64 |
|
65 sstr_t k = sstrdub(key); |
|
66 |
|
67 hashmap_elm_t *elm = malloc(sizeof(hashmap_elm_t)); |
|
68 elm->data = value; |
|
69 elm->hash = hash; |
|
70 elm->key = k; |
|
71 |
|
72 map->map[hash%map->len] = sdlist_add(list, elm); |
|
73 } |
|
74 |
|
75 void* hashmap_get(hashmap_t *map, sstr_t key) { |
|
76 int hash = hashmap_hash(key.ptr, key.length); |
|
77 sdlist_t *list = map->map[hash%map->len]; |
|
78 void *value = NULL; |
|
79 |
|
80 while (list != NULL) { |
|
81 hashmap_elm_t *elm = list->data; |
|
82 if (elm->hash == hash && |
|
83 strncmp(key.ptr, elm->key.ptr, key.length) == 0) { |
|
84 value = elm->data; |
|
85 break; |
|
86 } |
|
87 list = list->next; |
|
88 } |
|
89 |
|
90 return value; |
|
91 } |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 /* hash function */ |
|
97 int hashmap_hash(char *data, size_t len) { |
|
98 /* murmur hash 2 */ |
|
99 |
|
100 int m = 0x5bd1e995; |
|
101 int r = 24; |
|
102 |
|
103 int h = 25 ^ len; |
|
104 |
|
105 int i = 0; |
|
106 while (len >= 4) { |
|
107 int k = data[i + 0] & 0xFF; |
|
108 k |= (data[i + 1] & 0xFF) << 8; |
|
109 k |= (data[i + 2] & 0xFF) << 16; |
|
110 k |= (data[i + 3] & 0xFF) << 24; |
|
111 |
|
112 k *= m; |
|
113 k ^= k >> r; |
|
114 k *= m; |
|
115 |
|
116 h *= m; |
|
117 h ^= k; |
|
118 |
|
119 i += 4; |
|
120 len -= 4; |
|
121 } |
|
122 |
|
123 switch (len) { |
|
124 case 3: h ^= (data[i + 2] & 0xFF) << 16; |
|
125 case 2: h ^= (data[i + 1] & 0xFF) << 8; |
|
126 case 1: h ^= (data[i + 0] & 0xFF); h *= m; |
|
127 } |
|
128 |
|
129 h ^= h >> 13; |
|
130 h *= m; |
|
131 h ^= h >> 15; |
|
132 |
|
133 return h; |
|
134 } |