1 /* Sequential list data type implemented by an 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_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 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.allow_duplicates = allow_duplicates;
67 list->elements = NULL;
75 gl_array_create (gl_list_implementation_t implementation,
76 gl_listelement_equals_fn equals_fn,
77 gl_listelement_hashcode_fn hashcode_fn,
78 bool allow_duplicates,
79 size_t count, const void **contents)
81 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
83 list->base.vtable = implementation;
84 list->base.equals_fn = equals_fn;
85 list->base.hashcode_fn = hashcode_fn;
86 list->base.allow_duplicates = allow_duplicates;
89 list->elements = XNMALLOC (count, const void *);
90 memcpy (list->elements, contents, count * sizeof (const void *));
93 list->elements = NULL;
95 list->allocated = count;
101 gl_array_size (gl_list_t list)
107 gl_array_node_value (gl_list_t list, gl_list_node_t node)
109 uintptr_t index = NODE_TO_INDEX (node);
110 if (!(index < list->count))
111 /* Invalid argument. */
113 return list->elements[index];
116 static gl_list_node_t
117 gl_array_next_node (gl_list_t list, gl_list_node_t node)
119 uintptr_t index = NODE_TO_INDEX (node);
120 if (!(index < list->count))
121 /* Invalid argument. */
124 if (index < list->count)
125 return INDEX_TO_NODE (index);
130 static gl_list_node_t
131 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
133 uintptr_t index = NODE_TO_INDEX (node);
134 if (!(index < list->count))
135 /* Invalid argument. */
138 return INDEX_TO_NODE (index - 1);
144 gl_array_get_at (gl_list_t list, size_t position)
146 size_t count = list->count;
148 if (!(position < count))
149 /* Invalid argument. */
151 return list->elements[position];
154 static gl_list_node_t
155 gl_array_set_at (gl_list_t list, size_t position, const void *elt)
157 size_t count = list->count;
159 if (!(position < count))
160 /* Invalid argument. */
162 list->elements[position] = elt;
163 return INDEX_TO_NODE (position);
167 gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
170 size_t count = list->count;
172 if (!(start_index <= end_index && end_index <= count))
173 /* Invalid arguments. */
176 if (start_index < end_index)
178 gl_listelement_equals_fn equals = list->base.equals_fn;
183 for (i = start_index;;)
185 if (equals (elt, list->elements[i]))
196 for (i = start_index;;)
198 if (elt == list->elements[i])
209 static gl_list_node_t
210 gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
213 size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
214 return INDEX_TO_NODE (index);
217 /* Ensure that list->allocated > list->count. */
219 grow (gl_list_t list)
221 size_t new_allocated;
225 new_allocated = xtimes (list->allocated, 2);
226 new_allocated = xsum (new_allocated, 1);
227 memory_size = xtimes (new_allocated, sizeof (const void *));
228 if (size_overflow_p (memory_size))
229 /* Overflow, would lead to out of memory. */
231 memory = (const void **) xrealloc (list->elements, memory_size);
235 list->elements = memory;
236 list->allocated = new_allocated;
239 static gl_list_node_t
240 gl_array_add_first (gl_list_t list, const void *elt)
242 size_t count = list->count;
243 const void **elements;
246 if (count == list->allocated)
248 elements = list->elements;
249 for (i = count; i > 0; i--)
250 elements[i] = elements[i - 1];
252 list->count = count + 1;
253 return INDEX_TO_NODE (0);
256 static gl_list_node_t
257 gl_array_add_last (gl_list_t list, const void *elt)
259 size_t count = list->count;
261 if (count == list->allocated)
263 list->elements[count] = elt;
264 list->count = count + 1;
265 return INDEX_TO_NODE (count);
268 static gl_list_node_t
269 gl_array_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
271 size_t count = list->count;
272 uintptr_t index = NODE_TO_INDEX (node);
274 const void **elements;
277 if (!(index < count))
278 /* Invalid argument. */
281 if (count == list->allocated)
283 elements = list->elements;
284 for (i = count; i > position; i--)
285 elements[i] = elements[i - 1];
286 elements[position] = elt;
287 list->count = count + 1;
288 return INDEX_TO_NODE (position);
291 static gl_list_node_t
292 gl_array_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
294 size_t count = list->count;
295 uintptr_t index = NODE_TO_INDEX (node);
297 const void **elements;
300 if (!(index < count))
301 /* Invalid argument. */
303 position = index + 1;
304 if (count == list->allocated)
306 elements = list->elements;
307 for (i = count; i > position; i--)
308 elements[i] = elements[i - 1];
309 elements[position] = elt;
310 list->count = count + 1;
311 return INDEX_TO_NODE (position);
314 static gl_list_node_t
315 gl_array_add_at (gl_list_t list, size_t position, const void *elt)
317 size_t count = list->count;
318 const void **elements;
321 if (!(position <= count))
322 /* Invalid argument. */
324 if (count == list->allocated)
326 elements = list->elements;
327 for (i = count; i > position; i--)
328 elements[i] = elements[i - 1];
329 elements[position] = elt;
330 list->count = count + 1;
331 return INDEX_TO_NODE (position);
335 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
337 size_t count = list->count;
338 uintptr_t index = NODE_TO_INDEX (node);
340 const void **elements;
343 if (!(index < count))
344 /* Invalid argument. */
347 elements = list->elements;
348 for (i = position + 1; i < count; i++)
349 elements[i - 1] = elements[i];
350 list->count = count - 1;
355 gl_array_remove_at (gl_list_t list, size_t position)
357 size_t count = list->count;
358 const void **elements;
361 if (!(position < count))
362 /* Invalid argument. */
364 elements = list->elements;
365 for (i = position + 1; i < count; i++)
366 elements[i - 1] = elements[i];
367 list->count = count - 1;
372 gl_array_remove (gl_list_t list, const void *elt)
374 size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
375 if (position == (size_t)(-1))
378 return gl_array_remove_at (list, position);
382 gl_array_list_free (gl_list_t list)
384 if (list->elements != NULL)
385 free (list->elements);
389 /* --------------------- gl_list_iterator_t Data Type --------------------- */
391 static gl_list_iterator_t
392 gl_array_iterator (gl_list_t list)
394 gl_list_iterator_t result;
396 result.vtable = list->base.vtable;
398 result.count = list->count;
399 result.p = list->elements + 0;
400 result.q = list->elements + list->count;
409 static gl_list_iterator_t
410 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
412 gl_list_iterator_t result;
414 if (!(start_index <= end_index && end_index <= list->count))
415 /* Invalid arguments. */
417 result.vtable = list->base.vtable;
419 result.count = list->count;
420 result.p = list->elements + start_index;
421 result.q = list->elements + end_index;
431 gl_array_iterator_next (gl_list_iterator_t *iterator,
432 const void **eltp, gl_list_node_t *nodep)
434 gl_list_t list = iterator->list;
435 if (iterator->count != list->count)
437 if (iterator->count != list->count + 1)
438 /* Concurrent modifications were done on the list. */
440 /* The last returned element was removed. */
442 iterator->p = (const void **) iterator->p - 1;
443 iterator->q = (const void **) iterator->q - 1;
445 if (iterator->p < iterator->q)
447 const void **p = (const void **) iterator->p;
450 *nodep = INDEX_TO_NODE (p - list->elements);
459 gl_array_iterator_free (gl_list_iterator_t *iterator)
463 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
466 gl_array_sortedlist_indexof_from_to (gl_list_t list,
467 gl_listelement_compar_fn compar,
468 size_t low, size_t high,
471 if (!(low <= high && high <= list->count))
472 /* Invalid arguments. */
476 /* At each loop iteration, low < high; for indices < low the values
477 are smaller than ELT; for indices >= high the values are greater
478 than ELT. So, if the element occurs in the list, it is at
479 low <= position < high. */
482 size_t mid = low + (high - low) / 2; /* low <= mid < high */
483 int cmp = compar (list->elements[mid], elt);
491 /* We have an element equal to ELT at index MID. But we need
492 the minimal such index. */
494 /* At each loop iteration, low <= high and
495 compar (list->elements[high], elt) == 0,
496 and we know that the first occurrence of the element is at
497 low <= position <= high. */
500 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
501 int cmp2 = compar (list->elements[mid2], elt);
506 /* The list was not sorted. */
519 /* Here low == high. */
525 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
528 return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
532 static gl_list_node_t
533 gl_array_sortedlist_search_from_to (gl_list_t list,
534 gl_listelement_compar_fn compar,
535 size_t low, size_t high,
539 gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
540 return INDEX_TO_NODE (index);
543 static gl_list_node_t
544 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
548 gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
549 return INDEX_TO_NODE (index);
552 static gl_list_node_t
553 gl_array_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
556 size_t count = list->count;
560 /* At each loop iteration, low <= high; for indices < low the values are
561 smaller than ELT; for indices >= high the values are greater than ELT. */
564 size_t mid = low + (high - low) / 2; /* low <= mid < high */
565 int cmp = compar (list->elements[mid], elt);
577 return gl_array_add_at (list, low, elt);
581 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
584 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
585 if (index == (size_t)(-1))
588 return gl_array_remove_at (list, index);
592 const struct gl_list_implementation gl_array_list_implementation =
594 gl_array_create_empty,
599 gl_array_previous_node,
602 gl_array_search_from_to,
603 gl_array_indexof_from_to,
609 gl_array_remove_node,
614 gl_array_iterator_from_to,
615 gl_array_iterator_next,
616 gl_array_iterator_free,
617 gl_array_sortedlist_search,
618 gl_array_sortedlist_search_from_to,
619 gl_array_sortedlist_indexof,
620 gl_array_sortedlist_indexof_from_to,
621 gl_array_sortedlist_add,
622 gl_array_sortedlist_remove