1 /* Ordered set data type implemented by an array.
2 Copyright (C) 2006-2007, 2009-2020 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 (set->elements[high], threshold) >= 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_update (gl_oset_t set
, const void *elt
,
272 void (*action
) (const void * /*elt*/, void * /*action_data*/),
275 /* Like gl_array_remove, action (...), gl_array_nx_add, except that we don't
276 actually remove ELT. */
277 /* Remember the old position. */
278 size_t old_index
= gl_array_indexof (set
, elt
);
280 action (elt
, action_data
);
281 /* Determine the new position. */
282 if (old_index
!= (size_t)(-1))
284 size_t count
= set
->count
;
288 gl_setelement_compar_fn compar
= set
->base
.compar_fn
;
294 size_t mid
= old_index
- 1;
295 int cmp
= (compar
!= NULL
296 ? compar (set
->elements
[mid
], elt
)
297 : (set
->elements
[mid
] > elt
? 1 :
298 set
->elements
[mid
] < elt
? -1 : 0));
311 /* Two adjacent elements are the same. */
312 gl_array_remove_at (set
, old_index
);
322 /* At each loop iteration, low <= high; for indices < low the values
323 are smaller than ELT; for indices >= high the values are greater
324 than ELT. So, if the element occurs in the list, it is at
325 low <= position < high. */
328 size_t mid
= low
+ (high
- low
) / 2; /* low <= mid < high */
329 int cmp
= (compar
!= NULL
330 ? compar (set
->elements
[mid
], elt
)
331 : (set
->elements
[mid
] > elt
? 1 :
332 set
->elements
[mid
] < elt
? -1 : 0));
340 /* Two elements are the same. */
341 gl_array_remove_at (set
, old_index
);
348 /* Move the element from old_index to low. */
349 size_t new_index
= low
;
350 const void **elements
= set
->elements
;
353 for (i
= old_index
; i
> new_index
; i
--)
354 elements
[i
] = elements
[i
- 1];
355 elements
[new_index
] = elt
;
360 /* low > old_index. */
361 /* Move the element from old_index to low - 1. */
362 size_t new_index
= low
- 1;
364 if (new_index
> old_index
)
366 const void **elements
= set
->elements
;
369 for (i
= old_index
; i
< new_index
; i
++)
370 elements
[i
] = elements
[i
+ 1];
371 elements
[new_index
] = elt
;
381 gl_array_free (gl_oset_t set
)
383 if (set
->elements
!= NULL
)
385 if (set
->base
.dispose_fn
!= NULL
)
387 size_t count
= set
->count
;
391 gl_setelement_dispose_fn dispose
= set
->base
.dispose_fn
;
392 const void **elements
= set
->elements
;
395 dispose (*elements
++);
399 free (set
->elements
);
404 /* --------------------- gl_oset_iterator_t Data Type --------------------- */
406 static gl_oset_iterator_t
407 gl_array_iterator (gl_oset_t set
)
409 gl_oset_iterator_t result
;
411 result
.vtable
= set
->base
.vtable
;
413 result
.count
= set
->count
;
414 result
.p
= set
->elements
+ 0;
415 result
.q
= set
->elements
+ set
->count
;
416 #if defined GCC_LINT || defined lint
425 gl_array_iterator_next (gl_oset_iterator_t
*iterator
, const void **eltp
)
427 gl_oset_t set
= iterator
->set
;
428 if (iterator
->count
!= set
->count
)
430 if (iterator
->count
!= set
->count
+ 1)
431 /* Concurrent modifications were done on the set. */
433 /* The last returned element was removed. */
435 iterator
->p
= (const void **) iterator
->p
- 1;
436 iterator
->q
= (const void **) iterator
->q
- 1;
438 if (iterator
->p
< iterator
->q
)
440 const void **p
= (const void **) iterator
->p
;
450 gl_array_iterator_free (gl_oset_iterator_t
*iterator
)
455 const struct gl_oset_implementation gl_array_oset_implementation
=
457 gl_array_nx_create_empty
,
460 gl_array_search_atleast
,
466 gl_array_iterator_next
,
467 gl_array_iterator_free