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 =
62 (struct gl_list_impl *) xmalloc (sizeof (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.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 bool allow_duplicates,
80 size_t count, const void **contents)
82 struct gl_list_impl *list =
83 (struct gl_list_impl *) xmalloc (sizeof (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.allow_duplicates = allow_duplicates;
92 (const void **) xmalloc (count * sizeof (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];
119 static gl_list_node_t
120 gl_array_next_node (gl_list_t list, gl_list_node_t node)
122 uintptr_t index = NODE_TO_INDEX (node);
123 if (!(index < list->count))
124 /* Invalid argument. */
127 if (index < list->count)
128 return INDEX_TO_NODE (index);
133 static gl_list_node_t
134 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
136 uintptr_t index = NODE_TO_INDEX (node);
137 if (!(index < list->count))
138 /* Invalid argument. */
141 return INDEX_TO_NODE (index - 1);
147 gl_array_get_at (gl_list_t list, size_t position)
149 size_t count = list->count;
151 if (!(position < count))
152 /* Invalid argument. */
154 return list->elements[position];
157 static gl_list_node_t
158 gl_array_set_at (gl_list_t list, size_t position, const void *elt)
160 size_t count = list->count;
162 if (!(position < count))
163 /* Invalid argument. */
165 list->elements[position] = elt;
166 return INDEX_TO_NODE (position);
170 gl_array_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
173 size_t count = list->count;
175 if (!(start_index <= end_index && end_index <= count))
176 /* Invalid arguments. */
179 if (start_index < end_index)
181 gl_listelement_equals_fn equals = list->base.equals_fn;
186 for (i = start_index;;)
188 if (equals (elt, list->elements[i]))
199 for (i = start_index;;)
201 if (elt == list->elements[i])
212 static gl_list_node_t
213 gl_array_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
216 size_t index = gl_array_indexof_from_to (list, start_index, end_index, elt);
217 return INDEX_TO_NODE (index);
220 /* Ensure that list->allocated > list->count. */
222 grow (gl_list_t list)
224 size_t new_allocated;
228 new_allocated = xtimes (list->allocated, 2);
229 new_allocated = xsum (new_allocated, 1);
230 memory_size = xtimes (new_allocated, sizeof (const void *));
231 if (size_overflow_p (memory_size))
232 /* Overflow, would lead to out of memory. */
234 memory = (const void **) xrealloc (list->elements, memory_size);
238 list->elements = memory;
239 list->allocated = new_allocated;
242 static gl_list_node_t
243 gl_array_add_first (gl_list_t list, const void *elt)
245 size_t count = list->count;
246 const void **elements;
249 if (count == list->allocated)
251 elements = list->elements;
252 for (i = count; i > 0; i--)
253 elements[i] = elements[i - 1];
255 list->count = count + 1;
256 return INDEX_TO_NODE (0);
259 static gl_list_node_t
260 gl_array_add_last (gl_list_t list, const void *elt)
262 size_t count = list->count;
264 if (count == list->allocated)
266 list->elements[count] = elt;
267 list->count = count + 1;
268 return INDEX_TO_NODE (count);
271 static gl_list_node_t
272 gl_array_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
274 size_t count = list->count;
275 uintptr_t index = NODE_TO_INDEX (node);
277 const void **elements;
280 if (!(index < count))
281 /* Invalid argument. */
284 if (count == list->allocated)
286 elements = list->elements;
287 for (i = count; i > position; i--)
288 elements[i] = elements[i - 1];
289 elements[position] = elt;
290 list->count = count + 1;
291 return INDEX_TO_NODE (position);
294 static gl_list_node_t
295 gl_array_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
297 size_t count = list->count;
298 uintptr_t index = NODE_TO_INDEX (node);
300 const void **elements;
303 if (!(index < count))
304 /* Invalid argument. */
306 position = index + 1;
307 if (count == list->allocated)
309 elements = list->elements;
310 for (i = count; i > position; i--)
311 elements[i] = elements[i - 1];
312 elements[position] = elt;
313 list->count = count + 1;
314 return INDEX_TO_NODE (position);
317 static gl_list_node_t
318 gl_array_add_at (gl_list_t list, size_t position, const void *elt)
320 size_t count = list->count;
321 const void **elements;
324 if (!(position <= count))
325 /* Invalid argument. */
327 if (count == list->allocated)
329 elements = list->elements;
330 for (i = count; i > position; i--)
331 elements[i] = elements[i - 1];
332 elements[position] = elt;
333 list->count = count + 1;
334 return INDEX_TO_NODE (position);
338 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
340 size_t count = list->count;
341 uintptr_t index = NODE_TO_INDEX (node);
343 const void **elements;
346 if (!(index < count))
347 /* Invalid argument. */
350 elements = list->elements;
351 for (i = position + 1; i < count; i++)
352 elements[i - 1] = elements[i];
353 list->count = count - 1;
358 gl_array_remove_at (gl_list_t list, size_t position)
360 size_t count = list->count;
361 const void **elements;
364 if (!(position < count))
365 /* Invalid argument. */
367 elements = list->elements;
368 for (i = position + 1; i < count; i++)
369 elements[i - 1] = elements[i];
370 list->count = count - 1;
375 gl_array_remove (gl_list_t list, const void *elt)
377 size_t position = gl_array_indexof_from_to (list, 0, list->count, elt);
378 if (position == (size_t)(-1))
381 return gl_array_remove_at (list, position);
385 gl_array_list_free (gl_list_t list)
387 if (list->elements != NULL)
388 free (list->elements);
392 /* --------------------- gl_list_iterator_t Data Type --------------------- */
394 static gl_list_iterator_t
395 gl_array_iterator (gl_list_t list)
397 gl_list_iterator_t result;
399 result.vtable = list->base.vtable;
401 result.count = list->count;
402 result.p = list->elements + 0;
403 result.q = list->elements + list->count;
412 static gl_list_iterator_t
413 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
415 gl_list_iterator_t result;
417 if (!(start_index <= end_index && end_index <= list->count))
418 /* Invalid arguments. */
420 result.vtable = list->base.vtable;
422 result.count = list->count;
423 result.p = list->elements + start_index;
424 result.q = list->elements + end_index;
434 gl_array_iterator_next (gl_list_iterator_t *iterator,
435 const void **eltp, gl_list_node_t *nodep)
437 gl_list_t list = iterator->list;
438 if (iterator->count != list->count)
440 if (iterator->count != list->count + 1)
441 /* Concurrent modifications were done on the list. */
443 /* The last returned element was removed. */
448 if (iterator->p < iterator->q)
450 const void **p = (const void **) iterator->p;
453 *nodep = INDEX_TO_NODE (p - list->elements);
462 gl_array_iterator_free (gl_list_iterator_t *iterator)
466 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
469 gl_array_sortedlist_indexof_from_to (gl_list_t list,
470 gl_listelement_compar_fn compar,
471 size_t low, size_t high,
474 if (!(low <= high && high <= list->count))
475 /* Invalid arguments. */
479 /* At each loop iteration, low < high; for indices < low the values
480 are smaller than ELT; for indices >= high the values are greater
481 than ELT. So, if the element occurs in the list, it is at
482 low <= position < high. */
485 size_t mid = low + (high - low) / 2; /* low <= mid < high */
486 int cmp = compar (list->elements[mid], elt);
494 /* We have an element equal to ELT at index MID. But we need
495 the minimal such index. */
497 /* At each loop iteration, low <= high and
498 compar (list->elements[high], elt) == 0,
499 and we know that the first occurrence of the element is at
500 low <= position <= high. */
503 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
504 int cmp2 = compar (list->elements[mid2], elt);
509 /* The list was not sorted. */
522 /* Here low == high. */
528 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
531 return gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count,
535 static gl_list_node_t
536 gl_array_sortedlist_search_from_to (gl_list_t list,
537 gl_listelement_compar_fn compar,
538 size_t low, size_t high,
542 gl_array_sortedlist_indexof_from_to (list, compar, low, high, elt);
543 return INDEX_TO_NODE (index);
546 static gl_list_node_t
547 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
551 gl_array_sortedlist_indexof_from_to (list, compar, 0, list->count, elt);
552 return INDEX_TO_NODE (index);
555 static gl_list_node_t
556 gl_array_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
559 size_t count = list->count;
563 /* At each loop iteration, low <= high; for indices < low the values are
564 smaller than ELT; for indices >= high the values are greater than ELT. */
567 size_t mid = low + (high - low) / 2; /* low <= mid < high */
568 int cmp = compar (list->elements[mid], elt);
580 return gl_array_add_at (list, low, elt);
584 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
587 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
588 if (index == (size_t)(-1))
591 return gl_array_remove_at (list, index);
595 const struct gl_list_implementation gl_array_list_implementation =
597 gl_array_create_empty,
602 gl_array_previous_node,
605 gl_array_search_from_to,
606 gl_array_indexof_from_to,
612 gl_array_remove_node,
617 gl_array_iterator_from_to,
618 gl_array_iterator_next,
619 gl_array_iterator_free,
620 gl_array_sortedlist_search,
621 gl_array_sortedlist_search_from_to,
622 gl_array_sortedlist_indexof,
623 gl_array_sortedlist_indexof_from_to,
624 gl_array_sortedlist_add,
625 gl_array_sortedlist_remove