|
|
1.1 root 1: /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2: *
3: * ***** BEGIN LICENSE BLOCK *****
4: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5: *
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/
10: *
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
14: * License.
15: *
16: * The Original Code is Mozilla Communicator client code, released
17: * March 31, 1998.
18: *
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.
23: *
24: * Contributor(s):
25: *
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.
37: *
38: * ***** END LICENSE BLOCK ***** */
39:
40: #ifndef jshash_h___
41: #define jshash_h___
42: /*
43: * API to portable hash table code.
44: */
45: #include <stddef.h>
46: #include <stdio.h>
47: #include "jstypes.h"
48: #include "jscompat.h"
49:
50: JS_BEGIN_EXTERN_C
51:
52: typedef uint32 JSHashNumber;
53: typedef struct JSHashEntry JSHashEntry;
54: typedef struct JSHashTable JSHashTable;
55:
56: #define JS_HASH_BITS 32
57: #define JS_GOLDEN_RATIO 0x9E3779B9U
58:
59: typedef JSHashNumber (* JS_DLL_CALLBACK JSHashFunction)(const void *key);
60: typedef intN (* JS_DLL_CALLBACK JSHashComparator)(const void *v1, const void *v2);
61: typedef intN (* JS_DLL_CALLBACK JSHashEnumerator)(JSHashEntry *he, intN i, void *arg);
62:
63: /* Flag bits in JSHashEnumerator's return value */
64: #define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
65: #define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
66: #define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
67: #define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
68:
69: typedef struct JSHashAllocOps {
70: void * (*allocTable)(void *pool, size_t size);
71: void (*freeTable)(void *pool, void *item);
72: JSHashEntry * (*allocEntry)(void *pool, const void *key);
73: void (*freeEntry)(void *pool, JSHashEntry *he, uintN flag);
74: } JSHashAllocOps;
75:
76: #define HT_FREE_VALUE 0 /* just free the entry's value */
77: #define HT_FREE_ENTRY 1 /* free value and entire entry */
78:
79: struct JSHashEntry {
80: JSHashEntry *next; /* hash chain linkage */
81: JSHashNumber keyHash; /* key hash function result */
82: const void *key; /* ptr to opaque key */
83: void *value; /* ptr to opaque value */
84: };
85:
86: struct JSHashTable {
87: JSHashEntry **buckets; /* vector of hash buckets */
88: uint32 nentries; /* number of entries in table */
89: uint32 shift; /* multiplicative hash shift */
90: JSHashFunction keyHash; /* key hash function */
91: JSHashComparator keyCompare; /* key comparison function */
92: JSHashComparator valueCompare; /* value comparison function */
93: JSHashAllocOps *allocOps; /* allocation operations */
94: void *allocPriv; /* allocation private data */
95: #ifdef HASHMETER
96: uint32 nlookups; /* total number of lookups */
97: uint32 nsteps; /* number of hash chains traversed */
98: uint32 ngrows; /* number of table expansions */
99: uint32 nshrinks; /* number of table contractions */
100: #endif
101: };
102:
103: /*
104: * Create a new hash table.
105: * If allocOps is null, use default allocator ops built on top of malloc().
106: */
107: extern JS_PUBLIC_API(JSHashTable *)
108: JS_NewHashTable(uint32 n, JSHashFunction keyHash,
109: JSHashComparator keyCompare, JSHashComparator valueCompare,
110: JSHashAllocOps *allocOps, void *allocPriv);
111:
112: extern JS_PUBLIC_API(void)
113: JS_HashTableDestroy(JSHashTable *ht);
114:
115: /* Low level access methods */
116: extern JS_PUBLIC_API(JSHashEntry **)
117: JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key);
118:
119: extern JS_PUBLIC_API(JSHashEntry *)
120: JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **hep, JSHashNumber keyHash,
121: const void *key, void *value);
122:
123: extern JS_PUBLIC_API(void)
124: JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he);
125:
126: /* Higher level access methods */
127: extern JS_PUBLIC_API(JSHashEntry *)
128: JS_HashTableAdd(JSHashTable *ht, const void *key, void *value);
129:
130: extern JS_PUBLIC_API(JSBool)
131: JS_HashTableRemove(JSHashTable *ht, const void *key);
132:
133: extern JS_PUBLIC_API(intN)
134: JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg);
135:
136: extern JS_PUBLIC_API(void *)
137: JS_HashTableLookup(JSHashTable *ht, const void *key);
138:
139: extern JS_PUBLIC_API(intN)
140: JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp);
141:
142: /* General-purpose C string hash function. */
143: extern JS_PUBLIC_API(JSHashNumber)
144: JS_HashString(const void *key);
145:
146: /* Stub function just returns v1 == v2 */
147: extern JS_PUBLIC_API(intN)
148: JS_CompareValues(const void *v1, const void *v2);
149:
150: JS_END_EXTERN_C
151:
152: #endif /* jshash_h___ */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.