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: #ifndef plhash_h___ michael@0: #define plhash_h___ michael@0: /* michael@0: * API to portable hash table code. michael@0: */ michael@0: #include michael@0: #include "prtypes.h" michael@0: michael@0: PR_BEGIN_EXTERN_C michael@0: michael@0: typedef struct PLHashEntry PLHashEntry; michael@0: typedef struct PLHashTable PLHashTable; michael@0: typedef PRUint32 PLHashNumber; michael@0: #define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */ michael@0: typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key); michael@0: typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2); michael@0: michael@0: typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg); michael@0: michael@0: /* Flag bits in PLHashEnumerator's return value */ michael@0: #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */ michael@0: #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */ michael@0: #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */ michael@0: #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */ michael@0: michael@0: typedef struct PLHashAllocOps { michael@0: void * (PR_CALLBACK *allocTable)(void *pool, PRSize size); michael@0: void (PR_CALLBACK *freeTable)(void *pool, void *item); michael@0: PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key); michael@0: void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag); michael@0: } PLHashAllocOps; michael@0: michael@0: #define HT_FREE_VALUE 0 /* just free the entry's value */ michael@0: #define HT_FREE_ENTRY 1 /* free value and entire entry */ michael@0: michael@0: struct PLHashEntry { michael@0: PLHashEntry *next; /* hash chain linkage */ michael@0: PLHashNumber keyHash; /* key hash function result */ michael@0: const void *key; /* ptr to opaque key */ michael@0: void *value; /* ptr to opaque value */ michael@0: }; michael@0: michael@0: struct PLHashTable { michael@0: PLHashEntry **buckets; /* vector of hash buckets */ michael@0: PRUint32 nentries; /* number of entries in table */ michael@0: PRUint32 shift; /* multiplicative hash shift */ michael@0: PLHashFunction keyHash; /* key hash function */ michael@0: PLHashComparator keyCompare; /* key comparison function */ michael@0: PLHashComparator valueCompare; /* value comparison function */ michael@0: const PLHashAllocOps *allocOps; /* allocation operations */ michael@0: void *allocPriv; /* allocation private data */ michael@0: #ifdef HASHMETER michael@0: PRUint32 nlookups; /* total number of lookups */ michael@0: PRUint32 nsteps; /* number of hash chains traversed */ michael@0: PRUint32 ngrows; /* number of table expansions */ michael@0: PRUint32 nshrinks; /* number of table contractions */ michael@0: #endif michael@0: }; michael@0: michael@0: /* michael@0: * Create a new hash table. michael@0: * If allocOps is null, use default allocator ops built on top of malloc(). michael@0: */ michael@0: PR_EXTERN(PLHashTable *) michael@0: PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash, michael@0: PLHashComparator keyCompare, PLHashComparator valueCompare, michael@0: const PLHashAllocOps *allocOps, void *allocPriv); michael@0: michael@0: PR_EXTERN(void) michael@0: PL_HashTableDestroy(PLHashTable *ht); michael@0: michael@0: /* Higher level access methods */ michael@0: PR_EXTERN(PLHashEntry *) michael@0: PL_HashTableAdd(PLHashTable *ht, const void *key, void *value); michael@0: michael@0: PR_EXTERN(PRBool) michael@0: PL_HashTableRemove(PLHashTable *ht, const void *key); michael@0: michael@0: PR_EXTERN(void *) michael@0: PL_HashTableLookup(PLHashTable *ht, const void *key); michael@0: michael@0: PR_EXTERN(void *) michael@0: PL_HashTableLookupConst(PLHashTable *ht, const void *key); michael@0: michael@0: PR_EXTERN(PRIntn) michael@0: PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg); michael@0: michael@0: /* General-purpose C string hash function. */ michael@0: PR_EXTERN(PLHashNumber) michael@0: PL_HashString(const void *key); michael@0: michael@0: /* Compare strings using strcmp(), return true if equal. */ michael@0: PR_EXTERN(PRIntn) michael@0: PL_CompareStrings(const void *v1, const void *v2); michael@0: michael@0: /* Stub function just returns v1 == v2 */ michael@0: PR_EXTERN(PRIntn) michael@0: PL_CompareValues(const void *v1, const void *v2); michael@0: michael@0: /* Low level access methods */ michael@0: PR_EXTERN(PLHashEntry **) michael@0: PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key); michael@0: michael@0: PR_EXTERN(PLHashEntry **) michael@0: PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash, michael@0: const void *key); michael@0: michael@0: PR_EXTERN(PLHashEntry *) michael@0: PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash, michael@0: const void *key, void *value); michael@0: michael@0: PR_EXTERN(void) michael@0: PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he); michael@0: michael@0: /* This can be trivially implemented using PL_HashTableEnumerateEntries. */ michael@0: PR_EXTERN(PRIntn) michael@0: PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp); michael@0: michael@0: PR_END_EXTERN_C michael@0: michael@0: #endif /* plhash_h___ */