exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / hamt.h
blob91d2f15e571213874ffba76ba66b7cefb246b0f1
1 /* (Persistent) hash array mapped tries.
2 Copyright (C) 2021-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Marc Nieper-Wißkirchen <marc@nieper-wisskirchen.de>, 2021. */
19 /* This module provides a persistent version of hash array mapped
20 tries (hamts) that can be used in place of hash tables when pure
21 (functional) operations are needed.
23 A hash function and an equivalence predicate has to be provided for
24 the elements that can be inserted, replaced and removed in a hamt.
25 A hamt cannot contain duplicates that compare equal.
27 Each non-destructive updating operation returns a new hamt, which
28 shares structure with the original one. Destructive updates only
29 effect the hamt, on which the destructive operation is applied.
30 For example, given a hamt HAMT1, any non-destructive update
31 operation (e.g. hamt_insert) will result in a new hamt HAMT2.
32 Whatever further operations (destructive or not, including freeing
33 a hamt) are applied to HAMT1 won't change HAMT2 and vice versa. To
34 free all the memory, hash_free has therefore to be applied to both
35 HAMT1 and HAMT2.
37 If persistence is not needed, transient hash tables are probably
38 faster.
40 See also: Phil Bagwell (2000). Ideal Hash Trees (Report). Infoscience
41 Department, École Polytechnique Fédérale de Lausanne.
43 http://infoscience.epfl.ch/record/64398/files/idealhashtrees.pdf */
45 #ifndef _GL_HAMT_H
46 #define _GL_HAMT_H
48 /* This file uses _GL_INLINE_HEADER_BEGIN, _GL_INLINE, _GL_ATTRIBUTE_DEALLOC,
49 _GL_ATTRIBUTE_NODISCARD. */
50 #if !_GL_CONFIG_H_INCLUDED
51 #error "Please include config.h first."
52 #endif
54 _GL_INLINE_HEADER_BEGIN
55 #ifndef _GL_HAMT_INLINE
56 # define _GL_HAMT_INLINE _GL_INLINE
57 #endif
59 /* The GL_HAMT_THREAD_SAFE flag is set if the implementation of hamts
60 is thread-safe as long as two threads do not simultaneously access
61 the same hamt. This is non-trivial as different hamts may share
62 some structure.
63 We can define it only when the compiler supports _Atomic. For GCC,
64 it is supported starting with GCC 4.9. */
66 #if (__GNUC__ + (__GNUC_MINOR__ >= 9) > 4) \
67 && __STDC_VERSION__ >= 201112L && !defined __STD_NO_ATOMICS__ \
68 && !defined __cplusplus
69 # define GL_HAMT_THREAD_SAFE 1
70 #else
71 # define GL_HAMT_THREAD_SAFE 0
72 #endif
74 #include <stddef.h>
75 #include <stdint.h>
77 /* Hash values are of type size_t. For each level of the trie, we use
78 5 bits (corresponding to lg2 of the width of a 32-bit word. */
79 #define _GL_HAMT_MAX_DEPTH ((SIZE_WIDTH + 4) / 5)
81 /************/
82 /* Elements */
83 /************/
85 /* A hamt stores pointers to elements. Each element has to be a
86 struct whose initial member is of the type Hamt_entry. You need to
87 define this struct yourself. It will typically contain an
88 Hamt_entry, a key, and, optionally, a value.
90 An element is conceptually owned by a hamt as soon as it is
91 inserted. It will be automatically freed as soon as the last hamt
92 containing it is freed. */
93 typedef struct
95 #if GL_HAMT_THREAD_SAFE
96 _Atomic
97 #endif
98 size_t ref_count;
99 } Hamt_entry;
101 /* Initialize *ELT, which has to point to a structure as described
102 above and return ELT type-cast.
104 Before an element is inserted into any hamt, whether once or
105 multiple times, it has to be initialized exactly once. */
106 _GL_HAMT_INLINE Hamt_entry *
107 hamt_element (void *elt)
109 Hamt_entry *entry = elt;
110 entry->ref_count = 0; /* This assumes that element_entry ==
111 0. */
112 return entry;
115 /*************************/
116 /* Opaque Hamt Structure */
117 /*************************/
119 /* In user-code, hamts are accessed through pointers to the opaque
120 Hamt type. Two hamts are said to be the same if and only if their
121 pointers are equal. */
122 typedef struct hamt Hamt;
124 /*************/
125 /* Interface */
126 /*************/
128 /* A hash function has to be pure, and two elements that compare equal
129 have to have the same hash value. For a hash function to be a good
130 one, it is important that it uses all SIZE_WIDTH bits in
131 size_t. */
132 typedef size_t (Hamt_hasher) (const void *elt);
134 /* A comparison function has to be pure, and two elements that have
135 equal pointers have to compare equal. */
136 typedef bool (Hamt_comparator) (const void *elt1, const void *elt2);
138 /* A user-defined function that is called when the last hamt
139 containing a particular element is freed. */
140 typedef void (Hamt_freer) (Hamt_entry *elt);
142 /****************************/
143 /* Creation and Destruction */
144 /****************************/
146 /* Free the resources solely allocated by HAMT and all elements solely
147 contained in it. */
148 extern void hamt_free (Hamt *hamt);
150 /* Create and return a new and empty hash array mapped trie. */
151 _GL_ATTRIBUTE_NODISCARD
152 extern Hamt *hamt_create (Hamt_hasher *hasher, Hamt_comparator *comparator,
153 Hamt_freer *freer)
154 _GL_ATTRIBUTE_DEALLOC (hamt_free, 1);
156 /* Return a copy of HAMT, which is not the same in the sense above.
157 This procedure can be used, for example, so that two threads can
158 access the same data independently. */
159 _GL_ATTRIBUTE_NODISCARD
160 extern Hamt *hamt_copy (Hamt *hamt)
161 _GL_ATTRIBUTE_DEALLOC (hamt_free, 1);
163 /**********/
164 /* Lookup */
165 /**********/
167 /* If ELT matches an entry in HAMT, return this entry. Otherwise,
168 return NULL. */
169 extern Hamt_entry *hamt_lookup (const Hamt *hamt, const void *elt);
171 /**************************/
172 /* Insertion and Deletion */
173 /**************************/
175 /* If *ELT_PTR matches an element already in HAMT, set *ELT_PTR to the
176 existing element and return the original hamt. Otherwise, insert
177 *ELT_PTR into a copy of the hamt and return the copy. */
178 _GL_ATTRIBUTE_NODISCARD
179 extern Hamt *hamt_insert (Hamt *hamt, Hamt_entry **elt_ptr);
181 /* If *ELT_PTR matches an element already in HAMT, set *ELT_PTR to the
182 existing element, remove the element from a copy of the hamt and
183 return the copy. Otherwise, return the original hamt. */
184 _GL_ATTRIBUTE_NODISCARD
185 extern Hamt *hamt_remove (Hamt *hamt, Hamt_entry **elt_ptr);
187 /* Insert *ELT_PTR into a copy of HAMT and return the copy. If an
188 existing element was replaced, set *ELT_PTR to this element, and to
189 NULL otherwise. */
190 _GL_ATTRIBUTE_NODISCARD
191 extern Hamt *hamt_replace (Hamt *hamt, Hamt_entry **elt_ptr);
193 /*************/
194 /* Iteration */
195 /*************/
197 /* A processor function is called during walking of a hamt, which
198 returns true to continue the walking. */
199 typedef bool (Hamt_processor) (Hamt_entry *elt, void *data);
201 /* Call PROC for every entry of the hamt until it returns false. The
202 first argument to the processor is the entry, the second argument
203 is the value of DATA as received. Return the number of calls that
204 returned true. During processing, the hamt mustn't be
205 modified. */
206 extern size_t hamt_do_while (const Hamt *hamt, Hamt_processor *proc,
207 void *data);
209 /* An alternative interface to iterating through the entry of a hamt
210 that does not make use of a higher-order function like
211 hamt_do_while uses the Hamt_iterator type, which can be allocated
212 through automatic variables on the stack. As a hamt iterator
213 operates on a copy of a hamt, the original hamt can modified
214 (including freeing it) without affecting the iterator. */
215 struct hamt_iterator
217 Hamt* hamt;
218 int depth;
219 size_t path;
220 size_t position;
221 Hamt_entry *entry[_GL_HAMT_MAX_DEPTH + 1];
223 typedef struct hamt_iterator Hamt_iterator;
225 /* Create of copy of HAMT and return an initialized ITER on the
226 copy. */
227 extern Hamt_iterator hamt_iterator (Hamt *hamt);
229 /* Free the resources allocated for ITER, including the hamt copy
230 associated with it. */
231 extern void hamt_iterator_free (Hamt_iterator *iter);
233 /* Return an independent copy of ITER that is initially in the same
234 state. Any operation on the original iterator (including freeing
235 it) doesn't affect the iterator copy and vice versa. */
236 extern Hamt_iterator hamt_iterator_copy (Hamt_iterator *iter);
238 /* Return true if ITER is not at the end, place the current element in
239 *ELT_PTR and move the iterator forward. Otherwise, return
240 false. */
241 extern bool hamt_iterator_next (Hamt_iterator *iter,
242 Hamt_entry **elt_ptr);
244 /***********************/
245 /* Destructive Updates */
246 /***********************/
248 /* If *ELT_PTR matches an element already in HAMT, set *ELT_PTR to the
249 element from the table and return false. Otherwise, insert *ELT_PTR
250 destructively into the hamt and return true. */
251 extern bool hamt_insert_x (Hamt *hamt, Hamt_entry **elt_ptr);
253 /* Insert ELT destructively into HAMT. If an existing element was
254 replaced, return true. Otherwise, return false. */
255 extern bool hamt_replace_x (Hamt *hamt, Hamt_entry *elt);
257 /* If ELT matches an element already in HAMT, remove the element
258 destructively from the hamt and return true. Otherwise, return
259 false. */
260 extern bool hamt_remove_x (Hamt *hamt, Hamt_entry *elt);
262 _GL_INLINE_HEADER_END
264 #endif /* _GL_HAMT_H */