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