]> git.ipfire.org Git - pakfire.git/blob - tools/hashtab.h
system: bogomips is spelled differently on ARM.
[pakfire.git] / tools / hashtab.h
1 /* An expandable hash tables datatype.
2 Copyright (C) 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov (vmakarov@cygnus.com).
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 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18
19 /* This package implements basic hash table functionality. It is possible
20 to search for an entry, create an entry and destroy an entry.
21
22 Elements in the table are generic pointers.
23
24 The size of the table is not fixed; if the occupancy of the table
25 grows too high the hash table will be expanded.
26
27 The abstract data implementation is based on generalized Algorithm D
28 from Knuth's book "The art of computer programming". Hash table is
29 expanded by creation of new hash table and transferring elements from
30 the old table to the new table. */
31
32 #ifndef __HASHTAB_H__
33 #define __HASHTAB_H__
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif /* __cplusplus */
38
39 /* The type for a hash code. */
40 typedef unsigned int hashval_t;
41
42 /* Callback function pointer types. */
43
44 /* Calculate hash of a table entry. */
45 typedef hashval_t (*htab_hash) (const void *);
46
47 /* Compare a table entry with a possible entry. The entry already in
48 the table always comes first, so the second element can be of a
49 different type (but in this case htab_find and htab_find_slot
50 cannot be used; instead the variants that accept a hash value
51 must be used). */
52 typedef int (*htab_eq) (const void *, const void *);
53
54 /* Cleanup function called whenever a live element is removed from
55 the hash table. */
56 typedef void (*htab_del) (void *);
57
58 /* Function called by htab_traverse for each live element. The first
59 arg is the slot of the element (which can be passed to htab_clear_slot
60 if desired), the second arg is the auxiliary pointer handed to
61 htab_traverse. Return 1 to continue scan, 0 to stop. */
62 typedef int (*htab_trav) (void **, void *);
63
64 /* Hash tables are of the following type. The structure
65 (implementation) of this type is not needed for using the hash
66 tables. All work with hash table should be executed only through
67 functions mentioned below. */
68
69 struct htab
70 {
71 /* Pointer to hash function. */
72 htab_hash hash_f;
73
74 /* Pointer to comparison function. */
75 htab_eq eq_f;
76
77 /* Pointer to cleanup function. */
78 htab_del del_f;
79
80 /* Table itself. */
81 void **entries;
82
83 /* Current size (in entries) of the hash table */
84 size_t size;
85
86 /* Current number of elements including also deleted elements */
87 size_t n_elements;
88
89 /* Current number of deleted elements in the table */
90 size_t n_deleted;
91
92 /* The following member is used for debugging. Its value is number
93 of all calls of `htab_find_slot' for the hash table. */
94 unsigned int searches;
95
96 /* The following member is used for debugging. Its value is number
97 of collisions fixed for time of work with the hash table. */
98 unsigned int collisions;
99
100 /* This is non-zero if we are allowed to return NULL for function calls
101 that allocate memory. */
102 int return_allocation_failure;
103 };
104
105 typedef struct htab *htab_t;
106
107 /* An enum saying whether we insert into the hash table or not. */
108 enum insert_option {NO_INSERT, INSERT};
109
110 /* The prototypes of the package functions. */
111
112 /* This function is like htab_create, but may return NULL if memory
113 allocation fails, and also signals that htab_find_slot_with_hash and
114 htab_find_slot are allowed to return NULL when inserting. */
115 extern htab_t htab_try_create (size_t, htab_hash, htab_eq, htab_del);
116 extern void htab_delete (htab_t);
117 extern void htab_empty (htab_t);
118
119 extern void *htab_find (htab_t, const void *);
120 extern void **htab_find_slot (htab_t, const void *, enum insert_option);
121 extern void *htab_find_with_hash (htab_t, const void *, hashval_t);
122 extern void **htab_find_slot_with_hash (htab_t, const void *, hashval_t,
123 enum insert_option);
124 extern void htab_clear_slot (htab_t, void **);
125 extern void htab_remove_elt (htab_t, void *);
126
127 extern void htab_traverse (htab_t, htab_trav, void *);
128
129 extern size_t htab_size (htab_t);
130 extern size_t htab_elements (htab_t);
131 extern double htab_collisions (htab_t);
132
133 /* A hash function for pointers. */
134 extern htab_hash htab_hash_pointer;
135
136 /* An equality function for pointers. */
137 extern htab_eq htab_eq_pointer;
138
139 #ifdef __cplusplus
140 }
141 #endif /* __cplusplus */
142
143 #endif /* __HASHTAB_H */