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>
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)
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.
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. */
24 typedef unsigned long (*hash_func_t
) __P((void const *key
));
25 typedef int (*hash_cmp_func_t
) __P((void const *x
, void const *y
));
26 typedef void (*hash_map_func_t
) __P((void const *item
));
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_collisions
; /* # of failed calls to comparison function */
35 unsigned long ht_lookups
; /* # of queries */
36 unsigned int ht_rehashes
; /* # of times we've expanded table */
37 hash_func_t ht_hash_1
; /* primary hash function */
38 hash_func_t ht_hash_2
; /* secondary hash function */
39 hash_cmp_func_t ht_compare
; /* comparison function */
42 typedef int (*qsort_cmp_t
) __P((void const *, void const *));
44 void hash_init
__P((struct hash_table
*ht
, unsigned long size
,
45 hash_func_t hash_1
, hash_func_t hash_2
, hash_cmp_func_t hash_cmp
));
46 void hash_load
__P((struct hash_table
*ht
, void *item_table
,
47 unsigned long cardinality
, unsigned long size
));
48 void **hash_find_slot
__P((struct hash_table
*ht
, void const *key
));
49 void *hash_find_item
__P((struct hash_table
*ht
, void const *key
));
50 const void *hash_insert
__P((struct hash_table
*ht
, void *item
));
51 const void *hash_insert_at
__P((struct hash_table
*ht
, void *item
, void const *slot
));
52 const void *hash_delete
__P((struct hash_table
*ht
, void const *item
));
53 const void *hash_delete_at
__P((struct hash_table
*ht
, void const *slot
));
54 void hash_delete_items
__P((struct hash_table
*ht
));
55 void hash_free_items
__P((struct hash_table
*ht
));
56 void hash_free
__P((struct hash_table
*ht
, int free_items
));
57 void hash_map
__P((struct hash_table
*ht
, hash_map_func_t map
));
58 void hash_print_stats
__P((struct hash_table
*ht
, FILE *out_FILE
));
59 void **hash_dump
__P((struct hash_table
*ht
, void **vector_0
, qsort_cmp_t compare
));
61 extern void *hash_deleted_item
;
62 #define HASH_VACANT(item) \
63 ((item) == 0 || (const void *) (item) == hash_deleted_item)
66 /* hash and comparison macros for string keys. */
68 #define STRING_HASH_1(_key_, _result_) { \
69 unsigned char const *kk = (unsigned char const *) (_key_) - 1; \
71 (_result_) += (*kk << (kk[1] & 0xf)); \
73 #define return_STRING_HASH_1(_key_) do { \
74 unsigned long result = 0; \
75 STRING_HASH_1 ((_key_), result); \
79 #define STRING_HASH_2(_key_, _result_) do { \
80 unsigned char const *kk = (unsigned char const *) (_key_) - 1; \
82 (_result_) += (*kk << (kk[1] & 0x7)); \
84 #define return_STRING_HASH_2(_key_) do { \
85 unsigned long result = 0; \
86 STRING_HASH_2 ((_key_), result); \
90 #define STRING_COMPARE(_x_, _y_, _result_) do { \
91 unsigned char const *xx = (unsigned char const *) (_x_) - 1; \
92 unsigned char const *yy = (unsigned char const *) (_y_) - 1; \
94 if (*++xx == '\0') { \
98 } while (*xx == *++yy); \
99 (_result_) = *xx - *yy; \
101 #define return_STRING_COMPARE(_x_, _y_) do { \
103 STRING_COMPARE (_x_, _y_, result); \
107 /* hash and comparison macros for integer keys. */
109 #define INTEGER_HASH_1(_key_, _result_) do { \
110 (_result_) += ((unsigned long)(_key_)); \
112 #define return_INTEGER_HASH_1(_key_) do { \
113 unsigned long result = 0; \
114 INTEGER_HASH_1 ((_key_), result); \
118 #define INTEGER_HASH_2(_key_, _result_) do { \
119 (_result_) += ~((unsigned long)(_key_)); \
121 #define return_INTEGER_HASH_2(_key_) do { \
122 unsigned long result = 0; \
123 INTEGER_HASH_2 ((_key_), result); \
127 #define INTEGER_COMPARE(_x_, _y_, _result_) do { \
128 (_result_) = _x_ - _y_; \
130 #define return_INTEGER_COMPARE(_x_, _y_) do { \
132 INTEGER_COMPARE (_x_, _y_, result); \
136 /* hash and comparison macros for address keys. */
138 #define ADDRESS_HASH_1(_key_, _result_) INTEGER_HASH_1 (((unsigned long)(_key_)) >> 3, (_result_))
139 #define ADDRESS_HASH_2(_key_, _result_) INTEGER_HASH_2 (((unsigned long)(_key_)) >> 3, (_result_))
140 #define ADDRESS_COMPARE(_x_, _y_, _result_) INTEGER_COMPARE ((_x_), (_y_), (_result_))
141 #define return_ADDRESS_HASH_1(_key_) return_INTEGER_HASH_1 (((unsigned long)(_key_)) >> 3)
142 #define return_ADDRESS_HASH_2(_key_) return_INTEGER_HASH_2 (((unsigned long)(_key_)) >> 3)
143 #define return_ADDRESS_COMPARE(_x_, _y_) return_INTEGER_COMPARE ((_x_), (_y_))
145 #endif /* not _hash_h_ */