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