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 |
|
---|
38 | #ifndef plhash_h___
|
---|
39 | #define plhash_h___
|
---|
40 | /*
|
---|
41 | * API to portable hash table code.
|
---|
42 | */
|
---|
43 | #include <stdio.h>
|
---|
44 | #include "prtypes.h"
|
---|
45 |
|
---|
46 | #ifdef VBOX_WITH_XPCOM_NAMESPACE_CLEANUP
|
---|
47 | #define PL_CompareStrings VBoxNsplPL_CompareStrings
|
---|
48 | #define PL_CompareValues VBoxNsplPL_CompareValues
|
---|
49 | #define PL_HashString VBoxNsplPL_HashString
|
---|
50 | #define PL_HashTableAdd VBoxNsplPL_HashTableAdd
|
---|
51 | #define PL_HashTableDestroy VBoxNsplPL_HashTableDestroy
|
---|
52 | #define PL_HashTableLookup VBoxNsplPL_HashTableLookup
|
---|
53 | #define PL_HashTableRemove VBoxNsplPL_HashTableRemove
|
---|
54 | #define PL_NewHashTable VBoxNsplPL_NewHashTable
|
---|
55 | #define PL_HashTableDump VBoxNsplPL_HashTableDump
|
---|
56 | #define PL_HashTableEnumerateEntries VBoxNsplPL_HashTableEnumerateEntries
|
---|
57 | #define PL_HashTableLookupConst VBoxNsplPL_HashTableLookupConst
|
---|
58 | #define PL_HashTableRawAdd VBoxNsplPL_HashTableRawAdd
|
---|
59 | #define PL_HashTableRawLookup VBoxNsplPL_HashTableRawLookup
|
---|
60 | #define PL_HashTableRawLookupConst VBoxNsplPL_HashTableRawLookupConst
|
---|
61 | #define PL_HashTableRawRemove VBoxNsplPL_HashTableRawRemove
|
---|
62 | #endif /* VBOX_WITH_XPCOM_NAMESPACE_CLEANUP */
|
---|
63 |
|
---|
64 | PR_BEGIN_EXTERN_C
|
---|
65 |
|
---|
66 | typedef struct PLHashEntry PLHashEntry;
|
---|
67 | typedef struct PLHashTable PLHashTable;
|
---|
68 | typedef PRUint32 PLHashNumber;
|
---|
69 | #define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */
|
---|
70 | typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key);
|
---|
71 | typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2);
|
---|
72 |
|
---|
73 | #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP) /* for nsSpaceManager.cpp */
|
---|
74 | PR_END_EXTERN_C /* and nsHTMLDocument.cpp */
|
---|
75 | #endif
|
---|
76 | typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
|
---|
77 |
|
---|
78 | #if defined(XP_OS2_VACPP) && defined(VACPP_FLIP)
|
---|
79 | PR_BEGIN_EXTERN_C
|
---|
80 | #endif
|
---|
81 |
|
---|
82 | /* Flag bits in PLHashEnumerator's return value */
|
---|
83 | #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
|
---|
84 | #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
|
---|
85 | #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
|
---|
86 | #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
|
---|
87 |
|
---|
88 | typedef struct PLHashAllocOps {
|
---|
89 | void * (PR_CALLBACK *allocTable)(void *pool, PRSize size);
|
---|
90 | void (PR_CALLBACK *freeTable)(void *pool, void *item);
|
---|
91 | PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key);
|
---|
92 | void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
|
---|
93 | } PLHashAllocOps;
|
---|
94 |
|
---|
95 | #define HT_FREE_VALUE 0 /* just free the entry's value */
|
---|
96 | #define HT_FREE_ENTRY 1 /* free value and entire entry */
|
---|
97 |
|
---|
98 | struct PLHashEntry {
|
---|
99 | PLHashEntry *next; /* hash chain linkage */
|
---|
100 | PLHashNumber keyHash; /* key hash function result */
|
---|
101 | const void *key; /* ptr to opaque key */
|
---|
102 | void *value; /* ptr to opaque value */
|
---|
103 | };
|
---|
104 |
|
---|
105 | struct PLHashTable {
|
---|
106 | PLHashEntry **buckets; /* vector of hash buckets */
|
---|
107 | PRUint32 nentries; /* number of entries in table */
|
---|
108 | PRUint32 shift; /* multiplicative hash shift */
|
---|
109 | PLHashFunction keyHash; /* key hash function */
|
---|
110 | PLHashComparator keyCompare; /* key comparison function */
|
---|
111 | PLHashComparator valueCompare; /* value comparison function */
|
---|
112 | const PLHashAllocOps *allocOps; /* allocation operations */
|
---|
113 | void *allocPriv; /* allocation private data */
|
---|
114 | #ifdef HASHMETER
|
---|
115 | PRUint32 nlookups; /* total number of lookups */
|
---|
116 | PRUint32 nsteps; /* number of hash chains traversed */
|
---|
117 | PRUint32 ngrows; /* number of table expansions */
|
---|
118 | PRUint32 nshrinks; /* number of table contractions */
|
---|
119 | #endif
|
---|
120 | };
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Create a new hash table.
|
---|
124 | * If allocOps is null, use default allocator ops built on top of malloc().
|
---|
125 | */
|
---|
126 | PR_EXTERN(PLHashTable *)
|
---|
127 | PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
|
---|
128 | PLHashComparator keyCompare, PLHashComparator valueCompare,
|
---|
129 | const PLHashAllocOps *allocOps, void *allocPriv);
|
---|
130 |
|
---|
131 | PR_EXTERN(void)
|
---|
132 | PL_HashTableDestroy(PLHashTable *ht);
|
---|
133 |
|
---|
134 | /* Higher level access methods */
|
---|
135 | PR_EXTERN(PLHashEntry *)
|
---|
136 | PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
|
---|
137 |
|
---|
138 | PR_EXTERN(PRBool)
|
---|
139 | PL_HashTableRemove(PLHashTable *ht, const void *key);
|
---|
140 |
|
---|
141 | PR_EXTERN(void *)
|
---|
142 | PL_HashTableLookup(PLHashTable *ht, const void *key);
|
---|
143 |
|
---|
144 | PR_EXTERN(void *)
|
---|
145 | PL_HashTableLookupConst(PLHashTable *ht, const void *key);
|
---|
146 |
|
---|
147 | PR_EXTERN(PRIntn)
|
---|
148 | PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
|
---|
149 |
|
---|
150 | /* General-purpose C string hash function. */
|
---|
151 | PR_EXTERN(PLHashNumber)
|
---|
152 | PL_HashString(const void *key);
|
---|
153 |
|
---|
154 | /* Compare strings using strcmp(), return true if equal. */
|
---|
155 | PR_EXTERN(PRIntn)
|
---|
156 | PL_CompareStrings(const void *v1, const void *v2);
|
---|
157 |
|
---|
158 | /* Stub function just returns v1 == v2 */
|
---|
159 | PR_EXTERN(PRIntn)
|
---|
160 | PL_CompareValues(const void *v1, const void *v2);
|
---|
161 |
|
---|
162 | /* Low level access methods */
|
---|
163 | PR_EXTERN(PLHashEntry **)
|
---|
164 | PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
|
---|
165 |
|
---|
166 | PR_EXTERN(PLHashEntry **)
|
---|
167 | PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
|
---|
168 | const void *key);
|
---|
169 |
|
---|
170 | PR_EXTERN(PLHashEntry *)
|
---|
171 | PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
|
---|
172 | const void *key, void *value);
|
---|
173 |
|
---|
174 | PR_EXTERN(void)
|
---|
175 | PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
|
---|
176 |
|
---|
177 | /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
|
---|
178 | PR_EXTERN(PRIntn)
|
---|
179 | PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
|
---|
180 |
|
---|
181 | PR_END_EXTERN_C
|
---|
182 |
|
---|
183 | #endif /* plhash_h___ */
|
---|