|
|
1.1 root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2: *
3: * The contents of this file are subject to the Netscape Public
4: * License Version 1.1 (the "License"); you may not use this file
5: * except in compliance with the License. You may obtain a copy of
6: * the License at http://www.mozilla.org/NPL/
7: *
8: * Software distributed under the License is distributed on an "AS
9: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10: * implied. See the License for the specific language governing
11: * rights and limitations under the License.
12: *
13: * The Original Code is Mozilla Communicator client code, released
14: * March 31, 1998.
15: *
16: * The Initial Developer of the Original Code is Netscape
17: * Communications Corporation. Portions created by Netscape are
18: * Copyright (C) 1998 Netscape Communications Corporation. All
19: * Rights Reserved.
20: *
21: * Contributor(s):
22: *
23: * Alternatively, the contents of this file may be used under the
24: * terms of the GNU Public License (the "GPL"), in which case the
25: * provisions of the GPL are applicable instead of those above.
26: * If you wish to allow use of your version of this file only
27: * under the terms of the GPL and not to allow others to use your
28: * version of this file under the NPL, indicate your decision by
29: * deleting the provisions above and replace them with the notice
30: * and other provisions required by the GPL. If you do not delete
31: * the provisions above, a recipient may use your version of this
32: * file under either the NPL or the GPL.
33: */
34:
35: #ifndef jshash_h___
36: #define jshash_h___
37: /*
38: * API to portable hash table code.
39: */
40: #include <stddef.h>
41: #include <stdio.h>
42: #include "jstypes.h"
43: #include "jscompat.h"
44:
45: JS_BEGIN_EXTERN_C
46:
47: typedef uint32 JSHashNumber;
48: typedef struct JSHashEntry JSHashEntry;
49: typedef struct JSHashTable JSHashTable;
50:
51: #define JS_HASH_BITS 32
52: #define JS_GOLDEN_RATIO 0x9E3779B9U
53:
54: typedef JSHashNumber (* JS_DLL_CALLBACK JSHashFunction)(const void *key);
55: typedef intN (* JS_DLL_CALLBACK JSHashComparator)(const void *v1, const void *v2);
56: typedef intN (* JS_DLL_CALLBACK JSHashEnumerator)(JSHashEntry *he, intN i, void *arg);
57:
58: /* Flag bits in JSHashEnumerator'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:
64: typedef struct JSHashAllocOps {
65: void * (*allocTable)(void *pool, size_t size);
66: void (*freeTable)(void *pool, void *item);
67: JSHashEntry * (*allocEntry)(void *pool, const void *key);
68: void (*freeEntry)(void *pool, JSHashEntry *he, uintN flag);
69: } JSHashAllocOps;
70:
71: #define HT_FREE_VALUE 0 /* just free the entry's value */
72: #define HT_FREE_ENTRY 1 /* free value and entire entry */
73:
74: struct JSHashEntry {
75: JSHashEntry *next; /* hash chain linkage */
76: JSHashNumber keyHash; /* key hash function result */
77: const void *key; /* ptr to opaque key */
78: void *value; /* ptr to opaque value */
79: };
80:
81: struct JSHashTable {
82: JSHashEntry **buckets; /* vector of hash buckets */
83: uint32 nentries; /* number of entries in table */
84: uint32 shift; /* multiplicative hash shift */
85: JSHashFunction keyHash; /* key hash function */
86: JSHashComparator keyCompare; /* key comparison function */
87: JSHashComparator valueCompare; /* value comparison function */
88: JSHashAllocOps *allocOps; /* allocation operations */
89: void *allocPriv; /* allocation private data */
90: #ifdef HASHMETER
91: uint32 nlookups; /* total number of lookups */
92: uint32 nsteps; /* number of hash chains traversed */
93: uint32 ngrows; /* number of table expansions */
94: uint32 nshrinks; /* number of table contractions */
95: #endif
96: };
97:
98: /*
99: * Create a new hash table.
100: * If allocOps is null, use default allocator ops built on top of malloc().
101: */
102: extern JS_PUBLIC_API(JSHashTable *)
103: JS_NewHashTable(uint32 n, JSHashFunction keyHash,
104: JSHashComparator keyCompare, JSHashComparator valueCompare,
105: JSHashAllocOps *allocOps, void *allocPriv);
106:
107: extern JS_PUBLIC_API(void)
108: JS_HashTableDestroy(JSHashTable *ht);
109:
110: /* Low level access methods */
111: extern JS_PUBLIC_API(JSHashEntry **)
112: JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key);
113:
114: extern JS_PUBLIC_API(JSHashEntry *)
115: JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **hep, JSHashNumber keyHash,
116: const void *key, void *value);
117:
118: extern JS_PUBLIC_API(void)
119: JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he);
120:
121: /* Higher level access methods */
122: extern JS_PUBLIC_API(JSHashEntry *)
123: JS_HashTableAdd(JSHashTable *ht, const void *key, void *value);
124:
125: extern JS_PUBLIC_API(JSBool)
126: JS_HashTableRemove(JSHashTable *ht, const void *key);
127:
128: extern JS_PUBLIC_API(intN)
129: JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg);
130:
131: extern JS_PUBLIC_API(void *)
132: JS_HashTableLookup(JSHashTable *ht, const void *key);
133:
134: extern JS_PUBLIC_API(intN)
135: JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp);
136:
137: /* General-purpose C string hash function. */
138: extern JS_PUBLIC_API(JSHashNumber)
139: JS_HashString(const void *key);
140:
141: /* Stub function just returns v1 == v2 */
142: extern JS_PUBLIC_API(intN)
143: JS_CompareValues(const void *v1, const void *v2);
144:
145: JS_END_EXTERN_C
146:
147: #endif /* jshash_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.