hash_table.h revision 63e7a4c6e5bf51d8090046ebc5adcb4207448565
1/* 2 * Copyright © 2008 Intel Corporation 3 * 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: 10 * 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 13 * 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 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. 22 */ 23 24/** 25 * \file hash_table.h 26 * \brief Implementation of a generic, opaque hash table data type. 27 * 28 * \author Ian Romanick <ian.d.romanick@intel.com> 29 */ 30 31#ifndef HASH_TABLE_H 32#define HASH_TABLE_H 33 34#include <string.h> 35#include <stdlib.h> 36#include <stdint.h> 37#include <limits.h> 38#include <assert.h> 39 40struct string_to_uint_map; 41 42#ifdef __cplusplus 43extern "C" { 44#endif 45 46struct hash_table; 47 48typedef unsigned (*hash_func_t)(const void *key); 49typedef int (*hash_compare_func_t)(const void *key1, const void *key2); 50 51/** 52 * Hash table constructor 53 * 54 * Creates a hash table with the specified number of buckets. The supplied 55 * \c hash and \c compare routines are used when adding elements to the table 56 * and when searching for elements in the table. 57 * 58 * \param num_buckets Number of buckets (bins) in the hash table. 59 * \param hash Function used to compute hash value of input keys. 60 * \param compare Function used to compare keys. 61 */ 62extern struct hash_table *hash_table_ctor(unsigned num_buckets, 63 hash_func_t hash, hash_compare_func_t compare); 64 65 66/** 67 * Release all memory associated with a hash table 68 * 69 * \warning 70 * This function cannot release memory occupied either by keys or data. 71 */ 72extern void hash_table_dtor(struct hash_table *ht); 73 74 75/** 76 * Flush all entries from a hash table 77 * 78 * \param ht Table to be cleared of its entries. 79 */ 80extern void hash_table_clear(struct hash_table *ht); 81 82 83/** 84 * Search a hash table for a specific element 85 * 86 * \param ht Table to be searched 87 * \param key Key of the desired element 88 * 89 * \return 90 * The \c data value supplied to \c hash_table_insert when the element with 91 * the matching key was added. If no matching key exists in the table, 92 * \c NULL is returned. 93 */ 94extern void *hash_table_find(struct hash_table *ht, const void *key); 95 96 97/** 98 * Add an element to a hash table 99 * 100 * \warning 101 * If \c key is already in the hash table, it will be added again. Future 102 * calls to \c hash_table_find and \c hash_table_remove will return or remove, 103 * repsectively, the most recently added instance of \c key. 104 * 105 * \warning 106 * The value passed by \c key is kept in the hash table and is used by later 107 * calls to \c hash_table_find. 108 * 109 * \sa hash_table_replace 110 */ 111extern void hash_table_insert(struct hash_table *ht, void *data, 112 const void *key); 113 114/** 115 * Add an element to a hash table with replacement 116 * 117 * \warning 118 * If \c key is already in the hash table, \c data will \b replace the most 119 * recently inserted \c data (see the warning in \c hash_table_insert) for 120 * that key. 121 * 122 * \sa hash_table_insert 123 */ 124extern void hash_table_replace(struct hash_table *ht, void *data, 125 const void *key); 126 127/** 128 * Remove a specific element from a hash table. 129 */ 130extern void hash_table_remove(struct hash_table *ht, const void *key); 131 132/** 133 * Compute hash value of a string 134 * 135 * Computes the hash value of a string using the DJB2 algorithm developed by 136 * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon 137 * a time. I was unable to find the original posting in the archives. 138 * 139 * \param key Pointer to a NUL terminated string to be hashed. 140 * 141 * \sa hash_table_string_compare 142 */ 143extern unsigned hash_table_string_hash(const void *key); 144 145 146/** 147 * Compare two strings used as keys 148 * 149 * This is just a macro wrapper around \c strcmp. 150 * 151 * \sa hash_table_string_hash 152 */ 153#define hash_table_string_compare ((hash_compare_func_t) strcmp) 154 155 156/** 157 * Compute hash value of a pointer 158 * 159 * \param key Pointer to be used as a hash key 160 * 161 * \note 162 * The memory pointed to by \c key is \b never accessed. The value of \c key 163 * itself is used as the hash key 164 * 165 * \sa hash_table_pointer_compare 166 */ 167unsigned 168hash_table_pointer_hash(const void *key); 169 170 171/** 172 * Compare two pointers used as keys 173 * 174 * \sa hash_table_pointer_hash 175 */ 176int 177hash_table_pointer_compare(const void *key1, const void *key2); 178 179void 180hash_table_call_foreach(struct hash_table *ht, 181 void (*callback)(const void *key, 182 void *data, 183 void *closure), 184 void *closure); 185 186struct string_to_uint_map * 187string_to_uint_map_ctor(); 188 189void 190string_to_uint_map_dtor(struct string_to_uint_map *); 191 192 193#ifdef __cplusplus 194} 195 196/** 197 * Map from a string (name) to an unsigned integer value 198 * 199 * \note 200 * Because of the way this class interacts with the \c hash_table 201 * implementation, values of \c UINT_MAX cannot be stored in the map. 202 */ 203struct string_to_uint_map { 204public: 205 string_to_uint_map() 206 { 207 this->ht = hash_table_ctor(0, hash_table_string_hash, 208 hash_table_string_compare); 209 } 210 211 ~string_to_uint_map() 212 { 213 hash_table_call_foreach(this->ht, delete_key, NULL); 214 hash_table_dtor(this->ht); 215 } 216 217 /** 218 * Remove all mappings from this map. 219 */ 220 void clear() 221 { 222 hash_table_clear(this->ht); 223 } 224 225 /** 226 * Get the value associated with a particular key 227 * 228 * \return 229 * If \c key is found in the map, \c true is returned. Otherwise \c false 230 * is returned. 231 * 232 * \note 233 * If \c key is not found in the table, \c value is not modified. 234 */ 235 bool get(unsigned &value, const char *key) 236 { 237 const intptr_t v = 238 (intptr_t) hash_table_find(this->ht, (const void *) key); 239 240 if (v == 0) 241 return false; 242 243 value = (unsigned)(v - 1); 244 return true; 245 } 246 247 void put(unsigned value, const char *key) 248 { 249 /* The low-level hash table structure returns NULL if key is not in the 250 * hash table. However, users of this map might want to store zero as a 251 * valid value in the table. Bias the value by +1 so that a 252 * user-specified zero is stored as 1. This enables ::get to tell the 253 * difference between a user-specified zero (returned as 1 by 254 * hash_table_find) and the key not in the table (returned as 0 by 255 * hash_table_find). 256 * 257 * The net effect is that we can't store UINT_MAX in the table. This is 258 * because UINT_MAX+1 = 0. 259 */ 260 assert(value != UINT_MAX); 261 hash_table_replace(this->ht, 262 (void *) (intptr_t) (value + 1), 263 strdup(key)); 264 } 265 266private: 267 static void delete_key(const void *key, void *data, void *closure) 268 { 269 (void) data; 270 (void) closure; 271 272 free((char *)key); 273 } 274 275 struct hash_table *ht; 276}; 277 278#endif /* __cplusplus */ 279#endif /* HASH_TABLE_H */ 280