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, 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);
108 /* Ensure that set->allocated > set->count. */
112 size_t new_allocated;
116 new_allocated = xtimes (set->allocated, 2);
117 new_allocated = xsum (new_allocated, 1);
118 memory_size = xtimes (new_allocated, sizeof (const void *));
119 if (size_overflow_p (memory_size))
120 /* Overflow, would lead to out of memory. */
122 memory = (const void **) xrealloc (set->elements, memory_size);
126 set->elements = memory;
127 set->allocated = new_allocated;
130 /* Add the given element ELT at the given position,
131 0 <= position <= gl_oset_size (set). */
133 gl_array_add_at (gl_oset_t set, size_t position, const void *elt)
135 size_t count = set->count;
136 const void **elements;
139 if (count == set->allocated)
141 elements = set->elements;
142 for (i = count; i > position; i--)
143 elements[i] = elements[i - 1];
144 elements[position] = elt;
145 set->count = count + 1;
148 /* Remove the element at the given position,
149 0 <= position < gl_oset_size (set). */
151 gl_array_remove_at (gl_oset_t set, size_t position)
153 size_t count = set->count;
154 const void **elements;
157 elements = set->elements;
158 for (i = position + 1; i < count; i++)
159 elements[i - 1] = elements[i];
160 set->count = count - 1;
164 gl_array_add (gl_oset_t set, const void *elt)
166 size_t count = set->count;
171 gl_setelement_compar_fn compar = set->base.compar_fn;
174 /* At each loop iteration, low < high; for indices < low the values
175 are smaller than ELT; for indices >= high the values are greater
176 than ELT. So, if the element occurs in the list, is at
177 low <= position < high. */
180 size_t mid = low + (high - low) / 2; /* low <= mid < high */
181 int cmp = (compar != NULL
182 ? compar (set->elements[mid], elt)
183 : (set->elements[mid] > elt ? 1 :
184 set->elements[mid] < elt ? -1 : 0));
195 gl_array_add_at (set, low, elt);
200 gl_array_remove (gl_oset_t set, const void *elt)
202 size_t index = gl_array_indexof (set, elt);
203 if (index != (size_t)(-1))
205 gl_array_remove_at (set, index);
213 gl_array_free (gl_oset_t set)
215 if (set->elements != NULL)
216 free (set->elements);
220 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
222 static gl_oset_iterator_t
223 gl_array_iterator (gl_oset_t set)
225 gl_oset_iterator_t result;
227 result.vtable = set->base.vtable;
229 result.count = set->count;
230 result.p = set->elements + 0;
231 result.q = set->elements + set->count;
241 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp)
243 gl_oset_t set = iterator->set;
244 if (iterator->count != set->count)
246 if (iterator->count != set->count + 1)
247 /* Concurrent modifications were done on the set. */
249 /* The last returned element was removed. */
254 if (iterator->p < iterator->q)
256 const void **p = (const void **) iterator->p;
266 gl_array_iterator_free (gl_oset_iterator_t *iterator)
271 const struct gl_oset_implementation gl_array_oset_implementation =
273 gl_array_create_empty,
280 gl_array_iterator_next,
281 gl_array_iterator_free