Fri Dec 12 23:54:37 2003 Ben Pfaff <blp@gnu.org>
[pspp-builds.git] / src / hash.h
1 /* PSPP - computes sample statistics.
2    Copyright (C) 1997-9, 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    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
18    02111-1307, USA. */
19
20 #if !hash_h
21 #define hash_h 1
22
23 #include <stddef.h>
24
25 typedef int hsh_compare_func (const void *, const void *, void *param);
26 typedef unsigned hsh_hash_func (const void *, void *param);
27 typedef void hsh_free_func (void *, void *param);
28
29 /* Hash table iterator (opaque). */
30 struct hsh_iterator
31   {
32     size_t next;                /* Index of next entry. */
33   };
34
35 /* Prime numbers and hash functions. */
36 unsigned hsh_hash_bytes (const void *, size_t);
37 unsigned hsh_hash_string (const char *);
38 unsigned hsh_hash_int (int);
39
40 /* Hash tables. */
41 struct hsh_table *hsh_create (int m, hsh_compare_func *,
42                               hsh_hash_func *, hsh_free_func *,
43                               void *param);
44 void hsh_clear (struct hsh_table *);
45 void hsh_destroy (struct hsh_table *);
46 void **hsh_sort (struct hsh_table *);
47
48 /* Search and insertion. */
49 void **hsh_probe (struct hsh_table *, const void *);
50 void *hsh_find (struct hsh_table *, const void *);
51 int hsh_delete (struct hsh_table *, const void *);
52
53 /* Iteration. */
54 void *hsh_first (struct hsh_table *, struct hsh_iterator *);
55 void *hsh_next (struct hsh_table *, struct hsh_iterator *);
56
57 /* Search and insertion with assertion. */
58 #if GLOBAL_DEBUGGING
59 void hsh_force_insert (struct hsh_table *, void *);
60 void *hsh_force_find (struct hsh_table *, const void *);
61 void hsh_force_delete (struct hsh_table *, const void *);
62 #else
63 #define hsh_force_insert(A, B)  ((void) (*hsh_probe (A, B) = B))
64 #define hsh_force_find(A, B)    (hsh_find (A, B))
65 #define hsh_force_delete(A, B)  ((void) hsh_delete (A, B))
66 #endif
67
68 /* Number of entries in hash table H. */
69 size_t hsh_count (struct hsh_table *);
70
71 /* Debugging. */
72 #if GLOBAL_DEBUGGING
73 void hsh_dump (struct hsh_table *);
74 #endif
75
76 #endif /* hash_h */