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. */
24 #include "gl_array_oset.h"
30 /* Checked size_t computations. */
33 /* -------------------------- gl_oset_t Data Type -------------------------- */
35 /* Concrete gl_oset_impl type, valid for this file only. */
38 struct gl_oset_impl_base base;
39 /* An array of ALLOCATED elements, of which the first COUNT are used.
40 0 <= COUNT <= ALLOCATED. */
41 const void **elements;
47 gl_array_create_empty (gl_oset_implementation_t implementation,
48 gl_setelement_compar_fn compar_fn)
50 struct gl_oset_impl *set =
51 (struct gl_oset_impl *) xmalloc (sizeof (struct gl_oset_impl));
53 set->base.vtable = implementation;
54 set->base.compar_fn = compar_fn;
63 gl_array_size (gl_oset_t set)
69 gl_array_indexof (gl_oset_t set, const void *elt)
71 size_t count = set->count;
75 gl_setelement_compar_fn compar = set->base.compar_fn;
79 /* At each loop iteration, low < high; for indices < low the values
80 are smaller than ELT; for indices >= high the values are greater
81 than ELT. So, if the element occurs in the list, is at
82 low <= position < high. */
85 size_t mid = low + (high - low) / 2; /* low <= mid < high */
86 int cmp = (compar != NULL
87 ? compar (set->elements[mid], elt)
88 : (set->elements[mid] > elt ? 1 :
89 set->elements[mid] < elt ? -1 : 0));
96 /* We have an element equal to ELT at index MID. */
105 gl_array_search (gl_oset_t set, const void *elt)
107 return gl_array_indexof (set, elt) != (size_t)(-1);
110 /* Ensure that set->allocated > set->count. */
114 size_t new_allocated;
118 new_allocated = xtimes (set->allocated, 2);
119 new_allocated = xsum (new_allocated, 1);
120 memory_size = xtimes (new_allocated, sizeof (const void *));
121 if (size_overflow_p (memory_size))
122 /* Overflow, would lead to out of memory. */
124 memory = (const void **) xrealloc (set->elements, memory_size);
128 set->elements = memory;
129 set->allocated = new_allocated;
132 /* Add the given element ELT at the given position,
133 0 <= position <= gl_oset_size (set). */
135 gl_array_add_at (gl_oset_t set, size_t position, const void *elt)
137 size_t count = set->count;
138 const void **elements;
141 if (count == set->allocated)
143 elements = set->elements;
144 for (i = count; i > position; i--)
145 elements[i] = elements[i - 1];
146 elements[position] = elt;
147 set->count = count + 1;
150 /* Remove the element at the given position,
151 0 <= position < gl_oset_size (set). */
153 gl_array_remove_at (gl_oset_t set, size_t position)
155 size_t count = set->count;
156 const void **elements;
159 elements = set->elements;
160 for (i = position + 1; i < count; i++)
161 elements[i - 1] = elements[i];
162 set->count = count - 1;
166 gl_array_add (gl_oset_t set, const void *elt)
168 size_t count = set->count;
173 gl_setelement_compar_fn compar = set->base.compar_fn;
176 /* At each loop iteration, low < high; for indices < low the values
177 are smaller than ELT; for indices >= high the values are greater
178 than ELT. So, if the element occurs in the list, is at
179 low <= position < high. */
182 size_t mid = low + (high - low) / 2; /* low <= mid < high */
183 int cmp = (compar != NULL
184 ? compar (set->elements[mid], elt)
185 : (set->elements[mid] > elt ? 1 :
186 set->elements[mid] < elt ? -1 : 0));
197 gl_array_add_at (set, low, elt);
202 gl_array_remove (gl_oset_t set, const void *elt)
204 size_t index = gl_array_indexof (set, elt);
205 if (index != (size_t)(-1))
207 gl_array_remove_at (set, index);
215 gl_array_free (gl_oset_t set)
217 if (set->elements != NULL)
218 free (set->elements);
222 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
224 static gl_oset_iterator_t
225 gl_array_iterator (gl_oset_t set)
227 gl_oset_iterator_t result;
229 result.vtable = set->base.vtable;
231 result.count = set->count;
232 result.p = set->elements + 0;
233 result.q = set->elements + set->count;
239 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
241 gl_oset_t set = iterator->set;
242 if (iterator->count != set->count)
244 if (iterator->count != set->count + 1)
245 /* Concurrent modifications were done on the set. */
247 /* The last returned element was removed. */
252 if (iterator->p < iterator->q)
254 const void **p = (const void **) iterator->p;
264 gl_array_iterator_free (gl_oset_iterator_t *iterator)
269 const struct gl_oset_implementation gl_array_oset_implementation =
271 gl_array_create_empty,
278 gl_array_iterator_next,
279 gl_array_iterator_free