hash: declare some functions with the warn_unused_result attribute
[gnulib.git] / lib / hash.h
1 /* hash - hashing table processing.
2    Copyright (C) 1998, 1999, 2001, 2003, 2009 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 3 of the License, or
8    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
17
18 /* A generic hash table package.  */
19
20 /* Make sure USE_OBSTACK is defined to 1 if you want the allocator to use
21    obstacks instead of malloc, and recompile `hash.c' with same setting.  */
22
23 #ifndef HASH_H_
24 # define HASH_H_
25
26 # include <stdio.h>
27 # include <stdbool.h>
28
29 /* The warn_unused_result attribute appeared first in gcc-3.4.0 */
30 # ifndef __attribute__
31 #  if __GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4)
32 #   define __attribute__(x)
33 #  endif
34 # endif
35
36 # ifndef ATTRIBUTE_WUR
37 #  define ATTRIBUTE_WUR __attribute__ ((__warn_unused_result__))
38 # endif
39
40 typedef size_t (*Hash_hasher) (const void *, size_t);
41 typedef bool (*Hash_comparator) (const void *, const void *);
42 typedef void (*Hash_data_freer) (void *);
43 typedef bool (*Hash_processor) (void *, void *);
44
45 struct hash_entry
46   {
47     void *data;
48     struct hash_entry *next;
49   };
50
51 struct hash_tuning
52   {
53     /* This structure is mainly used for `hash_initialize', see the block
54        documentation of `hash_reset_tuning' for more complete comments.  */
55
56     float shrink_threshold;     /* ratio of used buckets to trigger a shrink */
57     float shrink_factor;        /* ratio of new smaller size to original size */
58     float growth_threshold;     /* ratio of used buckets to trigger a growth */
59     float growth_factor;        /* ratio of new bigger size to original size */
60     bool is_n_buckets;          /* if CANDIDATE really means table size */
61   };
62
63 typedef struct hash_tuning Hash_tuning;
64
65 struct hash_table;
66
67 typedef struct hash_table Hash_table;
68
69 /* Information and lookup.  */
70 size_t hash_get_n_buckets (const Hash_table *);
71 size_t hash_get_n_buckets_used (const Hash_table *);
72 size_t hash_get_n_entries (const Hash_table *);
73 size_t hash_get_max_bucket_length (const Hash_table *);
74 bool hash_table_ok (const Hash_table *);
75 void hash_print_statistics (const Hash_table *, FILE *);
76 void *hash_lookup (const Hash_table *, const void *);
77
78 /* Walking.  */
79 void *hash_get_first (const Hash_table *);
80 void *hash_get_next (const Hash_table *, const void *);
81 size_t hash_get_entries (const Hash_table *, void **, size_t);
82 size_t hash_do_for_each (const Hash_table *, Hash_processor, void *);
83
84 /* Allocation and clean-up.  */
85 size_t hash_string (const char *, size_t);
86 void hash_reset_tuning (Hash_tuning *);
87 Hash_table *hash_initialize (size_t, const Hash_tuning *,
88                              Hash_hasher, Hash_comparator,
89                              Hash_data_freer) ATTRIBUTE_WUR;
90 void hash_clear (Hash_table *);
91 void hash_free (Hash_table *);
92
93 /* Insertion and deletion.  */
94 bool hash_rehash (Hash_table *, size_t) ATTRIBUTE_WUR;
95 void *hash_insert (Hash_table *, const void *) ATTRIBUTE_WUR;
96 void *hash_delete (Hash_table *, const void *);
97
98 #endif