* lib/stdint_.h (uintmax_t): Fix typo: int64_t -> uint64_t.
[gnulib.git] / lib / gl_array_oset.c
1 /* Ordered set data type implemented by an 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_array_oset.h"
23
24 #include <stdlib.h>
25
26 #include "xalloc.h"
27
28 /* Checked size_t computations.  */
29 #include "xsize.h"
30
31 /* -------------------------- gl_oset_t Data Type -------------------------- */
32
33 /* Concrete gl_oset_impl type, valid for this file only.  */
34 struct gl_oset_impl
35 {
36   struct gl_oset_impl_base base;
37   /* An array of ALLOCATED elements, of which the first COUNT are used.
38      0 <= COUNT <= ALLOCATED.  */
39   const void **elements;
40   size_t count;
41   size_t allocated;
42 };
43
44 static gl_oset_t
45 gl_array_create_empty (gl_oset_implementation_t implementation,
46                        gl_setelement_compar_fn compar_fn)
47 {
48   struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl);
49
50   set->base.vtable = implementation;
51   set->base.compar_fn = compar_fn;
52   set->elements = NULL;
53   set->count = 0;
54   set->allocated = 0;
55
56   return set;
57 }
58
59 static size_t
60 gl_array_size (gl_oset_t set)
61 {
62   return set->count;
63 }
64
65 static size_t
66 gl_array_indexof (gl_oset_t set, const void *elt)
67 {
68   size_t count = set->count;
69
70   if (count > 0)
71     {
72       gl_setelement_compar_fn compar = set->base.compar_fn;
73       size_t low = 0;
74       size_t high = count;
75
76       /* At each loop iteration, low < high; for indices < low the values
77          are smaller than ELT; for indices >= high the values are greater
78          than ELT.  So, if the element occurs in the list, it is at
79          low <= position < high.  */
80       do
81         {
82           size_t mid = low + (high - low) / 2; /* low <= mid < high */
83           int cmp = (compar != NULL
84                      ? compar (set->elements[mid], elt)
85                      : (set->elements[mid] > elt ? 1 :
86                         set->elements[mid] < elt ? -1 : 0));
87
88           if (cmp < 0)
89             low = mid + 1;
90           else if (cmp > 0)
91             high = mid;
92           else /* cmp == 0 */
93             /* We have an element equal to ELT at index MID.  */
94             return mid;
95         }
96       while (low < high);
97     }
98   return (size_t)(-1);
99 }
100
101 static bool
102 gl_array_search (gl_oset_t set, const void *elt)
103 {
104   return gl_array_indexof (set, elt) != (size_t)(-1);
105 }
106
107 static bool
108 gl_array_search_atleast (gl_oset_t set,
109                          gl_setelement_threshold_fn threshold_fn,
110                          const void *threshold,
111                          const void **eltp)
112 {
113   size_t count = set->count;
114
115   if (count > 0)
116     {
117       size_t low = 0;
118       size_t high = count;
119
120       /* At each loop iteration, low < high; for indices < low the values are
121          smaller than THRESHOLD; for indices >= high the values are nonexistent.
122          So, if an element >= THRESHOLD occurs in the list, it is at
123          low <= position < high.  */
124       do
125         {
126           size_t mid = low + (high - low) / 2; /* low <= mid < high */
127
128           if (! threshold_fn (set->elements[mid], threshold))
129             low = mid + 1;
130           else
131             {
132               /* We have an element >= THRESHOLD at index MID.  But we need the
133                  minimal such index.  */
134               high = mid;
135               /* At each loop iteration, low <= high and
136                    compar (list->elements[high], value) >= 0,
137                  and we know that the first occurrence of the element is at
138                  low <= position <= high.  */
139               while (low < high)
140                 {
141                   size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
142
143                   if (! threshold_fn (set->elements[mid2], threshold))
144                     low = mid2 + 1;
145                   else
146                     high = mid2;
147                 }
148               *eltp = set->elements[low];
149               return true;
150             }
151         }
152       while (low < high);
153     }
154   return false;
155 }
156
157 /* Ensure that set->allocated > set->count.  */
158 static void
159 grow (gl_oset_t set)
160 {
161   size_t new_allocated;
162   size_t memory_size;
163   const void **memory;
164
165   new_allocated = xtimes (set->allocated, 2);
166   new_allocated = xsum (new_allocated, 1);
167   memory_size = xtimes (new_allocated, sizeof (const void *));
168   if (size_overflow_p (memory_size))
169     /* Overflow, would lead to out of memory.  */
170     xalloc_die ();
171   memory = (const void **) xrealloc (set->elements, memory_size);
172   if (memory == NULL)
173     /* Out of memory.  */
174     xalloc_die ();
175   set->elements = memory;
176   set->allocated = new_allocated;
177 }
178
179 /* Add the given element ELT at the given position,
180    0 <= position <= gl_oset_size (set).  */
181 static inline void
182 gl_array_add_at (gl_oset_t set, size_t position, const void *elt)
183 {
184   size_t count = set->count;
185   const void **elements;
186   size_t i;
187
188   if (count == set->allocated)
189     grow (set);
190   elements = set->elements;
191   for (i = count; i > position; i--)
192     elements[i] = elements[i - 1];
193   elements[position] = elt;
194   set->count = count + 1;
195 }
196
197 /* Remove the element at the given position,
198    0 <= position < gl_oset_size (set).  */
199 static inline void
200 gl_array_remove_at (gl_oset_t set, size_t position)
201 {
202   size_t count = set->count;
203   const void **elements;
204   size_t i;
205
206   elements = set->elements;
207   for (i = position + 1; i < count; i++)
208     elements[i - 1] = elements[i];
209   set->count = count - 1;
210 }
211
212 static bool
213 gl_array_add (gl_oset_t set, const void *elt)
214 {
215   size_t count = set->count;
216   size_t low = 0;
217
218   if (count > 0)
219     {
220       gl_setelement_compar_fn compar = set->base.compar_fn;
221       size_t high = count;
222
223       /* At each loop iteration, low < high; for indices < low the values
224          are smaller than ELT; for indices >= high the values are greater
225          than ELT.  So, if the element occurs in the list, it is at
226          low <= position < high.  */
227       do
228         {
229           size_t mid = low + (high - low) / 2; /* low <= mid < high */
230           int cmp = (compar != NULL
231                      ? compar (set->elements[mid], elt)
232                      : (set->elements[mid] > elt ? 1 :
233                         set->elements[mid] < elt ? -1 : 0));
234
235           if (cmp < 0)
236             low = mid + 1;
237           else if (cmp > 0)
238             high = mid;
239           else /* cmp == 0 */
240             return false;
241         }
242       while (low < high);
243     }
244   gl_array_add_at (set, low, elt);
245   return true;
246 }
247
248 static bool
249 gl_array_remove (gl_oset_t set, const void *elt)
250 {
251   size_t index = gl_array_indexof (set, elt);
252   if (index != (size_t)(-1))
253     {
254       gl_array_remove_at (set, index);
255       return true;
256     }
257   else
258     return false;
259 }
260
261 static void
262 gl_array_free (gl_oset_t set)
263 {
264   if (set->elements != NULL)
265     free (set->elements);
266   free (set);
267 }
268
269 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
270
271 static gl_oset_iterator_t
272 gl_array_iterator (gl_oset_t set)
273 {
274   gl_oset_iterator_t result;
275
276   result.vtable = set->base.vtable;
277   result.set = set;
278   result.count = set->count;
279   result.p = set->elements + 0;
280   result.q = set->elements + set->count;
281 #ifdef lint
282   result.i = 0;
283   result.j = 0;
284 #endif
285
286   return result;
287 }
288
289 static bool
290 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
291 {
292   gl_oset_t set = iterator->set;
293   if (iterator->count != set->count)
294     {
295       if (iterator->count != set->count + 1)
296         /* Concurrent modifications were done on the set.  */
297         abort ();
298       /* The last returned element was removed.  */
299       iterator->count--;
300       iterator->p--;
301       iterator->q--;
302     }
303   if (iterator->p < iterator->q)
304     {
305       const void **p = (const void **) iterator->p;
306       *eltp = *p;
307       iterator->p = p + 1;
308       return true;
309     }
310   else
311     return false;
312 }
313
314 static void
315 gl_array_iterator_free (gl_oset_iterator_t *iterator)
316 {
317 }
318
319
320 const struct gl_oset_implementation gl_array_oset_implementation =
321   {
322     gl_array_create_empty,
323     gl_array_size,
324     gl_array_search,
325     gl_array_search_atleast,
326     gl_array_add,
327     gl_array_remove,
328     gl_array_free,
329     gl_array_iterator,
330     gl_array_iterator_next,
331     gl_array_iterator_free
332   };