1 /* Ordered set data type implemented by an array.
2 Copyright (C) 2006-2007, 2009-2018 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 3 of the License, or
8 (at your option) any later version.
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, see <https://www.gnu.org/licenses/>. */
21 #include "gl_array_oset.h"
25 /* Checked size_t computations. */
28 /* -------------------------- gl_oset_t Data Type -------------------------- */
30 /* Concrete gl_oset_impl type, valid for this file only. */
33 struct gl_oset_impl_base base
;
34 /* An array of ALLOCATED elements, of which the first COUNT are used.
35 0 <= COUNT <= ALLOCATED. */
36 const void **elements
;
42 gl_array_nx_create_empty (gl_oset_implementation_t implementation
,
43 gl_setelement_compar_fn compar_fn
,
44 gl_setelement_dispose_fn dispose_fn
)
46 struct gl_oset_impl
*set
=
47 (struct gl_oset_impl
*) malloc (sizeof (struct gl_oset_impl
));
52 set
->base
.vtable
= implementation
;
53 set
->base
.compar_fn
= compar_fn
;
54 set
->base
.dispose_fn
= dispose_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, it 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);
111 gl_array_search_atleast (gl_oset_t set
,
112 gl_setelement_threshold_fn threshold_fn
,
113 const void *threshold
,
116 size_t count
= set
->count
;
123 /* At each loop iteration, low < high; for indices < low the values are
124 smaller than THRESHOLD; for indices >= high the values are nonexistent.
125 So, if an element >= THRESHOLD occurs in the list, it is at
126 low <= position < high. */
129 size_t mid
= low
+ (high
- low
) / 2; /* low <= mid < high */
131 if (! threshold_fn (set
->elements
[mid
], threshold
))
135 /* We have an element >= THRESHOLD at index MID. But we need the
136 minimal such index. */
138 /* At each loop iteration, low <= high and
139 compar (list->elements[high], value) >= 0,
140 and we know that the first occurrence of the element is at
141 low <= position <= high. */
144 size_t mid2
= low
+ (high
- low
) / 2; /* low <= mid2 < high */
146 if (! threshold_fn (set
->elements
[mid2
], threshold
))
151 *eltp
= set
->elements
[low
];
160 /* Ensure that set->allocated > set->count.
161 Return 0 upon success, -1 upon out-of-memory. */
165 size_t new_allocated
;
169 new_allocated
= xtimes (set
->allocated
, 2);
170 new_allocated
= xsum (new_allocated
, 1);
171 memory_size
= xtimes (new_allocated
, sizeof (const void *));
172 if (size_overflow_p (memory_size
))
173 /* Overflow, would lead to out of memory. */
175 memory
= (const void **) realloc (set
->elements
, memory_size
);
179 set
->elements
= memory
;
180 set
->allocated
= new_allocated
;
184 /* Add the given element ELT at the given position,
185 0 <= position <= gl_oset_size (set).
186 Return 1 upon success, -1 upon out-of-memory. */
188 gl_array_nx_add_at (gl_oset_t set
, size_t position
, const void *elt
)
190 size_t count
= set
->count
;
191 const void **elements
;
194 if (count
== set
->allocated
)
197 elements
= set
->elements
;
198 for (i
= count
; i
> position
; i
--)
199 elements
[i
] = elements
[i
- 1];
200 elements
[position
] = elt
;
201 set
->count
= count
+ 1;
205 /* Remove the element at the given position,
206 0 <= position < gl_oset_size (set). */
208 gl_array_remove_at (gl_oset_t set
, size_t position
)
210 size_t count
= set
->count
;
211 const void **elements
;
214 elements
= set
->elements
;
215 if (set
->base
.dispose_fn
!= NULL
)
216 set
->base
.dispose_fn (elements
[position
]);
217 for (i
= position
+ 1; i
< count
; i
++)
218 elements
[i
- 1] = elements
[i
];
219 set
->count
= count
- 1;
223 gl_array_nx_add (gl_oset_t set
, const void *elt
)
225 size_t count
= set
->count
;
230 gl_setelement_compar_fn compar
= set
->base
.compar_fn
;
233 /* At each loop iteration, low < high; for indices < low the values
234 are smaller than ELT; for indices >= high the values are greater
235 than ELT. So, if the element occurs in the list, it is at
236 low <= position < high. */
239 size_t mid
= low
+ (high
- low
) / 2; /* low <= mid < high */
240 int cmp
= (compar
!= NULL
241 ? compar (set
->elements
[mid
], elt
)
242 : (set
->elements
[mid
] > elt
? 1 :
243 set
->elements
[mid
] < elt
? -1 : 0));
254 return gl_array_nx_add_at (set
, low
, elt
);
258 gl_array_remove (gl_oset_t set
, const void *elt
)
260 size_t index
= gl_array_indexof (set
, elt
);
261 if (index
!= (size_t)(-1))
263 gl_array_remove_at (set
, index
);
271 gl_array_free (gl_oset_t set
)
273 if (set
->elements
!= NULL
)
275 if (set
->base
.dispose_fn
!= NULL
)
277 size_t count
= set
->count
;
281 gl_setelement_dispose_fn dispose
= set
->base
.dispose_fn
;
282 const void **elements
= set
->elements
;
285 dispose (*elements
++);
289 free (set
->elements
);
294 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
296 static gl_oset_iterator_t
297 gl_array_iterator (gl_oset_t set
)
299 gl_oset_iterator_t result
;
301 result
.vtable
= set
->base
.vtable
;
303 result
.count
= set
->count
;
304 result
.p
= set
->elements
+ 0;
305 result
.q
= set
->elements
+ set
->count
;
306 #if defined GCC_LINT || defined lint
315 gl_array_iterator_next (gl_oset_iterator_t
*iterator
, const void **eltp
)
317 gl_oset_t set
= iterator
->set
;
318 if (iterator
->count
!= set
->count
)
320 if (iterator
->count
!= set
->count
+ 1)
321 /* Concurrent modifications were done on the set. */
323 /* The last returned element was removed. */
325 iterator
->p
= (const void **) iterator
->p
- 1;
326 iterator
->q
= (const void **) iterator
->q
- 1;
328 if (iterator
->p
< iterator
->q
)
330 const void **p
= (const void **) iterator
->p
;
340 gl_array_iterator_free (gl_oset_iterator_t
*iterator
)
345 const struct gl_oset_implementation gl_array_oset_implementation
=
347 gl_array_nx_create_empty
,
350 gl_array_search_atleast
,
355 gl_array_iterator_next
,
356 gl_array_iterator_free