michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * PL hash table package. michael@0: */ michael@0: #include "plhash.h" michael@0: #include "prbit.h" michael@0: #include "prlog.h" michael@0: #include "prmem.h" michael@0: #include "prtypes.h" michael@0: #include michael@0: #include michael@0: michael@0: /* Compute the number of buckets in ht */ michael@0: #define NBUCKETS(ht) (1 << (PL_HASH_BITS - (ht)->shift)) michael@0: michael@0: /* The smallest table has 16 buckets */ michael@0: #define MINBUCKETSLOG2 4 michael@0: #define MINBUCKETS (1 << MINBUCKETSLOG2) michael@0: michael@0: /* Compute the maximum entries given n buckets that we will tolerate, ~90% */ michael@0: #define OVERLOADED(n) ((n) - ((n) >> 3)) michael@0: michael@0: /* Compute the number of entries below which we shrink the table by half */ michael@0: #define UNDERLOADED(n) (((n) > MINBUCKETS) ? ((n) >> 2) : 0) michael@0: michael@0: /* michael@0: ** Stubs for default hash allocator ops. michael@0: */ michael@0: static void * PR_CALLBACK michael@0: DefaultAllocTable(void *pool, PRSize size) michael@0: { michael@0: return PR_MALLOC(size); michael@0: } michael@0: michael@0: static void PR_CALLBACK michael@0: DefaultFreeTable(void *pool, void *item) michael@0: { michael@0: PR_Free(item); michael@0: } michael@0: michael@0: static PLHashEntry * PR_CALLBACK michael@0: DefaultAllocEntry(void *pool, const void *key) michael@0: { michael@0: return PR_NEW(PLHashEntry); michael@0: } michael@0: michael@0: static void PR_CALLBACK michael@0: DefaultFreeEntry(void *pool, PLHashEntry *he, PRUintn flag) michael@0: { michael@0: if (flag == HT_FREE_ENTRY) michael@0: PR_Free(he); michael@0: } michael@0: michael@0: static PLHashAllocOps defaultHashAllocOps = { michael@0: DefaultAllocTable, DefaultFreeTable, michael@0: DefaultAllocEntry, DefaultFreeEntry michael@0: }; michael@0: michael@0: PR_IMPLEMENT(PLHashTable *) michael@0: PL_NewHashTable(PRUint32 n, PLHashFunction keyHash, michael@0: PLHashComparator keyCompare, PLHashComparator valueCompare, michael@0: const PLHashAllocOps *allocOps, void *allocPriv) michael@0: { michael@0: PLHashTable *ht; michael@0: PRSize nb; michael@0: michael@0: if (n <= MINBUCKETS) { michael@0: n = MINBUCKETSLOG2; michael@0: } else { michael@0: n = PR_CeilingLog2(n); michael@0: if ((PRInt32)n < 0) michael@0: return 0; michael@0: } michael@0: michael@0: if (!allocOps) allocOps = &defaultHashAllocOps; michael@0: michael@0: ht = (PLHashTable*)((*allocOps->allocTable)(allocPriv, sizeof *ht)); michael@0: if (!ht) michael@0: return 0; michael@0: memset(ht, 0, sizeof *ht); michael@0: ht->shift = PL_HASH_BITS - n; michael@0: n = 1 << n; michael@0: nb = n * sizeof(PLHashEntry *); michael@0: ht->buckets = (PLHashEntry**)((*allocOps->allocTable)(allocPriv, nb)); michael@0: if (!ht->buckets) { michael@0: (*allocOps->freeTable)(allocPriv, ht); michael@0: return 0; michael@0: } michael@0: memset(ht->buckets, 0, nb); michael@0: michael@0: ht->keyHash = keyHash; michael@0: ht->keyCompare = keyCompare; michael@0: ht->valueCompare = valueCompare; michael@0: ht->allocOps = allocOps; michael@0: ht->allocPriv = allocPriv; michael@0: return ht; michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PL_HashTableDestroy(PLHashTable *ht) michael@0: { michael@0: PRUint32 i, n; michael@0: PLHashEntry *he, *next; michael@0: const PLHashAllocOps *allocOps = ht->allocOps; michael@0: void *allocPriv = ht->allocPriv; michael@0: michael@0: n = NBUCKETS(ht); michael@0: for (i = 0; i < n; i++) { michael@0: for (he = ht->buckets[i]; he; he = next) { michael@0: next = he->next; michael@0: (*allocOps->freeEntry)(allocPriv, he, HT_FREE_ENTRY); michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: memset(ht->buckets, 0xDB, n * sizeof ht->buckets[0]); michael@0: #endif michael@0: (*allocOps->freeTable)(allocPriv, ht->buckets); michael@0: #ifdef DEBUG michael@0: memset(ht, 0xDB, sizeof *ht); michael@0: #endif michael@0: (*allocOps->freeTable)(allocPriv, ht); michael@0: } michael@0: michael@0: /* michael@0: ** Multiplicative hash, from Knuth 6.4. michael@0: */ michael@0: #define GOLDEN_RATIO 0x9E3779B9U /* 2/(1+sqrt(5))*(2^32) */ michael@0: michael@0: PR_IMPLEMENT(PLHashEntry **) michael@0: PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key) michael@0: { michael@0: PLHashEntry *he, **hep, **hep0; michael@0: PLHashNumber h; michael@0: michael@0: #ifdef HASHMETER michael@0: ht->nlookups++; michael@0: #endif michael@0: h = keyHash * GOLDEN_RATIO; michael@0: h >>= ht->shift; michael@0: hep = hep0 = &ht->buckets[h]; michael@0: while ((he = *hep) != 0) { michael@0: if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) { michael@0: /* Move to front of chain if not already there */ michael@0: if (hep != hep0) { michael@0: *hep = he->next; michael@0: he->next = *hep0; michael@0: *hep0 = he; michael@0: } michael@0: return hep0; michael@0: } michael@0: hep = &he->next; michael@0: #ifdef HASHMETER michael@0: ht->nsteps++; michael@0: #endif michael@0: } michael@0: return hep; michael@0: } michael@0: michael@0: /* michael@0: ** Same as PL_HashTableRawLookup but doesn't reorder the hash entries. michael@0: */ michael@0: PR_IMPLEMENT(PLHashEntry **) michael@0: PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash, michael@0: const void *key) michael@0: { michael@0: PLHashEntry *he, **hep; michael@0: PLHashNumber h; michael@0: michael@0: #ifdef HASHMETER michael@0: ht->nlookups++; michael@0: #endif michael@0: h = keyHash * GOLDEN_RATIO; michael@0: h >>= ht->shift; michael@0: hep = &ht->buckets[h]; michael@0: while ((he = *hep) != 0) { michael@0: if (he->keyHash == keyHash && (*ht->keyCompare)(key, he->key)) { michael@0: break; michael@0: } michael@0: hep = &he->next; michael@0: #ifdef HASHMETER michael@0: ht->nsteps++; michael@0: #endif michael@0: } michael@0: return hep; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PLHashEntry *) michael@0: PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, michael@0: PLHashNumber keyHash, const void *key, void *value) michael@0: { michael@0: PRUint32 i, n; michael@0: PLHashEntry *he, *next, **oldbuckets; michael@0: PRSize nb; michael@0: michael@0: /* Grow the table if it is overloaded */ michael@0: n = NBUCKETS(ht); michael@0: if (ht->nentries >= OVERLOADED(n)) { michael@0: oldbuckets = ht->buckets; michael@0: nb = 2 * n * sizeof(PLHashEntry *); michael@0: ht->buckets = (PLHashEntry**) michael@0: ((*ht->allocOps->allocTable)(ht->allocPriv, nb)); michael@0: if (!ht->buckets) { michael@0: ht->buckets = oldbuckets; michael@0: return 0; michael@0: } michael@0: memset(ht->buckets, 0, nb); michael@0: #ifdef HASHMETER michael@0: ht->ngrows++; michael@0: #endif michael@0: ht->shift--; michael@0: michael@0: for (i = 0; i < n; i++) { michael@0: for (he = oldbuckets[i]; he; he = next) { michael@0: next = he->next; michael@0: hep = PL_HashTableRawLookup(ht, he->keyHash, he->key); michael@0: PR_ASSERT(*hep == 0); michael@0: he->next = 0; michael@0: *hep = he; michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]); michael@0: #endif michael@0: (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets); michael@0: hep = PL_HashTableRawLookup(ht, keyHash, key); michael@0: } michael@0: michael@0: /* Make a new key value entry */ michael@0: he = (*ht->allocOps->allocEntry)(ht->allocPriv, key); michael@0: if (!he) michael@0: return 0; michael@0: he->keyHash = keyHash; michael@0: he->key = key; michael@0: he->value = value; michael@0: he->next = *hep; michael@0: *hep = he; michael@0: ht->nentries++; michael@0: return he; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PLHashEntry *) michael@0: PL_HashTableAdd(PLHashTable *ht, const void *key, void *value) michael@0: { michael@0: PLHashNumber keyHash; michael@0: PLHashEntry *he, **hep; michael@0: michael@0: keyHash = (*ht->keyHash)(key); michael@0: hep = PL_HashTableRawLookup(ht, keyHash, key); michael@0: if ((he = *hep) != 0) { michael@0: /* Hit; see if values match */ michael@0: if ((*ht->valueCompare)(he->value, value)) { michael@0: /* key,value pair is already present in table */ michael@0: return he; michael@0: } michael@0: if (he->value) michael@0: (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_VALUE); michael@0: he->value = value; michael@0: return he; michael@0: } michael@0: return PL_HashTableRawAdd(ht, hep, keyHash, key, value); michael@0: } michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he) michael@0: { michael@0: PRUint32 i, n; michael@0: PLHashEntry *next, **oldbuckets; michael@0: PRSize nb; michael@0: michael@0: *hep = he->next; michael@0: (*ht->allocOps->freeEntry)(ht->allocPriv, he, HT_FREE_ENTRY); michael@0: michael@0: /* Shrink table if it's underloaded */ michael@0: n = NBUCKETS(ht); michael@0: if (--ht->nentries < UNDERLOADED(n)) { michael@0: oldbuckets = ht->buckets; michael@0: nb = n * sizeof(PLHashEntry*) / 2; michael@0: ht->buckets = (PLHashEntry**)( michael@0: (*ht->allocOps->allocTable)(ht->allocPriv, nb)); michael@0: if (!ht->buckets) { michael@0: ht->buckets = oldbuckets; michael@0: return; michael@0: } michael@0: memset(ht->buckets, 0, nb); michael@0: #ifdef HASHMETER michael@0: ht->nshrinks++; michael@0: #endif michael@0: ht->shift++; michael@0: michael@0: for (i = 0; i < n; i++) { michael@0: for (he = oldbuckets[i]; he; he = next) { michael@0: next = he->next; michael@0: hep = PL_HashTableRawLookup(ht, he->keyHash, he->key); michael@0: PR_ASSERT(*hep == 0); michael@0: he->next = 0; michael@0: *hep = he; michael@0: } michael@0: } michael@0: #ifdef DEBUG michael@0: memset(oldbuckets, 0xDB, n * sizeof oldbuckets[0]); michael@0: #endif michael@0: (*ht->allocOps->freeTable)(ht->allocPriv, oldbuckets); michael@0: } michael@0: } michael@0: michael@0: PR_IMPLEMENT(PRBool) michael@0: PL_HashTableRemove(PLHashTable *ht, const void *key) michael@0: { michael@0: PLHashNumber keyHash; michael@0: PLHashEntry *he, **hep; michael@0: michael@0: keyHash = (*ht->keyHash)(key); michael@0: hep = PL_HashTableRawLookup(ht, keyHash, key); michael@0: if ((he = *hep) == 0) michael@0: return PR_FALSE; michael@0: michael@0: /* Hit; remove element */ michael@0: PL_HashTableRawRemove(ht, hep, he); michael@0: return PR_TRUE; michael@0: } michael@0: michael@0: PR_IMPLEMENT(void *) michael@0: PL_HashTableLookup(PLHashTable *ht, const void *key) michael@0: { michael@0: PLHashNumber keyHash; michael@0: PLHashEntry *he, **hep; michael@0: michael@0: keyHash = (*ht->keyHash)(key); michael@0: hep = PL_HashTableRawLookup(ht, keyHash, key); michael@0: if ((he = *hep) != 0) { michael@0: return he->value; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: ** Same as PL_HashTableLookup but doesn't reorder the hash entries. michael@0: */ michael@0: PR_IMPLEMENT(void *) michael@0: PL_HashTableLookupConst(PLHashTable *ht, const void *key) michael@0: { michael@0: PLHashNumber keyHash; michael@0: PLHashEntry *he, **hep; michael@0: michael@0: keyHash = (*ht->keyHash)(key); michael@0: hep = PL_HashTableRawLookupConst(ht, keyHash, key); michael@0: if ((he = *hep) != 0) { michael@0: return he->value; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: /* michael@0: ** Iterate over the entries in the hash table calling func for each michael@0: ** entry found. Stop if "f" says to (return value & PR_ENUMERATE_STOP). michael@0: ** Return a count of the number of elements scanned. michael@0: */ michael@0: PR_IMPLEMENT(int) michael@0: PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg) michael@0: { michael@0: PLHashEntry *he, **hep; michael@0: PRUint32 i, nbuckets; michael@0: int rv, n = 0; michael@0: PLHashEntry *todo = 0; michael@0: michael@0: nbuckets = NBUCKETS(ht); michael@0: for (i = 0; i < nbuckets; i++) { michael@0: hep = &ht->buckets[i]; michael@0: while ((he = *hep) != 0) { michael@0: rv = (*f)(he, n, arg); michael@0: n++; michael@0: if (rv & (HT_ENUMERATE_REMOVE | HT_ENUMERATE_UNHASH)) { michael@0: *hep = he->next; michael@0: if (rv & HT_ENUMERATE_REMOVE) { michael@0: he->next = todo; michael@0: todo = he; michael@0: } michael@0: } else { michael@0: hep = &he->next; michael@0: } michael@0: if (rv & HT_ENUMERATE_STOP) { michael@0: goto out; michael@0: } michael@0: } michael@0: } michael@0: michael@0: out: michael@0: hep = &todo; michael@0: while ((he = *hep) != 0) { michael@0: PL_HashTableRawRemove(ht, hep, he); michael@0: } michael@0: return n; michael@0: } michael@0: michael@0: #ifdef HASHMETER michael@0: #include michael@0: #include michael@0: michael@0: PR_IMPLEMENT(void) michael@0: PL_HashTableDumpMeter(PLHashTable *ht, PLHashEnumerator dump, FILE *fp) michael@0: { michael@0: double mean, variance; michael@0: PRUint32 nchains, nbuckets; michael@0: PRUint32 i, n, maxChain, maxChainLen; michael@0: PLHashEntry *he; michael@0: michael@0: variance = 0; michael@0: nchains = 0; michael@0: maxChainLen = 0; michael@0: nbuckets = NBUCKETS(ht); michael@0: for (i = 0; i < nbuckets; i++) { michael@0: he = ht->buckets[i]; michael@0: if (!he) michael@0: continue; michael@0: nchains++; michael@0: for (n = 0; he; he = he->next) michael@0: n++; michael@0: variance += n * n; michael@0: if (n > maxChainLen) { michael@0: maxChainLen = n; michael@0: maxChain = i; michael@0: } michael@0: } michael@0: mean = (double)ht->nentries / nchains; michael@0: variance = fabs(variance / nchains - mean * mean); michael@0: michael@0: fprintf(fp, "\nHash table statistics:\n"); michael@0: fprintf(fp, " number of lookups: %u\n", ht->nlookups); michael@0: fprintf(fp, " number of entries: %u\n", ht->nentries); michael@0: fprintf(fp, " number of grows: %u\n", ht->ngrows); michael@0: fprintf(fp, " number of shrinks: %u\n", ht->nshrinks); michael@0: fprintf(fp, " mean steps per hash: %g\n", (double)ht->nsteps michael@0: / ht->nlookups); michael@0: fprintf(fp, "mean hash chain length: %g\n", mean); michael@0: fprintf(fp, " standard deviation: %g\n", sqrt(variance)); michael@0: fprintf(fp, " max hash chain length: %u\n", maxChainLen); michael@0: fprintf(fp, " max hash chain: [%u]\n", maxChain); michael@0: michael@0: for (he = ht->buckets[maxChain], i = 0; he; he = he->next, i++) michael@0: if ((*dump)(he, i, fp) != HT_ENUMERATE_NEXT) michael@0: break; michael@0: } michael@0: #endif /* HASHMETER */ michael@0: michael@0: PR_IMPLEMENT(int) michael@0: PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp) michael@0: { michael@0: int count; michael@0: michael@0: count = PL_HashTableEnumerateEntries(ht, dump, fp); michael@0: #ifdef HASHMETER michael@0: PL_HashTableDumpMeter(ht, dump, fp); michael@0: #endif michael@0: return count; michael@0: } michael@0: michael@0: PR_IMPLEMENT(PLHashNumber) michael@0: PL_HashString(const void *key) michael@0: { michael@0: PLHashNumber h; michael@0: const PRUint8 *s; michael@0: michael@0: h = 0; michael@0: for (s = (const PRUint8*)key; *s; s++) michael@0: h = PR_ROTATE_LEFT32(h, 4) ^ *s; michael@0: return h; michael@0: } michael@0: michael@0: PR_IMPLEMENT(int) michael@0: PL_CompareStrings(const void *v1, const void *v2) michael@0: { michael@0: return strcmp((const char*)v1, (const char*)v2) == 0; michael@0: } michael@0: michael@0: PR_IMPLEMENT(int) michael@0: PL_CompareValues(const void *v1, const void *v2) michael@0: { michael@0: return v1 == v2; michael@0: }