1 /* Sequential list data type implemented by an array.
2 Copyright (C) 2006-2020 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
21 #include "gl_array_list.h"
28 /* Checked size_t computations. */
31 /* -------------------------- gl_list_t Data Type -------------------------- */
33 /* Concrete gl_list_impl type, valid for this file only. */
36 struct gl_list_impl_base base
;
37 /* An array of ALLOCATED elements, of which the first COUNT are used.
38 0 <= COUNT <= ALLOCATED. */
39 const void **elements
;
44 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
46 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
47 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
50 gl_array_nx_create_empty (gl_list_implementation_t implementation
,
51 gl_listelement_equals_fn equals_fn
,
52 gl_listelement_hashcode_fn hashcode_fn
,
53 gl_listelement_dispose_fn dispose_fn
,
54 bool allow_duplicates
)
56 struct gl_list_impl
*list
=
57 (struct gl_list_impl
*) malloc (sizeof (struct gl_list_impl
));
62 list
->base
.vtable
= implementation
;
63 list
->base
.equals_fn
= equals_fn
;
64 list
->base
.hashcode_fn
= hashcode_fn
;
65 list
->base
.dispose_fn
= dispose_fn
;
66 list
->base
.allow_duplicates
= allow_duplicates
;
67 list
->elements
= NULL
;
75 gl_array_nx_create (gl_list_implementation_t implementation
,
76 gl_listelement_equals_fn equals_fn
,
77 gl_listelement_hashcode_fn hashcode_fn
,
78 gl_listelement_dispose_fn dispose_fn
,
79 bool allow_duplicates
,
80 size_t count
, const void **contents
)
82 struct gl_list_impl
*list
=
83 (struct gl_list_impl
*) malloc (sizeof (struct gl_list_impl
));
88 list
->base
.vtable
= implementation
;
89 list
->base
.equals_fn
= equals_fn
;
90 list
->base
.hashcode_fn
= hashcode_fn
;
91 list
->base
.dispose_fn
= dispose_fn
;
92 list
->base
.allow_duplicates
= allow_duplicates
;
95 if (size_overflow_p (xtimes (count
, sizeof (const void *))))
97 list
->elements
= (const void **) malloc (count
* sizeof (const void *));
98 if (list
->elements
== NULL
)
100 memcpy (list
->elements
, contents
, count
* sizeof (const void *));
103 list
->elements
= NULL
;
105 list
->allocated
= count
;
115 gl_array_size (gl_list_t list
)
120 static const void * _GL_ATTRIBUTE_PURE
121 gl_array_node_value (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. */
127 return list
->elements
[index
];
131 gl_array_node_nx_set_value (gl_list_t list
, gl_list_node_t node
,
134 uintptr_t index
= NODE_TO_INDEX (node
);
135 if (!(index
< list
->count
))
136 /* Invalid argument. */
138 list
->elements
[index
] = elt
;
142 static gl_list_node_t _GL_ATTRIBUTE_PURE
143 gl_array_next_node (gl_list_t list
, gl_list_node_t node
)
145 uintptr_t index
= NODE_TO_INDEX (node
);
146 if (!(index
< list
->count
))
147 /* Invalid argument. */
150 if (index
< list
->count
)
151 return INDEX_TO_NODE (index
);
156 static gl_list_node_t _GL_ATTRIBUTE_PURE
157 gl_array_previous_node (gl_list_t list
, gl_list_node_t node
)
159 uintptr_t index
= NODE_TO_INDEX (node
);
160 if (!(index
< list
->count
))
161 /* Invalid argument. */
164 return INDEX_TO_NODE (index
- 1);
169 static const void * _GL_ATTRIBUTE_PURE
170 gl_array_get_at (gl_list_t list
, size_t position
)
172 size_t count
= list
->count
;
174 if (!(position
< count
))
175 /* Invalid argument. */
177 return list
->elements
[position
];
180 static gl_list_node_t
181 gl_array_nx_set_at (gl_list_t list
, size_t position
, const void *elt
)
183 size_t count
= list
->count
;
185 if (!(position
< count
))
186 /* Invalid argument. */
188 list
->elements
[position
] = elt
;
189 return INDEX_TO_NODE (position
);
193 gl_array_indexof_from_to (gl_list_t list
, size_t start_index
, size_t end_index
,
196 size_t count
= list
->count
;
198 if (!(start_index
<= end_index
&& end_index
<= count
))
199 /* Invalid arguments. */
202 if (start_index
< end_index
)
204 gl_listelement_equals_fn equals
= list
->base
.equals_fn
;
209 for (i
= start_index
;;)
211 if (equals (elt
, list
->elements
[i
]))
222 for (i
= start_index
;;)
224 if (elt
== list
->elements
[i
])
235 static gl_list_node_t
236 gl_array_search_from_to (gl_list_t list
, size_t start_index
, size_t end_index
,
239 size_t index
= gl_array_indexof_from_to (list
, start_index
, end_index
, elt
);
240 return INDEX_TO_NODE (index
);
243 /* Ensure that list->allocated > list->count.
244 Return 0 upon success, -1 upon out-of-memory. */
246 grow (gl_list_t list
)
248 size_t new_allocated
;
252 new_allocated
= xtimes (list
->allocated
, 2);
253 new_allocated
= xsum (new_allocated
, 1);
254 memory_size
= xtimes (new_allocated
, sizeof (const void *));
255 if (size_overflow_p (memory_size
))
256 /* Overflow, would lead to out of memory. */
258 memory
= (const void **) realloc (list
->elements
, memory_size
);
262 list
->elements
= memory
;
263 list
->allocated
= new_allocated
;
267 static gl_list_node_t
268 gl_array_nx_add_first (gl_list_t list
, const void *elt
)
270 size_t count
= list
->count
;
271 const void **elements
;
274 if (count
== list
->allocated
)
277 elements
= list
->elements
;
278 for (i
= count
; i
> 0; i
--)
279 elements
[i
] = elements
[i
- 1];
281 list
->count
= count
+ 1;
282 return INDEX_TO_NODE (0);
285 static gl_list_node_t
286 gl_array_nx_add_last (gl_list_t list
, const void *elt
)
288 size_t count
= list
->count
;
290 if (count
== list
->allocated
)
293 list
->elements
[count
] = elt
;
294 list
->count
= count
+ 1;
295 return INDEX_TO_NODE (count
);
298 static gl_list_node_t
299 gl_array_nx_add_before (gl_list_t list
, gl_list_node_t node
, const void *elt
)
301 size_t count
= list
->count
;
302 uintptr_t index
= NODE_TO_INDEX (node
);
304 const void **elements
;
307 if (!(index
< count
))
308 /* Invalid argument. */
311 if (count
== list
->allocated
)
314 elements
= list
->elements
;
315 for (i
= count
; i
> position
; i
--)
316 elements
[i
] = elements
[i
- 1];
317 elements
[position
] = elt
;
318 list
->count
= count
+ 1;
319 return INDEX_TO_NODE (position
);
322 static gl_list_node_t
323 gl_array_nx_add_after (gl_list_t list
, gl_list_node_t node
, const void *elt
)
325 size_t count
= list
->count
;
326 uintptr_t index
= NODE_TO_INDEX (node
);
328 const void **elements
;
331 if (!(index
< count
))
332 /* Invalid argument. */
334 position
= index
+ 1;
335 if (count
== list
->allocated
)
338 elements
= list
->elements
;
339 for (i
= count
; i
> position
; i
--)
340 elements
[i
] = elements
[i
- 1];
341 elements
[position
] = elt
;
342 list
->count
= count
+ 1;
343 return INDEX_TO_NODE (position
);
346 static gl_list_node_t
347 gl_array_nx_add_at (gl_list_t list
, size_t position
, const void *elt
)
349 size_t count
= list
->count
;
350 const void **elements
;
353 if (!(position
<= count
))
354 /* Invalid argument. */
356 if (count
== list
->allocated
)
359 elements
= list
->elements
;
360 for (i
= count
; i
> position
; i
--)
361 elements
[i
] = elements
[i
- 1];
362 elements
[position
] = elt
;
363 list
->count
= count
+ 1;
364 return INDEX_TO_NODE (position
);
368 gl_array_remove_node (gl_list_t list
, gl_list_node_t node
)
370 size_t count
= list
->count
;
371 uintptr_t index
= NODE_TO_INDEX (node
);
373 const void **elements
;
376 if (!(index
< count
))
377 /* Invalid argument. */
380 elements
= list
->elements
;
381 if (list
->base
.dispose_fn
!= NULL
)
382 list
->base
.dispose_fn (elements
[position
]);
383 for (i
= position
+ 1; i
< count
; i
++)
384 elements
[i
- 1] = elements
[i
];
385 list
->count
= count
- 1;
390 gl_array_remove_at (gl_list_t list
, size_t position
)
392 size_t count
= list
->count
;
393 const void **elements
;
396 if (!(position
< count
))
397 /* Invalid argument. */
399 elements
= list
->elements
;
400 if (list
->base
.dispose_fn
!= NULL
)
401 list
->base
.dispose_fn (elements
[position
]);
402 for (i
= position
+ 1; i
< count
; i
++)
403 elements
[i
- 1] = elements
[i
];
404 list
->count
= count
- 1;
409 gl_array_remove (gl_list_t list
, const void *elt
)
411 size_t position
= gl_array_indexof_from_to (list
, 0, list
->count
, elt
);
412 if (position
== (size_t)(-1))
415 return gl_array_remove_at (list
, position
);
419 gl_array_list_free (gl_list_t list
)
421 if (list
->elements
!= NULL
)
423 if (list
->base
.dispose_fn
!= NULL
)
425 size_t count
= list
->count
;
429 gl_listelement_dispose_fn dispose
= list
->base
.dispose_fn
;
430 const void **elements
= list
->elements
;
433 dispose (*elements
++);
437 free (list
->elements
);
442 /* --------------------- gl_list_iterator_t Data Type --------------------- */
444 static gl_list_iterator_t
445 gl_array_iterator (gl_list_t list
)
447 gl_list_iterator_t result
;
449 result
.vtable
= list
->base
.vtable
;
451 result
.count
= list
->count
;
452 result
.p
= list
->elements
+ 0;
453 result
.q
= list
->elements
+ list
->count
;
454 #if defined GCC_LINT || defined lint
462 static gl_list_iterator_t
463 gl_array_iterator_from_to (gl_list_t list
, size_t start_index
, size_t end_index
)
465 gl_list_iterator_t result
;
467 if (!(start_index
<= end_index
&& end_index
<= list
->count
))
468 /* Invalid arguments. */
470 result
.vtable
= list
->base
.vtable
;
472 result
.count
= list
->count
;
473 result
.p
= list
->elements
+ start_index
;
474 result
.q
= list
->elements
+ end_index
;
475 #if defined GCC_LINT || defined lint
484 gl_array_iterator_next (gl_list_iterator_t
*iterator
,
485 const void **eltp
, gl_list_node_t
*nodep
)
487 gl_list_t list
= iterator
->list
;
488 if (iterator
->count
!= list
->count
)
490 if (iterator
->count
!= list
->count
+ 1)
491 /* Concurrent modifications were done on the list. */
493 /* The last returned element was removed. */
495 iterator
->p
= (const void **) iterator
->p
- 1;
496 iterator
->q
= (const void **) iterator
->q
- 1;
498 if (iterator
->p
< iterator
->q
)
500 const void **p
= (const void **) iterator
->p
;
503 *nodep
= INDEX_TO_NODE (p
- list
->elements
);
512 gl_array_iterator_free (gl_list_iterator_t
*iterator _GL_UNUSED
)
516 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
519 gl_array_sortedlist_indexof_from_to (gl_list_t list
,
520 gl_listelement_compar_fn compar
,
521 size_t low
, size_t high
,
524 if (!(low
<= high
&& high
<= list
->count
))
525 /* Invalid arguments. */
529 /* At each loop iteration, low < high; for indices < low the values
530 are smaller than ELT; for indices >= high the values are greater
531 than ELT. So, if the element occurs in the list, it is at
532 low <= position < high. */
535 size_t mid
= low
+ (high
- low
) / 2; /* low <= mid < high */
536 int cmp
= compar (list
->elements
[mid
], elt
);
544 /* We have an element equal to ELT at index MID. But we need
545 the minimal such index. */
547 /* At each loop iteration, low <= high and
548 compar (list->elements[high], elt) == 0,
549 and we know that the first occurrence of the element is at
550 low <= position <= high. */
553 size_t mid2
= low
+ (high
- low
) / 2; /* low <= mid2 < high */
554 int cmp2
= compar (list
->elements
[mid2
], elt
);
559 /* The list was not sorted. */
572 /* Here low == high. */
578 gl_array_sortedlist_indexof (gl_list_t list
, gl_listelement_compar_fn compar
,
581 return gl_array_sortedlist_indexof_from_to (list
, compar
, 0, list
->count
,
585 static gl_list_node_t
586 gl_array_sortedlist_search_from_to (gl_list_t list
,
587 gl_listelement_compar_fn compar
,
588 size_t low
, size_t high
,
592 gl_array_sortedlist_indexof_from_to (list
, compar
, low
, high
, elt
);
593 return INDEX_TO_NODE (index
);
596 static gl_list_node_t
597 gl_array_sortedlist_search (gl_list_t list
, gl_listelement_compar_fn compar
,
601 gl_array_sortedlist_indexof_from_to (list
, compar
, 0, list
->count
, elt
);
602 return INDEX_TO_NODE (index
);
605 static gl_list_node_t
606 gl_array_sortedlist_nx_add (gl_list_t list
, gl_listelement_compar_fn compar
,
609 size_t count
= list
->count
;
613 /* At each loop iteration, low <= high; for indices < low the values are
614 smaller than ELT; for indices >= high the values are greater than ELT. */
617 size_t mid
= low
+ (high
- low
) / 2; /* low <= mid < high */
618 int cmp
= compar (list
->elements
[mid
], elt
);
630 return gl_array_nx_add_at (list
, low
, elt
);
634 gl_array_sortedlist_remove (gl_list_t list
, gl_listelement_compar_fn compar
,
637 size_t index
= gl_array_sortedlist_indexof (list
, compar
, elt
);
638 if (index
== (size_t)(-1))
641 return gl_array_remove_at (list
, index
);
645 const struct gl_list_implementation gl_array_list_implementation
=
647 gl_array_nx_create_empty
,
651 gl_array_node_nx_set_value
,
653 gl_array_previous_node
,
656 gl_array_search_from_to
,
657 gl_array_indexof_from_to
,
658 gl_array_nx_add_first
,
659 gl_array_nx_add_last
,
660 gl_array_nx_add_before
,
661 gl_array_nx_add_after
,
663 gl_array_remove_node
,
668 gl_array_iterator_from_to
,
669 gl_array_iterator_next
,
670 gl_array_iterator_free
,
671 gl_array_sortedlist_search
,
672 gl_array_sortedlist_search_from_to
,
673 gl_array_sortedlist_indexof
,
674 gl_array_sortedlist_indexof_from_to
,
675 gl_array_sortedlist_nx_add
,
676 gl_array_sortedlist_remove