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