plhash.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef plhash_h___
  38. #define plhash_h___
  39. /*
  40. * API to portable hash table code.
  41. */
  42. #include <stdio.h>
  43. #include "prtypes.h"
  44. PR_BEGIN_EXTERN_C
  45. typedef struct PLHashEntry PLHashEntry;
  46. typedef struct PLHashTable PLHashTable;
  47. typedef PRUint32 PLHashNumber;
  48. #define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */
  49. typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key);
  50. typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2);
  51. #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP) /* for nsSpaceManager.cpp */
  52. PR_END_EXTERN_C /* and nsHTMLDocument.cpp */
  53. #endif
  54. typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
  55. #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP)
  56. PR_BEGIN_EXTERN_C
  57. #endif
  58. /* Flag bits in PLHashEnumerator's return value */
  59. #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
  60. #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
  61. #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
  62. #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
  63. typedef struct PLHashAllocOps {
  64. void * (PR_CALLBACK *allocTable)(void *pool, PRSize size);
  65. void (PR_CALLBACK *freeTable)(void *pool, void *item);
  66. PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key);
  67. void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
  68. } PLHashAllocOps;
  69. #define HT_FREE_VALUE 0 /* just free the entry's value */
  70. #define HT_FREE_ENTRY 1 /* free value and entire entry */
  71. struct PLHashEntry {
  72. PLHashEntry *next; /* hash chain linkage */
  73. PLHashNumber keyHash; /* key hash function result */
  74. const void *key; /* ptr to opaque key */
  75. void *value; /* ptr to opaque value */
  76. };
  77. struct PLHashTable {
  78. PLHashEntry **buckets; /* vector of hash buckets */
  79. PRUint32 nentries; /* number of entries in table */
  80. PRUint32 shift; /* multiplicative hash shift */
  81. PLHashFunction keyHash; /* key hash function */
  82. PLHashComparator keyCompare; /* key comparison function */
  83. PLHashComparator valueCompare; /* value comparison function */
  84. const PLHashAllocOps *allocOps; /* allocation operations */
  85. void *allocPriv; /* allocation private data */
  86. #ifdef HASHMETER
  87. PRUint32 nlookups; /* total number of lookups */
  88. PRUint32 nsteps; /* number of hash chains traversed */
  89. PRUint32 ngrows; /* number of table expansions */
  90. PRUint32 nshrinks; /* number of table contractions */
  91. #endif
  92. };
  93. /*
  94. * Create a new hash table.
  95. * If allocOps is null, use default allocator ops built on top of malloc().
  96. */
  97. PR_EXTERN(PLHashTable *)
  98. PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
  99. PLHashComparator keyCompare, PLHashComparator valueCompare,
  100. const PLHashAllocOps *allocOps, void *allocPriv);
  101. PR_EXTERN(void)
  102. PL_HashTableDestroy(PLHashTable *ht);
  103. /* Higher level access methods */
  104. PR_EXTERN(PLHashEntry *)
  105. PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
  106. PR_EXTERN(PRBool)
  107. PL_HashTableRemove(PLHashTable *ht, const void *key);
  108. PR_EXTERN(void *)
  109. PL_HashTableLookup(PLHashTable *ht, const void *key);
  110. PR_EXTERN(void *)
  111. PL_HashTableLookupConst(PLHashTable *ht, const void *key);
  112. PR_EXTERN(PRIntn)
  113. PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
  114. /* General-purpose C string hash function. */
  115. PR_EXTERN(PLHashNumber)
  116. PL_HashString(const void *key);
  117. /* Compare strings using strcmp(), return true if equal. */
  118. PR_EXTERN(PRIntn)
  119. PL_CompareStrings(const void *v1, const void *v2);
  120. /* Stub function just returns v1 == v2 */
  121. PR_EXTERN(PRIntn)
  122. PL_CompareValues(const void *v1, const void *v2);
  123. /* Low level access methods */
  124. PR_EXTERN(PLHashEntry **)
  125. PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
  126. PR_EXTERN(PLHashEntry **)
  127. PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
  128. const void *key);
  129. PR_EXTERN(PLHashEntry *)
  130. PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
  131. const void *key, void *value);
  132. PR_EXTERN(void)
  133. PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
  134. /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
  135. PR_EXTERN(PRIntn)
  136. PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
  137. PR_END_EXTERN_C
  138. #endif /* plhash_h___ */