.h files should stand alone, but we shouldn't include <sys/types.h>
[gnulib.git] / lib / hash.h
1 /* hash - hashing table processing.
2    Copyright (C) 1998, 1999, 2001 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 # ifndef PARAMS
30 #  if PROTOTYPES || __STDC__
31 #   define PARAMS(Args) Args
32 #  else
33 #   define PARAMS(Args) ()
34 #  endif
35 # endif
36
37 typedef unsigned (*Hash_hasher) PARAMS ((const void *, unsigned));
38 typedef bool (*Hash_comparator) PARAMS ((const void *, const void *));
39 typedef void (*Hash_data_freer) PARAMS ((void *));
40 typedef bool (*Hash_processor) PARAMS ((void *, void *));
41
42 struct hash_entry
43   {
44     void *data;
45     struct hash_entry *next;
46   };
47
48 struct hash_tuning
49   {
50     /* This structure is mainly used for `hash_initialize', see the block
51        documentation of `hash_reset_tuning' for more complete comments.  */
52
53     float shrink_threshold;     /* ratio of used buckets to trigger a shrink */
54     float shrink_factor;        /* ratio of new smaller size to original size */
55     float growth_threshold;     /* ratio of used buckets to trigger a growth */
56     float growth_factor;        /* ratio of new bigger size to original size */
57     bool is_n_buckets;          /* if CANDIDATE really means table size */
58   };
59
60 typedef struct hash_tuning Hash_tuning;
61
62 struct hash_table;
63
64 typedef struct hash_table Hash_table;
65
66 /* Information and lookup.  */
67 unsigned hash_get_n_buckets PARAMS ((const Hash_table *));
68 unsigned hash_get_n_buckets_used PARAMS ((const Hash_table *));
69 unsigned hash_get_n_entries PARAMS ((const Hash_table *));
70 unsigned hash_get_max_bucket_length PARAMS ((const Hash_table *));
71 bool hash_table_ok PARAMS ((const Hash_table *));
72 void hash_print_statistics PARAMS ((const Hash_table *, FILE *));
73 void *hash_lookup PARAMS ((const Hash_table *, const void *));
74
75 /* Walking.  */
76 void *hash_get_first PARAMS ((const Hash_table *));
77 void *hash_get_next PARAMS ((const Hash_table *, const void *));
78 unsigned hash_get_entries PARAMS ((const Hash_table *, void **, unsigned));
79 unsigned hash_do_for_each PARAMS ((const Hash_table *, Hash_processor, void *));
80
81 /* Allocation and clean-up.  */
82 unsigned hash_string PARAMS ((const char *, unsigned));
83 void hash_reset_tuning PARAMS ((Hash_tuning *));
84 Hash_table *hash_initialize PARAMS ((unsigned, const Hash_tuning *,
85                                      Hash_hasher, Hash_comparator,
86                                      Hash_data_freer));
87 void hash_clear PARAMS ((Hash_table *));
88 void hash_free PARAMS ((Hash_table *));
89
90 /* Insertion and deletion.  */
91 bool hash_rehash PARAMS ((Hash_table *, unsigned));
92 void *hash_insert PARAMS ((Hash_table *, const void *));
93 void *hash_delete PARAMS ((Hash_table *, const void *));
94
95 #endif