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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
41 * API to portable hash table code.
48 typedef struct PLHashEntry PLHashEntry
;
49 typedef struct PLHashTable PLHashTable
;
50 typedef PRUint32 PLHashNumber
;
51 #define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */
52 typedef PLHashNumber (PR_CALLBACK
*PLHashFunction
)(const void *key
);
53 typedef PRIntn (PR_CALLBACK
*PLHashComparator
)(const void *v1
, const void *v2
);
55 typedef PRIntn (PR_CALLBACK
*PLHashEnumerator
)(PLHashEntry
*he
, PRIntn i
, void *arg
);
57 /* Flag bits in PLHashEnumerator's return value */
58 #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
59 #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
60 #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
61 #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
);
70 #define HT_FREE_VALUE 0 /* just free the entry's value */
71 #define HT_FREE_ENTRY 1 /* free value and entire entry */
74 PLHashEntry
*next
; /* hash chain linkage */
75 PLHashNumber keyHash
; /* key hash function result */
76 const void *key
; /* ptr to opaque key */
77 void *value
; /* ptr to opaque value */
81 PLHashEntry
**buckets
; /* vector of hash buckets */
82 PRUint32 nentries
; /* number of entries in table */
83 PRUint32 shift
; /* multiplicative hash shift */
84 PLHashFunction keyHash
; /* key hash function */
85 PLHashComparator keyCompare
; /* key comparison function */
86 PLHashComparator valueCompare
; /* value comparison function */
87 const PLHashAllocOps
*allocOps
; /* allocation operations */
88 void *allocPriv
; /* allocation private data */
90 PRUint32 nlookups
; /* total number of lookups */
91 PRUint32 nsteps
; /* number of hash chains traversed */
92 PRUint32 ngrows
; /* number of table expansions */
93 PRUint32 nshrinks
; /* number of table contractions */
98 * Create a new hash table.
99 * If allocOps is null, use default allocator ops built on top of malloc().
101 PR_EXTERN(PLHashTable
*)
102 PL_NewHashTable(PRUint32 numBuckets
, PLHashFunction keyHash
,
103 PLHashComparator keyCompare
, PLHashComparator valueCompare
,
104 const PLHashAllocOps
*allocOps
, void *allocPriv
);
107 PL_HashTableDestroy(PLHashTable
*ht
);
109 /* Higher level access methods */
110 PR_EXTERN(PLHashEntry
*)
111 PL_HashTableAdd(PLHashTable
*ht
, const void *key
, void *value
);
114 PL_HashTableRemove(PLHashTable
*ht
, const void *key
);
117 PL_HashTableLookup(PLHashTable
*ht
, const void *key
);
120 PL_HashTableLookupConst(PLHashTable
*ht
, const void *key
);
123 PL_HashTableEnumerateEntries(PLHashTable
*ht
, PLHashEnumerator f
, void *arg
);
125 /* General-purpose C string hash function. */
126 PR_EXTERN(PLHashNumber
)
127 PL_HashString(const void *key
);
129 /* Compare strings using strcmp(), return true if equal. */
131 PL_CompareStrings(const void *v1
, const void *v2
);
133 /* Stub function just returns v1 == v2 */
135 PL_CompareValues(const void *v1
, const void *v2
);
137 /* Low level access methods */
138 PR_EXTERN(PLHashEntry
**)
139 PL_HashTableRawLookup(PLHashTable
*ht
, PLHashNumber keyHash
, const void *key
);
141 PR_EXTERN(PLHashEntry
**)
142 PL_HashTableRawLookupConst(PLHashTable
*ht
, PLHashNumber keyHash
,
145 PR_EXTERN(PLHashEntry
*)
146 PL_HashTableRawAdd(PLHashTable
*ht
, PLHashEntry
**hep
, PLHashNumber keyHash
,
147 const void *key
, void *value
);
150 PL_HashTableRawRemove(PLHashTable
*ht
, PLHashEntry
**hep
, PLHashEntry
*he
);
152 /* This can be trivially implemented using PL_HashTableEnumerateEntries. */
154 PL_HashTableDump(PLHashTable
*ht
, PLHashEnumerator dump
, FILE *fp
);
158 #endif /* plhash_h___ */