1 /* Sequential list data type implemented by an array.
2 Copyright (C) 2006-2021 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/>. */
21 #include "gl_array_list.h"
28 /* Checked size_t computations. */
31 /* -------------------------- gl_list_t Data Type -------------------------- */
33 /* Concrete gl_list_impl type, valid for this file only. */
36 struct gl_list_impl_base base;
37 /* An array of ALLOCATED elements, of which the first COUNT are used.
38 0 <= COUNT <= ALLOCATED. */
39 const void **elements;
44 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
46 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
47 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
50 gl_array_nx_create_empty (gl_list_implementation_t implementation,
51 gl_listelement_equals_fn equals_fn,
52 gl_listelement_hashcode_fn hashcode_fn,
53 gl_listelement_dispose_fn dispose_fn,
54 bool allow_duplicates)
56 struct gl_list_impl *list =
57 (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
62 list->base.vtable = implementation;
63 list->base.equals_fn = equals_fn;
64 list->base.hashcode_fn = hashcode_fn;
65 list->base.dispose_fn = dispose_fn;
66 list->base.allow_duplicates = allow_duplicates;
67 list->elements = NULL;
75 gl_array_nx_create (gl_list_implementation_t implementation,
76 gl_listelement_equals_fn equals_fn,
77 gl_listelement_hashcode_fn hashcode_fn,
78 gl_listelement_dispose_fn dispose_fn,
79 bool allow_duplicates,
80 size_t count, const void **contents)
82 struct gl_list_impl *list =
83 (struct gl_list_impl *) malloc (sizeof (struct gl_list_impl));
88 list->base.vtable = implementation;
89 list->base.equals_fn = equals_fn;
90 list->base.hashcode_fn = hashcode_fn;
91 list->base.dispose_fn = dispose_fn;
92 list->base.allow_duplicates = allow_duplicates;
95 if (size_overflow_p (xtimes (count, sizeof (const void *))))
97 list->elements = (const void **) malloc (count * sizeof (const void *));
98 if (list->elements == NULL)
100 memcpy (list->elements, contents, count * sizeof (const void *));
103 list->elements = NULL;
105 list->allocated = count;
114 static size_t _GL_ATTRIBUTE_PURE
115 gl_array_size (gl_list_t list)
120 static const void * _GL_ATTRIBUTE_PURE
121 gl_array_node_value (gl_list_t list, gl_list_node_t node)
123 uintptr_t index = NODE_TO_INDEX (node);
124 if (!(index < list->count))
125 /* Invalid argument. */
127 return list->elements[index];
131 gl_array_node_nx_set_value (gl_list_t list, gl_list_node_t node,
134 uintptr_t index = NODE_TO_INDEX (node);
135 if (!(index < list->count))
136 /* Invalid argument. */
138 list->elements[index] = elt;
142 static gl_list_node_t _GL_ATTRIBUTE_PURE
143 gl_array_next_node (gl_list_t list, gl_list_node_t node)
145 uintptr_t index = NODE_TO_INDEX (node);
146 if (!(index < list->count))
147 /* Invalid argument. */
150 if (index < list->count)
151 return INDEX_TO_NODE (index);
156 static gl_list_node_t _GL_ATTRIBUTE_PURE
157 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
159 uintptr_t index = NODE_TO_INDEX (node);
160 if (!(index < list->count))
161 /* Invalid argument. */
164 return INDEX_TO_NODE (index - 1);
169 static gl_list_node_t _GL_ATTRIBUTE_PURE
170 gl_array_first_node (gl_list_t list)
173 return INDEX_TO_NODE (0);
178 static gl_list_node_t _GL_ATTRIBUTE_PURE
179 gl_array_last_node (gl_list_t list)
182 return INDEX_TO_NODE (list->count - 1);
187 static const void * _GL_ATTRIBUTE_PURE
188 gl_array_get_at (gl_list_t list, size_t position)
190 size_t count = list->count;
192 if (!(position < count))
193 /* Invalid argument. */
195 return list->elements[position];
198 static gl_list_node_t
199 gl_array_nx_set_at (gl_list_t list, size_t position, const void *elt)
201 size_t count = list->count;
203 if (!(position < count))
204 /* Invalid argument. */
206 list->elements[position] = elt;
207 return INDEX_TO_NODE (position);
210 static size_t _GL_ATTRIBUTE_PURE
211 gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
214 size_t count = list->count;
216 if (!(start_index <= end_index && end_index <= count))
217 /* Invalid arguments. */
220 if (start_index < end_index)
222 gl_listelement_equals_fn equals = list->base.equals_fn;
227 for (i = start_index;;)
229 if (equals (elt, list->elements[i]))
240 for (i = start_index;;)
242 if (elt == list->elements[i])
253 static gl_list_node_t _GL_ATTRIBUTE_PURE
254 gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
257 size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
258 return INDEX_TO_NODE (index);
261 /* Ensure that list->allocated > list->count.
262 Return 0 upon success, -1 upon out-of-memory. */
264 grow (gl_list_t list)
266 size_t new_allocated;
270 new_allocated = xtimes (list->allocated, 2);
271 new_allocated = xsum (new_allocated, 1);
272 memory_size = xtimes (new_allocated, sizeof (const void *));
273 if (size_overflow_p (memory_size))
274 /* Overflow, would lead to out of memory. */
276 memory = (const void **) realloc (list->elements, memory_size);
280 list->elements = memory;
281 list->allocated = new_allocated;
285 static gl_list_node_t
286 gl_array_nx_add_first (gl_list_t list, const void *elt)
288 size_t count = list->count;
289 const void **elements;
292 if (count == list->allocated)
295 elements = list->elements;
296 for (i = count; i > 0; i--)
297 elements[i] = elements[i - 1];
299 list->count = count + 1;
300 return INDEX_TO_NODE (0);
303 static gl_list_node_t
304 gl_array_nx_add_last (gl_list_t list, const void *elt)
306 size_t count = list->count;
308 if (count == list->allocated)
311 list->elements[count] = elt;
312 list->count = count + 1;
313 return INDEX_TO_NODE (count);
316 static gl_list_node_t
317 gl_array_nx_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
319 size_t count = list->count;
320 uintptr_t index = NODE_TO_INDEX (node);
322 const void **elements;
325 if (!(index < count))
326 /* Invalid argument. */
329 if (count == list->allocated)
332 elements = list->elements;
333 for (i = count; i > position; i--)
334 elements[i] = elements[i - 1];
335 elements[position] = elt;
336 list->count = count + 1;
337 return INDEX_TO_NODE (position);
340 static gl_list_node_t
341 gl_array_nx_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
343 size_t count = list->count;
344 uintptr_t index = NODE_TO_INDEX (node);
346 const void **elements;
349 if (!(index < count))
350 /* Invalid argument. */
352 position = index + 1;
353 if (count == list->allocated)
356 elements = list->elements;
357 for (i = count; i > position; i--)
358 elements[i] = elements[i - 1];
359 elements[position] = elt;
360 list->count = count + 1;
361 return INDEX_TO_NODE (position);
364 static gl_list_node_t
365 gl_array_nx_add_at (gl_list_t list, size_t position, const void *elt)
367 size_t count = list->count;
368 const void **elements;
371 if (!(position <= count))
372 /* Invalid argument. */
374 if (count == list->allocated)
377 elements = list->elements;
378 for (i = count; i > position; i--)
379 elements[i] = elements[i - 1];
380 elements[position] = elt;
381 list->count = count + 1;
382 return INDEX_TO_NODE (position);
386 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
388 size_t count = list->count;
389 uintptr_t index = NODE_TO_INDEX (node);
391 const void **elements;
394 if (!(index < count))
395 /* Invalid argument. */
398 elements = list->elements;
399 if (list->base.dispose_fn != NULL)
400 list->base.dispose_fn (elements[position]);
401 for (i = position + 1; i < count; i++)
402 elements[i - 1] = elements[i];
403 list->count = count - 1;
408 gl_array_remove_at (gl_list_t list, size_t position)
410 size_t count = list->count;
411 const void **elements;
414 if (!(position < count))
415 /* Invalid argument. */
417 elements = list->elements;
418 if (list->base.dispose_fn != NULL)
419 list->base.dispose_fn (elements[position]);
420 for (i = position + 1; i < count; i++)
421 elements[i - 1] = elements[i];
422 list->count = count - 1;
427 gl_array_remove (gl_list_t list, const void *elt)
429 size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
430 if (position == (size_t)(-1))
433 return gl_array_remove_at (list, position);
437 gl_array_list_free (gl_list_t list)
439 if (list->elements != NULL)
441 if (list->base.dispose_fn != NULL)
443 size_t count = list->count;
447 gl_listelement_dispose_fn dispose = list->base.dispose_fn;
448 const void **elements = list->elements;
451 dispose (*elements++);
455 free (list->elements);
460 /* --------------------- gl_list_iterator_t Data Type --------------------- */
462 static gl_list_iterator_t _GL_ATTRIBUTE_PURE
463 gl_array_iterator (gl_list_t list)
465 gl_list_iterator_t result;
467 result.vtable = list->base.vtable;
469 result.count = list->count;
470 result.p = list->elements + 0;
471 result.q = list->elements + list->count;
472 #if defined GCC_LINT || defined lint
480 static gl_list_iterator_t _GL_ATTRIBUTE_PURE
481 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
483 gl_list_iterator_t result;
485 if (!(start_index <= end_index && end_index <= list->count))
486 /* Invalid arguments. */
488 result.vtable = list->base.vtable;
490 result.count = list->count;
491 result.p = list->elements + start_index;
492 result.q = list->elements + end_index;
493 #if defined GCC_LINT || defined lint
502 gl_array_iterator_next (gl_list_iterator_t *iterator,
503 const void **eltp, gl_list_node_t *nodep)
505 gl_list_t list = iterator->list;
506 if (iterator->count != list->count)
508 if (iterator->count != list->count + 1)
509 /* Concurrent modifications were done on the list. */
511 /* The last returned element was removed. */
513 iterator->p = (const void **) iterator->p - 1;
514 iterator->q = (const void **) iterator->q - 1;
516 if (iterator->p < iterator->q)
518 const void **p = (const void **) iterator->p;
521 *nodep = INDEX_TO_NODE (p - list->elements);
530 gl_array_iterator_free (gl_list_iterator_t *iterator _GL_ATTRIBUTE_MAYBE_UNUSED)
534 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
536 static size_t _GL_ATTRIBUTE_PURE
537 gl_array_sortedlist_indexof_from_to (gl_list_t list,
538 gl_listelement_compar_fn compar,
539 size_t low, size_t high,
542 if (!(low <= high && high <= list->count))
543 /* Invalid arguments. */
547 /* At each loop iteration, low < high; for indices < low the values
548 are smaller than ELT; for indices >= high the values are greater
549 than ELT. So, if the element occurs in the list, it is at
550 low <= position < high. */
553 size_t mid = low + (high - low) / 2; /* low <= mid < high */
554 int cmp = compar (list->elements[mid], elt);
562 /* We have an element equal to ELT at index MID. But we need
563 the minimal such index. */
565 /* At each loop iteration, low <= high and
566 compar (list->elements[high], elt) == 0,
567 and we know that the first occurrence of the element is at
568 low <= position <= high. */
571 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
572 int cmp2 = compar (list->elements[mid2], elt);
577 /* The list was not sorted. */
590 /* Here low == high. */
595 static size_t _GL_ATTRIBUTE_PURE
596 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
599 return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
603 static gl_list_node_t _GL_ATTRIBUTE_PURE
604 gl_array_sortedlist_search_from_to (gl_list_t list,
605 gl_listelement_compar_fn compar,
606 size_t low, size_t high,
610 gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
611 return INDEX_TO_NODE (index);
614 static gl_list_node_t _GL_ATTRIBUTE_PURE
615 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
619 gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
620 return INDEX_TO_NODE (index);
623 static gl_list_node_t
624 gl_array_sortedlist_nx_add (gl_list_t list, gl_listelement_compar_fn compar,
627 size_t count = list->count;
631 /* At each loop iteration, low <= high; for indices < low the values are
632 smaller than ELT; for indices >= high the values are greater than ELT. */
635 size_t mid = low + (high - low) / 2; /* low <= mid < high */
636 int cmp = compar (list->elements[mid], elt);
648 return gl_array_nx_add_at (list, low, elt);
652 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
655 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
656 if (index == (size_t)(-1))
659 return gl_array_remove_at (list, index);
663 const struct gl_list_implementation gl_array_list_implementation =
665 gl_array_nx_create_empty,
669 gl_array_node_nx_set_value,
671 gl_array_previous_node,
676 gl_array_search_from_to,
677 gl_array_indexof_from_to,
678 gl_array_nx_add_first,
679 gl_array_nx_add_last,
680 gl_array_nx_add_before,
681 gl_array_nx_add_after,
683 gl_array_remove_node,
688 gl_array_iterator_from_to,
689 gl_array_iterator_next,
690 gl_array_iterator_free,
691 gl_array_sortedlist_search,
692 gl_array_sortedlist_search_from_to,
693 gl_array_sortedlist_indexof,
694 gl_array_sortedlist_indexof_from_to,
695 gl_array_sortedlist_nx_add,
696 gl_array_sortedlist_remove