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