1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6#ifndef plhash_h___
7#define plhash_h___
8/*
9 * API to portable hash table code.
10 */
11#include <stdio.h>
12#include "prtypes.h"
13
14PR_BEGIN_EXTERN_C
15
16typedef struct PLHashEntry PLHashEntry;
17typedef struct PLHashTable PLHashTable;
18typedef PRUint32 PLHashNumber;
19#define PL_HASH_BITS 32 /* Number of bits in PLHashNumber */
20typedef PLHashNumber (PR_CALLBACK *PLHashFunction)(const void *key);
21typedef PRIntn (PR_CALLBACK *PLHashComparator)(const void *v1, const void *v2);
22
23typedef PRIntn (PR_CALLBACK *PLHashEnumerator)(PLHashEntry *he, PRIntn i, void *arg);
24
25/* Flag bits in PLHashEnumerator's return value */
26#define HT_ENUMERATE_NEXT 0 /* continue enumerating entries */
27#define HT_ENUMERATE_STOP 1 /* stop enumerating entries */
28#define HT_ENUMERATE_REMOVE 2 /* remove and free the current entry */
29#define HT_ENUMERATE_UNHASH 4 /* just unhash the current entry */
30
31typedef struct PLHashAllocOps {
32 void * (PR_CALLBACK *allocTable)(void *pool, PRSize size);
33 void (PR_CALLBACK *freeTable)(void *pool, void *item);
34 PLHashEntry * (PR_CALLBACK *allocEntry)(void *pool, const void *key);
35 void (PR_CALLBACK *freeEntry)(void *pool, PLHashEntry *he, PRUintn flag);
36} PLHashAllocOps;
37
38#define HT_FREE_VALUE 0 /* just free the entry's value */
39#define HT_FREE_ENTRY 1 /* free value and entire entry */
40
41struct PLHashEntry {
42 PLHashEntry *next; /* hash chain linkage */
43 PLHashNumber keyHash; /* key hash function result */
44 const void *key; /* ptr to opaque key */
45 void *value; /* ptr to opaque value */
46};
47
48struct PLHashTable {
49 PLHashEntry **buckets; /* vector of hash buckets */
50 PRUint32 nentries; /* number of entries in table */
51 PRUint32 shift; /* multiplicative hash shift */
52 PLHashFunction keyHash; /* key hash function */
53 PLHashComparator keyCompare; /* key comparison function */
54 PLHashComparator valueCompare; /* value comparison function */
55 const PLHashAllocOps *allocOps; /* allocation operations */
56 void *allocPriv; /* allocation private data */
57#ifdef HASHMETER
58 PRUint32 nlookups; /* total number of lookups */
59 PRUint32 nsteps; /* number of hash chains traversed */
60 PRUint32 ngrows; /* number of table expansions */
61 PRUint32 nshrinks; /* number of table contractions */
62#endif
63};
64
65/*
66 * Create a new hash table.
67 * If allocOps is null, use default allocator ops built on top of malloc().
68 */
69PR_EXTERN(PLHashTable *)
70PL_NewHashTable(PRUint32 numBuckets, PLHashFunction keyHash,
71 PLHashComparator keyCompare, PLHashComparator valueCompare,
72 const PLHashAllocOps *allocOps, void *allocPriv);
73
74PR_EXTERN(void)
75PL_HashTableDestroy(PLHashTable *ht);
76
77/* Higher level access methods */
78PR_EXTERN(PLHashEntry *)
79PL_HashTableAdd(PLHashTable *ht, const void *key, void *value);
80
81PR_EXTERN(PRBool)
82PL_HashTableRemove(PLHashTable *ht, const void *key);
83
84PR_EXTERN(void *)
85PL_HashTableLookup(PLHashTable *ht, const void *key);
86
87PR_EXTERN(void *)
88PL_HashTableLookupConst(PLHashTable *ht, const void *key);
89
90PR_EXTERN(PRIntn)
91PL_HashTableEnumerateEntries(PLHashTable *ht, PLHashEnumerator f, void *arg);
92
93/* General-purpose C string hash function. */
94PR_EXTERN(PLHashNumber)
95PL_HashString(const void *key);
96
97/* Compare strings using strcmp(), return true if equal. */
98PR_EXTERN(PRIntn)
99PL_CompareStrings(const void *v1, const void *v2);
100
101/* Stub function just returns v1 == v2 */
102PR_EXTERN(PRIntn)
103PL_CompareValues(const void *v1, const void *v2);
104
105/* Low level access methods */
106PR_EXTERN(PLHashEntry **)
107PL_HashTableRawLookup(PLHashTable *ht, PLHashNumber keyHash, const void *key);
108
109PR_EXTERN(PLHashEntry **)
110PL_HashTableRawLookupConst(PLHashTable *ht, PLHashNumber keyHash,
111 const void *key);
112
113PR_EXTERN(PLHashEntry *)
114PL_HashTableRawAdd(PLHashTable *ht, PLHashEntry **hep, PLHashNumber keyHash,
115 const void *key, void *value);
116
117PR_EXTERN(void)
118PL_HashTableRawRemove(PLHashTable *ht, PLHashEntry **hep, PLHashEntry *he);
119
120/* This can be trivially implemented using PL_HashTableEnumerateEntries. */
121PR_EXTERN(PRIntn)
122PL_HashTableDump(PLHashTable *ht, PLHashEnumerator dump, FILE *fp);
123
124PR_END_EXTERN_C
125
126#endif /* plhash_h___ */
127

source code of include/nspr/plhash.h