Copied from old documentation. Removed all Gedcom_val details.
[gedcom-parse.git] / gedcom / hash.h
1 /* This file is taken from kazlib 1.20 (see URL
2    http://users.footprints.net/~kaz/kazlib.html)
3    Only this initial comment has been added and a protection for the original
4    CVS keywords.
5    The next comment gives the original copyright notice.
6 */
7
8 /*
9  * Hash Table Data Type
10  * Copyright (C) 1997 Kaz Kylheku <kaz@ashi.footprints.net>
11  *
12  * Free Software License:
13  *
14  * All rights are reserved by the author, with the following exceptions:
15  * Permission is granted to freely reproduce and distribute this software,
16  * possibly in exchange for a fee, provided that this copyright notice appears
17  * intact. Permission is also granted to adapt this software to produce
18  * derivative works, as long as the modified versions carry this copyright
19  * notice and additional notices stating that the work has been modified.
20  * This source code may be translated into executable form and incorporated
21  * into proprietary software; there is no requirement for such software to
22  * contain a copyright notice related to this source.
23  *
24  * $kkId: hash.h,v 1.22.2.7 2000/11/13 01:36:45 kaz Exp $
25  * $kkName: kazlib_1_20 $
26  */
27
28 #ifndef HASH_H
29 #define HASH_H
30
31 #include <limits.h>
32 #ifdef KAZLIB_SIDEEFFECT_DEBUG
33 #include "sfx.h"
34 #endif
35
36 /*
37  * Blurb for inclusion into C++ translation units
38  */
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 typedef unsigned long hashcount_t;
45 #define HASHCOUNT_T_MAX ULONG_MAX
46
47 typedef unsigned long hash_val_t;
48 #define HASH_VAL_T_MAX ULONG_MAX
49
50 extern int hash_val_t_bit;
51
52 #ifndef HASH_VAL_T_BIT
53 #define HASH_VAL_T_BIT ((int) hash_val_t_bit)
54 #endif
55
56 /*
57  * Hash chain node structure.
58  * Notes:
59  * 1. This preprocessing directive is for debugging purposes.  The effect is
60  *    that if the preprocessor symbol KAZLIB_OPAQUE_DEBUG is defined prior to the
61  *    inclusion of this header,  then the structure shall be declared as having
62  *    the single member   int __OPAQUE__.   This way, any attempts by the
63  *    client code to violate the principles of information hiding (by accessing
64  *    the structure directly) can be diagnosed at translation time. However,
65  *    note the resulting compiled unit is not suitable for linking.
66  * 2. This is a pointer to the next node in the chain. In the last node of a
67  *    chain, this pointer is null.
68  * 3. The key is a pointer to some user supplied data that contains a unique
69  *    identifier for each hash node in a given table. The interpretation of
70  *    the data is up to the user. When creating or initializing a hash table,
71  *    the user must supply a pointer to a function for comparing two keys,
72  *    and a pointer to a function for hashing a key into a numeric value.
73  * 4. The value is a user-supplied pointer to void which may refer to
74  *    any data object. It is not interpreted in any way by the hashing
75  *    module.
76  * 5. The hashed key is stored in each node so that we don't have to rehash
77  *    each key when the table must grow or shrink.
78  */
79
80 typedef struct hnode_t {
81     #if defined(HASH_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)   /* 1 */
82     struct hnode_t *hash_next;          /* 2 */
83     const void *hash_key;               /* 3 */
84     void *hash_data;                    /* 4 */
85     hash_val_t hash_hkey;               /* 5 */
86     #else
87     int hash_dummy;
88     #endif
89 } hnode_t;
90
91 /*
92  * The comparison function pointer type. A comparison function takes two keys
93  * and produces a value of -1 if the left key is less than the right key, a
94  * value of 0 if the keys are equal, and a value of 1 if the left key is
95  * greater than the right key.
96  */
97
98 typedef int (*hash_comp_t)(const void *, const void *);
99
100 /*
101  * The hashing function performs some computation on a key and produces an
102  * integral value of type hash_val_t based on that key. For best results, the
103  * function should have a good randomness properties in *all* significant bits
104  * over the set of keys that are being inserted into a given hash table. In
105  * particular, the most significant bits of hash_val_t are most significant to
106  * the hash module. Only as the hash table expands are less significant bits
107  * examined. Thus a function that has good distribution in its upper bits but
108  * not lower is preferrable to one that has poor distribution in the upper bits
109  * but not the lower ones.
110  */
111
112 typedef hash_val_t (*hash_fun_t)(const void *);
113
114 /*
115  * allocator functions
116  */
117
118 typedef hnode_t *(*hnode_alloc_t)(void *);
119 typedef void (*hnode_free_t)(hnode_t *, void *);
120
121 /*
122  * This is the hash table control structure. It keeps track of information
123  * about a hash table, as well as the hash table itself.
124  * Notes:
125  * 1.  Pointer to the hash table proper. The table is an array of pointers to
126  *     hash nodes (of type hnode_t). If the table is empty, every element of
127  *     this table is a null pointer. A non-null entry points to the first
128  *     element of a chain of nodes.
129  * 2.  This member keeps track of the size of the hash table---that is, the
130  *     number of chain pointers.
131  * 3.  The count member maintains the number of elements that are presently
132  *     in the hash table.
133  * 4.  The maximum count is the greatest number of nodes that can populate this
134  *     table. If the table contains this many nodes, no more can be inserted,
135  *     and the hash_isfull() function returns true.
136  * 5.  The high mark is a population threshold, measured as a number of nodes,
137  *     which, if exceeded, will trigger a table expansion. Only dynamic hash
138  *     tables are subject to this expansion.
139  * 6.  The low mark is a minimum population threshold, measured as a number of
140  *     nodes. If the table population drops below this value, a table shrinkage
141  *     will occur. Only dynamic tables are subject to this reduction.  No table
142  *     will shrink beneath a certain absolute minimum number of nodes.
143  * 7.  This is the a pointer to the hash table's comparison function. The
144  *     function is set once at initialization or creation time.
145  * 8.  Pointer to the table's hashing function, set once at creation or
146  *     initialization time.
147  * 9.  The current hash table mask. If the size of the hash table is 2^N,
148  *     this value has its low N bits set to 1, and the others clear. It is used
149  *     to select bits from the result of the hashing function to compute an
150  *     index into the table.
151  * 10. A flag which indicates whether the table is to be dynamically resized. It
152  *     is set to 1 in dynamically allocated tables, 0 in tables that are
153  *     statically allocated.
154  */
155
156 typedef struct hash_t {
157     #if defined(HASH_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
158     struct hnode_t **hash_table;                /* 1 */
159     hashcount_t hash_nchains;                   /* 2 */
160     hashcount_t hash_nodecount;                 /* 3 */
161     hashcount_t hash_maxcount;                  /* 4 */
162     hashcount_t hash_highmark;                  /* 5 */
163     hashcount_t hash_lowmark;                   /* 6 */
164     hash_comp_t hash_compare;                   /* 7 */
165     hash_fun_t hash_function;                   /* 8 */
166     hnode_alloc_t hash_allocnode;
167     hnode_free_t hash_freenode;
168     void *hash_context;
169     hash_val_t hash_mask;                       /* 9 */
170     int hash_dynamic;                           /* 10 */
171     #else
172     int hash_dummy;
173     #endif
174 } hash_t;
175
176 /*
177  * Hash scanner structure, used for traversals of the data structure.
178  * Notes:
179  * 1. Pointer to the hash table that is being traversed.
180  * 2. Reference to the current chain in the table being traversed (the chain
181  *    that contains the next node that shall be retrieved).
182  * 3. Pointer to the node that will be retrieved by the subsequent call to
183  *    hash_scan_next().
184  */
185
186 typedef struct hscan_t {
187     #if defined(HASH_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
188     hash_t *hash_table;         /* 1 */
189     hash_val_t hash_chain;      /* 2 */
190     hnode_t *hash_next;         /* 3 */
191     #else
192     int hash_dummy;
193     #endif
194 } hscan_t;
195
196 extern hash_t *hash_create(hashcount_t, hash_comp_t, hash_fun_t);
197 extern void hash_set_allocator(hash_t *, hnode_alloc_t, hnode_free_t, void *);
198 extern void hash_destroy(hash_t *);
199 extern void hash_free_nodes(hash_t *);
200 extern void hash_free(hash_t *);
201 extern hash_t *hash_init(hash_t *, hashcount_t, hash_comp_t,
202         hash_fun_t, hnode_t **, hashcount_t);
203 extern void hash_insert(hash_t *, hnode_t *, const void *);
204 extern hnode_t *hash_lookup(hash_t *, const void *);
205 extern hnode_t *hash_delete(hash_t *, hnode_t *);
206 extern int hash_alloc_insert(hash_t *, const void *, void *);
207 extern void hash_delete_free(hash_t *, hnode_t *);
208
209 extern void hnode_put(hnode_t *, void *);
210 extern void *hnode_get(hnode_t *);
211 extern const void *hnode_getkey(hnode_t *);
212 extern hashcount_t hash_count(hash_t *);
213 extern hashcount_t hash_size(hash_t *);
214
215 extern int hash_isfull(hash_t *);
216 extern int hash_isempty(hash_t *);
217
218 extern void hash_scan_begin(hscan_t *, hash_t *);
219 extern hnode_t *hash_scan_next(hscan_t *);
220 extern hnode_t *hash_scan_delete(hash_t *, hnode_t *);
221 extern void hash_scan_delfree(hash_t *, hnode_t *);
222
223 extern int hash_verify(hash_t *);
224
225 extern hnode_t *hnode_create(void *);
226 extern hnode_t *hnode_init(hnode_t *, void *);
227 extern void hnode_destroy(hnode_t *);
228
229 #if defined(HASH_IMPLEMENTATION) || !defined(KAZLIB_OPAQUE_DEBUG)
230 #ifdef KAZLIB_SIDEEFFECT_DEBUG
231 #define hash_isfull(H) (SFX_CHECK(H)->hash_nodecount == (H)->hash_maxcount)
232 #else
233 #define hash_isfull(H) ((H)->hash_nodecount == (H)->hash_maxcount)
234 #endif
235 #define hash_isempty(H) ((H)->hash_nodecount == 0)
236 #define hash_count(H) ((H)->hash_nodecount)
237 #define hash_size(H) ((H)->hash_nchains)
238 #define hnode_get(N) ((N)->hash_data)
239 #define hnode_getkey(N) ((N)->hash_key)
240 #define hnode_put(N, V) ((N)->hash_data = (V))
241 #endif
242
243 #ifdef __cplusplus
244 }
245 #endif
246
247 #endif