1 /* Sequential list data type implemented by an array.
2 Copyright (C) 2006-2007 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_array_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 first COUNT are used.
44 0 <= COUNT <= ALLOCATED. */
45 const void **elements;
50 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
52 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
53 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
56 gl_array_create_empty (gl_list_implementation_t implementation,
57 gl_listelement_equals_fn equals_fn,
58 gl_listelement_hashcode_fn hashcode_fn,
59 gl_listelement_dispose_fn dispose_fn,
60 bool allow_duplicates)
62 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
64 list->base.vtable = implementation;
65 list->base.equals_fn = equals_fn;
66 list->base.hashcode_fn = hashcode_fn;
67 list->base.dispose_fn = dispose_fn;
68 list->base.allow_duplicates = allow_duplicates;
69 list->elements = NULL;
77 gl_array_create (gl_list_implementation_t implementation,
78 gl_listelement_equals_fn equals_fn,
79 gl_listelement_hashcode_fn hashcode_fn,
80 gl_listelement_dispose_fn dispose_fn,
81 bool allow_duplicates,
82 size_t count, const void **contents)
84 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
86 list->base.vtable = implementation;
87 list->base.equals_fn = equals_fn;
88 list->base.hashcode_fn = hashcode_fn;
89 list->base.dispose_fn = dispose_fn;
90 list->base.allow_duplicates = allow_duplicates;
93 list->elements = XNMALLOC (count, const void *);
94 memcpy (list->elements, contents, count * sizeof (const void *));
97 list->elements = NULL;
99 list->allocated = count;
105 gl_array_size (gl_list_t list)
111 gl_array_node_value (gl_list_t list, gl_list_node_t node)
113 uintptr_t index = NODE_TO_INDEX (node);
114 if (!(index < list->count))
115 /* Invalid argument. */
117 return list->elements[index];
120 static gl_list_node_t
121 gl_array_next_node (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. */
128 if (index < list->count)
129 return INDEX_TO_NODE (index);
134 static gl_list_node_t
135 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
137 uintptr_t index = NODE_TO_INDEX (node);
138 if (!(index < list->count))
139 /* Invalid argument. */
142 return INDEX_TO_NODE (index - 1);
148 gl_array_get_at (gl_list_t list, size_t position)
150 size_t count = list->count;
152 if (!(position < count))
153 /* Invalid argument. */
155 return list->elements[position];
158 static gl_list_node_t
159 gl_array_set_at (gl_list_t list, size_t position, const void *elt)
161 size_t count = list->count;
163 if (!(position < count))
164 /* Invalid argument. */
166 list->elements[position] = elt;
167 return INDEX_TO_NODE (position);
171 gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
174 size_t count = list->count;
176 if (!(start_index <= end_index && end_index <= count))
177 /* Invalid arguments. */
180 if (start_index < end_index)
182 gl_listelement_equals_fn equals = list->base.equals_fn;
187 for (i = start_index;;)
189 if (equals (elt, list->elements[i]))
200 for (i = start_index;;)
202 if (elt == list->elements[i])
213 static gl_list_node_t
214 gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
217 size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
218 return INDEX_TO_NODE (index);
221 /* Ensure that list->allocated > list->count. */
223 grow (gl_list_t list)
225 size_t new_allocated;
229 new_allocated = xtimes (list->allocated, 2);
230 new_allocated = xsum (new_allocated, 1);
231 memory_size = xtimes (new_allocated, sizeof (const void *));
232 if (size_overflow_p (memory_size))
233 /* Overflow, would lead to out of memory. */
235 memory = (const void **) xrealloc (list->elements, memory_size);
239 list->elements = memory;
240 list->allocated = new_allocated;
243 static gl_list_node_t
244 gl_array_add_first (gl_list_t list, const void *elt)
246 size_t count = list->count;
247 const void **elements;
250 if (count == list->allocated)
252 elements = list->elements;
253 for (i = count; i > 0; i--)
254 elements[i] = elements[i - 1];
256 list->count = count + 1;
257 return INDEX_TO_NODE (0);
260 static gl_list_node_t
261 gl_array_add_last (gl_list_t list, const void *elt)
263 size_t count = list->count;
265 if (count == list->allocated)
267 list->elements[count] = elt;
268 list->count = count + 1;
269 return INDEX_TO_NODE (count);
272 static gl_list_node_t
273 gl_array_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
275 size_t count = list->count;
276 uintptr_t index = NODE_TO_INDEX (node);
278 const void **elements;
281 if (!(index < count))
282 /* Invalid argument. */
285 if (count == list->allocated)
287 elements = list->elements;
288 for (i = count; i > position; i--)
289 elements[i] = elements[i - 1];
290 elements[position] = elt;
291 list->count = count + 1;
292 return INDEX_TO_NODE (position);
295 static gl_list_node_t
296 gl_array_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
298 size_t count = list->count;
299 uintptr_t index = NODE_TO_INDEX (node);
301 const void **elements;
304 if (!(index < count))
305 /* Invalid argument. */
307 position = index + 1;
308 if (count == list->allocated)
310 elements = list->elements;
311 for (i = count; i > position; i--)
312 elements[i] = elements[i - 1];
313 elements[position] = elt;
314 list->count = count + 1;
315 return INDEX_TO_NODE (position);
318 static gl_list_node_t
319 gl_array_add_at (gl_list_t list, size_t position, const void *elt)
321 size_t count = list->count;
322 const void **elements;
325 if (!(position <= count))
326 /* Invalid argument. */
328 if (count == list->allocated)
330 elements = list->elements;
331 for (i = count; i > position; i--)
332 elements[i] = elements[i - 1];
333 elements[position] = elt;
334 list->count = count + 1;
335 return INDEX_TO_NODE (position);
339 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
341 size_t count = list->count;
342 uintptr_t index = NODE_TO_INDEX (node);
344 const void **elements;
347 if (!(index < count))
348 /* Invalid argument. */
351 elements = list->elements;
352 if (list->base.dispose_fn != NULL)
353 list->base.dispose_fn (elements[position]);
354 for (i = position + 1; i < count; i++)
355 elements[i - 1] = elements[i];
356 list->count = count - 1;
361 gl_array_remove_at (gl_list_t list, size_t position)
363 size_t count = list->count;
364 const void **elements;
367 if (!(position < count))
368 /* Invalid argument. */
370 elements = list->elements;
371 if (list->base.dispose_fn != NULL)
372 list->base.dispose_fn (elements[position]);
373 for (i = position + 1; i < count; i++)
374 elements[i - 1] = elements[i];
375 list->count = count - 1;
380 gl_array_remove (gl_list_t list, const void *elt)
382 size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
383 if (position == (size_t)(-1))
386 return gl_array_remove_at (list, position);
390 gl_array_list_free (gl_list_t list)
392 if (list->elements != NULL)
394 if (list->base.dispose_fn != NULL)
396 size_t count = list->count;
400 gl_listelement_dispose_fn dispose = list->base.dispose_fn;
401 const void **elements = list->elements;
404 dispose (*elements++);
408 free (list->elements);
413 /* --------------------- gl_list_iterator_t Data Type --------------------- */
415 static gl_list_iterator_t
416 gl_array_iterator (gl_list_t list)
418 gl_list_iterator_t result;
420 result.vtable = list->base.vtable;
422 result.count = list->count;
423 result.p = list->elements + 0;
424 result.q = list->elements + list->count;
433 static gl_list_iterator_t
434 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
436 gl_list_iterator_t result;
438 if (!(start_index <= end_index && end_index <= list->count))
439 /* Invalid arguments. */
441 result.vtable = list->base.vtable;
443 result.count = list->count;
444 result.p = list->elements + start_index;
445 result.q = list->elements + end_index;
455 gl_array_iterator_next (gl_list_iterator_t *iterator,
456 const void **eltp, gl_list_node_t *nodep)
458 gl_list_t list = iterator->list;
459 if (iterator->count != list->count)
461 if (iterator->count != list->count + 1)
462 /* Concurrent modifications were done on the list. */
464 /* The last returned element was removed. */
466 iterator->p = (const void **) iterator->p - 1;
467 iterator->q = (const void **) iterator->q - 1;
469 if (iterator->p < iterator->q)
471 const void **p = (const void **) iterator->p;
474 *nodep = INDEX_TO_NODE (p - list->elements);
483 gl_array_iterator_free (gl_list_iterator_t *iterator)
487 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
490 gl_array_sortedlist_indexof_from_to (gl_list_t list,
491 gl_listelement_compar_fn compar,
492 size_t low, size_t high,
495 if (!(low <= high && high <= list->count))
496 /* Invalid arguments. */
500 /* At each loop iteration, low < high; for indices < low the values
501 are smaller than ELT; for indices >= high the values are greater
502 than ELT. So, if the element occurs in the list, it is at
503 low <= position < high. */
506 size_t mid = low + (high - low) / 2; /* low <= mid < high */
507 int cmp = compar (list->elements[mid], elt);
515 /* We have an element equal to ELT at index MID. But we need
516 the minimal such index. */
518 /* At each loop iteration, low <= high and
519 compar (list->elements[high], elt) == 0,
520 and we know that the first occurrence of the element is at
521 low <= position <= high. */
524 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
525 int cmp2 = compar (list->elements[mid2], elt);
530 /* The list was not sorted. */
543 /* Here low == high. */
549 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
552 return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
556 static gl_list_node_t
557 gl_array_sortedlist_search_from_to (gl_list_t list,
558 gl_listelement_compar_fn compar,
559 size_t low, size_t high,
563 gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
564 return INDEX_TO_NODE (index);
567 static gl_list_node_t
568 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
572 gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
573 return INDEX_TO_NODE (index);
576 static gl_list_node_t
577 gl_array_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
580 size_t count = list->count;
584 /* At each loop iteration, low <= high; for indices < low the values are
585 smaller than ELT; for indices >= high the values are greater than ELT. */
588 size_t mid = low + (high - low) / 2; /* low <= mid < high */
589 int cmp = compar (list->elements[mid], elt);
601 return gl_array_add_at (list, low, elt);
605 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
608 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
609 if (index == (size_t)(-1))
612 return gl_array_remove_at (list, index);
616 const struct gl_list_implementation gl_array_list_implementation =
618 gl_array_create_empty,
623 gl_array_previous_node,
626 gl_array_search_from_to,
627 gl_array_indexof_from_to,
633 gl_array_remove_node,
638 gl_array_iterator_from_to,
639 gl_array_iterator_next,
640 gl_array_iterator_free,
641 gl_array_sortedlist_search,
642 gl_array_sortedlist_search_from_to,
643 gl_array_sortedlist_indexof,
644 gl_array_sortedlist_indexof_from_to,
645 gl_array_sortedlist_add,
646 gl_array_sortedlist_remove