1 /* Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk> */
2 
3 #ifndef __HASHTABLE_CWC22_H__
4 #define __HASHTABLE_CWC22_H__
5 
6 struct hashtable;
7 
8 /*****************************************************************************
9  * create_hashtable
10 
11  * @name                    create_hashtable
12  * @param   ctx             talloc context to use for allocations
13  * @param   name            talloc name of the hashtable
14  * @param   hashfunction    function for hashing keys
15  * @param   key_eq_fn       function for determining key equality
16  * @param   flags           flags HASHTABLE_*
17  * @return                  newly created hashtable or NULL on failure
18  */
19 
20 /* Let hashtable_destroy() free the entries' values. */
21 #define HASHTABLE_FREE_VALUE (1U << 0)
22 /* Let hashtable_remove() and hashtable_destroy() free the entries' keys. */
23 #define HASHTABLE_FREE_KEY   (1U << 1)
24 
25 struct hashtable *
26 create_hashtable(const void *ctx, const char *name,
27                  unsigned int (*hashfunction) (const void *),
28                  int (*key_eq_fn) (const void *, const void *),
29                  unsigned int flags
30 );
31 
32 /*****************************************************************************
33  * hashtable_add
34 
35  * @name        hashtable_add
36  * @param   h   the hashtable to insert into
37  * @param   k   the key - hashtable claims ownership and will free on removal
38  * @param   v   the value - does not claim ownership
39  * @return      zero for successful insertion
40  *
41  * This function will cause the table to expand if the insertion would take
42  * the ratio of entries to table size over the maximum load factor.
43  */
44 
45 int
46 hashtable_add(struct hashtable *h, const void *k, void *v);
47 
48 /*****************************************************************************
49  * hashtable_replace
50 
51  * @name        hashtable_nsert
52  * @param   h   the hashtable to insert into
53  * @param   k   the key - hashtable claims ownership and will free on removal
54  * @param   v   the value - does not claim ownership
55  * @return      zero for successful insertion
56  *
57  * This function does check for an entry being present before replacing it
58  * with a new value.
59  */
60 
61 int
62 hashtable_replace(struct hashtable *h, const void *k, void *v);
63 
64 /*****************************************************************************
65  * hashtable_search
66 
67  * @name        hashtable_search
68  * @param   h   the hashtable to search
69  * @param   k   the key to search for  - does not claim ownership
70  * @return      the value associated with the key, or NULL if none found
71  */
72 
73 void *
74 hashtable_search(const struct hashtable *h, const void *k);
75 
76 /*****************************************************************************
77  * hashtable_remove
78 
79  * @name        hashtable_remove
80  * @param   h   the hashtable to remove the item from
81  * @param   k   the key to search for  - does not claim ownership
82  */
83 
84 void
85 hashtable_remove(struct hashtable *h, const void *k);
86 
87 /*****************************************************************************
88  * hashtable_iterate
89 
90  * @name           hashtable_iterate
91  * @param   h      the hashtable
92  * @param   func   function to call for each entry
93  * @param   arg    user supplied parameter for func
94  * @return         0 if okay, non-zero return value of func (and iteration
95  *                 was aborted)
96  *
97  * Iterates over all entries in the hashtable and calls func with the
98  * key, value, and the user supplied parameter.
99  * func returning a non-zero value will abort the iteration. In case func is
100  * removing an entry other than itself from the hashtable, it must return a
101  * non-zero value in order to abort the iteration. Inserting entries is
102  * allowed, but it is undefined whether func will be called for those new
103  * entries during this iteration.
104  */
105 int
106 hashtable_iterate(struct hashtable *h,
107                   int (*func)(const void *k, void *v, void *arg), void *arg);
108 
109 /*****************************************************************************
110  * hashtable_destroy
111 
112  * @name        hashtable_destroy
113  * @param   h   the hashtable
114  */
115 
116 void
117 hashtable_destroy(struct hashtable *h);
118 
119 #endif /* __HASHTABLE_CWC22_H__ */
120 
121 /*
122  * Copyright (c) 2002, Christopher Clark
123  * All rights reserved.
124  *
125  * Redistribution and use in source and binary forms, with or without
126  * modification, are permitted provided that the following conditions
127  * are met:
128  *
129  * * Redistributions of source code must retain the above copyright
130  * notice, this list of conditions and the following disclaimer.
131  *
132  * * Redistributions in binary form must reproduce the above copyright
133  * notice, this list of conditions and the following disclaimer in the
134  * documentation and/or other materials provided with the distribution.
135  *
136  * * Neither the name of the original author; nor the names of any contributors
137  * may be used to endorse or promote products derived from this software
138  * without specific prior written permission.
139  *
140  *
141  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
142  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
143  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
144  * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER
145  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
146  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
147  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
148  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
149  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
150  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
151  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
152 */
153