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 | typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
|
---|
73 |
|
---|
74 | /* Flag bits in PLHashEnumerator's return value */
|
---|
75 | #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
|
---|
76 | #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
|
---|
77 | #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
|
---|
78 | #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
|
---|
79 |
|
---|
80 | typedef struct PLHashAllocOps {
|
---|
81 | void * (PR_CALLBACK *allocTable)(void *pool, PRSize size);
|
---|
82 | void (PR_CALLBACK *freeTable)(void *pool, void *item);
|
---|
83 | PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key);
|
---|
84 | void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
|
---|
85 | } PLHashAllocOps;
|
---|
86 |
|
---|
87 | #define HT_FREE_VALUE 0 /* just free the entry's value */
|
---|
88 | #define HT_FREE_ENTRY 1 /* free value and entire entry */
|
---|
89 |
|
---|
90 | struct PLHashEntry {
|
---|
91 | PLHashEntry *next; /* hash chain linkage */
|
---|
92 | PLHashNumber keyHash; /* key hash function result */
|
---|
93 | const void *key; /* ptr to opaque key */
|
---|
94 | void *value; /* ptr to opaque value */
|
---|
95 | };
|
---|
96 |
|
---|
97 | struct PLHashTable {
|
---|
98 | PLHashEntry **buckets; /* vector of hash buckets */
|
---|
99 | PRUint32 nentries; /* number of entries in table */
|
---|
100 | PRUint32 shift; /* multiplicative hash shift */
|
---|
101 | PLHashFunction keyHash; /* key hash function */
|
---|
102 | PLHashComparator keyCompare; /* key comparison function */
|
---|
103 | PLHashComparator valueCompare; /* value comparison function */
|
---|
104 | const PLHashAllocOps *allocOps; /* allocation operations */
|
---|
105 | void *allocPriv; /* allocation private data */
|
---|
106 | };
|
---|
107 |
|
---|
108 | /*
|
---|
109 | * Create a new hash table.
|
---|
110 | * If allocOps is null, use default allocator ops built on top of malloc().
|
---|
111 | */
|
---|
112 | PR_EXTERN(PLHashTable *)
|
---|
113 | PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
|
---|
114 | PLHashComparator keyCompare, PLHashComparator valueCompare,
|
---|
115 | const PLHashAllocOps *allocOps, void *allocPriv);
|
---|
116 |
|
---|
117 | PR_EXTERN(void)
|
---|
118 | PL_HashTableDestroy(PLHashTable *ht);
|
---|
119 |
|
---|
120 | /* Higher level access methods */
|
---|
121 | PR_EXTERN(PLHashEntry *)
|
---|
122 | PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
|
---|
123 |
|
---|
124 | PR_EXTERN(PRBool)
|
---|
125 | PL_HashTableRemove(PLHashTable *ht, const void *key);
|
---|
126 |
|
---|
127 | PR_EXTERN(void *)
|
---|
128 | PL_HashTableLookup(PLHashTable *ht, const void *key);
|
---|
129 |
|
---|
130 | PR_EXTERN(void *)
|
---|
131 | PL_HashTableLookupConst(PLHashTable *ht, const void *key);
|
---|
132 |
|
---|
133 | PR_EXTERN(PRIntn)
|
---|
134 | PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
|
---|
135 |
|
---|
136 | /* General-purpose C string hash function. */
|
---|
137 | PR_EXTERN(PLHashNumber)
|
---|
138 | PL_HashString(const void *key);
|
---|
139 |
|
---|
140 | /* Compare strings using strcmp(), return true if equal. */
|
---|
141 | PR_EXTERN(PRIntn)
|
---|
142 | PL_CompareStrings(const void *v1, const void *v2);
|
---|
143 |
|
---|
144 | /* Stub function just returns v1 == v2 */
|
---|
145 | PR_EXTERN(PRIntn)
|
---|
146 | PL_CompareValues(const void *v1, const void *v2);
|
---|
147 |
|
---|
148 | /* Low level access methods */
|
---|
149 | PR_EXTERN(PLHashEntry **)
|
---|
150 | PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
|
---|
151 |
|
---|
152 | PR_EXTERN(PLHashEntry **)
|
---|
153 | PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
|
---|
154 | const void *key);
|
---|
155 |
|
---|
156 | PR_EXTERN(PLHashEntry *)
|
---|
157 | PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
|
---|
158 | const void *key, void *value);
|
---|
159 |
|
---|
160 | PR_EXTERN(void)
|
---|
161 | PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
|
---|
162 |
|
---|
163 | /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
|
---|
164 | PR_EXTERN(PRIntn)
|
---|
165 | PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
|
---|
166 |
|
---|
167 | PR_END_EXTERN_C
|
---|
168 |
|
---|
169 | #endif /* plhash_h___ */
|
---|