1 /* Ordered set data type implemented by an array.
2 Copyright (C) 2006-2007 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,
47 gl_setelement_dispose_fn dispose_fn)
49 struct gl_oset_impl *set = XMALLOC (struct gl_oset_impl);
51 set->base.vtable = implementation;
52 set->base.compar_fn = compar_fn;
53 set->base.dispose_fn = dispose_fn;
62 gl_array_size (gl_oset_t set)
68 gl_array_indexof (gl_oset_t set, const void *elt)
70 size_t count = set->count;
74 gl_setelement_compar_fn compar = set->base.compar_fn;
78 /* At each loop iteration, low < high; for indices < low the values
79 are smaller than ELT; for indices >= high the values are greater
80 than ELT. So, if the element occurs in the list, it is at
81 low <= position < high. */
84 size_t mid = low + (high - low) / 2; /* low <= mid < high */
85 int cmp = (compar != NULL
86 ? compar (set->elements[mid], elt)
87 : (set->elements[mid] > elt ? 1 :
88 set->elements[mid] < elt ? -1 : 0));
95 /* We have an element equal to ELT at index MID. */
104 gl_array_search (gl_oset_t set, const void *elt)
106 return gl_array_indexof (set, elt) != (size_t)(-1);
110 gl_array_search_atleast (gl_oset_t set,
111 gl_setelement_threshold_fn threshold_fn,
112 const void *threshold,
115 size_t count = set->count;
122 /* At each loop iteration, low < high; for indices < low the values are
123 smaller than THRESHOLD; for indices >= high the values are nonexistent.
124 So, if an element >= THRESHOLD occurs in the list, it is at
125 low <= position < high. */
128 size_t mid = low + (high - low) / 2; /* low <= mid < high */
130 if (! threshold_fn (set->elements[mid], threshold))
134 /* We have an element >= THRESHOLD at index MID. But we need the
135 minimal such index. */
137 /* At each loop iteration, low <= high and
138 compar (list->elements[high], value) >= 0,
139 and we know that the first occurrence of the element is at
140 low <= position <= high. */
143 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */
145 if (! threshold_fn (set->elements[mid2], threshold))
150 *eltp = set->elements[low];
159 /* Ensure that set->allocated > set->count. */
163 size_t new_allocated;
167 new_allocated = xtimes (set->allocated, 2);
168 new_allocated = xsum (new_allocated, 1);
169 memory_size = xtimes (new_allocated, sizeof (const void *));
170 if (size_overflow_p (memory_size))
171 /* Overflow, would lead to out of memory. */
173 memory = (const void **) xrealloc (set->elements, memory_size);
177 set->elements = memory;
178 set->allocated = new_allocated;
181 /* Add the given element ELT at the given position,
182 0 <= position <= gl_oset_size (set). */
184 gl_array_add_at (gl_oset_t set, size_t position, const void *elt)
186 size_t count = set->count;
187 const void **elements;
190 if (count == set->allocated)
192 elements = set->elements;
193 for (i = count; i > position; i--)
194 elements[i] = elements[i - 1];
195 elements[position] = elt;
196 set->count = count + 1;
199 /* Remove the element at the given position,
200 0 <= position < gl_oset_size (set). */
202 gl_array_remove_at (gl_oset_t set, size_t position)
204 size_t count = set->count;
205 const void **elements;
208 elements = set->elements;
209 if (set->base.dispose_fn != NULL)
210 set->base.dispose_fn (elements[position]);
211 for (i = position + 1; i < count; i++)
212 elements[i - 1] = elements[i];
213 set->count = count - 1;
217 gl_array_add (gl_oset_t set, const void *elt)
219 size_t count = set->count;
224 gl_setelement_compar_fn compar = set->base.compar_fn;
227 /* At each loop iteration, low < high; for indices < low the values
228 are smaller than ELT; for indices >= high the values are greater
229 than ELT. So, if the element occurs in the list, it is at
230 low <= position < high. */
233 size_t mid = low + (high - low) / 2; /* low <= mid < high */
234 int cmp = (compar != NULL
235 ? compar (set->elements[mid], elt)
236 : (set->elements[mid] > elt ? 1 :
237 set->elements[mid] < elt ? -1 : 0));
248 gl_array_add_at (set, low, elt);
253 gl_array_remove (gl_oset_t set, const void *elt)
255 size_t index = gl_array_indexof (set, elt);
256 if (index != (size_t)(-1))
258 gl_array_remove_at (set, index);
266 gl_array_free (gl_oset_t set)
268 if (set->elements != NULL)
270 if (set->base.dispose_fn != NULL)
272 size_t count = set->count;
276 gl_setelement_dispose_fn dispose = set->base.dispose_fn;
277 const void **elements = set->elements;
280 dispose (*elements++);
284 free (set->elements);
289 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
291 static gl_oset_iterator_t
292 gl_array_iterator (gl_oset_t set)
294 gl_oset_iterator_t result;
296 result.vtable = set->base.vtable;
298 result.count = set->count;
299 result.p = set->elements + 0;
300 result.q = set->elements + set->count;
310 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
312 gl_oset_t set = iterator->set;
313 if (iterator->count != set->count)
315 if (iterator->count != set->count + 1)
316 /* Concurrent modifications were done on the set. */
318 /* The last returned element was removed. */
320 iterator->p = (const void **) iterator->p - 1;
321 iterator->q = (const void **) iterator->q - 1;
323 if (iterator->p < iterator->q)
325 const void **p = (const void **) iterator->p;
335 gl_array_iterator_free (gl_oset_iterator_t *iterator)
340 const struct gl_oset_implementation gl_array_oset_implementation =
342 gl_array_create_empty,
345 gl_array_search_atleast,
350 gl_array_iterator_next,
351 gl_array_iterator_free