1 /* Sequential list data type implemented by an array.
2 Copyright (C) 2006-2008 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 <http://www.gnu.org/licenses/>. */
21 #include "gl_array_list.h"
29 /* Checked size_t computations. */
33 # define uintptr_t unsigned long
36 /* -------------------------- gl_list_t Data Type -------------------------- */
38 /* Concrete gl_list_impl type, valid for this file only. */
41 struct gl_list_impl_base base;
42 /* An array of ALLOCATED elements, of which the first COUNT are used.
43 0 <= COUNT <= ALLOCATED. */
44 const void **elements;
49 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
51 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
52 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
55 gl_array_create_empty (gl_list_implementation_t implementation,
56 gl_listelement_equals_fn equals_fn,
57 gl_listelement_hashcode_fn hashcode_fn,
58 gl_listelement_dispose_fn dispose_fn,
59 bool allow_duplicates)
61 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
63 list->base.vtable = implementation;
64 list->base.equals_fn = equals_fn;
65 list->base.hashcode_fn = hashcode_fn;
66 list->base.dispose_fn = dispose_fn;
67 list->base.allow_duplicates = allow_duplicates;
68 list->elements = NULL;
76 gl_array_create (gl_list_implementation_t implementation,
77 gl_listelement_equals_fn equals_fn,
78 gl_listelement_hashcode_fn hashcode_fn,
79 gl_listelement_dispose_fn dispose_fn,
80 bool allow_duplicates,
81 size_t count, const void **contents)
83 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
85 list->base.vtable = implementation;
86 list->base.equals_fn = equals_fn;
87 list->base.hashcode_fn = hashcode_fn;
88 list->base.dispose_fn = dispose_fn;
89 list->base.allow_duplicates = allow_duplicates;
92 list->elements = XNMALLOC (count, const void *);
93 memcpy (list->elements, contents, count * sizeof (const void *));
96 list->elements = NULL;
98 list->allocated = count;
104 gl_array_size (gl_list_t list)
110 gl_array_node_value (gl_list_t list, gl_list_node_t node)
112 uintptr_t index = NODE_TO_INDEX (node);
113 if (!(index < list->count))
114 /* Invalid argument. */
116 return list->elements[index];
120 gl_array_node_set_value (gl_list_t list, gl_list_node_t node, const void *elt)
122 uintptr_t index = NODE_TO_INDEX (node);
123 if (!(index < list->count))
124 /* Invalid argument. */
126 list->elements[index] = elt;
129 static gl_list_node_t
130 gl_array_next_node (gl_list_t list, gl_list_node_t node)
132 uintptr_t index = NODE_TO_INDEX (node);
133 if (!(index < list->count))
134 /* Invalid argument. */
137 if (index < list->count)
138 return INDEX_TO_NODE (index);
143 static gl_list_node_t
144 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
146 uintptr_t index = NODE_TO_INDEX (node);
147 if (!(index < list->count))
148 /* Invalid argument. */
151 return INDEX_TO_NODE (index - 1);
157 gl_array_get_at (gl_list_t list, size_t position)
159 size_t count = list->count;
161 if (!(position < count))
162 /* Invalid argument. */
164 return list->elements[position];
167 static gl_list_node_t
168 gl_array_set_at (gl_list_t list, size_t position, const void *elt)
170 size_t count = list->count;
172 if (!(position < count))
173 /* Invalid argument. */
175 list->elements[position] = elt;
176 return INDEX_TO_NODE (position);
180 gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
183 size_t count = list->count;
185 if (!(start_index <= end_index && end_index <= count))
186 /* Invalid arguments. */
189 if (start_index < end_index)
191 gl_listelement_equals_fn equals = list->base.equals_fn;
196 for (i = start_index;;)
198 if (equals (elt, list->elements[i]))
209 for (i = start_index;;)
211 if (elt == list->elements[i])
222 static gl_list_node_t
223 gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
226 size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
227 return INDEX_TO_NODE (index);
230 /* Ensure that list->allocated > list->count. */
232 grow (gl_list_t list)
234 size_t new_allocated;
238 new_allocated = xtimes (list->allocated, 2);
239 new_allocated = xsum (new_allocated, 1);
240 memory_size = xtimes (new_allocated, sizeof (const void *));
241 if (size_overflow_p (memory_size))
242 /* Overflow, would lead to out of memory. */
244 memory = (const void **) xrealloc (list->elements, memory_size);
248 list->elements = memory;
249 list->allocated = new_allocated;
252 static gl_list_node_t
253 gl_array_add_first (gl_list_t list, const void *elt)
255 size_t count = list->count;
256 const void **elements;
259 if (count == list->allocated)
261 elements = list->elements;
262 for (i = count; i > 0; i--)
263 elements[i] = elements[i - 1];
265 list->count = count + 1;
266 return INDEX_TO_NODE (0);
269 static gl_list_node_t
270 gl_array_add_last (gl_list_t list, const void *elt)
272 size_t count = list->count;
274 if (count == list->allocated)
276 list->elements[count] = elt;
277 list->count = count + 1;
278 return INDEX_TO_NODE (count);
281 static gl_list_node_t
282 gl_array_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
284 size_t count = list->count;
285 uintptr_t index = NODE_TO_INDEX (node);
287 const void **elements;
290 if (!(index < count))
291 /* Invalid argument. */
294 if (count == list->allocated)
296 elements = list->elements;
297 for (i = count; i > position; i--)
298 elements[i] = elements[i - 1];
299 elements[position] = elt;
300 list->count = count + 1;
301 return INDEX_TO_NODE (position);
304 static gl_list_node_t
305 gl_array_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
307 size_t count = list->count;
308 uintptr_t index = NODE_TO_INDEX (node);
310 const void **elements;
313 if (!(index < count))
314 /* Invalid argument. */
316 position = index + 1;
317 if (count == list->allocated)
319 elements = list->elements;
320 for (i = count; i > position; i--)
321 elements[i] = elements[i - 1];
322 elements[position] = elt;
323 list->count = count + 1;
324 return INDEX_TO_NODE (position);
327 static gl_list_node_t
328 gl_array_add_at (gl_list_t list, size_t position, const void *elt)
330 size_t count = list->count;
331 const void **elements;
334 if (!(position <= count))
335 /* Invalid argument. */
337 if (count == list->allocated)
339 elements = list->elements;
340 for (i = count; i > position; i--)
341 elements[i] = elements[i - 1];
342 elements[position] = elt;
343 list->count = count + 1;
344 return INDEX_TO_NODE (position);
348 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
350 size_t count = list->count;
351 uintptr_t index = NODE_TO_INDEX (node);
353 const void **elements;
356 if (!(index < count))
357 /* Invalid argument. */
360 elements = list->elements;
361 if (list->base.dispose_fn != NULL)
362 list->base.dispose_fn (elements[position]);
363 for (i = position + 1; i < count; i++)
364 elements[i - 1] = elements[i];
365 list->count = count - 1;
370 gl_array_remove_at (gl_list_t list, size_t position)
372 size_t count = list->count;
373 const void **elements;
376 if (!(position < count))
377 /* Invalid argument. */
379 elements = list->elements;
380 if (list->base.dispose_fn != NULL)
381 list->base.dispose_fn (elements[position]);
382 for (i = position + 1; i < count; i++)
383 elements[i - 1] = elements[i];
384 list->count = count - 1;
389 gl_array_remove (gl_list_t list, const void *elt)
391 size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
392 if (position == (size_t)(-1))
395 return gl_array_remove_at (list, position);
399 gl_array_list_free (gl_list_t list)
401 if (list->elements != NULL)
403 if (list->base.dispose_fn != NULL)
405 size_t count = list->count;
409 gl_listelement_dispose_fn dispose = list->base.dispose_fn;
410 const void **elements = list->elements;
413 dispose (*elements++);
417 free (list->elements);
422 /* --------------------- gl_list_iterator_t Data Type --------------------- */
424 static gl_list_iterator_t
425 gl_array_iterator (gl_list_t list)
427 gl_list_iterator_t result;
429 result.vtable = list->base.vtable;
431 result.count = list->count;
432 result.p = list->elements + 0;
433 result.q = list->elements + list->count;
442 static gl_list_iterator_t
443 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
445 gl_list_iterator_t result;
447 if (!(start_index <= end_index && end_index <= list->count))
448 /* Invalid arguments. */
450 result.vtable = list->base.vtable;
452 result.count = list->count;
453 result.p = list->elements + start_index;
454 result.q = list->elements + end_index;
464 gl_array_iterator_next (gl_list_iterator_t *iterator,
465 const void **eltp, gl_list_node_t *nodep)
467 gl_list_t list = iterator->list;
468 if (iterator->count != list->count)
470 if (iterator->count != list->count + 1)
471 /* Concurrent modifications were done on the list. */
473 /* The last returned element was removed. */
475 iterator->p = (const void **) iterator->p - 1;
476 iterator->q = (const void **) iterator->q - 1;
478 if (iterator->p < iterator->q)
480 const void **p = (const void **) iterator->p;
483 *nodep = INDEX_TO_NODE (p - list->elements);
492 gl_array_iterator_free (gl_list_iterator_t *iterator)
496 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
499 gl_array_sortedlist_indexof_from_to (gl_list_t list,
500 gl_listelement_compar_fn compar,
501 size_t low, size_t high,
504 if (!(low <= high && high <= list->count))
505 /* Invalid arguments. */
509 /* At each loop iteration, low < high; for indices < low the values
510 are smaller than ELT; for indices >= high the values are greater
511 than ELT. So, if the element occurs in the list, it is at
512 low <= position < high. */
515 size_t mid = low + (high - low) / 2; /* low <= mid < high */
516 int cmp = compar (list->elements[mid], elt);
524 /* We have an element equal to ELT at index MID. But we need
525 the minimal such index. */
527 /* At each loop iteration, low <= high and
528 compar (list->elements[high], elt) == 0,
529 and we know that the first occurrence of the element is at
530 low <= position <= high. */
533 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
534 int cmp2 = compar (list->elements[mid2], elt);
539 /* The list was not sorted. */
552 /* Here low == high. */
558 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
561 return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
565 static gl_list_node_t
566 gl_array_sortedlist_search_from_to (gl_list_t list,
567 gl_listelement_compar_fn compar,
568 size_t low, size_t high,
572 gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
573 return INDEX_TO_NODE (index);
576 static gl_list_node_t
577 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
581 gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
582 return INDEX_TO_NODE (index);
585 static gl_list_node_t
586 gl_array_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
589 size_t count = list->count;
593 /* At each loop iteration, low <= high; for indices < low the values are
594 smaller than ELT; for indices >= high the values are greater than ELT. */
597 size_t mid = low + (high - low) / 2; /* low <= mid < high */
598 int cmp = compar (list->elements[mid], elt);
610 return gl_array_add_at (list, low, elt);
614 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
617 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
618 if (index == (size_t)(-1))
621 return gl_array_remove_at (list, index);
625 const struct gl_list_implementation gl_array_list_implementation =
627 gl_array_create_empty,
631 gl_array_node_set_value,
633 gl_array_previous_node,
636 gl_array_search_from_to,
637 gl_array_indexof_from_to,
643 gl_array_remove_node,
648 gl_array_iterator_from_to,
649 gl_array_iterator_next,
650 gl_array_iterator_free,
651 gl_array_sortedlist_search,
652 gl_array_sortedlist_search_from_to,
653 gl_array_sortedlist_indexof,
654 gl_array_sortedlist_indexof_from_to,
655 gl_array_sortedlist_add,
656 gl_array_sortedlist_remove