1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 #ifndef _HASH_H
20 #define _HASH_H
21 #include "lg.h"
22 #include "error.h"
23 
24 /*
25  * $Source: n:/project/lib/src/dstruct/RCS/hash.h $
26  * $Revision: 1.5 $
27  * $Author: mahk $
28  * $Date: 1994/01/18 08:16:01 $
29  *
30  * $Log: hash.h $
31  * Revision 1.5  1994/01/18  08:16:01  mahk
32  * Added hash_copy
33  *
34  * Revision 1.4  1993/08/06  13:46:46  mahk
35  * changed libdbg.h to _dstruct.h
36  *
37  * Revision 1.3  1993/06/14  21:11:50  xemu
38  * step func
39  *
40  * Revision 1.2  1993/03/25  19:55:25  mahk
41  * Added rep exposure warning.
42  *
43  * Revision 1.1  1993/03/25  19:20:09  mahk
44  * Initial revision
45  *
46  *
47  */
48 
49 // Equivalence function, returns values are as per strcmp
50 // i.e. 0 if data1 == data2, >0  if data1 > data2, <0 if data1 < data2.
51 typedef int (*Equfunc)(void* data1,void* data2);
52 
53 // hashing function: data1 == data2 ==> hash(data1) == hash(data2)
54 typedef int (*Hashfunc)(void* data);
55 
56 
57 
58 typedef struct _hashtable
59 {
60    int size;
61    int sizelog2;
62    int elemsize;
63    int fullness;
64    Equfunc efunc;
65    Hashfunc hfunc;
66    char *statvec;
67    char *vec;
68 } Hashtable;
69 
70 
71 
72 errtype hash_init(Hashtable* h, int elemsize, int vecsize, Hashfunc hfunc, Equfunc efunc);
73 // initialize a hashtable with the specified hashfunc and equfunc, using elemsize as
74 // the size of an element, and using vecsize as the initial table size.
75 
76 errtype hash_set(Hashtable* h,void* elem);
77 // insert an element into a hashtable, overwriting any element
78 // that is equal to it.
79 
80 errtype hash_insert(Hashtable* h,void* elem);
81 // REQUIRES there is no member of h equal to "elem".
82 // inserts "elem" into hashtable h.  Faster than hash_set.
83 
84 
85 
86 errtype hash_lookup(Hashtable* h, void* elem, void** result);
87 // Looks up elem in the hashtable, sets *result to point to the
88 // element in h which is equal to elem.  Or NULL if no such element
89 // exists.
90 // WARNING WARNING DANGER WILL ROBINSON.  HEINOUS REP EXPOSURE.
91 // MODIFY **RESULT AT YOUR OWN PERIL
92 
93 errtype hash_delete(Hashtable* h, void* elem);
94 // Find and remove the element in h which is equal to elem,
95 // or do nothing if no such element exists.
96 
97 
98 typedef uchar (*HashIterFunc)(void* elem, void* data);
99 
100 errtype hash_iter(Hashtable* h, HashIterFunc ifunc, void* data);
101 // Applies ifunc(elem,data) to every element of h, one at a time, until
102 // ifunc returns true.
103 
104 errtype hash_copy(Hashtable* t, Hashtable* s);
105 // Initializes t to be a copy of s
106 
107 errtype hash_step(Hashtable *h, void **result, int *index);
108 // Will step through a hashtable, returning the elements one at a time.
109 
110 errtype hash_destroy(Hashtable* h);
111 // Destroys hashtable h.  Does not free h itself, but frees
112 // subordinate data structures.
113 
114 #endif // _HASH_H
115