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.
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_oset.h"
28 /* Checked size_t computations. */
31 /* -------------------------- gl_oset_t Data Type -------------------------- */
33 /* Concrete gl_oset_impl type, valid for this file only. */
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;
45 gl_array_create_empty (gl_oset_implementation_t implementation,
46 gl_setelement_compar_fn compar_fn)
48 struct gl_oset_impl *set =
49 (struct gl_oset_impl *) xmalloc (sizeof (struct gl_oset_impl));
51 set->base.vtable = implementation;
52 set->base.compar_fn = compar_fn;
61 gl_array_size (gl_oset_t set)
67 gl_array_indexof (gl_oset_t set, const void *elt)
69 size_t count = set->count;
73 gl_setelement_compar_fn compar = set->base.compar_fn;
77 /* At each loop iteration, low < high; for indices < low the values
78 are smaller than ELT; for indices >= high the values are greater
79 than ELT. So, if the element occurs in the list, it is at
80 low <= position < high. */
83 size_t mid = low + (high - low) / 2; /* low <= mid < high */
84 int cmp = (compar != NULL
85 ? compar (set->elements[mid], elt)
86 : (set->elements[mid] > elt ? 1 :
87 set->elements[mid] < elt ? -1 : 0));
94 /* We have an element equal to ELT at index MID. */
103 gl_array_search (gl_oset_t set, const void *elt)
105 return gl_array_indexof (set, elt) != (size_t)(-1);
109 gl_array_search_atleast (gl_oset_t set,
110 gl_setelement_threshold_fn threshold_fn,
111 const void *threshold,
114 size_t count = set->count;
121 /* At each loop iteration, low < high; for indices < low the values are
122 smaller than THRESHOLD; for indices >= high the values are nonexistent.
123 So, if an element >= THRESHOLD occurs in the list, it is at
124 low <= position < high. */
127 size_t mid = low + (high - low) / 2; /* low <= mid < high */
129 if (! threshold_fn (set->elements[mid], threshold))
133 /* We have an element >= THRESHOLD at index MID. But we need the
134 minimal such index. */
136 /* At each loop iteration, low <= high and
137 compar (list->elements[high], value) >= 0,
138 and we know that the first occurrence of the element is at
139 low <= position <= high. */
142 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
144 if (! threshold_fn (set->elements[mid2], threshold))
149 *eltp = set->elements[low];
158 /* Ensure that set->allocated > set->count. */
162 size_t new_allocated;
166 new_allocated = xtimes (set->allocated, 2);
167 new_allocated = xsum (new_allocated, 1);
168 memory_size = xtimes (new_allocated, sizeof (const void *));
169 if (size_overflow_p (memory_size))
170 /* Overflow, would lead to out of memory. */
172 memory = (const void **) xrealloc (set->elements, memory_size);
176 set->elements = memory;
177 set->allocated = new_allocated;
180 /* Add the given element ELT at the given position,
181 0 <= position <= gl_oset_size (set). */
183 gl_array_add_at (gl_oset_t set, size_t position, const void *elt)
185 size_t count = set->count;
186 const void **elements;
189 if (count == set->allocated)
191 elements = set->elements;
192 for (i = count; i > position; i--)
193 elements[i] = elements[i - 1];
194 elements[position] = elt;
195 set->count = count + 1;
198 /* Remove the element at the given position,
199 0 <= position < gl_oset_size (set). */
201 gl_array_remove_at (gl_oset_t set, size_t position)
203 size_t count = set->count;
204 const void **elements;
207 elements = set->elements;
208 for (i = position + 1; i < count; i++)
209 elements[i - 1] = elements[i];
210 set->count = count - 1;
214 gl_array_add (gl_oset_t set, const void *elt)
216 size_t count = set->count;
221 gl_setelement_compar_fn compar = set->base.compar_fn;
224 /* At each loop iteration, low < high; for indices < low the values
225 are smaller than ELT; for indices >= high the values are greater
226 than ELT. So, if the element occurs in the list, it is at
227 low <= position < high. */
230 size_t mid = low + (high - low) / 2; /* low <= mid < high */
231 int cmp = (compar != NULL
232 ? compar (set->elements[mid], elt)
233 : (set->elements[mid] > elt ? 1 :
234 set->elements[mid] < elt ? -1 : 0));
245 gl_array_add_at (set, low, elt);
250 gl_array_remove (gl_oset_t set, const void *elt)
252 size_t index = gl_array_indexof (set, elt);
253 if (index != (size_t)(-1))
255 gl_array_remove_at (set, index);
263 gl_array_free (gl_oset_t set)
265 if (set->elements != NULL)
266 free (set->elements);
270 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
272 static gl_oset_iterator_t
273 gl_array_iterator (gl_oset_t set)
275 gl_oset_iterator_t result;
277 result.vtable = set->base.vtable;
279 result.count = set->count;
280 result.p = set->elements + 0;
281 result.q = set->elements + set->count;
291 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
293 gl_oset_t set = iterator->set;
294 if (iterator->count != set->count)
296 if (iterator->count != set->count + 1)
297 /* Concurrent modifications were done on the set. */
299 /* The last returned element was removed. */
304 if (iterator->p < iterator->q)
306 const void **p = (const void **) iterator->p;
316 gl_array_iterator_free (gl_oset_iterator_t *iterator)
321 const struct gl_oset_implementation gl_array_oset_implementation =
323 gl_array_create_empty,
326 gl_array_search_atleast,
331 gl_array_iterator_next,
332 gl_array_iterator_free