]>
git.datanom.net - hashtable.git/blob - hashtable.c
28 static uint32_t hash_table_index(Hashtable
* ht
, const void* key
) {
29 return (ht
->hash(key
) % ht
->size
);
32 static void hash_table_default_print(void* data
) {
36 Hashtable
* hash_table_create(HashFunc hf
, EqualFunc ef
) {
37 return hash_table_create_full(hf
, ef
, TABLESIZE
, NULL
, NULL
);
40 Hashtable
* hash_table_create_notify(HashFunc hf
, EqualFunc ef
, FreeFunc ff
) {
41 return hash_table_create_full(hf
, ef
, TABLESIZE
, ff
, NULL
);
44 Hashtable
* hash_table_create_full(HashFunc hf
, EqualFunc ef
, uint32_t size
, FreeFunc ff
, PrintFunc pf
) {
45 Hashtable
* ht
= malloc(sizeof(*ht
));
57 ht
->print
= hash_table_default_print
;
59 ht
->nodes
= calloc(sizeof(Node
*), ht
->size
);
63 void hash_table_destroy(Hashtable
* ht
) {
64 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
65 while (ht
->nodes
[i
]) {
66 Node
* node
= ht
->nodes
[i
];
67 ht
->nodes
[i
] = ht
->nodes
[i
]->next
;
77 void hash_table_set_print_func(Hashtable
* ht
, PrintFunc pf
) {
81 void hash_table_print(Hashtable
* ht
) {
82 printf("Start Table\n");
83 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
84 if (ht
->nodes
[i
] == NULL
) {
85 //printf("\t%i\t---\n", i);
87 printf("\t%i\t\n", i
);
88 Node
* node
= ht
->nodes
[i
];
89 while (node
!= NULL
) {
90 printf("\"%p\" - (", node
->key
);
91 ht
->print(node
->data
);
98 printf("End Table\n");
101 bool hash_table_insert(Hashtable
* ht
, const void* key
, void* obj
) {
102 if (key
== NULL
|| obj
== NULL
|| ht
== NULL
) return false;
105 if (hash_table_lookup(ht
, key
) != NULL
) return false;
107 uint32_t index
= hash_table_index(ht
, key
);
109 Node
* node
= malloc(sizeof(*node
));
111 node
->key
= (void*)key
;
113 node
->next
= ht
->nodes
[index
];
114 ht
->nodes
[index
] = node
;
118 void* hash_table_lookup(Hashtable
* ht
, const void* key
) {
119 if (key
== NULL
|| ht
== NULL
) return false;
120 uint32_t index
= hash_table_index(ht
, key
);
122 Node
* node
= ht
->nodes
[index
];
123 while (node
!= NULL
&& ht
->compare(node
->data
, key
) == false) {
127 if (node
== NULL
) return NULL
;
128 //printf("find: %p -> found: %p\n", key, node->data);
133 void* hash_table_delete(Hashtable
* ht
, const void* key
) {
134 if (key
== NULL
|| ht
== NULL
) return false;
135 uint64_t index
= hash_table_index(ht
, key
);
137 Node
* node
= ht
->nodes
[index
];
139 while (node
!= NULL
&& ht
->compare(node
->data
, key
) == false) {
144 if (node
== NULL
) return NULL
;
146 ht
->nodes
[index
] = node
->next
;
148 prev
->next
= node
->next
;
151 void* result
= node
->data
;
156 List
* hash_table_keys(Hashtable
* ht
) {
161 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
162 if (ht
->nodes
[i
] != NULL
) {
163 Node
* node
= ht
->nodes
[i
];
164 while (node
!= NULL
) {
166 head
= calloc(sizeof(List
*), 1);
170 head
->next
= calloc(sizeof(List
*), 1);
173 head
->data
= ht
->nodes
[i
]->key
;
183 List
* hash_table_values(Hashtable
* ht
) {
185 values
= head
= NULL
;
188 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
189 if (ht
->nodes
[i
] != NULL
) {
190 Node
* node
= ht
->nodes
[i
];
191 while (node
!= NULL
) {
193 head
= calloc(sizeof(List
*), 1);
197 head
->next
= calloc(sizeof(List
*), 1);
200 head
->data
= ht
->nodes
[i
]->data
;
210 void hash_table_list_free(List
* list
) {
211 while (list
!= NULL
) {
218 void hash_table_foreach(Hashtable
* ht
, ForeachFunc ff
, void* userdata
) {
220 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
221 if (ht
->nodes
[i
] != NULL
) {
222 Node
* node
= ht
->nodes
[i
];
223 while (node
!= NULL
) {
224 ff(node
->key
, node
->data
, userdata
);
232 void hash_table_iter_init (HashtableIter
* iter
, Hashtable
* ht
) {
233 RealIter
* ri
= (RealIter
*) iter
;
235 if (ri
!= NULL
&& ht
!= NULL
) {
241 bool hash_table_iter_next(HashtableIter
* iter
, void** key
, void** value
) {
242 RealIter
* ri
= (RealIter
*) iter
;
251 if (pos
>= ri
->table
->size
) {
255 node
= ri
->table
->nodes
[pos
];
256 } while (node
== NULL
);
269 bool hash_table_contains(Hashtable
* ht
, const void* key
) {
270 if (ht
== NULL
|| key
== NULL
) return false;
272 if (hash_table_lookup(ht
, key
) == NULL
)
277 uint64_t hash_table_size(Hashtable
*ht
) {
280 if (ht
== NULL
) return size
;
282 for (uint32_t i
= 0; i
< ht
->size
; i
++) {
283 if (ht
->nodes
[i
] != NULL
) {
284 Node
* node
= ht
->nodes
[i
];
285 while (node
!= NULL
) {
295 bool hash_table_replace(Hashtable
*ht
, void* key
, void* value
) {
296 if (ht
== NULL
|| key
== NULL
|| value
== NULL
) return false;
298 uint32_t index
= hash_table_index(ht
, key
);
300 Node
* node
= ht
->nodes
[index
];
301 while (node
!= NULL
&& ht
->compare(node
->data
, key
) == false) {
306 return hash_table_insert(ht
, key
, value
);
316 uint64_t int_hash(const void* key
) {
317 return *(uint64_t*) key
;
320 uint64_t str_hash(const void* key
) {
321 const signed char* p
;
324 for (p
= key
; *p
!= '\0'; p
++) {
325 h
= (h
<< 5) + h
+ *p
;
331 uint64_t ptr_hash(const void* key
) {
332 return (uint64_t)key
;
335 bool int_equal(const void* a
, const void* b
) {
336 return *((const int*) a
) == *((const int*) b
);
339 bool str_equal(const void* a
, const void* b
) {
340 const char *string1
= (const char*) a
;
341 const char *string2
= (const char*) b
;
343 return strcmp (string1
, string2
) == 0;
346 bool ptr_equal(const void* a
, const void* b
) {
This page took 0.116909 seconds and 6 git commands to generate.