49 do { |
49 do { |
50 struct cx_hash_map_element_s *next = elem->next; |
50 struct cx_hash_map_element_s *next = elem->next; |
51 // invoke the destructor |
51 // invoke the destructor |
52 cx_invoke_destructor(map, elem->data); |
52 cx_invoke_destructor(map, elem->data); |
53 // free the key data |
53 // free the key data |
54 cxFree(map->allocator, elem->key.data.obj); |
54 cxFree(map->allocator, (void *) elem->key.data); |
55 // free the node |
55 // free the node |
56 cxFree(map->allocator, elem); |
56 cxFree(map->allocator, elem); |
57 // proceed |
57 // proceed |
58 elem = next; |
58 elem = next; |
59 } while (elem != NULL); |
59 } while (elem != NULL); |
98 prev = elm; |
98 prev = elm; |
99 elm = elm->next; |
99 elm = elm->next; |
100 } |
100 } |
101 |
101 |
102 if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len && |
102 if (elm != NULL && elm->key.hash == hash && elm->key.len == key.len && |
103 memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) { |
103 memcmp(elm->key.data, key.data, key.len) == 0) { |
104 // overwrite existing element |
104 // overwrite existing element |
105 if (map->store_pointer) { |
105 if (map->store_pointer) { |
106 memcpy(elm->data, &value, sizeof(void *)); |
106 memcpy(elm->data, &value, sizeof(void *)); |
107 } else { |
107 } else { |
108 memcpy(elm->data, value, map->item_size); |
108 memcpy(elm->data, value, map->item_size); |
127 // copy the key |
127 // copy the key |
128 void *kd = cxMalloc(allocator, key.len); |
128 void *kd = cxMalloc(allocator, key.len); |
129 if (kd == NULL) { |
129 if (kd == NULL) { |
130 return -1; |
130 return -1; |
131 } |
131 } |
132 memcpy(kd, key.data.obj, key.len); |
132 memcpy(kd, key.data, key.len); |
133 e->key.data.obj = kd; |
133 e->key.data = kd; |
134 e->key.len = key.len; |
134 e->key.len = key.len; |
135 e->key.hash = hash; |
135 e->key.hash = hash; |
136 |
136 |
137 // insert the element into the linked list |
137 // insert the element into the linked list |
138 if (prev == NULL) { |
138 if (prev == NULL) { |
160 hash_map->buckets[slot] = elm->next; |
160 hash_map->buckets[slot] = elm->next; |
161 } else { |
161 } else { |
162 prev->next = elm->next; |
162 prev->next = elm->next; |
163 } |
163 } |
164 // free element |
164 // free element |
165 cxFree(hash_map->base.allocator, elm->key.data.obj); |
165 cxFree(hash_map->base.allocator, (void *) elm->key.data); |
166 cxFree(hash_map->base.allocator, elm); |
166 cxFree(hash_map->base.allocator, elm); |
167 // decrease size |
167 // decrease size |
168 hash_map->base.size--; |
168 hash_map->base.size--; |
169 } |
169 } |
170 |
170 |
194 size_t slot = hash % hash_map->bucket_count; |
194 size_t slot = hash % hash_map->bucket_count; |
195 struct cx_hash_map_element_s *elm = hash_map->buckets[slot]; |
195 struct cx_hash_map_element_s *elm = hash_map->buckets[slot]; |
196 struct cx_hash_map_element_s *prev = NULL; |
196 struct cx_hash_map_element_s *prev = NULL; |
197 while (elm && elm->key.hash <= hash) { |
197 while (elm && elm->key.hash <= hash) { |
198 if (elm->key.hash == hash && elm->key.len == key.len) { |
198 if (elm->key.hash == hash && elm->key.len == key.len) { |
199 if (memcmp(elm->key.data.obj, key.data.obj, key.len) == 0) { |
199 if (memcmp(elm->key.data, key.data, key.len) == 0) { |
200 void *data = NULL; |
200 void *data = NULL; |
201 if (destroy) { |
201 if (destroy) { |
202 cx_invoke_destructor(map, elm->data); |
202 cx_invoke_destructor(map, elm->data); |
203 } else { |
203 } else { |
204 if (map->store_pointer) { |
204 if (map->store_pointer) { |