added enabling/disabling virtual button overlay depending on input events
[rocksndiamonds.git] / src / libgame / hash.h
1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // hash.h
10 // ============================================================================
11
12 /*
13  * Copyright (C) 2002 Christopher Clark <firstname.lastname@cl.cam.ac.uk>
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this software and associated documentation files (the "Software"), to
17  * deal in the Software without restriction, including without limitation the
18  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19  * sell copies of the Software, and to permit persons to whom the Software is
20  * furnished to do so, subject to the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies of the Software and its documentation and acknowledgment shall be
24  * given in the documentation and software packages that this Software was
25  * used.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
31  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  * */
34
35 #ifndef HASH_H
36 #define HASH_H
37
38
39 /* Example of use:
40  *
41  *      struct hashtable  *h;
42  *      struct some_key   *k;
43  *      struct some_value *v;
44  *
45  *      static unsigned int         hash_from_key_fn( void *k );
46  *      static int                  keys_equal_fn ( void *key1, void *key2 );
47  *
48  *      h = create_hashtable(16, 0.75, hash_from_key_fn, keys_equal_fn);
49  *      k = (struct some_key *)     malloc(sizeof(struct some_key));
50  *      v = (struct some_value *)   malloc(sizeof(struct some_value));
51  *
52  *      (initialise k and v to suitable values)
53  * 
54  *      if (! hashtable_insert(h,k,v) )
55  *      {     exit(-1);               }
56  *
57  *      if (NULL == (found = hashtable_search(h,k) ))
58  *      {    printf("not found!");                  }
59  *
60  *      if (NULL == (found = hashtable_remove(h,k) ))
61  *      {    printf("Not found\n");                 }
62  *
63  */
64
65 /* Macros may be used to define type-safe(r) hashtable access functions, with
66  * methods specialized to take known key and value types as parameters.
67  * 
68  * Example:
69  *
70  * Insert this at the start of your file:
71  *
72  * DEFINE_HASHTABLE_INSERT(insert_some, struct some_key, struct some_value);
73  * DEFINE_HASHTABLE_SEARCH(search_some, struct some_key, struct some_value);
74  * DEFINE_HASHTABLE_REMOVE(remove_some, struct some_key, struct some_value);
75  *
76  * This defines the functions 'insert_some', 'search_some' and 'remove_some'.
77  * These operate just like hashtable_insert etc., with the same parameters,
78  * but their function signatures have 'struct some_key *' rather than
79  * 'void *', and hence can generate compile time errors if your program is
80  * supplying incorrect data as a key (and similarly for value).
81  *
82  * Note that the hash and key equality functions passed to create_hashtable
83  * still take 'void *' parameters instead of 'some key *'. This shouldn't be
84  * a difficult issue as they're only defined and passed once, and the other
85  * functions will ensure that only valid keys are supplied to them.
86  *
87  * The cost for this checking is increased code size and runtime overhead
88  * - if performance is important, it may be worth switching back to the
89  * unsafe methods once your program has been debugged with the safe methods.
90  * This just requires switching to some simple alternative defines - eg:
91  * #define insert_some hashtable_insert
92  *
93  */
94
95
96 /*****************************************************************************/
97 struct entry
98 {
99   void *k, *v;
100   unsigned int h;
101   struct entry *next;
102 };
103
104 struct hashtable
105 {
106   unsigned int tablelength;
107   struct entry **table;
108   unsigned int entrycount;
109   unsigned int loadlimit;
110   unsigned int (*hashfn) (void *k);
111   int (*eqfn) (void *k1, void *k2);
112 };
113
114 /*****************************************************************************/
115 struct hashtable_itr
116 {
117   struct hashtable *h;
118   struct entry *e;
119   unsigned int index;
120 };
121
122
123 /*****************************************************************************
124  * create_hashtable
125    
126  * @name                    create_hashtable
127  * @param   minsize         minimum initial size of hashtable
128  * @param   maxloadfactor   maximum ratio entries / tablesize
129  * @param   hashfunction    function for hashing keys
130  * @param   key_eq_fn       function for determining key equality
131  * @return                  newly created hashtable or NULL on failure
132  */
133
134 struct hashtable *
135 create_hashtable(unsigned int minsize, float maxloadfactor,
136                  unsigned int (*hashfunction) (void*),
137                  int (*key_eq_fn) (void*,void*));
138
139 /*****************************************************************************
140  * hashtable_insert
141    
142  * @name        hashtable_insert
143  * @param   h   the hashtable to insert into
144  * @param   k   the key - hashtable claims ownership and will free on removal
145  * @param   v   the value - does not claim ownership
146  * @return      non-zero for successful insertion
147  *
148  * This function will cause the table to expand if the insertion would take
149  * the ratio of entries to table size over the maximum load factor.
150  *
151  * This function does not check for repeated insertions with a duplicate key.
152  * The value returned when using a duplicate key is undefined -- when
153  * the hashtable changes size, the order of retrieval of duplicate key
154  * entries is reversed.
155  * If in doubt, remove before insert.
156  */
157
158 int 
159 hashtable_insert(struct hashtable *h, void *k, void *v);
160
161 #define DEFINE_HASHTABLE_INSERT(fnname, keytype, valuetype) \
162 int fnname (struct hashtable *h, keytype *k, valuetype *v) \
163 { \
164   return hashtable_insert(h,k,v); \
165 }
166
167 /*****************************************************************************
168  * hashtable_change
169    
170  * @name        hashtable_change
171  * @param   h   the hashtable to search
172  * @param   k   the key of the entry to change
173  * @param   v   the new value
174  * @return      non-zero for successful change
175  */
176
177 int 
178 hashtable_change(struct hashtable *h, void *k, void *v);
179
180 #define DEFINE_HASHTABLE_CHANGE(fnname, keytype, valuetype) \
181 int fnname (struct hashtable *h, keytype *k, valuetype *v) \
182 { \
183   return hashtable_change(h,k,v); \
184 }
185
186 /*****************************************************************************
187  * hashtable_search
188    
189  * @name        hashtable_search
190  * @param   h   the hashtable to search
191  * @param   k   the key to search for  - does not claim ownership
192  * @return      the value associated with the key, or NULL if none found
193  */
194
195 void *
196 hashtable_search(struct hashtable *h, void *k);
197
198 #define DEFINE_HASHTABLE_SEARCH(fnname, keytype, valuetype) \
199 valuetype * fnname (struct hashtable *h, keytype *k) \
200 { \
201   return (valuetype *) (hashtable_search(h,k)); \
202 }
203
204 /*****************************************************************************
205  * hashtable_remove
206    
207  * @name        hashtable_remove
208  * @param   h   the hashtable to remove the item from
209  * @param   k   the key to search for  - does not claim ownership
210  * @return      the value associated with the key, or NULL if none found
211  */
212
213 void * /* returns value */
214 hashtable_remove(struct hashtable *h, void *k);
215
216 #define DEFINE_HASHTABLE_REMOVE(fnname, keytype, valuetype) \
217 valuetype * fnname (struct hashtable *h, keytype *k) \
218 { \
219   return (valuetype *) (hashtable_remove(h,k)); \
220 }
221
222
223 /*****************************************************************************
224  * hashtable_count
225    
226  * @name        hashtable_count
227  * @return      the number of items stored in the hashtable
228  */
229 unsigned int
230 hashtable_count(struct hashtable *h);
231
232
233 /*****************************************************************************
234  * hashtable_destroy
235    
236  * @name        hashtable_destroy
237  * @param       free_values     whether to call 'free' on the remaining values
238  */
239
240 void
241 hashtable_destroy(struct hashtable *h, int free_values);
242
243
244 /*****************************************************************************/
245 /* hashtable_iterator
246  */
247
248 struct hashtable_itr *
249 hashtable_iterator(struct hashtable *h);
250
251 /*****************************************************************************/
252 /* key - return the key of the (key,value) pair at the current position */
253
254 void *
255 hashtable_iterator_key(struct hashtable_itr *i);
256
257 /*****************************************************************************/
258 /* value - return the value of the (key,value) pair at the current position */
259
260 void *
261 hashtable_iterator_value(struct hashtable_itr *i);
262
263 /*****************************************************************************/
264 /* advance - advance the iterator to the next element
265  *           returns zero if advanced to end of table */
266
267 int
268 hashtable_iterator_advance(struct hashtable_itr *itr);
269
270 #endif