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. */
24 #include "gl_array_list.h"
32 /* Checked size_t computations. */
36 # define uintptr_t unsigned long
39 /* -------------------------- gl_list_t Data Type -------------------------- */
41 /* Concrete gl_list_impl type, valid for this file only. */
44 struct gl_list_impl_base base;
45 /* An array of ALLOCATED elements, of which the first COUNT are used.
46 0 <= COUNT <= ALLOCATED. */
47 const void **elements;
52 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
54 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
55 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
58 gl_array_create_empty (gl_list_implementation_t implementation,
59 gl_listelement_equals_fn equals_fn,
60 gl_listelement_hashcode_fn hashcode_fn,
61 bool allow_duplicates)
63 struct gl_list_impl *list =
64 (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
66 list->base.vtable = implementation;
67 list->base.equals_fn = equals_fn;
68 list->base.hashcode_fn = hashcode_fn;
69 list->base.allow_duplicates = allow_duplicates;
70 list->elements = NULL;
78 gl_array_create (gl_list_implementation_t implementation,
79 gl_listelement_equals_fn equals_fn,
80 gl_listelement_hashcode_fn hashcode_fn,
81 bool allow_duplicates,
82 size_t count, const void **contents)
84 struct gl_list_impl *list =
85 (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
87 list->base.vtable = implementation;
88 list->base.equals_fn = equals_fn;
89 list->base.hashcode_fn = hashcode_fn;
90 list->base.allow_duplicates = allow_duplicates;
94 (const void **) xmalloc (count * sizeof (const void *));
95 memcpy (list->elements, contents, count * sizeof (const void *));
98 list->elements = NULL;
100 list->allocated = count;
106 gl_array_size (gl_list_t list)
112 gl_array_node_value (gl_list_t list, gl_list_node_t node)
114 uintptr_t index = NODE_TO_INDEX (node);
115 if (!(index < list->count))
116 /* Invalid argument. */
118 return list->elements[index];
121 static gl_list_node_t
122 gl_array_next_node (gl_list_t list, gl_list_node_t node)
124 uintptr_t index = NODE_TO_INDEX (node);
125 if (!(index < list->count))
126 /* Invalid argument. */
129 if (index < list->count)
130 return INDEX_TO_NODE (index);
135 static gl_list_node_t
136 gl_array_previous_node (gl_list_t list, gl_list_node_t node)
138 uintptr_t index = NODE_TO_INDEX (node);
139 if (!(index < list->count))
140 /* Invalid argument. */
143 return INDEX_TO_NODE (index - 1);
149 gl_array_get_at (gl_list_t list, size_t position)
151 size_t count = list->count;
153 if (!(position < count))
154 /* Invalid argument. */
156 return list->elements[position];
159 static gl_list_node_t
160 gl_array_set_at (gl_list_t list, size_t position, const void *elt)
162 size_t count = list->count;
164 if (!(position < count))
165 /* Invalid argument. */
167 list->elements[position] = elt;
168 return INDEX_TO_NODE (position);
172 gl_array_indexof (gl_list_t list, const void *elt)
174 size_t count = list->count;
177 gl_listelement_equals_fn equals = list->base.equals_fn;
184 if (equals (elt, list->elements[i]))
197 if (elt == list->elements[i])
208 static gl_list_node_t
209 gl_array_search (gl_list_t list, const void *elt)
211 size_t index = gl_array_indexof (list, elt);
212 return INDEX_TO_NODE (index);
215 /* Ensure that list->allocated > list->count. */
217 grow (gl_list_t list)
219 size_t new_allocated;
223 new_allocated = xtimes (list->allocated, 2);
224 new_allocated = xsum (new_allocated, 1);
225 memory_size = xtimes (new_allocated, sizeof (const void *));
226 if (size_overflow_p (memory_size))
227 /* Overflow, would lead to out of memory. */
229 memory = (const void **) xrealloc (list->elements, memory_size);
233 list->elements = memory;
234 list->allocated = new_allocated;
237 static gl_list_node_t
238 gl_array_add_first (gl_list_t list, const void *elt)
240 size_t count = list->count;
241 const void **elements;
244 if (count == list->allocated)
246 elements = list->elements;
247 for (i = count; i > 0; i--)
248 elements[i] = elements[i - 1];
250 list->count = count + 1;
251 return INDEX_TO_NODE (0);
254 static gl_list_node_t
255 gl_array_add_last (gl_list_t list, const void *elt)
257 size_t count = list->count;
259 if (count == list->allocated)
261 list->elements[count] = elt;
262 list->count = count + 1;
263 return INDEX_TO_NODE (count);
266 static gl_list_node_t
267 gl_array_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
269 size_t count = list->count;
270 uintptr_t index = NODE_TO_INDEX (node);
272 const void **elements;
275 if (!(index < count))
276 /* Invalid argument. */
279 if (count == list->allocated)
281 elements = list->elements;
282 for (i = count; i > position; i--)
283 elements[i] = elements[i - 1];
284 elements[position] = elt;
285 list->count = count + 1;
286 return INDEX_TO_NODE (position);
289 static gl_list_node_t
290 gl_array_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
292 size_t count = list->count;
293 uintptr_t index = NODE_TO_INDEX (node);
295 const void **elements;
298 if (!(index < count))
299 /* Invalid argument. */
301 position = index + 1;
302 if (count == list->allocated)
304 elements = list->elements;
305 for (i = count; i > position; i--)
306 elements[i] = elements[i - 1];
307 elements[position] = elt;
308 list->count = count + 1;
309 return INDEX_TO_NODE (position);
312 static gl_list_node_t
313 gl_array_add_at (gl_list_t list, size_t position, const void *elt)
315 size_t count = list->count;
316 const void **elements;
319 if (!(position <= count))
320 /* Invalid argument. */
322 if (count == list->allocated)
324 elements = list->elements;
325 for (i = count; i > position; i--)
326 elements[i] = elements[i - 1];
327 elements[position] = elt;
328 list->count = count + 1;
329 return INDEX_TO_NODE (position);
333 gl_array_remove_node (gl_list_t list, gl_list_node_t node)
335 size_t count = list->count;
336 uintptr_t index = NODE_TO_INDEX (node);
338 const void **elements;
341 if (!(index < count))
342 /* Invalid argument. */
345 elements = list->elements;
346 for (i = position + 1; i < count; i++)
347 elements[i - 1] = elements[i];
348 list->count = count - 1;
353 gl_array_remove_at (gl_list_t list, size_t position)
355 size_t count = list->count;
356 const void **elements;
359 if (!(position < count))
360 /* Invalid argument. */
362 elements = list->elements;
363 for (i = position + 1; i < count; i++)
364 elements[i - 1] = elements[i];
365 list->count = count - 1;
370 gl_array_remove (gl_list_t list, const void *elt)
372 size_t position = gl_array_indexof (list, elt);
373 if (position == (size_t)(-1))
376 return gl_array_remove_at (list, position);
380 gl_array_list_free (gl_list_t list)
382 if (list->elements != NULL)
383 free (list->elements);
387 /* --------------------- gl_list_iterator_t Data Type --------------------- */
389 static gl_list_iterator_t
390 gl_array_iterator (gl_list_t list)
392 gl_list_iterator_t result;
394 result.vtable = list->base.vtable;
396 result.count = list->count;
397 result.p = list->elements + 0;
398 result.q = list->elements + list->count;
403 static gl_list_iterator_t
404 gl_array_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
406 gl_list_iterator_t result;
408 if (!(start_index <= end_index && end_index <= list->count))
409 /* Invalid arguments. */
411 result.vtable = list->base.vtable;
413 result.count = list->count;
414 result.p = list->elements + start_index;
415 result.q = list->elements + end_index;
421 gl_array_iterator_next (gl_list_iterator_t *iterator,
422 const void **eltp, gl_list_node_t *nodep)
424 gl_list_t list = iterator->list;
425 if (iterator->count != list->count)
427 if (iterator->count != list->count + 1)
428 /* Concurrent modifications were done on the list. */
430 /* The last returned element was removed. */
435 if (iterator->p < iterator->q)
437 const void **p = (const void **) iterator->p;
440 *nodep = INDEX_TO_NODE (p - list->elements);
449 gl_array_iterator_free (gl_list_iterator_t *iterator)
453 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
456 gl_array_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
459 size_t count = list->count;
466 /* At each loop iteration, low < high; for indices < low the values
467 are smaller than ELT; for indices >= high the values are greater
468 than ELT. So, if the element occurs in the list, is at
469 low <= position < high. */
472 size_t mid = low + (high - low) / 2; /* low <= mid < high */
473 int cmp = compar (list->elements[mid], elt);
481 /* We have an element equal to ELT at index MID. But we need
482 the minimal such index. */
484 /* At each loop iteration, low <= high and
485 compar (list->elements[high], elt) == 0,
486 and we know that the first occurrence of the element is at
487 low <= position <= high. */
490 size_t mid2 = low + (high - low) / 2; /* low <= mid < high */
491 int cmp2 = compar (list->elements[mid2], elt);
496 /* The list was not sorted. */
509 /* Here low == high. */
514 static gl_list_node_t
515 gl_array_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
518 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
519 return INDEX_TO_NODE (index);
522 static gl_list_node_t
523 gl_array_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
526 size_t count = list->count;
530 /* At each loop iteration, low <= high; for indices < low the values are
531 smaller than ELT; for indices >= high the values are greater than ELT. */
534 size_t mid = low + (high - low) / 2; /* low <= mid < high */
535 int cmp = compar (list->elements[mid], elt);
547 return gl_array_add_at (list, low, elt);
551 gl_array_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
554 size_t index = gl_array_sortedlist_indexof (list, compar, elt);
555 if (index == (size_t)(-1))
558 return gl_array_remove_at (list, index);
562 const struct gl_list_implementation gl_array_list_implementation =
564 gl_array_create_empty,
569 gl_array_previous_node,
579 gl_array_remove_node,
584 gl_array_iterator_from_to,
585 gl_array_iterator_next,
586 gl_array_iterator_free,
587 gl_array_sortedlist_search,
588 gl_array_sortedlist_indexof,
589 gl_array_sortedlist_add,
590 gl_array_sortedlist_remove