| 1 |
/***************************************************************************** |
| 2 |
* $Id: hash.h 2950 2005-01-18 20:09:32Z dun $ |
| 3 |
***************************************************************************** |
| 4 |
* Copyright (C) 2003-2005 The Regents of the University of California. |
| 5 |
* Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). |
| 6 |
* Written by Chris Dunlap <[email protected]>. |
| 7 |
* |
| 8 |
* This file is from LSD-Tools, the LLNL Software Development Toolbox. |
| 9 |
* |
| 10 |
* This is free software; you can redistribute it and/or modify it |
| 11 |
* under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation; either version 2 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* This is distributed in the hope that it will be useful, but WITHOUT |
| 16 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 18 |
* for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License; |
| 21 |
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
| 22 |
* Suite 330, Boston, MA 02111-1307 USA. |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
|
| 26 |
#ifndef LSD_HASH_H |
| 27 |
#define LSD_HASH_H |
| 28 |
|
| 29 |
|
| 30 |
/***************************************************************************** |
| 31 |
* Notes |
| 32 |
*****************************************************************************/ |
| 33 |
/* |
| 34 |
* If an item's key is modified after insertion, the hash will be unable to |
| 35 |
* locate it if the new key should hash to a different slot in the table. |
| 36 |
* |
| 37 |
* If NDEBUG is not defined, internal debug code will be enabled; this is |
| 38 |
* intended for development use only. Production code should define NDEBUG. |
| 39 |
* |
| 40 |
* If WITH_LSD_FATAL_ERROR_FUNC is defined, the linker will expect to |
| 41 |
* find an external lsd_fatal_error(file,line,mesg) function. By default, |
| 42 |
* lsd_fatal_error(file,line,mesg) is a macro definition that aborts. |
| 43 |
* This macro may be redefined to invoke another routine instead. |
| 44 |
* |
| 45 |
* If WITH_LSD_NOMEM_ERROR_FUNC is defined, the linker will expect to |
| 46 |
* find an external lsd_nomem_error(file,line,mesg) function. By default, |
| 47 |
* lsd_nomem_error(file,line,mesg) is a macro definition that returns NULL. |
| 48 |
* This macro may be redefined to invoke another routine instead. |
| 49 |
* |
| 50 |
* If WITH_PTHREADS is defined, these routines will be thread-safe. |
| 51 |
*/ |
| 52 |
|
| 53 |
|
| 54 |
/***************************************************************************** |
| 55 |
* Data Types |
| 56 |
*****************************************************************************/ |
| 57 |
|
| 58 |
typedef struct hash * hash_t; |
| 59 |
/* |
| 60 |
* Hash table opaque data type. |
| 61 |
*/ |
| 62 |
|
| 63 |
typedef unsigned int (*hash_key_f) (const void *key); |
| 64 |
/* |
| 65 |
* Function prototype for the hash function responsible for converting |
| 66 |
* the data's [key] into an unsigned integer hash value. |
| 67 |
*/ |
| 68 |
|
| 69 |
typedef int (*hash_cmp_f) (const void *key1, const void *key2); |
| 70 |
/* |
| 71 |
* Function prototype for comparing two keys. |
| 72 |
* Returns zero if both keys are equal; o/w, returns nonzero. |
| 73 |
*/ |
| 74 |
|
| 75 |
typedef void (*hash_del_f) (void *data); |
| 76 |
/* |
| 77 |
* Function prototype for de-allocating a data item stored within a hash. |
| 78 |
* This function is responsible for freeing all memory associated with |
| 79 |
* the [data] item, including any subordinate items. |
| 80 |
*/ |
| 81 |
|
| 82 |
typedef int (*hash_arg_f) (void *data, const void *key, void *arg); |
| 83 |
/* |
| 84 |
* Function prototype for operating on each element in the hash table. |
| 85 |
* The function will be invoked once for each [data] item in the hash, |
| 86 |
* with the item's [key] and the specified [arg] being passed in as args. |
| 87 |
*/ |
| 88 |
|
| 89 |
|
| 90 |
/***************************************************************************** |
| 91 |
* Functions |
| 92 |
*****************************************************************************/ |
| 93 |
|
| 94 |
hash_t hash_create (int size, |
| 95 |
hash_key_f key_f, hash_cmp_f cmp_f, hash_del_f del_f); |
| 96 |
/* |
| 97 |
* Creates and returns a new hash table on success. |
| 98 |
* Returns lsd_nomem_error() with errno=ENOMEM if memory allocation fails. |
| 99 |
* Returns NULL with errno=EINVAL if [keyf] or [cmpf] is not specified. |
| 100 |
* The [size] is the number of slots in the table; a larger table requires |
| 101 |
* more memory, but generally provide quicker access times. If set <= 0, |
| 102 |
* the default size is used. |
| 103 |
* The [keyf] function converts a key into a hash value. |
| 104 |
* The [cmpf] function determines whether two keys are equal. |
| 105 |
* The [delf] function de-allocates memory used by items in the hash; |
| 106 |
* if set to NULL, memory associated with these items will not be freed |
| 107 |
* when the hash is destroyed. |
| 108 |
*/ |
| 109 |
|
| 110 |
void hash_destroy (hash_t h); |
| 111 |
/* |
| 112 |
* Destroys hash table [h]. If a deletion function was specified when the |
| 113 |
* hash was created, it will be called for each item contained within. |
| 114 |
* Abadoning a hash without calling hash_destroy() will cause a memory leak. |
| 115 |
*/ |
| 116 |
|
| 117 |
int hash_is_empty (hash_t h); |
| 118 |
/* |
| 119 |
* Returns non-zero if hash table [h] is empty; o/w, returns zero. |
| 120 |
*/ |
| 121 |
|
| 122 |
int hash_count (hash_t h); |
| 123 |
/* |
| 124 |
* Returns the number of items in hash table [h]. |
| 125 |
*/ |
| 126 |
|
| 127 |
void * hash_find (hash_t h, const void *key); |
| 128 |
/* |
| 129 |
* Searches for the item corresponding to [key] in hash table [h]. |
| 130 |
* Returns a ptr to the found item's data on success. |
| 131 |
* Returns NULL with errno=0 if no matching item is found. |
| 132 |
* Returns NULL with errno=EINVAL if [key] is not specified. |
| 133 |
*/ |
| 134 |
|
| 135 |
void * hash_insert (hash_t h, const void *key, void *data); |
| 136 |
/* |
| 137 |
* Inserts [data] with the corresponding [key] into hash table [h]; |
| 138 |
* note that it is permissible for [key] to be set equal to [data]. |
| 139 |
* Returns a ptr to the inserted item's data on success. |
| 140 |
* Returns NULL with errno=EEXIST if [key] already exists in the hash. |
| 141 |
* Returns NULL with errno=EINVAL if [key] or [data] is not specified. |
| 142 |
* Returns lsd_nomem_error() with errno=ENOMEM if memory allocation fails. |
| 143 |
*/ |
| 144 |
|
| 145 |
void * hash_remove (hash_t h, const void *key); |
| 146 |
/* |
| 147 |
* Removes the item corresponding to [key] from hash table [h]. |
| 148 |
* Returns a ptr to the removed item's data on success. |
| 149 |
* Returns NULL with errno=0 if no matching item is found. |
| 150 |
* Returns NULL with errno=EINVAL if [key] is not specified. |
| 151 |
*/ |
| 152 |
|
| 153 |
int hash_delete_if (hash_t h, hash_arg_f argf, void *arg); |
| 154 |
/* |
| 155 |
* Conditionally deletes (and de-allocates) items from hash table [h]. |
| 156 |
* The [argf] function is invoked once for each item in the hash, with |
| 157 |
* [arg] being passed in as an argument. Items for which [argf] returns |
| 158 |
* greater-than-zero are deleted. |
| 159 |
* Returns the number of items deleted. |
| 160 |
* Returns -1 with errno=EINVAL if [argf] is not specified. |
| 161 |
*/ |
| 162 |
|
| 163 |
int hash_for_each (hash_t h, hash_arg_f argf, void *arg); |
| 164 |
/* |
| 165 |
* Invokes the [argf] function once for each item in hash table [h], |
| 166 |
* with [arg] being passed in as an argument. |
| 167 |
* Returns the number of items for which [argf] returns greater-than-zero. |
| 168 |
* Returns -1 with errno=EINVAL if [argf] is not specified. |
| 169 |
*/ |
| 170 |
|
| 171 |
unsigned int hash_key_string (const char *str); |
| 172 |
/* |
| 173 |
* A hash_key_f function that hashes the string [str]. |
| 174 |
*/ |
| 175 |
|
| 176 |
|
| 177 |
#endif /* !LSD_HASH_H */ |