1 | /* hashtab.h,v 1.2 2004/09/14 22:27:33 bird Exp */
|
---|
2 | /** @file
|
---|
3 | * GNU, -liberty.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /* An expandable hash tables datatype.
|
---|
7 | Copyright (C) 1999, 2000 Free Software Foundation, Inc.
|
---|
8 | Contributed by Vladimir Makarov (vmakarov@cygnus.com).
|
---|
9 |
|
---|
10 | This program is free software; you can redistribute it and/or modify
|
---|
11 | it under the terms of the GNU General Public License as published by
|
---|
12 | the Free Software Foundation; either version 2 of the License, or
|
---|
13 | (at your option) any later version.
|
---|
14 |
|
---|
15 | This program is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
18 | GNU General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU General Public License
|
---|
21 | along with this program; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
|
---|
23 |
|
---|
24 | /* This package implements basic hash table functionality. It is possible
|
---|
25 | to search for an entry, create an entry and destroy an entry.
|
---|
26 |
|
---|
27 | Elements in the table are generic pointers.
|
---|
28 |
|
---|
29 | The size of the table is not fixed; if the occupancy of the table
|
---|
30 | grows too high the hash table will be expanded.
|
---|
31 |
|
---|
32 | The abstract data implementation is based on generalized Algorithm D
|
---|
33 | from Knuth's book "The art of computer programming". Hash table is
|
---|
34 | expanded by creation of new hash table and transferring elements from
|
---|
35 | the old table to the new table. */
|
---|
36 |
|
---|
37 | #ifndef __HASHTAB_H__
|
---|
38 | #define __HASHTAB_H__
|
---|
39 |
|
---|
40 | #ifdef __cplusplus
|
---|
41 | extern "C" {
|
---|
42 | #endif /* __cplusplus */
|
---|
43 |
|
---|
44 | #include <ansidecl.h>
|
---|
45 |
|
---|
46 | /* The type for a hash code. */
|
---|
47 | typedef unsigned int hashval_t;
|
---|
48 |
|
---|
49 | /* Callback function pointer types. */
|
---|
50 |
|
---|
51 | /* Calculate hash of a table entry. */
|
---|
52 | typedef hashval_t (*htab_hash) PARAMS ((const void *));
|
---|
53 |
|
---|
54 | /* Compare a table entry with a possible entry. The entry already in
|
---|
55 | the table always comes first, so the second element can be of a
|
---|
56 | different type (but in this case htab_find and htab_find_slot
|
---|
57 | cannot be used; instead the variants that accept a hash value
|
---|
58 | must be used). */
|
---|
59 | typedef int (*htab_eq) PARAMS ((const void *, const void *));
|
---|
60 |
|
---|
61 | /* Cleanup function called whenever a live element is removed from
|
---|
62 | the hash table. */
|
---|
63 | typedef void (*htab_del) PARAMS ((void *));
|
---|
64 |
|
---|
65 | /* Function called by htab_traverse for each live element. The first
|
---|
66 | arg is the slot of the element (which can be passed to htab_clear_slot
|
---|
67 | if desired), the second arg is the auxiliary pointer handed to
|
---|
68 | htab_traverse. Return 1 to continue scan, 0 to stop. */
|
---|
69 | typedef int (*htab_trav) PARAMS ((void **, void *));
|
---|
70 |
|
---|
71 | /* Hash tables are of the following type. The structure
|
---|
72 | (implementation) of this type is not needed for using the hash
|
---|
73 | tables. All work with hash table should be executed only through
|
---|
74 | functions mentioned below. */
|
---|
75 |
|
---|
76 | struct htab
|
---|
77 | {
|
---|
78 | /* Pointer to hash function. */
|
---|
79 | htab_hash hash_f;
|
---|
80 |
|
---|
81 | /* Pointer to comparison function. */
|
---|
82 | htab_eq eq_f;
|
---|
83 |
|
---|
84 | /* Pointer to cleanup function. */
|
---|
85 | htab_del del_f;
|
---|
86 |
|
---|
87 | /* Table itself. */
|
---|
88 | PTR *entries;
|
---|
89 |
|
---|
90 | /* Current size (in entries) of the hash table */
|
---|
91 | size_t size;
|
---|
92 |
|
---|
93 | /* Current number of elements including also deleted elements */
|
---|
94 | size_t n_elements;
|
---|
95 |
|
---|
96 | /* Current number of deleted elements in the table */
|
---|
97 | size_t n_deleted;
|
---|
98 |
|
---|
99 | /* The following member is used for debugging. Its value is number
|
---|
100 | of all calls of `htab_find_slot' for the hash table. */
|
---|
101 | unsigned int searches;
|
---|
102 |
|
---|
103 | /* The following member is used for debugging. Its value is number
|
---|
104 | of collisions fixed for time of work with the hash table. */
|
---|
105 | unsigned int collisions;
|
---|
106 |
|
---|
107 | /* This is non-zero if we are allowed to return NULL for function calls
|
---|
108 | that allocate memory. */
|
---|
109 | int return_allocation_failure;
|
---|
110 | };
|
---|
111 |
|
---|
112 | typedef struct htab *htab_t;
|
---|
113 |
|
---|
114 | /* An enum saying whether we insert into the hash table or not. */
|
---|
115 | enum insert_option {NO_INSERT, INSERT};
|
---|
116 |
|
---|
117 | /* The prototypes of the package functions. */
|
---|
118 |
|
---|
119 | extern htab_t htab_create PARAMS ((size_t, htab_hash,
|
---|
120 | htab_eq, htab_del));
|
---|
121 |
|
---|
122 | /* This function is like htab_create, but may return NULL if memory
|
---|
123 | allocation fails, and also signals that htab_find_slot_with_hash and
|
---|
124 | htab_find_slot are allowed to return NULL when inserting. */
|
---|
125 | extern htab_t htab_try_create PARAMS ((size_t, htab_hash,
|
---|
126 | htab_eq, htab_del));
|
---|
127 | extern void htab_delete PARAMS ((htab_t));
|
---|
128 | extern void htab_empty PARAMS ((htab_t));
|
---|
129 |
|
---|
130 | extern PTR htab_find PARAMS ((htab_t, const void *));
|
---|
131 | extern PTR *htab_find_slot PARAMS ((htab_t, const void *,
|
---|
132 | enum insert_option));
|
---|
133 | extern PTR htab_find_with_hash PARAMS ((htab_t, const void *,
|
---|
134 | hashval_t));
|
---|
135 | extern PTR *htab_find_slot_with_hash PARAMS ((htab_t, const void *,
|
---|
136 | hashval_t,
|
---|
137 | enum insert_option));
|
---|
138 | extern void htab_clear_slot PARAMS ((htab_t, void **));
|
---|
139 | extern void htab_remove_elt PARAMS ((htab_t, void *));
|
---|
140 |
|
---|
141 | extern void htab_traverse PARAMS ((htab_t, htab_trav, void *));
|
---|
142 |
|
---|
143 | extern size_t htab_size PARAMS ((htab_t));
|
---|
144 | extern size_t htab_elements PARAMS ((htab_t));
|
---|
145 | extern double htab_collisions PARAMS ((htab_t));
|
---|
146 |
|
---|
147 | /* A hash function for pointers. */
|
---|
148 | extern htab_hash htab_hash_pointer;
|
---|
149 |
|
---|
150 | /* An equality function for pointers. */
|
---|
151 | extern htab_eq htab_eq_pointer;
|
---|
152 |
|
---|
153 | /* A hash function for null-terminated strings. */
|
---|
154 | extern hashval_t htab_hash_string PARAMS ((const PTR));
|
---|
155 |
|
---|
156 | #ifdef __cplusplus
|
---|
157 | }
|
---|
158 | #endif /* __cplusplus */
|
---|
159 |
|
---|
160 | #endif /* __HASHTAB_H */
|
---|