1 /* Sequential list data type implemented by a circular array.
2 Copyright (C) 2006 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 2, or (at your option)
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, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 #include "gl_carray_list.h"
30 /* Checked size_t computations. */
34 # define uintptr_t unsigned long
37 /* -------------------------- gl_list_t Data Type -------------------------- */
39 /* Concrete gl_list_impl type, valid for this file only. */
42 struct gl_list_impl_base base;
43 /* An array of ALLOCATED elements, of which the elements
44 OFFSET, (OFFSET + 1) % ALLOCATED, ..., (OFFSET + COUNT - 1) % ALLOCATED
45 are used. 0 <= COUNT <= ALLOCATED. Either OFFSET = ALLOCATED = 0 or
46 0 <= OFFSET < ALLOCATED. */
47 const void **elements;
53 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
55 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
56 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
59 gl_carray_create_empty (gl_list_implementation_t implementation,
60 gl_listelement_equals_fn equals_fn,
61 gl_listelement_hashcode_fn hashcode_fn,
62 bool allow_duplicates)
64 struct gl_list_impl *list =
65 (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
67 list->base.vtable = implementation;
68 list->base.equals_fn = equals_fn;
69 list->base.hashcode_fn = hashcode_fn;
70 list->base.allow_duplicates = allow_duplicates;
71 list->elements = NULL;
80 gl_carray_create (gl_list_implementation_t implementation,
81 gl_listelement_equals_fn equals_fn,
82 gl_listelement_hashcode_fn hashcode_fn,
83 bool allow_duplicates,
84 size_t count, const void **contents)
86 struct gl_list_impl *list =
87 (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
89 list->base.vtable = implementation;
90 list->base.equals_fn = equals_fn;
91 list->base.hashcode_fn = hashcode_fn;
92 list->base.allow_duplicates = allow_duplicates;
96 (const void **) xmalloc (count * sizeof (const void *));
97 memcpy (list->elements, contents, count * sizeof (const void *));
100 list->elements = NULL;
103 list->allocated = count;
109 gl_carray_size (gl_list_t list)
115 gl_carray_node_value (gl_list_t list, gl_list_node_t node)
117 uintptr_t index = NODE_TO_INDEX (node);
120 if (!(index < list->count))
121 /* Invalid argument. */
123 i = list->offset + index;
124 if (i >= list->allocated)
125 i -= list->allocated;
126 return list->elements[i];
129 static gl_list_node_t
130 gl_carray_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_carray_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_carray_get_at (gl_list_t list, size_t position)
159 size_t count = list->count;
162 if (!(position < count))
163 /* Invalid argument. */
165 i = list->offset + position;
166 if (i >= list->allocated)
167 i -= list->allocated;
168 return list->elements[i];
171 static gl_list_node_t
172 gl_carray_set_at (gl_list_t list, size_t position, const void *elt)
174 size_t count = list->count;
177 if (!(position < count))
178 /* Invalid argument. */
180 i = list->offset + position;
181 if (i >= list->allocated)
182 i -= list->allocated;
183 list->elements[i] = elt;
184 return INDEX_TO_NODE (position);
188 gl_carray_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
191 size_t count = list->count;
193 if (!(start_index <= end_index && end_index <= count))
194 /* Invalid arguments. */
197 if (start_index < end_index)
199 gl_listelement_equals_fn equals = list->base.equals_fn;
200 size_t allocated = list->allocated;
203 i_end = list->offset + end_index;
204 if (i_end >= allocated)
211 i = list->offset + start_index;
212 if (i >= allocated) /* can only happen if start_index > 0 */
217 if (equals (elt, list->elements[i]))
218 return (i >= list->offset ? i : i + allocated) - list->offset;
230 i = list->offset + start_index;
231 if (i >= allocated) /* can only happen if start_index > 0 */
236 if (elt == list->elements[i])
237 return (i >= list->offset ? i : i + allocated) - list->offset;
249 static gl_list_node_t
250 gl_carray_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
253 size_t index = gl_carray_indexof_from_to (list, start_index, end_index, elt);
254 return INDEX_TO_NODE (index);
257 /* Ensure that list->allocated > list->count. */
259 grow (gl_list_t list)
261 size_t new_allocated;
265 new_allocated = xtimes (list->allocated, 2);
266 new_allocated = xsum (new_allocated, 1);
267 memory_size = xtimes (new_allocated, sizeof (const void *));
268 if (size_overflow_p (memory_size))
269 /* Overflow, would lead to out of memory. */
271 if (list->offset > 0 && list->count > 0)
273 memory = (const void **) xmalloc (memory_size);
277 if (list->offset + list->count > list->allocated)
279 memcpy (memory, &list->elements[list->offset],
280 (list->allocated - list->offset) * sizeof (const void *));
281 memcpy (memory + (list->allocated - list->offset), list->elements,
282 (list->offset + list->count - list->allocated)
283 * sizeof (const void *));
287 memcpy (memory, &list->elements[list->offset],
288 list->count * sizeof (const void *));
289 if (list->elements != NULL)
290 free (list->elements);
294 memory = (const void **) xrealloc (list->elements, memory_size);
299 list->elements = memory;
301 list->allocated = new_allocated;
304 static gl_list_node_t
305 gl_carray_add_first (gl_list_t list, const void *elt)
307 size_t count = list->count;
309 if (count == list->allocated)
311 list->offset = (list->offset == 0 ? list->allocated : list->offset) - 1;
312 list->elements[list->offset] = elt;
313 list->count = count + 1;
314 return INDEX_TO_NODE (0);
317 static gl_list_node_t
318 gl_carray_add_last (gl_list_t list, const void *elt)
320 size_t count = list->count;
323 if (count == list->allocated)
325 i = list->offset + count;
326 if (i >= list->allocated)
327 i -= list->allocated;
328 list->elements[i] = elt;
329 list->count = count + 1;
330 return INDEX_TO_NODE (count);
333 static gl_list_node_t
334 gl_carray_add_at (gl_list_t list, size_t position, const void *elt)
336 size_t count = list->count;
337 const void **elements;
339 if (!(position <= count))
340 /* Invalid argument. */
342 if (count == list->allocated)
344 elements = list->elements;
345 if (position <= (count / 2))
347 /* Shift at most count/2 elements to the left. */
350 list->offset = (list->offset == 0 ? list->allocated : list->offset) - 1;
352 i2 = list->offset + position;
353 if (i2 >= list->allocated)
355 /* Here we must have list->offset > 0, hence list->allocated > 0. */
356 size_t i1 = list->allocated - 1;
357 i2 -= list->allocated;
358 for (i = list->offset; i < i1; i++)
359 elements[i] = elements[i + 1];
360 elements[i1] = elements[0];
361 for (i = 0; i < i2; i++)
362 elements[i] = elements[i + 1];
366 for (i = list->offset; i < i2; i++)
367 elements[i] = elements[i + 1];
373 /* Shift at most (count+1)/2 elements to the right. */
376 i1 = list->offset + position;
377 i3 = list->offset + count;
378 if (i1 >= list->allocated)
380 i1 -= list->allocated;
381 i3 -= list->allocated;
382 for (i = i3; i > i1; i--)
383 elements[i] = elements[i - 1];
385 else if (i3 >= list->allocated)
387 /* Here we must have list->offset > 0, hence list->allocated > 0. */
388 size_t i2 = list->allocated - 1;
389 i3 -= list->allocated;
390 for (i = i3; i > 0; i--)
391 elements[i] = elements[i - 1];
392 elements[0] = elements[i2];
393 for (i = i2; i > i1; i--)
394 elements[i] = elements[i - 1];
398 for (i = i3; i > i1; i--)
399 elements[i] = elements[i - 1];
403 list->count = count + 1;
404 return INDEX_TO_NODE (position);
407 static gl_list_node_t
408 gl_carray_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
410 size_t count = list->count;
411 uintptr_t index = NODE_TO_INDEX (node);
413 if (!(index < count))
414 /* Invalid argument. */
416 return gl_carray_add_at (list, index, elt);
419 static gl_list_node_t
420 gl_carray_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
422 size_t count = list->count;
423 uintptr_t index = NODE_TO_INDEX (node);
425 if (!(index < count))
426 /* Invalid argument. */
428 return gl_carray_add_at (list, index + 1, elt);
432 gl_carray_remove_at (gl_list_t list, size_t position)
434 size_t count = list->count;
435 const void **elements;
437 if (!(position < count))
438 /* Invalid argument. */
440 /* Here we know count > 0. */
441 elements = list->elements;
442 if (position <= ((count - 1) / 2))
444 /* Shift at most (count-1)/2 elements to the right. */
448 i2 = list->offset + position;
449 if (i2 >= list->allocated)
451 /* Here we must have list->offset > 0, hence list->allocated > 0. */
452 size_t i1 = list->allocated - 1;
453 i2 -= list->allocated;
454 for (i = i2; i > 0; i--)
455 elements[i] = elements[i - 1];
456 elements[0] = elements[i1];
457 for (i = i1; i > i0; i--)
458 elements[i] = elements[i - 1];
462 for (i = i2; i > i0; i--)
463 elements[i] = elements[i - 1];
467 list->offset = (i0 == list->allocated ? 0 : i0);
471 /* Shift at most count/2 elements to the left. */
474 i1 = list->offset + position;
475 i3 = list->offset + count - 1;
476 if (i1 >= list->allocated)
478 i1 -= list->allocated;
479 i3 -= list->allocated;
480 for (i = i1; i < i3; i++)
481 elements[i] = elements[i + 1];
483 else if (i3 >= list->allocated)
485 /* Here we must have list->offset > 0, hence list->allocated > 0. */
486 size_t i2 = list->allocated - 1;
487 i3 -= list->allocated;
488 for (i = i1; i < i2; i++)
489 elements[i] = elements[i + 1];
490 elements[i2] = elements[0];
491 for (i = 0; i < i3; i++)
492 elements[i] = elements[i + 1];
496 for (i = i1; i < i3; i++)
497 elements[i] = elements[i + 1];
500 list->count = count - 1;
505 gl_carray_remove_node (gl_list_t list, gl_list_node_t node)
507 size_t count = list->count;
508 uintptr_t index = NODE_TO_INDEX (node);
510 if (!(index < count))
511 /* Invalid argument. */
513 return gl_carray_remove_at (list, index);
517 gl_carray_remove (gl_list_t list, const void *elt)
519 size_t position = gl_carray_indexof_from_to (list, 0, list->count, elt);
520 if (position == (size_t)(-1))
523 return gl_carray_remove_at (list, position);
527 gl_carray_list_free (gl_list_t list)
529 if (list->elements != NULL)
530 free (list->elements);
534 /* --------------------- gl_list_iterator_t Data Type --------------------- */
536 static gl_list_iterator_t
537 gl_carray_iterator (gl_list_t list)
539 gl_list_iterator_t result;
541 result.vtable = list->base.vtable;
543 result.count = list->count;
545 result.j = list->count;
554 static gl_list_iterator_t
555 gl_carray_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
557 gl_list_iterator_t result;
559 if (!(start_index <= end_index && end_index <= list->count))
560 /* Invalid arguments. */
562 result.vtable = list->base.vtable;
564 result.count = list->count;
565 result.i = start_index;
566 result.j = end_index;
576 gl_carray_iterator_next (gl_list_iterator_t *iterator,
577 const void **eltp, gl_list_node_t *nodep)
579 gl_list_t list = iterator->list;
580 if (iterator->count != list->count)
582 if (iterator->count != list->count + 1)
583 /* Concurrent modifications were done on the list. */
585 /* The last returned element was removed. */
590 if (iterator->i < iterator->j)
592 size_t i = list->offset + iterator->i;
593 if (i >= list->allocated)
594 i -= list->allocated;
595 *eltp = list->elements[i];
597 *nodep = INDEX_TO_NODE (iterator->i);
606 gl_carray_iterator_free (gl_list_iterator_t *iterator)
610 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
613 gl_carray_sortedlist_indexof_from_to (gl_list_t list,
614 gl_listelement_compar_fn compar,
615 size_t low, size_t high,
618 if (!(low <= high && high <= list->count))
619 /* Invalid arguments. */
623 /* At each loop iteration, low < high; for indices < low the values
624 are smaller than ELT; for indices >= high the values are greater
625 than ELT. So, if the element occurs in the list, it is at
626 low <= position < high. */
629 size_t mid = low + (high - low) / 2; /* low <= mid < high */
633 i_mid = list->offset + mid;
634 if (i_mid >= list->allocated)
635 i_mid -= list->allocated;
637 cmp = compar (list->elements[i_mid], elt);
645 /* We have an element equal to ELT at index MID. But we need
646 the minimal such index. */
648 /* At each loop iteration, low <= high and
649 compar (list->elements[i_high], elt) == 0,
650 and we know that the first occurrence of the element is at
651 low <= position <= high. */
654 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
658 i_mid2 = list->offset + mid2;
659 if (i_mid2 >= list->allocated)
660 i_mid2 -= list->allocated;
662 cmp2 = compar (list->elements[i_mid2], elt);
667 /* The list was not sorted. */
680 /* Here low == high. */
686 gl_carray_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
689 return gl_carray_sortedlist_indexof_from_to (list, compar, 0, list->count,
693 static gl_list_node_t
694 gl_carray_sortedlist_search_from_to (gl_list_t list,
695 gl_listelement_compar_fn compar,
696 size_t low, size_t high,
700 gl_carray_sortedlist_indexof_from_to (list, compar, low, high, elt);
701 return INDEX_TO_NODE (index);
704 static gl_list_node_t
705 gl_carray_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
709 gl_carray_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
710 return INDEX_TO_NODE (index);
713 static gl_list_node_t
714 gl_carray_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
717 size_t count = list->count;
721 /* At each loop iteration, low <= high; for indices < low the values are
722 smaller than ELT; for indices >= high the values are greater than ELT. */
725 size_t mid = low + (high - low) / 2; /* low <= mid < high */
729 i_mid = list->offset + mid;
730 if (i_mid >= list->allocated)
731 i_mid -= list->allocated;
733 cmp = compar (list->elements[i_mid], elt);
745 return gl_carray_add_at (list, low, elt);
749 gl_carray_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
752 size_t index = gl_carray_sortedlist_indexof (list, compar, elt);
753 if (index == (size_t)(-1))
756 return gl_carray_remove_at (list, index);
760 const struct gl_list_implementation gl_carray_list_implementation =
762 gl_carray_create_empty,
765 gl_carray_node_value,
767 gl_carray_previous_node,
770 gl_carray_search_from_to,
771 gl_carray_indexof_from_to,
774 gl_carray_add_before,
777 gl_carray_remove_node,
782 gl_carray_iterator_from_to,
783 gl_carray_iterator_next,
784 gl_carray_iterator_free,
785 gl_carray_sortedlist_search,
786 gl_carray_sortedlist_search_from_to,
787 gl_carray_sortedlist_indexof,
788 gl_carray_sortedlist_indexof_from_to,
789 gl_carray_sortedlist_add,
790 gl_carray_sortedlist_remove