1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
|
/* (C) Copyright 2001, 2002, 2003, 2004, 2005 Stijn van Dongen
* (C) Copyright 2006, 2007, 2008, 2009 Stijn van Dongen
*
* This file is part of tingea. You can redistribute and/or modify tingea
* under the terms of the GNU General Public License; either version 3 of the
* License or (at your option) any later version. You should have received a
* copy of the GPL along with tingea, in the file COPYING.
*/
#ifndef tingea_hash_h
#define tingea_hash_h
#include <stdio.h>
/* TODO:
* - make sort routines for keys and values by key or value criteria.
* - make interface for storing integers, preferably without objectifying them.
* - shrink hashes dynamically.
*
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
** Implementation notes (a few).
*
*
* This hash interface is very powerful. It gives you more than enough
* rope to hang yourself and then some. It can be used as is, or wrappers
* can be made around it that restrict a caller's ability to err.
*
* The danger lies in the fact that this interface only does retrieval
* and storage of pointers (both for keys and values), and does not clone
* anything. Anything happening with the objects pointed to during the
* lifetime of the hash is the responsibility of the caller.
*
* What the interface cannot do currently is hash integers by value (rather
* than by reference). This functionality will probably be added someday.
* Features:
* o Searching, inserting, and deletion are all done by
* mcxHashSearch. It returns a pointer to mcxKV. In all modes, the
* caller can use the returned mcxKV* structure to obtain
* the 'val' and 'key' members.
*
* o Hashes grow automatically once the average load per bucket
* exceeds a settable threshold and if the hash was not declared
* constant.
*
* o Caller supplies both the hash function and the compare function.
* This interface provides several hash functions operating on a
* (void* base, int len) combo, where base is cast to char by the
* hash function. These functions can be used in creating custom hash
* functions for your custom objects.
*
* o You can (of course) have multiple hashes. This is not really
* a feature - however, since the idiotic <hsearch.h> does not offer
* this I thought I'd mention it.
*
* o Witness mcxHashWalkInit, mcxHashWalkStep.
*
* o There is mcxHashMerge.
*
* o mcxHashKeys, mcxHashKVs.
*
* Enjoy.
* Notes
* There is a utility hashfile.c (distributed in a separate package)
* that can be used to stress-test this module. It allows customization
* of several aspects, including the hash function that should be used.
*/
#include "types.h"
#include "list.h"
/* The hash struct is hidden. Use mcxHashGetSettings if you need
* to peek into the interior. Or read hash.c
*/
typedef struct mcxHash mcxHash;
typedef struct
{ void* key
; void* val
;
} mcxKV ;
mcxHash* mcxHashNew
( dim n_buckets
, u32 (*hash) (const void *a)
, int (*cmp) (const void *a, const void *b)
) ;
#define MCX_HASH_OPT_DEFAULTS 0
#define MCX_HASH_OPT_CONSTANT 1
#define MCX_HASH_OPT_UNUSED 2
void mcxHashSetOpts
( mcxHash* hash
, double load
, int option /* negative values will be ignored (feature) */
) ;
dim mcxHashMemSize
( mcxHash* hash
) ;
typedef struct mcxHashSettings
{ dim n_buckets
; dim n_entries
; float load
; mcxbits options
;
} mcxHashSettings ;
void mcxHashGetSettings
( mcxHash* hash
, mcxHashSettings* settings
) ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
** mcxHashSearch
*
* action returns
*
* MCX_DATUM_DELETE -> deleted mcxKV* or NULL if not present
* MCX_DATUM_INSERT -> new or present mcxKV*
* MCX_DATUM_FIND -> mcxKV* if present NULL otherwise.
*
* usage:
*
* Values have to be inserted by the caller into the returned KV struct.
* Make sure that keys point to objects that are constant
* (with respect to the cmp function) during the lifetime of the hash.
* YOU have to ensure the integrity of both keys and values.
* This enables you to do whatever suits you, such as appending to
* values.
*
* When inserting, check whether kv->key != key (where kv is returned value)
* if this is the case, an identically comparing key is already present.
* You may want to destroy one of the two keys and decide what to do
* with the value.
*
* When deleting, the key-value pair is removed from the hash *AND RETURNED
* TO CALLER* - you have to decide yourself what to do with it. You have to
* fetch the val and key members of the returned mcxKV object immediately:
* Subsequent inserts in the hash may reuse it. If the key was not present,
* a value of NULL is returned.
*
* When finding, life is simple. NULL if absent, matching kv otherwise.
*
* note:
*
* memory management of keys and values is totally up to caller.
* If usage is clean, you can use mcxHashFree for disposal of hash.
*/
#define mcxHashSearch(key, hash, ACTION) mcxHashSearchx(key, hash, ACTION, NULL)
mcxKV* mcxHashSearchx
( void* key
, mcxHash* hash
, mcxmode ACTION
, int* delta
) ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
** mcxHashMerge
*
* this one COPIES OBJECT POINTERS and DOES NOT CLONE.
* so after the merge, hash1 and hash2 keys and values should not be freed.
* In case there are equivalent keys in hash1 and hash2, this may
* cause trouble when the caller wants to do cleaning afterwards.
* This interface is still under development.
*
* hashd may be equal to hash1 or hash2, and it may also be NULL.
*/
mcxHash* mcxHashMerge
( mcxHash* hash1
, mcxHash* hash2
, mcxHash* hashd
, void* merge(void* val1, void* val2)
) ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
** mcxHashFree
*
* This only works if all keys are of the same type and/or all values
* are of the same type, and if your objects were created as expected by
* the free routines (presumably malloced heap memory) - be careful with
* constant objects like constant strings.
*
* freekey and freeval may not free their argument. This is because
* tingea does not allow routines that leave arguments in an
* inconsistent state, and free routines in tingea generally accept
* an argument of the form <type>** pptr.
* In the case of mcxHashFree this means that the interface may
* feel slighly more cumbersome.
* A way out would have been to make the callbacks of signature
*
* void freemem(void** mempp)
*
* The caller could access *mempp, cast it to the expected type,
* and later set *mempp to NULL. However, this would require
* new free routines for lots of types. With the current interface
* existing <type>Release routines can be used:
*
* The type of free routine expected by mcxHashFree is generally
* called <type>Release or <type>Release_v, e.g. mcxTingRelease.
* Release routines release all memory of a composite object except the
* memory which holds the outer struct.
*
* If one of key or val is *not* a composite type or is a composite type
* that does not contain malloced memory, use mcxHashFreeScalar.
*
* Both freekey and freeval may be NULL. When NULL, the corresponding
* KV member is not loooked at. This is useful e.g. when hashing objects
* owned by someone else.
*/
void mcxHashFree
( mcxHash** hashpp
, void freekey(void* keypp) /* (yourtype1** keypp) */
, void freeval(void* valpp) /* (yourtype2** valpp) */
) ;
void mcxHashFreeScalar
( void* scalar
) ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* It copies the pointers stored in the hash
*/
void** mcxHashKeys
( mcxHash* hash
, dim* n_entries
, int (*cmp)(const void*, const void*) /* works on keys */
, mcxbits opts /* unused yet */
) ;
/* Future options: SORT, SORT_DESC */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* It copies the pointers stored in the hash
*/
void** mcxHashKVs
( mcxHash* hash
, dim* n_entries
, int (*cmp)(const void*, const void*)
, mcxbits opts /* unused yet */
) ;
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Prints some information to stdout.
*/
void mcxHashStats
( FILE* fp
, mcxHash* hash
) ;
typedef struct mcxHashWalk mcxHashWalk;
mcxHashWalk* mcxHashWalkInit
( mcxHash *hash
) ;
mcxKV* mcxHashWalkStep
( mcxHashWalk* walk
, dim *i_bucket
) ;
void mcxHashWalkFree
( mcxHashWalk **walkpp
) ;
void mcxHashApply
( mcxHash* hash
, void (*cb)(const void* key, void* val, void* data)
, void* data
) ;
/* UNIX ELF hash */
/* POOR! */
u32 mcxELFhash
( const void *key
, u32 len
) ;
/* created by Bob Jenkins */
u32 mcxBJhash
( const void* key
, u32 len
) ;
/* One at a time hash, Bob Jenkins/Colin Plumb */
u32 mcxOAThash
( const void *key
, u32 len
) ;
/* created by Daniel Phillips */
u32 mcxDPhash
( const void* key
, u32 len
) ;
/* "Berkely Database" hash (from Ozan Yigit's page) */
/* POOR! */
u32 mcxBDBhash
( const void *key
, u32 len
) ;
/* Dan Bernstein hash (from Ozan Yigit's page) */
u32 mcxDJBhash
( const void *key
, u32 len
) ;
/* created by Chris Torek */
u32 mcxCThash
( const void* key
, u32 len
) ;
/* "GNU Emacs" hash (from m4) */
/* not among the best */
u32 mcxGEhash
( const void* key
, u32 len
) ;
/* Fowler Noll Vo hash */
u32 mcxFNVhash
( const void *buf
, u32 len
) ;
/* All experimental with weak points. */
u32 mcxSvDhash
( const void *key
, u32 len
) ;
u32 mcxSvD2hash
( const void *key
, u32 len
) ;
u32 mcxSvD1hash
( const void *key
, u32 len
) ;
/* uses mcxDPhash */
u32 mcxStrHash
( const void* s
) ;
int mcxStrCmp
( const void* a
, const void* b
) ;
#endif
|