Assume C89, so PARAMS isn't needed.
[gnulib.git] / lib / hash.h
1 /* hash - hashing table processing.
2    Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation, Inc.
3    Written by Jim Meyering <meyering@ascend.com>, 1998.
4
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)
8    any later version.
9
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.
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 Foundation,
17    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 /* A generic hash table package.  */
20
21 /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
22    obstacks instead of malloc, and recompile `hash.c' with same setting.  */
23
24 #ifndef HASH_H_
25 # define HASH_H_
26
27 # include <stdio.h>
28
29 typedef unsigned (*Hash_hasher) (const void *, unsigned);
30 typedef bool (*Hash_comparator) (const void *, const void *);
31 typedef void (*Hash_data_freer) (void *);
32 typedef bool (*Hash_processor) (void *, void *);
33
34 struct hash_entry
35   {
36     void *data;
37     struct hash_entry *next;
38   };
39
40 struct hash_tuning
41   {
42     /* This structure is mainly used for `hash_initialize', see the block
43        documentation of `hash_reset_tuning' for more complete comments.  */
44
45     float shrink_threshold;     /* ratio of used buckets to trigger a shrink */
46     float shrink_factor;        /* ratio of new smaller size to original size */
47     float growth_threshold;     /* ratio of used buckets to trigger a growth */
48     float growth_factor;        /* ratio of new bigger size to original size */
49     bool is_n_buckets;          /* if CANDIDATE really means table size */
50   };
51
52 typedef struct hash_tuning Hash_tuning;
53
54 struct hash_table;
55
56 typedef struct hash_table Hash_table;
57
58 /* Information and lookup.  */
59 unsigned hash_get_n_buckets (const Hash_table *);
60 unsigned hash_get_n_buckets_used (const Hash_table *);
61 unsigned hash_get_n_entries (const Hash_table *);
62 unsigned hash_get_max_bucket_length (const Hash_table *);
63 bool hash_table_ok (const Hash_table *);
64 void hash_print_statistics (const Hash_table *, FILE *);
65 void *hash_lookup (const Hash_table *, const void *);
66
67 /* Walking.  */
68 void *hash_get_first (const Hash_table *);
69 void *hash_get_next (const Hash_table *, const void *);
70 unsigned hash_get_entries (const Hash_table *, void **, unsigned);
71 unsigned hash_do_for_each (const Hash_table *, Hash_processor, void *);
72
73 /* Allocation and clean-up.  */
74 unsigned hash_string (const char *, unsigned);
75 void hash_reset_tuning (Hash_tuning *);
76 Hash_table *hash_initialize (unsigned, const Hash_tuning *,
77                              Hash_hasher, Hash_comparator,
78                              Hash_data_freer);
79 void hash_clear (Hash_table *);
80 void hash_free (Hash_table *);
81
82 /* Insertion and deletion.  */
83 bool hash_rehash (Hash_table *, unsigned);
84 void *hash_insert (Hash_table *, const void *);
85 void *hash_delete (Hash_table *, const void *);
86
87 #endif