Kenneth Graunke | 6f510a4 | 2010-06-21 11:22:11 -0700 | [diff] [blame^] | 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 | #ifdef __cplusplus |
| 35 | extern "C" { |
| 36 | #endif |
| 37 | |
| 38 | #include <string.h> |
| 39 | |
| 40 | struct hash_table; |
| 41 | |
| 42 | typedef unsigned (*hash_func_t)(const void *key); |
| 43 | typedef int (*hash_compare_func_t)(const void *key1, const void *key2); |
| 44 | |
| 45 | /** |
| 46 | * Hash table constructor |
| 47 | * |
| 48 | * Creates a hash table with the specified number of buckets. The supplied |
| 49 | * \c hash and \c compare routines are used when adding elements to the table |
| 50 | * and when searching for elements in the table. |
| 51 | * |
| 52 | * \param num_buckets Number of buckets (bins) in the hash table. |
| 53 | * \param hash Function used to compute hash value of input keys. |
| 54 | * \param compare Function used to compare keys. |
| 55 | */ |
| 56 | extern struct hash_table *hash_table_ctor(unsigned num_buckets, |
| 57 | hash_func_t hash, hash_compare_func_t compare); |
| 58 | |
| 59 | |
| 60 | /** |
| 61 | * Release all memory associated with a hash table |
| 62 | * |
| 63 | * \warning |
| 64 | * This function cannot release memory occupied either by keys or data. |
| 65 | */ |
| 66 | extern void hash_table_dtor(struct hash_table *ht); |
| 67 | |
| 68 | |
| 69 | /** |
| 70 | * Flush all entries from a hash table |
| 71 | * |
| 72 | * \param ht Table to be cleared of its entries. |
| 73 | */ |
| 74 | extern void hash_table_clear(struct hash_table *ht); |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * Search a hash table for a specific element |
| 79 | * |
| 80 | * \param ht Table to be searched |
| 81 | * \param key Key of the desired element |
| 82 | * |
| 83 | * \return |
| 84 | * The \c data value supplied to \c hash_table_insert when the element with |
| 85 | * the matching key was added. If no matching key exists in the table, |
| 86 | * \c NULL is returned. |
| 87 | */ |
| 88 | extern void *hash_table_find(struct hash_table *ht, const void *key); |
| 89 | |
| 90 | |
| 91 | /** |
| 92 | * Add an element to a hash table |
| 93 | */ |
| 94 | extern void hash_table_insert(struct hash_table *ht, void *data, |
| 95 | const void *key); |
| 96 | |
| 97 | |
| 98 | /** |
| 99 | * Compute hash value of a string |
| 100 | * |
| 101 | * Computes the hash value of a string using the DJB2 algorithm developed by |
| 102 | * Professor Daniel J. Bernstein. It was published on comp.lang.c once upon |
| 103 | * a time. I was unable to find the original posting in the archives. |
| 104 | * |
| 105 | * \param key Pointer to a NUL terminated string to be hashed. |
| 106 | * |
| 107 | * \sa hash_table_string_compare |
| 108 | */ |
| 109 | extern unsigned hash_table_string_hash(const void *key); |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * Compare two strings used as keys |
| 114 | * |
| 115 | * This is just a macro wrapper around \c strcmp. |
| 116 | * |
| 117 | * \sa hash_table_string_hash |
| 118 | */ |
| 119 | #define hash_table_string_compare ((hash_compare_func_t) strcmp) |
| 120 | |
| 121 | #ifdef __cplusplus |
| 122 | }; |
| 123 | #endif |
| 124 | |
| 125 | #endif /* HASH_TABLE_H */ |