4693a6d779ab0492f713a730d0f482372ea281f3
[pspp-builds.git] / src / libpspp / hash.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #if !hash_h
20 #define hash_h 1
21
22 #include <stddef.h>
23 #include <stdbool.h>
24
25 typedef int hsh_compare_func (const void *, const void *, const void *aux);
26 typedef unsigned hsh_hash_func (const void *, const void *aux);
27 typedef void hsh_free_func (void *, const void *aux);
28
29 /* Hash table iterator (opaque). */
30 struct hsh_iterator
31   {
32     size_t next;                /* Index of next entry. */
33   };
34
35 /* Hash functions. */
36 unsigned hsh_hash_bytes (const void *, size_t);
37 unsigned hsh_hash_string (const char *);
38 unsigned hsh_hash_case_string (const char *);
39 unsigned hsh_hash_int (int);
40 unsigned hsh_hash_double (double);
41
42 /* Hash tables. */
43 struct hsh_table *hsh_create (int m, hsh_compare_func *,
44                               hsh_hash_func *, hsh_free_func *,
45                               const void *aux);
46
47 struct pool;
48 struct hsh_table *hsh_create_pool (struct pool *pool, int m,
49                                    hsh_compare_func *,
50                                    hsh_hash_func *, hsh_free_func *,
51                                    const void *aux);
52
53 void hsh_clear (struct hsh_table *);
54 void hsh_destroy (struct hsh_table *);
55 void *const *hsh_sort (struct hsh_table *);
56 void *const *hsh_data (struct hsh_table *);
57 void **hsh_sort_copy (struct hsh_table *);
58 void **hsh_data_copy (struct hsh_table *);
59
60 /* Search and insertion. */
61 void **hsh_probe (struct hsh_table *, const void *);
62 void *hsh_insert (struct hsh_table *, void *);
63 void *hsh_replace (struct hsh_table *, void *);
64 void *hsh_find (struct hsh_table *, const void *);
65 bool hsh_delete (struct hsh_table *, const void *);
66
67 /* Iteration. */
68 void *hsh_first (struct hsh_table *, struct hsh_iterator *);
69 void *hsh_next (struct hsh_table *, struct hsh_iterator *);
70
71 /* Search and insertion with assertion. */
72 #if DEBUGGING
73 void hsh_force_insert (struct hsh_table *, void *);
74 void *hsh_force_find (struct hsh_table *, const void *);
75 void hsh_force_delete (struct hsh_table *, const void *);
76 #else
77 #define hsh_force_insert(A, B)  ((void) (*hsh_probe (A, B) = B))
78 #define hsh_force_find(A, B)    (hsh_find (A, B))
79 #define hsh_force_delete(A, B)  ((void) hsh_delete (A, B))
80 #endif
81
82 /* Number of entries in hash table H. */
83 size_t hsh_count (struct hsh_table *);
84
85 /* Debugging. */
86 #if DEBUGGING
87 void hsh_dump (struct hsh_table *);
88 #endif
89
90
91 /* Const Wrappers for the above */
92
93 static inline struct const_hsh_table *
94 const_hsh_create (int m,
95                   hsh_compare_func *hcf,
96                   hsh_hash_func *hhf, hsh_free_func *hff,
97                   const void *aux)
98 {
99   return (struct const_hsh_table *) hsh_create (m, hcf, hhf, hff, aux);
100 }
101
102
103
104 static inline struct const_hsh_table *
105 const_hsh_create_pool (struct pool *pool, int m,
106                        hsh_compare_func *cf,
107                        hsh_hash_func *hf, hsh_free_func *ff,
108                        const void *aux)
109 {
110   return (struct const_hsh_table *) hsh_create_pool (pool, m, cf, hf, ff, aux);
111 }
112
113
114 static inline void
115 const_hsh_clear (struct const_hsh_table *h)
116 {
117   hsh_clear ( (struct hsh_table *) h);
118 }
119
120 static inline void
121 const_hsh_destroy (struct const_hsh_table *h)
122 {
123   hsh_destroy ( (struct hsh_table *) h);
124 }
125
126 static inline void *const *
127 const_hsh_sort (struct const_hsh_table *h)
128 {
129   return hsh_sort ( (struct hsh_table *) h);
130 }
131
132 static inline void *const *
133 const_hsh_data (struct const_hsh_table *h)
134 {
135   return hsh_data ( (struct hsh_table *) h);
136 }
137
138 static inline void **
139 const_hsh_sort_copy (struct const_hsh_table *h)
140 {
141   return hsh_sort_copy ( (struct hsh_table *) h);
142 }
143
144 static inline void **
145 const_hsh_data_copy (struct const_hsh_table *h)
146 {
147   return hsh_data_copy ( (struct hsh_table *) h);
148 }
149
150
151 static inline size_t
152 const_hsh_count (struct const_hsh_table *h)
153 {
154   return hsh_count ( (struct hsh_table *) h);
155 }
156
157 static inline void *
158 const_hsh_insert (struct const_hsh_table *h, const void *item)
159 {
160   return hsh_insert ( (struct hsh_table *) h, (void *) item);
161 }
162
163 static inline void *
164 const_hsh_replace (struct const_hsh_table *h, const void *item)
165 {
166   return hsh_replace ( (struct hsh_table *) h, (void *) item);
167 }
168
169 static inline void *
170 const_hsh_find (struct const_hsh_table *h, const void *item)
171 {
172   return hsh_find ( (struct hsh_table *) h, (void *) item);
173 }
174
175 static inline bool
176 const_hsh_delete (struct const_hsh_table *h, const void *item)
177 {
178   return hsh_delete ( (struct hsh_table *)h, (void *) item);
179 }
180
181
182 static inline void *
183 const_hsh_first (struct const_hsh_table *h, struct hsh_iterator *i)
184 {
185   return hsh_first ( (struct hsh_table *) h, i);
186 }
187
188 static inline void *
189 const_hsh_next (struct const_hsh_table *h, struct hsh_iterator *i)
190 {
191   return hsh_next ( (struct hsh_table *) h, i);
192 }
193
194
195 #endif /* hash_h */