1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * This is a copy of the NSPR hash-table library, but it has been slightly
42 * modified to allow an additional argument to be passed into the hash
43 * key-comparison function. This is used to maintain thread-safety by
44 * passing in a JNIEnv pointer to the key-comparison function rather
45 * than storing it in a global. All types,function names, etc. have
46 * been renamed from their original NSPR names to protect the innocent.
52 * API to portable hash table code.
60 typedef struct JSJHashEntry JSJHashEntry
;
61 typedef struct JSJHashTable JSJHashTable
;
62 typedef JSUint32 JSJHashNumber
;
63 #define JSJ_HASH_BITS 32
64 typedef JSJHashNumber (* JSJHashFunction
)(const void *key
, void *arg
);
65 typedef JSIntn (* JSJHashComparator
)(const void *v1
, const void *v2
, void *arg
);
66 typedef JSIntn (* JSJHashEnumerator
)(JSJHashEntry
*he
, JSIntn i
, void *arg
);
68 /* Flag bits in JSJHashEnumerator's return value */
69 #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
70 #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
71 #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
72 #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
74 typedef struct JSJHashAllocOps
{
75 void * (*allocTable
)(void *pool
, size_t size
);
76 void (*freeTable
)(void *pool
, void *item
);
77 JSJHashEntry
* (*allocEntry
)(void *pool
, const void *key
);
78 void (*freeEntry
)(void *pool
, JSJHashEntry
*he
, JSUintn flag
);
81 #define HT_FREE_VALUE 0 /* just free the entry's value */
82 #define HT_FREE_ENTRY 1 /* free value and entire entry */
85 JSJHashEntry
*next
; /* hash chain linkage */
86 JSJHashNumber keyHash
; /* key hash function result */
87 const void *key
; /* ptr to opaque key */
88 void *value
; /* ptr to opaque value */
92 JSJHashEntry
**buckets
; /* vector of hash buckets */
93 JSUint32 nentries
; /* number of entries in table */
94 JSUint32 shift
; /* multiplicative hash shift */
95 JSJHashFunction keyHash
; /* key hash function */
96 JSJHashComparator keyCompare
; /* key comparison function */
97 JSJHashComparator valueCompare
; /* value comparison function */
98 JSJHashAllocOps
*allocOps
; /* allocation operations */
99 void *allocPriv
; /* allocation private data */
101 JSUint32 nlookups
; /* total number of lookups */
102 JSUint32 nsteps
; /* number of hash chains traversed */
103 JSUint32 ngrows
; /* number of table expansions */
104 JSUint32 nshrinks
; /* number of table contractions */
109 * Create a new hash table.
110 * If allocOps is null, use default allocator ops built on top of malloc().
112 JS_EXTERN_API(JSJHashTable
*)
113 JSJ_NewHashTable(JSUint32 n
, JSJHashFunction keyHash
,
114 JSJHashComparator keyCompare
, JSJHashComparator valueCompare
,
115 JSJHashAllocOps
*allocOps
, void *allocPriv
);
118 JSJ_HashTableDestroy(JSJHashTable
*ht
);
120 /* Low level access methods */
121 JS_EXTERN_API(JSJHashEntry
**)
122 JSJ_HashTableRawLookup(JSJHashTable
*ht
, JSJHashNumber keyHash
, const void *key
, void *arg
);
124 JS_EXTERN_API(JSJHashEntry
*)
125 JSJ_HashTableRawAdd(JSJHashTable
*ht
, JSJHashEntry
**hep
, JSJHashNumber keyHash
,
126 const void *key
, void *value
, void *arg
);
129 JSJ_HashTableRawRemove(JSJHashTable
*ht
, JSJHashEntry
**hep
, JSJHashEntry
*he
, void *arg
);
131 /* Higher level access methods */
132 JS_EXTERN_API(JSJHashEntry
*)
133 JSJ_HashTableAdd(JSJHashTable
*ht
, const void *key
, void *value
, void *arg
);
135 JS_EXTERN_API(JSBool
)
136 JSJ_HashTableRemove(JSJHashTable
*ht
, const void *key
, void *arg
);
138 JS_EXTERN_API(JSIntn
)
139 JSJ_HashTableEnumerateEntries(JSJHashTable
*ht
, JSJHashEnumerator f
, void *arg
);
141 JS_EXTERN_API(void *)
142 JSJ_HashTableLookup(JSJHashTable
*ht
, const void *key
, void *arg
);
144 JS_EXTERN_API(JSIntn
)
145 JSJ_HashTableDump(JSJHashTable
*ht
, JSJHashEnumerator dump
, FILE *fp
);
147 /* General-purpose C string hash function. */
148 JS_EXTERN_API(JSJHashNumber
)
149 JSJ_HashString(const void *key
);
151 /* Compare strings using strcmp(), return true if equal. */
153 JSJ_CompareStrings(const void *v1
, const void *v2
);
155 /* Stub function just returns v1 == v2 */
156 JS_EXTERN_API(JSIntn
)
157 JSJ_CompareValues(const void *v1
, const void *v2
);
161 #endif /* jsj_hash_h___ */