2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * \brief Implementation of a generic, opaque hash table data type.
28 * \author Ian Romanick <ian.d.romanick@intel.com>
31 #include "main/imports.h"
32 #include "main/simple_list.h"
33 #include "hash_table.h"
42 hash_compare_func_t compare;
45 struct node buckets[1];
57 hash_table_ctor(unsigned num_buckets, hash_func_t hash,
58 hash_compare_func_t compare)
60 struct hash_table *ht;
64 if (num_buckets < 16) {
68 ht = malloc(sizeof(*ht) + ((num_buckets - 1)
69 * sizeof(ht->buckets[0])));
72 ht->compare = compare;
73 ht->num_buckets = num_buckets;
75 for (i = 0; i < num_buckets; i++) {
76 make_empty_list(& ht->buckets[i]);
85 hash_table_dtor(struct hash_table *ht)
93 hash_table_clear(struct hash_table *ht)
100 for (i = 0; i < ht->num_buckets; i++) {
101 foreach_s(node, temp, & ht->buckets[i]) {
102 remove_from_list(node);
106 assert(is_empty_list(& ht->buckets[i]));
112 hash_table_find(struct hash_table *ht, const void *key)
114 const unsigned hash_value = (*ht->hash)(key);
115 const unsigned bucket = hash_value % ht->num_buckets;
118 foreach(node, & ht->buckets[bucket]) {
119 struct hash_node *hn = (struct hash_node *) node;
121 if ((*ht->compare)(hn->key, key) == 0) {
131 hash_table_insert(struct hash_table *ht, void *data, const void *key)
133 const unsigned hash_value = (*ht->hash)(key);
134 const unsigned bucket = hash_value % ht->num_buckets;
135 struct hash_node *node;
137 node = calloc(1, sizeof(*node));
142 insert_at_head(& ht->buckets[bucket], & node->link);
146 hash_table_remove(struct hash_table *ht, const void *key)
148 const unsigned hash_value = (*ht->hash)(key);
149 const unsigned bucket = hash_value % ht->num_buckets;
152 foreach(node, & ht->buckets[bucket]) {
153 struct hash_node *hn = (struct hash_node *) node;
155 if ((*ht->compare)(hn->key, key) == 0) {
156 remove_from_list(node);
164 hash_table_call_foreach(struct hash_table *ht,
165 void (*callback)(const void *key,
172 for (bucket = 0; bucket < ht->num_buckets; bucket++) {
173 struct node *node, *temp;
174 foreach_s(node, temp, &ht->buckets[bucket]) {
175 struct hash_node *hn = (struct hash_node *) node;
177 callback(hn->key, hn->data, closure);
183 hash_table_string_hash(const void *key)
185 const char *str = (const char *) key;
186 unsigned hash = 5381;
189 while (*str != '\0') {
190 hash = (hash * 33) + *str;
199 hash_table_pointer_hash(const void *key)
201 return (unsigned)((uintptr_t) key / sizeof(void *));
206 hash_table_pointer_compare(const void *key1, const void *key2)
208 return key1 == key2 ? 0 : 1;