plhash.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef plhash_h___
  6. #define plhash_h___
  7. /*
  8. * API to portable hash table code.
  9. */
  10. #include <stdio.h>
  11. #include "prtypes.h"
  12. PR_BEGIN_EXTERN_C
  13. typedef struct PLHashEntry PLHashEntry;
  14. typedef struct PLHashTable PLHashTable;
  15. typedef PRUint32 PLHashNumber;
  16. #define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */
  17. typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key);
  18. typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2);
  19. typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
  20. /* Flag bits in PLHashEnumerator's return value */
  21. #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
  22. #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
  23. #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
  24. #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
  25. typedef struct PLHashAllocOps {
  26. void * (PR_CALLBACK *allocTable)(void *pool, PRSize size);
  27. void (PR_CALLBACK *freeTable)(void *pool, void *item);
  28. PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key);
  29. void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
  30. } PLHashAllocOps;
  31. #define HT_FREE_VALUE 0 /* just free the entry's value */
  32. #define HT_FREE_ENTRY 1 /* free value and entire entry */
  33. struct PLHashEntry {
  34. PLHashEntry *next; /* hash chain linkage */
  35. PLHashNumber keyHash; /* key hash function result */
  36. const void *key; /* ptr to opaque key */
  37. void *value; /* ptr to opaque value */
  38. };
  39. struct PLHashTable {
  40. PLHashEntry **buckets; /* vector of hash buckets */
  41. PRUint32 nentries; /* number of entries in table */
  42. PRUint32 shift; /* multiplicative hash shift */
  43. PLHashFunction keyHash; /* key hash function */
  44. PLHashComparator keyCompare; /* key comparison function */
  45. PLHashComparator valueCompare; /* value comparison function */
  46. const PLHashAllocOps *allocOps; /* allocation operations */
  47. void *allocPriv; /* allocation private data */
  48. #ifdef HASHMETER
  49. PRUint32 nlookups; /* total number of lookups */
  50. PRUint32 nsteps; /* number of hash chains traversed */
  51. PRUint32 ngrows; /* number of table expansions */
  52. PRUint32 nshrinks; /* number of table contractions */
  53. #endif
  54. };
  55. /*
  56. * Create a new hash table.
  57. * If allocOps is null, use default allocator ops built on top of malloc().
  58. */
  59. PR_EXTERN(PLHashTable *)
  60. PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
  61. PLHashComparator keyCompare, PLHashComparator valueCompare,
  62. const PLHashAllocOps *allocOps, void *allocPriv);
  63. PR_EXTERN(void)
  64. PL_HashTableDestroy(PLHashTable *ht);
  65. /* Higher level access methods */
  66. PR_EXTERN(PLHashEntry *)
  67. PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
  68. PR_EXTERN(PRBool)
  69. PL_HashTableRemove(PLHashTable *ht, const void *key);
  70. PR_EXTERN(void *)
  71. PL_HashTableLookup(PLHashTable *ht, const void *key);
  72. PR_EXTERN(void *)
  73. PL_HashTableLookupConst(PLHashTable *ht, const void *key);
  74. PR_EXTERN(PRIntn)
  75. PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
  76. /* General-purpose C string hash function. */
  77. PR_EXTERN(PLHashNumber)
  78. PL_HashString(const void *key);
  79. /* Compare strings using strcmp(), return true if equal. */
  80. PR_EXTERN(PRIntn)
  81. PL_CompareStrings(const void *v1, const void *v2);
  82. /* Stub function just returns v1 == v2 */
  83. PR_EXTERN(PRIntn)
  84. PL_CompareValues(const void *v1, const void *v2);
  85. /* Low level access methods */
  86. PR_EXTERN(PLHashEntry **)
  87. PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
  88. PR_EXTERN(PLHashEntry **)
  89. PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
  90. const void *key);
  91. PR_EXTERN(PLHashEntry *)
  92. PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
  93. const void *key, void *value);
  94. PR_EXTERN(void)
  95. PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
  96. /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
  97. PR_EXTERN(PRIntn)
  98. PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
  99. PR_END_EXTERN_C
  100. #endif /* plhash_h___ */