Add searching operations, limited to a subsequence of the list.
[gnulib.git] / lib / gl_carray_list.c
1 /* Sequential list data type implemented by a circular array.
2    Copyright (C) 2006 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2006.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 #include <config.h>
20
21 /* Specification.  */
22 #include "gl_carray_list.h"
23
24 #include <stdlib.h>
25 /* Get memcpy.  */
26 #include <string.h>
27
28 #include "xalloc.h"
29
30 /* Checked size_t computations.  */
31 #include "xsize.h"
32
33 #ifndef uintptr_t
34 # define uintptr_t unsigned long
35 #endif
36
37 /* -------------------------- gl_list_t Data Type -------------------------- */
38
39 /* Concrete gl_list_impl type, valid for this file only.  */
40 struct gl_list_impl
41 {
42   struct gl_list_impl_base base;
43   /* An array of ALLOCATED elements, of which the elements
44      OFFSET, (OFFSET + 1) % ALLOCATED, ..., (OFFSET + COUNT - 1) % ALLOCATED
45      are used.  0 <= COUNT <= ALLOCATED.  Either OFFSET = ALLOCATED = 0 or
46      0 <= OFFSET < ALLOCATED.  */
47   const void **elements;
48   size_t offset;
49   size_t count;
50   size_t allocated;
51 };
52
53 /* struct gl_list_node_impl doesn't exist here.  The pointers are actually
54    indices + 1.  */
55 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
56 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
57
58 static gl_list_t
59 gl_carray_create_empty (gl_list_implementation_t implementation,
60                        gl_listelement_equals_fn equals_fn,
61                        gl_listelement_hashcode_fn hashcode_fn,
62                        bool allow_duplicates)
63 {
64   struct gl_list_impl *list =
65     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
66
67   list->base.vtable = implementation;
68   list->base.equals_fn = equals_fn;
69   list->base.hashcode_fn = hashcode_fn;
70   list->base.allow_duplicates = allow_duplicates;
71   list->elements = NULL;
72   list->offset = 0;
73   list->count = 0;
74   list->allocated = 0;
75
76   return list;
77 }
78
79 static gl_list_t
80 gl_carray_create (gl_list_implementation_t implementation,
81                  gl_listelement_equals_fn equals_fn,
82                  gl_listelement_hashcode_fn hashcode_fn,
83                  bool allow_duplicates,
84                  size_t count, const void **contents)
85 {
86   struct gl_list_impl *list =
87     (struct gl_list_impl *) xmalloc (sizeof (struct gl_list_impl));
88
89   list->base.vtable = implementation;
90   list->base.equals_fn = equals_fn;
91   list->base.hashcode_fn = hashcode_fn;
92   list->base.allow_duplicates = allow_duplicates;
93   if (count > 0)
94     {
95       list->elements =
96         (const void **) xmalloc (count * sizeof (const void *));
97       memcpy (list->elements, contents, count * sizeof (const void *));
98     }
99   else
100     list->elements = NULL;
101   list->offset = 0;
102   list->count = count;
103   list->allocated = count;
104
105   return list;
106 }
107
108 static size_t
109 gl_carray_size (gl_list_t list)
110 {
111   return list->count;
112 }
113
114 static const void *
115 gl_carray_node_value (gl_list_t list, gl_list_node_t node)
116 {
117   uintptr_t index = NODE_TO_INDEX (node);
118   size_t i;
119
120   if (!(index < list->count))
121     /* Invalid argument.  */
122     abort ();
123   i = list->offset + index;
124   if (i >= list->allocated)
125     i -= list->allocated;
126   return list->elements[i];
127 }
128
129 static gl_list_node_t
130 gl_carray_next_node (gl_list_t list, gl_list_node_t node)
131 {
132   uintptr_t index = NODE_TO_INDEX (node);
133   if (!(index < list->count))
134     /* Invalid argument.  */
135     abort ();
136   index++;
137   if (index < list->count)
138     return INDEX_TO_NODE (index);
139   else
140     return NULL;
141 }
142
143 static gl_list_node_t
144 gl_carray_previous_node (gl_list_t list, gl_list_node_t node)
145 {
146   uintptr_t index = NODE_TO_INDEX (node);
147   if (!(index < list->count))
148     /* Invalid argument.  */
149     abort ();
150   if (index > 0)
151     return INDEX_TO_NODE (index - 1);
152   else
153     return NULL;
154 }
155
156 static const void *
157 gl_carray_get_at (gl_list_t list, size_t position)
158 {
159   size_t count = list->count;
160   size_t i;
161
162   if (!(position < count))
163     /* Invalid argument.  */
164     abort ();
165   i = list->offset + position;
166   if (i >= list->allocated)
167     i -= list->allocated;
168   return list->elements[i];
169 }
170
171 static gl_list_node_t
172 gl_carray_set_at (gl_list_t list, size_t position, const void *elt)
173 {
174   size_t count = list->count;
175   size_t i;
176
177   if (!(position < count))
178     /* Invalid argument.  */
179     abort ();
180   i = list->offset + position;
181   if (i >= list->allocated)
182     i -= list->allocated;
183   list->elements[i] = elt;
184   return INDEX_TO_NODE (position);
185 }
186
187 static size_t
188 gl_carray_indexof_from_to (gl_list_t list, size_t start_index, size_t end_index,
189                            const void *elt)
190 {
191   size_t count = list->count;
192
193   if (!(start_index <= end_index && end_index <= count))
194     /* Invalid arguments.  */
195     abort ();
196
197   if (start_index < end_index)
198     {
199       gl_listelement_equals_fn equals = list->base.equals_fn;
200       size_t allocated = list->allocated;
201       size_t i_end;
202
203       i_end = list->offset + end_index;
204       if (i_end >= allocated)
205         i_end -= allocated;
206
207       if (equals != NULL)
208         {
209           size_t i;
210
211           i = list->offset + start_index;
212           if (i >= allocated) /* can only happen if start_index > 0 */
213             i -= allocated;
214
215           for (;;)
216             {
217               if (equals (elt, list->elements[i]))
218                 return (i >= list->offset ? i : i + allocated) - list->offset;
219               i++;
220               if (i == allocated)
221                 i = 0;
222               if (i == i_end)
223                 break;
224             }
225         }
226       else
227         {
228           size_t i;
229
230           i = list->offset + start_index;
231           if (i >= allocated) /* can only happen if start_index > 0 */
232             i -= allocated;
233
234           for (;;)
235             {
236               if (elt == list->elements[i])
237                 return (i >= list->offset ? i : i + allocated) - list->offset;
238               i++;
239               if (i == allocated)
240                 i = 0;
241               if (i == i_end)
242                 break;
243             }
244         }
245     }
246   return (size_t)(-1);
247 }
248
249 static gl_list_node_t
250 gl_carray_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
251                           const void *elt)
252 {
253   size_t index = gl_carray_indexof_from_to (list, start_index, end_index, elt);
254   return INDEX_TO_NODE (index);
255 }
256
257 /* Ensure that list->allocated > list->count.  */
258 static void
259 grow (gl_list_t list)
260 {
261   size_t new_allocated;
262   size_t memory_size;
263   const void **memory;
264
265   new_allocated = xtimes (list->allocated, 2);
266   new_allocated = xsum (new_allocated, 1);
267   memory_size = xtimes (new_allocated, sizeof (const void *));
268   if (size_overflow_p (memory_size))
269     /* Overflow, would lead to out of memory.  */
270     xalloc_die ();
271   if (list->offset > 0 && list->count > 0)
272     {
273       memory = (const void **) xmalloc (memory_size);
274       if (memory == NULL)
275         /* Out of memory.  */
276         xalloc_die ();
277       if (list->offset + list->count > list->allocated)
278         {
279           memcpy (memory, &list->elements[list->offset],
280                   (list->allocated - list->offset) * sizeof (const void *));
281           memcpy (memory + (list->allocated - list->offset), list->elements,
282                   (list->offset + list->count - list->allocated)
283                   * sizeof (const void *));
284
285         }
286       else
287         memcpy (memory, &list->elements[list->offset],
288                 list->count * sizeof (const void *));
289       if (list->elements != NULL)
290         free (list->elements);
291     }
292   else
293     {
294       memory = (const void **) xrealloc (list->elements, memory_size);
295       if (memory == NULL)
296         /* Out of memory.  */
297         xalloc_die ();
298     }
299   list->elements = memory;
300   list->offset = 0;
301   list->allocated = new_allocated;
302 }
303
304 static gl_list_node_t
305 gl_carray_add_first (gl_list_t list, const void *elt)
306 {
307   size_t count = list->count;
308
309   if (count == list->allocated)
310     grow (list);
311   list->offset = (list->offset == 0 ? list->allocated : list->offset) - 1;
312   list->elements[list->offset] = elt;
313   list->count = count + 1;
314   return INDEX_TO_NODE (0);
315 }
316
317 static gl_list_node_t
318 gl_carray_add_last (gl_list_t list, const void *elt)
319 {
320   size_t count = list->count;
321   size_t i;
322
323   if (count == list->allocated)
324     grow (list);
325   i = list->offset + count;
326   if (i >= list->allocated)
327     i -= list->allocated;
328   list->elements[i] = elt;
329   list->count = count + 1;
330   return INDEX_TO_NODE (count);
331 }
332
333 static gl_list_node_t
334 gl_carray_add_at (gl_list_t list, size_t position, const void *elt)
335 {
336   size_t count = list->count;
337   const void **elements;
338
339   if (!(position <= count))
340     /* Invalid argument.  */
341     abort ();
342   if (count == list->allocated)
343     grow (list);
344   elements = list->elements;
345   if (position <= (count / 2))
346     {
347       /* Shift at most count/2 elements to the left.  */
348       size_t i2, i;
349
350       list->offset = (list->offset == 0 ? list->allocated : list->offset) - 1;
351
352       i2 = list->offset + position;
353       if (i2 >= list->allocated)
354         {
355           /* Here we must have list->offset > 0, hence list->allocated > 0.  */
356           size_t i1 = list->allocated - 1;
357           i2 -= list->allocated;
358           for (i = list->offset; i < i1; i++)
359             elements[i] = elements[i + 1];
360           elements[i1] = elements[0];
361           for (i = 0; i < i2; i++)
362             elements[i] = elements[i + 1];
363         }
364       else
365         {
366           for (i = list->offset; i < i2; i++)
367             elements[i] = elements[i + 1];
368         }
369       elements[i2] = elt;
370     }
371   else
372     {
373       /* Shift at most (count+1)/2 elements to the right.  */
374       size_t i1, i3, i;
375
376       i1 = list->offset + position;
377       i3 = list->offset + count;
378       if (i1 >= list->allocated)
379         {
380           i1 -= list->allocated;
381           i3 -= list->allocated;
382           for (i = i3; i > i1; i--)
383             elements[i] = elements[i - 1];
384         }
385       else if (i3 >= list->allocated)
386         {
387           /* Here we must have list->offset > 0, hence list->allocated > 0.  */
388           size_t i2 = list->allocated - 1;
389           i3 -= list->allocated;
390           for (i = i3; i > 0; i--)
391             elements[i] = elements[i - 1];
392           elements[0] = elements[i2];
393           for (i = i2; i > i1; i--)
394             elements[i] = elements[i - 1];
395         }
396       else
397         {
398           for (i = i3; i > i1; i--)
399             elements[i] = elements[i - 1];
400         }
401       elements[i1] = elt;
402     }
403   list->count = count + 1;
404   return INDEX_TO_NODE (position);
405 }
406
407 static gl_list_node_t
408 gl_carray_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
409 {
410   size_t count = list->count;
411   uintptr_t index = NODE_TO_INDEX (node);
412
413   if (!(index < count))
414     /* Invalid argument.  */
415     abort ();
416   return gl_carray_add_at (list, index, elt);
417 }
418
419 static gl_list_node_t
420 gl_carray_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
421 {
422   size_t count = list->count;
423   uintptr_t index = NODE_TO_INDEX (node);
424
425   if (!(index < count))
426     /* Invalid argument.  */
427     abort ();
428   return gl_carray_add_at (list, index + 1, elt);
429 }
430
431 static bool
432 gl_carray_remove_at (gl_list_t list, size_t position)
433 {
434   size_t count = list->count;
435   const void **elements;
436
437   if (!(position < count))
438     /* Invalid argument.  */
439     abort ();
440   /* Here we know count > 0.  */
441   elements = list->elements;
442   if (position <= ((count - 1) / 2))
443     {
444       /* Shift at most (count-1)/2 elements to the right.  */
445       size_t i0, i2, i;
446
447       i0 = list->offset;
448       i2 = list->offset + position;
449       if (i2 >= list->allocated)
450         {
451           /* Here we must have list->offset > 0, hence list->allocated > 0.  */
452           size_t i1 = list->allocated - 1;
453           i2 -= list->allocated;
454           for (i = i2; i > 0; i--)
455             elements[i] = elements[i - 1];
456           elements[0] = elements[i1];
457           for (i = i1; i > i0; i--)
458             elements[i] = elements[i - 1];
459         }
460       else
461         {
462           for (i = i2; i > i0; i--)
463             elements[i] = elements[i - 1];
464         }
465
466       i0++;
467       list->offset = (i0 == list->allocated ? 0 : i0);
468     }
469   else
470     {
471       /* Shift at most count/2 elements to the left.  */
472       size_t i1, i3, i;
473
474       i1 = list->offset + position;
475       i3 = list->offset + count - 1;
476       if (i1 >= list->allocated)
477         {
478           i1 -= list->allocated;
479           i3 -= list->allocated;
480           for (i = i1; i < i3; i++)
481             elements[i] = elements[i + 1];
482         }
483       else if (i3 >= list->allocated)
484         {
485           /* Here we must have list->offset > 0, hence list->allocated > 0.  */
486           size_t i2 = list->allocated - 1;
487           i3 -= list->allocated;
488           for (i = i1; i < i2; i++)
489             elements[i] = elements[i + 1];
490           elements[i2] = elements[0];
491           for (i = 0; i < i3; i++)
492             elements[i] = elements[i + 1];
493         }
494       else
495         {
496           for (i = i1; i < i3; i++)
497             elements[i] = elements[i + 1];
498         }
499     }
500   list->count = count - 1;
501   return true;
502 }
503
504 static bool
505 gl_carray_remove_node (gl_list_t list, gl_list_node_t node)
506 {
507   size_t count = list->count;
508   uintptr_t index = NODE_TO_INDEX (node);
509
510   if (!(index < count))
511     /* Invalid argument.  */
512     abort ();
513   return gl_carray_remove_at (list, index);
514 }
515
516 static bool
517 gl_carray_remove (gl_list_t list, const void *elt)
518 {
519   size_t position = gl_carray_indexof_from_to (list, 0, list->count, elt);
520   if (position == (size_t)(-1))
521     return false;
522   else
523     return gl_carray_remove_at (list, position);
524 }
525
526 static void
527 gl_carray_list_free (gl_list_t list)
528 {
529   if (list->elements != NULL)
530     free (list->elements);
531   free (list);
532 }
533
534 /* --------------------- gl_list_iterator_t Data Type --------------------- */
535
536 static gl_list_iterator_t
537 gl_carray_iterator (gl_list_t list)
538 {
539   gl_list_iterator_t result;
540
541   result.vtable = list->base.vtable;
542   result.list = list;
543   result.count = list->count;
544   result.i = 0;
545   result.j = list->count;
546 #ifdef lint
547   result.p = 0;
548   result.q = 0;
549 #endif
550
551   return result;
552 }
553
554 static gl_list_iterator_t
555 gl_carray_iterator_from_to (gl_list_t list, size_t start_index, size_t end_index)
556 {
557   gl_list_iterator_t result;
558
559   if (!(start_index <= end_index && end_index <= list->count))
560     /* Invalid arguments.  */
561     abort ();
562   result.vtable = list->base.vtable;
563   result.list = list;
564   result.count = list->count;
565   result.i = start_index;
566   result.j = end_index;
567 #ifdef lint
568   result.p = 0;
569   result.q = 0;
570 #endif
571
572   return result;
573 }
574
575 static bool
576 gl_carray_iterator_next (gl_list_iterator_t *iterator,
577                          const void **eltp, gl_list_node_t *nodep)
578 {
579   gl_list_t list = iterator->list;
580   if (iterator->count != list->count)
581     {
582       if (iterator->count != list->count + 1)
583         /* Concurrent modifications were done on the list.  */
584         abort ();
585       /* The last returned element was removed.  */
586       iterator->count--;
587       iterator->i--;
588       iterator->j--;
589     }
590   if (iterator->i < iterator->j)
591     {
592       size_t i = list->offset + iterator->i;
593       if (i >= list->allocated)
594         i -= list->allocated;
595       *eltp = list->elements[i];
596       if (nodep != NULL)
597         *nodep = INDEX_TO_NODE (iterator->i);
598       iterator->i++;
599       return true;
600     }
601   else
602     return false;
603 }
604
605 static void
606 gl_carray_iterator_free (gl_list_iterator_t *iterator)
607 {
608 }
609
610 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
611
612 static size_t
613 gl_carray_sortedlist_indexof (gl_list_t list, gl_listelement_compar_fn compar,
614                               const void *elt)
615 {
616   size_t count = list->count;
617
618   if (count > 0)
619     {
620       size_t low = 0;
621       size_t high = count;
622
623       /* At each loop iteration, low < high; for indices < low the values
624          are smaller than ELT; for indices >= high the values are greater
625          than ELT.  So, if the element occurs in the list, it is at
626          low <= position < high.  */
627       do
628         {
629           size_t mid = low + (high - low) / 2; /* low <= mid < high */
630           size_t i_mid;
631           int cmp;
632
633           i_mid = list->offset + mid;
634           if (i_mid >= list->allocated)
635             i_mid -= list->allocated;
636
637           cmp = compar (list->elements[i_mid], elt);
638
639           if (cmp < 0)
640             low = mid + 1;
641           else if (cmp > 0)
642             high = mid;
643           else /* cmp == 0 */
644             {
645               /* We have an element equal to ELT at index MID.  But we need
646                  the minimal such index.  */
647               high = mid;
648               /* At each loop iteration, low <= high and
649                    compar (list->elements[i_high], elt) == 0,
650                  and we know that the first occurrence of the element is at
651                  low <= position <= high.  */
652               while (low < high)
653                 {
654                   size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
655                   size_t i_mid2;
656                   int cmp2;
657
658                   i_mid2 = list->offset + mid2;
659                   if (i_mid2 >= list->allocated)
660                     i_mid2 -= list->allocated;
661
662                   cmp2 = compar (list->elements[i_mid2], elt);
663
664                   if (cmp2 < 0)
665                     low = mid2 + 1;
666                   else if (cmp2 > 0)
667                     /* The list was not sorted.  */
668                     abort ();
669                   else /* cmp2 == 0 */
670                     {
671                       if (mid2 == low)
672                         break;
673                       high = mid2 - 1;
674                     }
675                 }
676               return low;
677             }
678         }
679       while (low < high);
680       /* Here low == high.  */
681     }
682   return (size_t)(-1);
683 }
684
685 static gl_list_node_t
686 gl_carray_sortedlist_search (gl_list_t list, gl_listelement_compar_fn compar,
687                              const void *elt)
688 {
689   size_t index = gl_carray_sortedlist_indexof (list, compar, elt);
690   return INDEX_TO_NODE (index);
691 }
692
693 static gl_list_node_t
694 gl_carray_sortedlist_add (gl_list_t list, gl_listelement_compar_fn compar,
695                           const void *elt)
696 {
697   size_t count = list->count;
698   size_t low = 0;
699   size_t high = count;
700
701   /* At each loop iteration, low <= high; for indices < low the values are
702      smaller than ELT; for indices >= high the values are greater than ELT.  */
703   while (low < high)
704     {
705       size_t mid = low + (high - low) / 2; /* low <= mid < high */
706       size_t i_mid;
707       int cmp;
708
709       i_mid = list->offset + mid;
710       if (i_mid >= list->allocated)
711         i_mid -= list->allocated;
712
713       cmp = compar (list->elements[i_mid], elt);
714
715       if (cmp < 0)
716         low = mid + 1;
717       else if (cmp > 0)
718         high = mid;
719       else /* cmp == 0 */
720         {
721           low = mid;
722           break;
723         }
724     }
725   return gl_carray_add_at (list, low, elt);
726 }
727
728 static bool
729 gl_carray_sortedlist_remove (gl_list_t list, gl_listelement_compar_fn compar,
730                              const void *elt)
731 {
732   size_t index = gl_carray_sortedlist_indexof (list, compar, elt);
733   if (index == (size_t)(-1))
734     return false;
735   else
736     return gl_carray_remove_at (list, index);
737 }
738
739
740 const struct gl_list_implementation gl_carray_list_implementation =
741   {
742     gl_carray_create_empty,
743     gl_carray_create,
744     gl_carray_size,
745     gl_carray_node_value,
746     gl_carray_next_node,
747     gl_carray_previous_node,
748     gl_carray_get_at,
749     gl_carray_set_at,
750     gl_carray_search_from_to,
751     gl_carray_indexof_from_to,
752     gl_carray_add_first,
753     gl_carray_add_last,
754     gl_carray_add_before,
755     gl_carray_add_after,
756     gl_carray_add_at,
757     gl_carray_remove_node,
758     gl_carray_remove_at,
759     gl_carray_remove,
760     gl_carray_list_free,
761     gl_carray_iterator,
762     gl_carray_iterator_from_to,
763     gl_carray_iterator_next,
764     gl_carray_iterator_free,
765     gl_carray_sortedlist_search,
766     gl_carray_sortedlist_indexof,
767     gl_carray_sortedlist_add,
768     gl_carray_sortedlist_remove
769   };