forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxp_hash.h
152 lines (122 loc) · 4.73 KB
/
xp_hash.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "NPL"); you may not use this file except in
* compliance with the NPL. You may obtain a copy of the NPL at
* http://www.mozilla.org/NPL/
*
* Software distributed under the NPL is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
* for the specific language governing rights and limitations under the
* NPL.
*
* The Initial Developer of this code under the NPL is Netscape
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
* Reserved.
*/
#ifndef _XP_HASH_
#define _XP_HASH_
#include "xp_list.h"
XP_BEGIN_PROTOS
typedef uint32 (*XP_HashingFunction) (const void *ele);
/* A hash compare function is like strcmp - it should return negative, zero,
or positive depending on the ordering and equality of its arguments.
*/
typedef int (*XP_HashCompFunction) (const void *ele1, const void *ele2);
/* get hash number from a string */
extern uint32 XP_StringHash (const void *xv);
/* Hash Tables.
*/
typedef struct xp_HashTable *XP_HashTable; /* opaque */
typedef XP_Bool (*XP_HashTableMapper) (XP_HashTable table,
const void *key, void *value,
void *closure);
/* Create a new, empty hash table object.
SIZE should be your best guess at how many items will go into this
table; if SIZE is too small, that's ok, but there will be a small
performance hit. The size need not be prime.
*/
extern XP_HashTable XP_HashTableNew (uint32 size,
XP_HashingFunction hash_fn,
XP_HashCompFunction compare_fn);
/* Clear and free the hash table.
*/
extern void XP_HashTableDestroy (XP_HashTable table);
/* Remove all entries from the hash table.
*/
extern void XP_Clrhash (XP_HashTable table);
/* Add an association between KEY and VALUE to the hash table.
An existing association will be replaced.
(Note that 0 is a legal value.)
This can only fail if we run out of memory.
*/
extern int XP_Puthash (XP_HashTable table, const void *key, void *value);
/* Remove the for KEY in the table, if it exists.
Returns FALSE if the key wasn't in the table.
*/
extern XP_Bool XP_Remhash (XP_HashTable table, const void *key);
/* Looks up KEY in the table and returns the corresponding value.
If KEY is not in the table, `default_value' will be returned instead.
(This is necessary since 0 is a valid value with which a key can be
associated.)
*/
extern void *XP_Gethash (XP_HashTable table, const void *key,
void *default_value);
/* Apply a function to each pair of elements in the hash table.
If that function returns FALSE, then the mapping stops prematurely.
The mapping function may call XP_Remhash() on the *current* key, but
not on any other key in this table. It also may not clear or destroy
the table.
*/
extern void XP_Maphash (XP_HashTable table, XP_HashTableMapper mapper,
void *closure);
/* Apply a function to each pair of elements in the hash table.
After calling the function, that pair will be removed from the table.
If the function returns FALSE, then the mapping stops prematurely.
Any items which were not mapped over will still remain in the table,
but those items which were mapped over will have been freed.
This could also be done by having the mapper function unconditionally
call XP_Remhash(), but using this function will be slightly more efficient.
*/
extern void XP_MapRemhash (XP_HashTable table, XP_HashTableMapper mapper,
void *closure);
/* ===========================================================================
Hash Lists, which aren't really hash tables.
*/
#define XP_HASH_DUPLICATE_OBJECT -99
typedef struct _XP_HashList {
XP_List **list;
int size;
XP_HashingFunction hash_func;
XP_HashCompFunction comp_func;
} XP_HashList;
/* create a hash list
*/
extern XP_HashList *
XP_HashListNew (int size, XP_HashingFunction hash_func, XP_HashCompFunction comp_func);
/* free a hash list
*/
extern void
XP_HashListDestroy (XP_HashList * hash_struct);
/* add an element to a hash list
*
* returns positive on success and negative on failure
*
* ERROR return codes
*
* XP_HASH_DUPLICATE_OBJECT
*/
extern int
XP_HashListAddObject (XP_HashList * hash_struct, void * new_ele);
/* finds an object by name in the hash list
*/
extern void *
XP_HashListFindObject (XP_HashList * hash_struct, void * ele);
/* removes an object by name from the hash list
* and returns the object if found
*/
extern void *
XP_HashListRemoveObject (XP_HashList * hash_struct, void * ele);
XP_END_PROTOS
#endif /* _XP_HASH_ */