Fix last ChangeLog entry.
[gnulib.git] / lib / gl_array_oset.c
blob86fb747b68f272d48e02e22c428ae83cace87b98
1 /* Ordered set data type implemented by an array.
2 Copyright (C) 2006-2007, 2009-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "gl_array_oset.h"
23 #include <stdlib.h>
25 /* Checked size_t computations. */
26 #include "xsize.h"
28 /* -------------------------- gl_oset_t Data Type -------------------------- */
30 /* Concrete gl_oset_impl type, valid for this file only. */
31 struct gl_oset_impl
33 struct gl_oset_impl_base base;
34 /* An array of ALLOCATED elements, of which the first COUNT are used.
35 0 <= COUNT <= ALLOCATED. */
36 const void **elements;
37 size_t count;
38 size_t allocated;
41 static gl_oset_t
42 gl_array_nx_create_empty (gl_oset_implementation_t implementation,
43 gl_setelement_compar_fn compar_fn,
44 gl_setelement_dispose_fn dispose_fn)
46 struct gl_oset_impl *set =
47 (struct gl_oset_impl *) malloc (sizeof (struct gl_oset_impl));
49 if (set == NULL)
50 return NULL;
52 set->base.vtable = implementation;
53 set->base.compar_fn = compar_fn;
54 set->base.dispose_fn = dispose_fn;
55 set->elements = NULL;
56 set->count = 0;
57 set->allocated = 0;
59 return set;
62 static size_t
63 gl_array_size (gl_oset_t set)
65 return set->count;
68 static size_t
69 gl_array_indexof (gl_oset_t set, const void *elt)
71 size_t count = set->count;
73 if (count > 0)
75 gl_setelement_compar_fn compar = set->base.compar_fn;
76 size_t low = 0;
77 size_t high = count;
79 /* At each loop iteration, low < high; for indices < low the values
80 are smaller than ELT; for indices >= high the values are greater
81 than ELT. So, if the element occurs in the list, it is at
82 low <= position < high. */
85 size_t mid = low + (high - low) / 2; /* low <= mid < high */
86 int cmp = (compar != NULL
87 ? compar (set->elements[mid], elt)
88 : (set->elements[mid] > elt ? 1 :
89 set->elements[mid] < elt ? -1 : 0));
91 if (cmp < 0)
92 low = mid + 1;
93 else if (cmp > 0)
94 high = mid;
95 else /* cmp == 0 */
96 /* We have an element equal to ELT at index MID. */
97 return mid;
99 while (low < high);
101 return (size_t)(-1);
104 static bool
105 gl_array_search (gl_oset_t set, const void *elt)
107 return gl_array_indexof (set, elt) != (size_t)(-1);
110 static bool
111 gl_array_search_atleast (gl_oset_t set,
112 gl_setelement_threshold_fn threshold_fn,
113 const void *threshold,
114 const void **eltp)
116 size_t count = set->count;
118 if (count > 0)
120 size_t low = 0;
121 size_t high = count;
123 /* At each loop iteration, low < high; for indices < low the values are
124 smaller than THRESHOLD; for indices >= high the values are nonexistent.
125 So, if an element >= THRESHOLD occurs in the list, it is at
126 low <= position < high. */
129 size_t mid = low + (high - low) / 2; /* low <= mid < high */
131 if (! threshold_fn (set->elements[mid], threshold))
132 low = mid + 1;
133 else
135 /* We have an element >= THRESHOLD at index MID. But we need the
136 minimal such index. */
137 high = mid;
138 /* At each loop iteration, low <= high and
139 compar (set->elements[high], threshold) >= 0,
140 and we know that the first occurrence of the element is at
141 low <= position <= high. */
142 while (low < high)
144 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
146 if (! threshold_fn (set->elements[mid2], threshold))
147 low = mid2 + 1;
148 else
149 high = mid2;
151 *eltp = set->elements[low];
152 return true;
155 while (low < high);
157 return false;
160 /* Ensure that set->allocated > set->count.
161 Return 0 upon success, -1 upon out-of-memory. */
162 static int
163 grow (gl_oset_t set)
165 size_t new_allocated;
166 size_t memory_size;
167 const void **memory;
169 new_allocated = xtimes (set->allocated, 2);
170 new_allocated = xsum (new_allocated, 1);
171 memory_size = xtimes (new_allocated, sizeof (const void *));
172 if (size_overflow_p (memory_size))
173 /* Overflow, would lead to out of memory. */
174 return -1;
175 memory = (const void **) realloc (set->elements, memory_size);
176 if (memory == NULL)
177 /* Out of memory. */
178 return -1;
179 set->elements = memory;
180 set->allocated = new_allocated;
181 return 0;
184 /* Add the given element ELT at the given position,
185 0 <= position <= gl_oset_size (set).
186 Return 1 upon success, -1 upon out-of-memory. */
187 static int
188 gl_array_nx_add_at (gl_oset_t set, size_t position, const void *elt)
190 size_t count = set->count;
191 const void **elements;
192 size_t i;
194 if (count == set->allocated)
195 if (grow (set) < 0)
196 return -1;
197 elements = set->elements;
198 for (i = count; i > position; i--)
199 elements[i] = elements[i - 1];
200 elements[position] = elt;
201 set->count = count + 1;
202 return 1;
205 /* Remove the element at the given position,
206 0 <= position < gl_oset_size (set). */
207 static void
208 gl_array_remove_at (gl_oset_t set, size_t position)
210 size_t count = set->count;
211 const void **elements;
212 size_t i;
214 elements = set->elements;
215 if (set->base.dispose_fn != NULL)
216 set->base.dispose_fn (elements[position]);
217 for (i = position + 1; i < count; i++)
218 elements[i - 1] = elements[i];
219 set->count = count - 1;
222 static int
223 gl_array_nx_add (gl_oset_t set, const void *elt)
225 size_t count = set->count;
226 size_t low = 0;
228 if (count > 0)
230 gl_setelement_compar_fn compar = set->base.compar_fn;
231 size_t high = count;
233 /* At each loop iteration, low < high; for indices < low the values
234 are smaller than ELT; for indices >= high the values are greater
235 than ELT. So, if the element occurs in the list, it is at
236 low <= position < high. */
239 size_t mid = low + (high - low) / 2; /* low <= mid < high */
240 int cmp = (compar != NULL
241 ? compar (set->elements[mid], elt)
242 : (set->elements[mid] > elt ? 1 :
243 set->elements[mid] < elt ? -1 : 0));
245 if (cmp < 0)
246 low = mid + 1;
247 else if (cmp > 0)
248 high = mid;
249 else /* cmp == 0 */
250 return false;
252 while (low < high);
254 return gl_array_nx_add_at (set, low, elt);
257 static bool
258 gl_array_remove (gl_oset_t set, const void *elt)
260 size_t index = gl_array_indexof (set, elt);
261 if (index != (size_t)(-1))
263 gl_array_remove_at (set, index);
264 return true;
266 else
267 return false;
270 static int
271 gl_array_update (gl_oset_t set, const void *elt,
272 void (*action) (const void * /*elt*/, void * /*action_data*/),
273 void *action_data)
275 /* Like gl_array_remove, action (...), gl_array_nx_add, except that we don't
276 actually remove ELT. */
277 /* Remember the old position. */
278 size_t old_index = gl_array_indexof (set, elt);
279 /* Invoke ACTION. */
280 action (elt, action_data);
281 /* Determine the new position. */
282 if (old_index != (size_t)(-1))
284 size_t count = set->count;
286 if (count > 1)
288 gl_setelement_compar_fn compar = set->base.compar_fn;
289 size_t low;
290 size_t high;
292 if (old_index > 0)
294 size_t mid = old_index - 1;
295 int cmp = (compar != NULL
296 ? compar (set->elements[mid], elt)
297 : (set->elements[mid] > elt ? 1 :
298 set->elements[mid] < elt ? -1 : 0));
299 if (cmp < 0)
301 low = old_index + 1;
302 high = count;
304 else if (cmp > 0)
306 low = 0;
307 high = mid;
309 else /* cmp == 0 */
311 /* Two adjacent elements are the same. */
312 gl_array_remove_at (set, old_index);
313 return -1;
316 else
318 low = old_index + 1;
319 high = count;
322 /* At each loop iteration, low <= high; for indices < low the values
323 are smaller than ELT; for indices >= high the values are greater
324 than ELT. So, if the element occurs in the list, it is at
325 low <= position < high. */
326 while (low < high)
328 size_t mid = low + (high - low) / 2; /* low <= mid < high */
329 int cmp = (compar != NULL
330 ? compar (set->elements[mid], elt)
331 : (set->elements[mid] > elt ? 1 :
332 set->elements[mid] < elt ? -1 : 0));
334 if (cmp < 0)
335 low = mid + 1;
336 else if (cmp > 0)
337 high = mid;
338 else /* cmp == 0 */
340 /* Two elements are the same. */
341 gl_array_remove_at (set, old_index);
342 return -1;
346 if (low < old_index)
348 /* Move the element from old_index to low. */
349 size_t new_index = low;
350 const void **elements = set->elements;
351 size_t i;
353 for (i = old_index; i > new_index; i--)
354 elements[i] = elements[i - 1];
355 elements[new_index] = elt;
356 return true;
358 else
360 /* low > old_index. */
361 /* Move the element from old_index to low - 1. */
362 size_t new_index = low - 1;
364 if (new_index > old_index)
366 const void **elements = set->elements;
367 size_t i;
369 for (i = old_index; i < new_index; i++)
370 elements[i] = elements[i + 1];
371 elements[new_index] = elt;
372 return true;
377 return false;
380 static void
381 gl_array_free (gl_oset_t set)
383 if (set->elements != NULL)
385 if (set->base.dispose_fn != NULL)
387 size_t count = set->count;
389 if (count > 0)
391 gl_setelement_dispose_fn dispose = set->base.dispose_fn;
392 const void **elements = set->elements;
395 dispose (*elements++);
396 while (--count > 0);
399 free (set->elements);
401 free (set);
404 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
406 static gl_oset_iterator_t
407 gl_array_iterator (gl_oset_t set)
409 gl_oset_iterator_t result;
411 result.vtable = set->base.vtable;
412 result.set = set;
413 result.count = set->count;
414 result.p = set->elements + 0;
415 result.q = set->elements + set->count;
416 #if defined GCC_LINT || defined lint
417 result.i = 0;
418 result.j = 0;
419 #endif
421 return result;
424 static bool
425 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
427 gl_oset_t set = iterator->set;
428 if (iterator->count != set->count)
430 if (iterator->count != set->count + 1)
431 /* Concurrent modifications were done on the set. */
432 abort ();
433 /* The last returned element was removed. */
434 iterator->count--;
435 iterator->p = (const void **) iterator->p - 1;
436 iterator->q = (const void **) iterator->q - 1;
438 if (iterator->p < iterator->q)
440 const void **p = (const void **) iterator->p;
441 *eltp = *p;
442 iterator->p = p + 1;
443 return true;
445 else
446 return false;
449 static void
450 gl_array_iterator_free (gl_oset_iterator_t *iterator)
455 const struct gl_oset_implementation gl_array_oset_implementation =
457 gl_array_nx_create_empty,
458 gl_array_size,
459 gl_array_search,
460 gl_array_search_atleast,
461 gl_array_nx_add,
462 gl_array_remove,
463 gl_array_update,
464 gl_array_free,
465 gl_array_iterator,
466 gl_array_iterator_next,
467 gl_array_iterator_free