nsprpub/lib/ds/plhash.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * PL hash table package.
michael@0 8 */
michael@0 9 #include "plhash.h"
michael@0 10 #include "prbit.h"
michael@0 11 #include "prlog.h"
michael@0 12 #include "prmem.h"
michael@0 13 #include "prtypes.h"
michael@0 14 #include <stdlib.h>
michael@0 15 #include <string.h>
michael@0 16
michael@0 17 /* Compute the number of buckets in ht */
michael@0 18 #define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift))
michael@0 19
michael@0 20 /* The smallest table has 16 buckets */
michael@0 21 #define MINBUCKETSLOG2 4
michael@0 22 #define MINBUCKETS (1 << MINBUCKETSLOG2)
michael@0 23
michael@0 24 /* Compute the maximum entries given n buckets that we will tolerate, ~90% */
michael@0 25 #define OVERLOADED(n) ((n) - ((n) >> 3))
michael@0 26
michael@0 27 /* Compute the number of entries below which we shrink the table by half */
michael@0 28 #define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0)
michael@0 29
michael@0 30 /*
michael@0 31 ** Stubs for default hash allocator ops.
michael@0 32 */
michael@0 33 static void * PR_CALLBACK
michael@0 34 DefaultAllocTable(void *pool, PRSize size)
michael@0 35 {
michael@0 36 return PR_MALLOC(size);
michael@0 37 }
michael@0 38
michael@0 39 static void PR_CALLBACK
michael@0 40 DefaultFreeTable(void *pool, void *item)
michael@0 41 {
michael@0 42 PR_Free(item);
michael@0 43 }
michael@0 44
michael@0 45 static PLHashEntry * PR_CALLBACK
michael@0 46 DefaultAllocEntry(void *pool, const void *key)
michael@0 47 {
michael@0 48 return PR_NEW(PLHashEntry);
michael@0 49 }
michael@0 50
michael@0 51 static void PR_CALLBACK
michael@0 52 DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag)
michael@0 53 {
michael@0 54 if (flag == HT_FREE_ENTRY)
michael@0 55 PR_Free(he);
michael@0 56 }
michael@0 57
michael@0 58 static PLHashAllocOps defaultHashAllocOps = {
michael@0 59 DefaultAllocTable, DefaultFreeTable,
michael@0 60 DefaultAllocEntry, DefaultFreeEntry
michael@0 61 };
michael@0 62
michael@0 63 PR_IMPLEMENT(PLHashTable *)
michael@0 64 PL_NewHashTable(PRUint32 n, PLHashFunction keyHash,
michael@0 65 PLHashComparator keyCompare, PLHashComparator valueCompare,
michael@0 66 const PLHashAllocOps *allocOps, void *allocPriv)
michael@0 67 {
michael@0 68 PLHashTable *ht;
michael@0 69 PRSize nb;
michael@0 70
michael@0 71 if (n <= MINBUCKETS) {
michael@0 72 n = MINBUCKETSLOG2;
michael@0 73 } else {
michael@0 74 n = PR_CeilingLog2(n);
michael@0 75 if ((PRInt32)n < 0)
michael@0 76 return 0;
michael@0 77 }
michael@0 78
michael@0 79 if (!allocOps) allocOps = &defaultHashAllocOps;
michael@0 80
michael@0 81 ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht));
michael@0 82 if (!ht)
michael@0 83 return 0;
michael@0 84 memset(ht, 0, sizeof *ht);
michael@0 85 ht->shift = PL_HASH_BITS - n;
michael@0 86 n = 1 << n;
michael@0 87 nb = n * sizeof(PLHashEntry *);
michael@0 88 ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb));
michael@0 89 if (!ht->buckets) {
michael@0 90 (*allocOps->freeTable)(allocPriv, ht);
michael@0 91 return 0;
michael@0 92 }
michael@0 93 memset(ht->buckets, 0, nb);
michael@0 94
michael@0 95 ht->keyHash = keyHash;
michael@0 96 ht->keyCompare = keyCompare;
michael@0 97 ht->valueCompare = valueCompare;
michael@0 98 ht->allocOps = allocOps;
michael@0 99 ht->allocPriv = allocPriv;
michael@0 100 return ht;
michael@0 101 }
michael@0 102
michael@0 103 PR_IMPLEMENT(void)
michael@0 104 PL_HashTableDestroy(PLHashTable *ht)
michael@0 105 {
michael@0 106 PRUint32 i, n;
michael@0 107 PLHashEntry *he, *next;
michael@0 108 const PLHashAllocOps *allocOps = ht->allocOps;
michael@0 109 void *allocPriv = ht->allocPriv;
michael@0 110
michael@0 111 n = NBUCKETS(ht);
michael@0 112 for (i = 0; i < n; i++) {
michael@0 113 for (he = ht->buckets[i]; he; he = next) {
michael@0 114 next = he->next;
michael@0 115 (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY);
michael@0 116 }
michael@0 117 }
michael@0 118 #ifdef DEBUG
michael@0 119 memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]);
michael@0 120 #endif
michael@0 121 (*allocOps->freeTable)(allocPriv, ht->buckets);
michael@0 122 #ifdef DEBUG
michael@0 123 memset(ht, 0xDB, sizeof *ht);
michael@0 124 #endif
michael@0 125 (*allocOps->freeTable)(allocPriv, ht);
michael@0 126 }
michael@0 127
michael@0 128 /*
michael@0 129 ** Multiplicative hash, from Knuth 6.4.
michael@0 130 */
michael@0 131 #define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */
michael@0 132
michael@0 133 PR_IMPLEMENT(PLHashEntry **)
michael@0 134 PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key)
michael@0 135 {
michael@0 136 PLHashEntry *he, **hep, **hep0;
michael@0 137 PLHashNumber h;
michael@0 138
michael@0 139 #ifdef HASHMETER
michael@0 140 ht->nlookups++;
michael@0 141 #endif
michael@0 142 h = keyHash * GOLDEN_RATIO;
michael@0 143 h >>= ht->shift;
michael@0 144 hep = hep0 = &ht->buckets[h];
michael@0 145 while ((he = *hep) != 0) {
michael@0 146 if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
michael@0 147 /* Move to front of chain if not already there */
michael@0 148 if (hep != hep0) {
michael@0 149 *hep = he->next;
michael@0 150 he->next = *hep0;
michael@0 151 *hep0 = he;
michael@0 152 }
michael@0 153 return hep0;
michael@0 154 }
michael@0 155 hep = &he->next;
michael@0 156 #ifdef HASHMETER
michael@0 157 ht->nsteps++;
michael@0 158 #endif
michael@0 159 }
michael@0 160 return hep;
michael@0 161 }
michael@0 162
michael@0 163 /*
michael@0 164 ** Same as PL_HashTableRawLookup but doesn't reorder the hash entries.
michael@0 165 */
michael@0 166 PR_IMPLEMENT(PLHashEntry **)
michael@0 167 PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
michael@0 168 const void *key)
michael@0 169 {
michael@0 170 PLHashEntry *he, **hep;
michael@0 171 PLHashNumber h;
michael@0 172
michael@0 173 #ifdef HASHMETER
michael@0 174 ht->nlookups++;
michael@0 175 #endif
michael@0 176 h = keyHash * GOLDEN_RATIO;
michael@0 177 h >>= ht->shift;
michael@0 178 hep = &ht->buckets[h];
michael@0 179 while ((he = *hep) != 0) {
michael@0 180 if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) {
michael@0 181 break;
michael@0 182 }
michael@0 183 hep = &he->next;
michael@0 184 #ifdef HASHMETER
michael@0 185 ht->nsteps++;
michael@0 186 #endif
michael@0 187 }
michael@0 188 return hep;
michael@0 189 }
michael@0 190
michael@0 191 PR_IMPLEMENT(PLHashEntry *)
michael@0 192 PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep,
michael@0 193 PLHashNumber keyHash, const void *key, void *value)
michael@0 194 {
michael@0 195 PRUint32 i, n;
michael@0 196 PLHashEntry *he, *next, **oldbuckets;
michael@0 197 PRSize nb;
michael@0 198
michael@0 199 /* Grow the table if it is overloaded */
michael@0 200 n = NBUCKETS(ht);
michael@0 201 if (ht->nentries >= OVERLOADED(n)) {
michael@0 202 oldbuckets = ht->buckets;
michael@0 203 nb = 2 * n * sizeof(PLHashEntry *);
michael@0 204 ht->buckets = (PLHashEntry**)
michael@0 205 ((*ht->allocOps->allocTable)(ht->allocPriv, nb));
michael@0 206 if (!ht->buckets) {
michael@0 207 ht->buckets = oldbuckets;
michael@0 208 return 0;
michael@0 209 }
michael@0 210 memset(ht->buckets, 0, nb);
michael@0 211 #ifdef HASHMETER
michael@0 212 ht->ngrows++;
michael@0 213 #endif
michael@0 214 ht->shift--;
michael@0 215
michael@0 216 for (i = 0; i < n; i++) {
michael@0 217 for (he = oldbuckets[i]; he; he = next) {
michael@0 218 next = he->next;
michael@0 219 hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
michael@0 220 PR_ASSERT(*hep == 0);
michael@0 221 he->next = 0;
michael@0 222 *hep = he;
michael@0 223 }
michael@0 224 }
michael@0 225 #ifdef DEBUG
michael@0 226 memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
michael@0 227 #endif
michael@0 228 (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
michael@0 229 hep = PL_HashTableRawLookup(ht, keyHash, key);
michael@0 230 }
michael@0 231
michael@0 232 /* Make a new key value entry */
michael@0 233 he = (*ht->allocOps->allocEntry)(ht->allocPriv, key);
michael@0 234 if (!he)
michael@0 235 return 0;
michael@0 236 he->keyHash = keyHash;
michael@0 237 he->key = key;
michael@0 238 he->value = value;
michael@0 239 he->next = *hep;
michael@0 240 *hep = he;
michael@0 241 ht->nentries++;
michael@0 242 return he;
michael@0 243 }
michael@0 244
michael@0 245 PR_IMPLEMENT(PLHashEntry *)
michael@0 246 PL_HashTableAdd(PLHashTable *ht, const void *key, void *value)
michael@0 247 {
michael@0 248 PLHashNumber keyHash;
michael@0 249 PLHashEntry *he, **hep;
michael@0 250
michael@0 251 keyHash = (*ht->keyHash)(key);
michael@0 252 hep = PL_HashTableRawLookup(ht, keyHash, key);
michael@0 253 if ((he = *hep) != 0) {
michael@0 254 /* Hit; see if values match */
michael@0 255 if ((*ht->valueCompare)(he->value, value)) {
michael@0 256 /* key,value pair is already present in table */
michael@0 257 return he;
michael@0 258 }
michael@0 259 if (he->value)
michael@0 260 (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE);
michael@0 261 he->value = value;
michael@0 262 return he;
michael@0 263 }
michael@0 264 return PL_HashTableRawAdd(ht, hep, keyHash, key, value);
michael@0 265 }
michael@0 266
michael@0 267 PR_IMPLEMENT(void)
michael@0 268 PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he)
michael@0 269 {
michael@0 270 PRUint32 i, n;
michael@0 271 PLHashEntry *next, **oldbuckets;
michael@0 272 PRSize nb;
michael@0 273
michael@0 274 *hep = he->next;
michael@0 275 (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY);
michael@0 276
michael@0 277 /* Shrink table if it's underloaded */
michael@0 278 n = NBUCKETS(ht);
michael@0 279 if (--ht->nentries < UNDERLOADED(n)) {
michael@0 280 oldbuckets = ht->buckets;
michael@0 281 nb = n * sizeof(PLHashEntry*) / 2;
michael@0 282 ht->buckets = (PLHashEntry**)(
michael@0 283 (*ht->allocOps->allocTable)(ht->allocPriv, nb));
michael@0 284 if (!ht->buckets) {
michael@0 285 ht->buckets = oldbuckets;
michael@0 286 return;
michael@0 287 }
michael@0 288 memset(ht->buckets, 0, nb);
michael@0 289 #ifdef HASHMETER
michael@0 290 ht->nshrinks++;
michael@0 291 #endif
michael@0 292 ht->shift++;
michael@0 293
michael@0 294 for (i = 0; i < n; i++) {
michael@0 295 for (he = oldbuckets[i]; he; he = next) {
michael@0 296 next = he->next;
michael@0 297 hep = PL_HashTableRawLookup(ht, he->keyHash, he->key);
michael@0 298 PR_ASSERT(*hep == 0);
michael@0 299 he->next = 0;
michael@0 300 *hep = he;
michael@0 301 }
michael@0 302 }
michael@0 303 #ifdef DEBUG
michael@0 304 memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]);
michael@0 305 #endif
michael@0 306 (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets);
michael@0 307 }
michael@0 308 }
michael@0 309
michael@0 310 PR_IMPLEMENT(PRBool)
michael@0 311 PL_HashTableRemove(PLHashTable *ht, const void *key)
michael@0 312 {
michael@0 313 PLHashNumber keyHash;
michael@0 314 PLHashEntry *he, **hep;
michael@0 315
michael@0 316 keyHash = (*ht->keyHash)(key);
michael@0 317 hep = PL_HashTableRawLookup(ht, keyHash, key);
michael@0 318 if ((he = *hep) == 0)
michael@0 319 return PR_FALSE;
michael@0 320
michael@0 321 /* Hit; remove element */
michael@0 322 PL_HashTableRawRemove(ht, hep, he);
michael@0 323 return PR_TRUE;
michael@0 324 }
michael@0 325
michael@0 326 PR_IMPLEMENT(void *)
michael@0 327 PL_HashTableLookup(PLHashTable *ht, const void *key)
michael@0 328 {
michael@0 329 PLHashNumber keyHash;
michael@0 330 PLHashEntry *he, **hep;
michael@0 331
michael@0 332 keyHash = (*ht->keyHash)(key);
michael@0 333 hep = PL_HashTableRawLookup(ht, keyHash, key);
michael@0 334 if ((he = *hep) != 0) {
michael@0 335 return he->value;
michael@0 336 }
michael@0 337 return 0;
michael@0 338 }
michael@0 339
michael@0 340 /*
michael@0 341 ** Same as PL_HashTableLookup but doesn't reorder the hash entries.
michael@0 342 */
michael@0 343 PR_IMPLEMENT(void *)
michael@0 344 PL_HashTableLookupConst(PLHashTable *ht, const void *key)
michael@0 345 {
michael@0 346 PLHashNumber keyHash;
michael@0 347 PLHashEntry *he, **hep;
michael@0 348
michael@0 349 keyHash = (*ht->keyHash)(key);
michael@0 350 hep = PL_HashTableRawLookupConst(ht, keyHash, key);
michael@0 351 if ((he = *hep) != 0) {
michael@0 352 return he->value;
michael@0 353 }
michael@0 354 return 0;
michael@0 355 }
michael@0 356
michael@0 357 /*
michael@0 358 ** Iterate over the entries in the hash table calling func for each
michael@0 359 ** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP).
michael@0 360 ** Return a count of the number of elements scanned.
michael@0 361 */
michael@0 362 PR_IMPLEMENT(int)
michael@0 363 PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg)
michael@0 364 {
michael@0 365 PLHashEntry *he, **hep;
michael@0 366 PRUint32 i, nbuckets;
michael@0 367 int rv, n = 0;
michael@0 368 PLHashEntry *todo = 0;
michael@0 369
michael@0 370 nbuckets = NBUCKETS(ht);
michael@0 371 for (i = 0; i < nbuckets; i++) {
michael@0 372 hep = &ht->buckets[i];
michael@0 373 while ((he = *hep) != 0) {
michael@0 374 rv = (*f)(he, n, arg);
michael@0 375 n++;
michael@0 376 if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) {
michael@0 377 *hep = he->next;
michael@0 378 if (rv & HT_ENUMERATE_REMOVE) {
michael@0 379 he->next = todo;
michael@0 380 todo = he;
michael@0 381 }
michael@0 382 } else {
michael@0 383 hep = &he->next;
michael@0 384 }
michael@0 385 if (rv & HT_ENUMERATE_STOP) {
michael@0 386 goto out;
michael@0 387 }
michael@0 388 }
michael@0 389 }
michael@0 390
michael@0 391 out:
michael@0 392 hep = &todo;
michael@0 393 while ((he = *hep) != 0) {
michael@0 394 PL_HashTableRawRemove(ht, hep, he);
michael@0 395 }
michael@0 396 return n;
michael@0 397 }
michael@0 398
michael@0 399 #ifdef HASHMETER
michael@0 400 #include <math.h>
michael@0 401 #include <stdio.h>
michael@0 402
michael@0 403 PR_IMPLEMENT(void)
michael@0 404 PL_HashTableDumpMeter(PLHashTable *ht, PLHashEnumerator dump, FILE *fp)
michael@0 405 {
michael@0 406 double mean, variance;
michael@0 407 PRUint32 nchains, nbuckets;
michael@0 408 PRUint32 i, n, maxChain, maxChainLen;
michael@0 409 PLHashEntry *he;
michael@0 410
michael@0 411 variance = 0;
michael@0 412 nchains = 0;
michael@0 413 maxChainLen = 0;
michael@0 414 nbuckets = NBUCKETS(ht);
michael@0 415 for (i = 0; i < nbuckets; i++) {
michael@0 416 he = ht->buckets[i];
michael@0 417 if (!he)
michael@0 418 continue;
michael@0 419 nchains++;
michael@0 420 for (n = 0; he; he = he->next)
michael@0 421 n++;
michael@0 422 variance += n * n;
michael@0 423 if (n > maxChainLen) {
michael@0 424 maxChainLen = n;
michael@0 425 maxChain = i;
michael@0 426 }
michael@0 427 }
michael@0 428 mean = (double)ht->nentries / nchains;
michael@0 429 variance = fabs(variance / nchains - mean * mean);
michael@0 430
michael@0 431 fprintf(fp, "\nHash table statistics:\n");
michael@0 432 fprintf(fp, " number of lookups: %u\n", ht->nlookups);
michael@0 433 fprintf(fp, " number of entries: %u\n", ht->nentries);
michael@0 434 fprintf(fp, " number of grows: %u\n", ht->ngrows);
michael@0 435 fprintf(fp, " number of shrinks: %u\n", ht->nshrinks);
michael@0 436 fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps
michael@0 437 / ht->nlookups);
michael@0 438 fprintf(fp, "mean hash chain length: %g\n", mean);
michael@0 439 fprintf(fp, " standard deviation: %g\n", sqrt(variance));
michael@0 440 fprintf(fp, " max hash chain length: %u\n", maxChainLen);
michael@0 441 fprintf(fp, " max hash chain: [%u]\n", maxChain);
michael@0 442
michael@0 443 for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++)
michael@0 444 if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT)
michael@0 445 break;
michael@0 446 }
michael@0 447 #endif /* HASHMETER */
michael@0 448
michael@0 449 PR_IMPLEMENT(int)
michael@0 450 PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp)
michael@0 451 {
michael@0 452 int count;
michael@0 453
michael@0 454 count = PL_HashTableEnumerateEntries(ht, dump, fp);
michael@0 455 #ifdef HASHMETER
michael@0 456 PL_HashTableDumpMeter(ht, dump, fp);
michael@0 457 #endif
michael@0 458 return count;
michael@0 459 }
michael@0 460
michael@0 461 PR_IMPLEMENT(PLHashNumber)
michael@0 462 PL_HashString(const void *key)
michael@0 463 {
michael@0 464 PLHashNumber h;
michael@0 465 const PRUint8 *s;
michael@0 466
michael@0 467 h = 0;
michael@0 468 for (s = (const PRUint8*)key; *s; s++)
michael@0 469 h = PR_ROTATE_LEFT32(h, 4) ^ *s;
michael@0 470 return h;
michael@0 471 }
michael@0 472
michael@0 473 PR_IMPLEMENT(int)
michael@0 474 PL_CompareStrings(const void *v1, const void *v2)
michael@0 475 {
michael@0 476 return strcmp((const char*)v1, (const char*)v2) == 0;
michael@0 477 }
michael@0 478
michael@0 479 PR_IMPLEMENT(int)
michael@0 480 PL_CompareValues(const void *v1, const void *v2)
michael@0 481 {
michael@0 482 return v1 == v2;
michael@0 483 }

mercurial