Debian Codegraph @

libidu/idu-hash.h

raw symbols SCIP
1 /* idu-hash.h -- decls for hash table
2 Copyright (C) 1995-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
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, or (at your option)
8 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
22 #include <stdio.h>
23
24 typedef unsigned long (*hash_func_t) (void const *key);
25 typedef int (*hash_cmp_func_t) (void const *x, void const *y);
26 typedef void (*hash_map_func_t) (void const *item);
27
28 struct hash_table
29 {
30 void **ht_vec;
31 unsigned long ht_size; /* total number of slots (power of 2) */
32 unsigned long ht_capacity; /* usable slots, limited by loading-factor */
33 unsigned long ht_fill; /* items in table */
34 unsigned long ht_empty_slots; /* empty slots not including deleted slots */
35 unsigned long ht_collisions; /* # of failed calls to comparison function */
36 unsigned long ht_lookups; /* # of queries */
37 unsigned int ht_rehashes; /* # of times we've expanded table */
38 hash_func_t ht_hash_1; /* primary hash function */
39 hash_func_t ht_hash_2; /* secondary hash function */
40 hash_cmp_func_t ht_compare; /* comparison function */
41 };
42
43 typedef int (*qsort_cmp_t) (void const *, void const *);
44
45 extern void hash_init (struct hash_table *ht, unsigned long size,
46 hash_func_t hash_1, hash_func_t hash_2,
47 hash_cmp_func_t hash_cmp);
48 extern void hash_load (struct hash_table *ht, void *item_table,
49 unsigned long cardinality, unsigned long size);
50 extern void **hash_find_slot (struct hash_table *ht, void const *key);
51 extern void *hash_find_item (struct hash_table *ht, void const *key);
52 extern void *hash_insert (struct hash_table *ht, void *item);
53 extern void *hash_insert_at (struct hash_table *ht, void *item,
54 void const *slot);
55 extern void *hash_delete (struct hash_table *ht, void const *item);
56 extern void *hash_delete_at (struct hash_table *ht, void const *slot);
57 extern void hash_delete_items (struct hash_table *ht);
58 extern void hash_free_items (struct hash_table *ht);
59 extern void hash_free (struct hash_table *ht, int free_items);
60 extern void hash_map (struct hash_table *ht, hash_map_func_t map);
61 extern void hash_print_stats (struct hash_table const *ht, FILE *out_FILE);
62 extern void **hash_dump (struct hash_table const *ht, void **vector_0,
63 qsort_cmp_t compare);
64
65 extern void *hash_deleted_item;
66 #define HASH_VACANT(item) ((item) == 0 || (void *) (item) == hash_deleted_item)
67
68
69 /* hash and comparison macros for string keys. */
70
71 #define STRING_HASH_1(_key_, _result_) { \
72 unsigned char const *kk = (unsigned char const *) (_key_) - 1; \
73 while (*++kk) \
74 (_result_) += (*kk << (kk[1] & 0xf)); \
75 } while (0)
76 #define return_STRING_HASH_1(_key_) do { \
77 unsigned long result = 0; \
78 STRING_HASH_1 ((_key_), result); \
79 return result; \
80 } while (0)
81
82 #define STRING_HASH_2(_key_, _result_) do { \
83 unsigned char const *kk = (unsigned char const *) (_key_) - 1; \
84 while (*++kk) \
85 (_result_) += (*kk << (kk[1] & 0x7)); \
86 } while (0)
87 #define return_STRING_HASH_2(_key_) do { \
88 unsigned long result = 0; \
89 STRING_HASH_2 ((_key_), result); \
90 return result; \
91 } while (0)
92
93 #define STRING_COMPARE(_x_, _y_, _result_) do { \
94 unsigned char const *xx = (unsigned char const *) (_x_) - 1; \
95 unsigned char const *yy = (unsigned char const *) (_y_) - 1; \
96 do { \
97 if (*++xx == '\0') { \
98 yy++; \
99 break; \
100 } \
101 } while (*xx == *++yy); \
102 (_result_) = *xx - *yy; \
103 } while (0)
104 #define return_STRING_COMPARE(_x_, _y_) do { \
105 int result; \
106 STRING_COMPARE (_x_, _y_, result); \
107 return result; \
108 } while (0)
109
110 /* hash and comparison macros for integer keys. */
111
112 #define INTEGER_HASH_1(_key_, _result_) do { \
113 (_result_) += ((unsigned long)(_key_)); \
114 } while (0)
115 #define return_INTEGER_HASH_1(_key_) do { \
116 unsigned long result = 0; \
117 INTEGER_HASH_1 ((_key_), result); \
118 return result; \
119 } while (0)
120
121 #define INTEGER_HASH_2(_key_, _result_) do { \
122 (_result_) += ~((unsigned long)(_key_)); \
123 } while (0)
124 #define return_INTEGER_HASH_2(_key_) do { \
125 unsigned long result = 0; \
126 INTEGER_HASH_2 ((_key_), result); \
127 return result; \
128 } while (0)
129
130 #define INTEGER_COMPARE(_x_, _y_, _result_) do { \
131 (_result_) = _x_ - _y_; \
132 } while (0)
133 #define return_INTEGER_COMPARE(_x_, _y_) do { \
134 int result; \
135 INTEGER_COMPARE (_x_, _y_, result); \
136 return result; \
137 } while (0)
138
139 /* hash and comparison macros for address keys. */
140
141 #define ADDRESS_HASH_1(_key_, _result_) INTEGER_HASH_1 (((unsigned long)(_key_)) >> 3, (_result_))
142 #define ADDRESS_HASH_2(_key_, _result_) INTEGER_HASH_2 (((unsigned long)(_key_)) >> 3, (_result_))
143 #define ADDRESS_COMPARE(_x_, _y_, _result_) INTEGER_COMPARE ((_x_), (_y_), (_result_))
144 #define return_ADDRESS_HASH_1(_key_) return_INTEGER_HASH_1 (((unsigned long)(_key_)) >> 3)
145 #define return_ADDRESS_HASH_2(_key_) return_INTEGER_HASH_2 (((unsigned long)(_key_)) >> 3)
146 #define return_ADDRESS_COMPARE(_x_, _y_) return_INTEGER_COMPARE ((_x_), (_y_))
147
148 #endif /* not _hash_h_ */