VLC 4.0.0-dev
vlc_vector.h
Go to the documentation of this file.
1/******************************************************************************
2 * vlc_vector.h
3 ******************************************************************************
4 * Copyright (C) 2018 VLC authors and VideoLAN
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
20#ifndef VLC_VECTOR_H
21#define VLC_VECTOR_H
22
23#include <assert.h>
24#include <stdbool.h>
25#include <stddef.h>
26
27/**
28 * \defgroup vector Vector
29 * \ingroup cext
30 * @{
31 * \file
32 * This provides convenience helpers for vectors.
33 */
34
35/**
36 * Vector struct body.
37 *
38 * A vector is a dynamic array, managed by the vlc_vector_* helpers.
39 *
40 * It is generic over the type of its items, so it is implemented as macros.
41 *
42 * To use a vector, a new type must be defined:
43 *
44 * \code
45 * struct vec_int VLC_VECTOR(int);
46 * \endcode
47 *
48 * The struct may be anonymous:
49 *
50 * \code
51 * struct VLC_VECTOR(const char *) names;
52 * \endcode
53 *
54 * It is convenient to define a typedef to an anonymous structure:
55 *
56 * \code
57 * typedef struct VLC_VECTOR(int) vec_int_t;
58 * \endcode
59 *
60 * Vector size is accessible via `vec.size`, and items are intended to be
61 * accessed directly, via `vec.data[i]`.
62 *
63 * Functions and macros having name ending with '_' are private.
64 */
65#define VLC_VECTOR(type) \
66{ \
67 size_t cap; \
68 size_t size; \
69 type *data; \
70}
71
72/**
73 * Static initializer for a vector.
74 */
75#define VLC_VECTOR_INITIALIZER { 0, 0, NULL }
77/**
78 * Initialize an empty vector.
79 */
80#define vlc_vector_init(pv) (void) \
81( \
82 /* cannot be implemented as do-while(0), called from vlc_vector_clear() */ \
83 (pv)->cap = 0, \
84 (pv)->size = 0, \
85 (pv)->data = NULL \
86)
87
88/**
89 * Destroy a vector.
90 *
91 * The vector may not be used anymore unless vlc_vector_init() is called.
92 */
93#define vlc_vector_destroy(pv) \
94 free((pv)->data)
95
96/**
97 * Clear a vector.
98 *
99 * Remove all items from the vector.
100 */
101#define vlc_vector_clear(pv) \
102( \
103 /* cannot be implemented as do-while(0), called from vlc_vector_resize_() */ \
104 vlc_vector_destroy(pv), \
105 vlc_vector_init(pv) \
106)
107
108/**
109 * The minimal allocation size, in number of items.
110 *
111 * Private.
112 */
113#define VLC_VECTOR_MINCAP_ ((size_t) 10)
115static inline size_t
116vlc_vector_min_(size_t a, size_t b)
118 return a < b ? a : b;
119}
120
121static inline size_t
122vlc_vector_max_(size_t a, size_t b)
124 return a > b ? a : b;
125}
126
127static inline size_t
128vlc_vector_between_(size_t x, size_t min, size_t max)
130 return vlc_vector_max_(min, vlc_vector_min_(max, x));
131}
132
133static inline size_t
134vlc_vector_enforce_size_t_(size_t value)
136 return value;
137}
138
139#define VLC_VECTOR_FAILFLAG_ (~(((size_t) -1) >> 1)) /* only the MSB */
141/**
142 * Realloc data and update vector fields.
143 *
144 * On reallocation success, return the reallocated array and update the vector
145 * capacity and size.
146 *
147 * On reallocation failure, return `ptr`, keep `*psize` untouched, and set the
148 * failflag in `*pcap` to indicate allocation failure (to be consumed by
149 * `vlc_vector_test_and_reset_failflag_()`).
150 *
151 * This weird behavior allows to simultaneously:
152 * - not require compiler extensions like "statement expressions"
153 * - keep the vector data, size and capacity unchanged on reallocation failure
154 * - not require output variables other than vector fields from the caller
155 * - not violate the strict aliasing rules
156 * - report the reallocation status (success or failure)
157 *
158 * Private.
159 *
160 * \param ptr the current data to realloc
161 * \param count the requested capacity, in number of items
162 * \param size the size of one item
163 * \param[in,out] pcap a pointer to the `cap` field of the vector
164 * \param[in,out] psize a pointer to the `size` field of the vector
165 * \return the reallocated array, or `ptr` if reallocation failed
166 */
167static inline void *
168vlc_vector_reallocdata_(void *ptr, size_t count, size_t size,
169 size_t *restrict pcap, size_t *restrict psize)
170{
171 void *n = vlc_reallocarray(ptr, count, size);
172 if (!n)
173 {
174 /* this vector implementation guarantees that the capacity may not
175 * exceed SIZE_MAX/2 (to prevent overflows), so we can use the MSB to
176 * report allocation failure */
177 *pcap |= VLC_VECTOR_FAILFLAG_;
178 return ptr;
179 }
180 *pcap = count;
181 *psize = vlc_vector_min_(*psize, count);
182 return n;
183}
184
185/**
186 * Test and reset the fail flag.
187 *
188 * \retval true if the flag was set
189 * \retval false if the flag was not set
190 */
191static inline bool
194 if (*pcap & VLC_VECTOR_FAILFLAG_)
195 {
196 *pcap &= ~VLC_VECTOR_FAILFLAG_;
197 return true;
198 }
199 return false;
200}
201
202/**
203 * Realloc the underlying array to `newcap`.
204 *
205 * Private.
206 *
207 * \param pv a pointer to the vector
208 * \param newcap (size_t) the requested size
209 * \retval true if no allocation failed
210 * \retval false on allocation failure (the vector is left untouched)
211 */
212#define vlc_vector_realloc_(pv, newcap) \
213( \
214 (pv)->data = vlc_vector_reallocdata_((pv)->data, newcap, \
215 sizeof(*(pv)->data), \
216 &(pv)->cap, &(pv)->size), \
217 !vlc_vector_test_and_reset_failflag_(&(pv)->cap) \
218)
219
220/**
221 * Resize the vector to `newcap` exactly.
222 *
223 * If `newcap` is 0, the vector is cleared.
224 *
225 * Private.
226 *
227 * \param pv a pointer to the vector
228 * \param newcap (size_t) the requested capacity
229 * \retval true if no allocation failed
230 * \retval false on allocation failure (the vector is left untouched)
231 */
232#define vlc_vector_resize_(pv, newcap) \
233( \
234 (pv)->cap == (newcap) /* nothing to do */ || \
235 ( \
236 (newcap) > 0 ? vlc_vector_realloc_(pv, newcap) \
237 : (vlc_vector_clear(pv), true) \
238 ) \
239)
240
241static inline size_t
242vlc_vector_growsize_(size_t value)
244 /* integer multiplication by 1.5 */
245 return value + (value >> 1);
246}
247
248/* SIZE_MAX/2 to fit in ssize_t, and so that cap*1.5 does not overflow. */
249#define vlc_vector_max_cap_(pv) (SIZE_MAX / 2 / sizeof(*(pv)->data))
251/**
252 * Increase the capacity of the vector to at least `mincap`.
253 *
254 * \param pv a pointer to the vector
255 * \param mincap (size_t) the requested capacity
256 * \retval true if no allocation failed
257 * \retval false on allocation failure (the vector is left untouched)
258 */
259#define vlc_vector_reserve(pv, mincap) \
260 /* avoid to allocate tiny arrays (< VLC_VECTOR_MINCAP_) */ \
261 vlc_vector_reserve_internal_(pv, \
262 vlc_vector_max_(mincap, VLC_VECTOR_MINCAP_))
263
264#define vlc_vector_reserve_internal_(pv, mincap) \
265( \
266 (mincap) <= (pv)->cap /* nothing to do */ || \
267 ( \
268 (mincap) <= vlc_vector_max_cap_(pv) /* not too big */ && \
269 vlc_vector_realloc_(pv, \
270 /* multiply by 1.5, force between [mincap, maxcap] */ \
271 vlc_vector_between_(vlc_vector_growsize_((pv)->cap), \
272 mincap, \
273 vlc_vector_max_cap_(pv))) \
274 ) \
275)
276
277/**
278 * Resize the vector so that its capacity equals its actual size.
279 *
280 * \param pv a pointer to the vector
281 */
282#define vlc_vector_shrink_to_fit(pv) \
283 (void) /* decreasing the size may not fail */ \
284 vlc_vector_resize_(pv, (pv)->size)
285
286/**
287 * Resize the vector down automatically.
288 *
289 * Shrink only when necessary (in practice when cap > (size+5)*1.5)
290 *
291 * \param pv a pointer to the vector
292 */
293#define vlc_vector_autoshrink(pv) (void) \
294( \
295 (pv)->cap <= VLC_VECTOR_MINCAP_ /* do not shrink to tiny length */ || \
296 (pv)->cap < vlc_vector_growsize_((pv)->size+5) /* no need to shrink */ || \
297 vlc_vector_resize_(pv, vlc_vector_max_((pv)->size+5, VLC_VECTOR_MINCAP_)) \
298)
299
300#define vlc_vector_check_same_ptr_type_(a, b) \
301 (void) ((a) == (b)) /* warn on type mismatch */
302
303/**
304 * Push an item at the end of the vector.
305 *
306 * The amortized complexity is O(1).
307 *
308 * \param pv a pointer to the vector
309 * \param item the item to append
310 * \retval true if no allocation failed
311 * \retval false on allocation failure (the vector is left untouched)
312 */
313#define vlc_vector_push(pv, item) \
314( \
315 vlc_vector_reserve(pv, (pv)->size + 1) && \
316 ( \
317 (pv)->data[(pv)->size++] = (item), \
318 true \
319 ) \
320)
321
322/**
323 * Append `count` items at the end of the vector.
324 *
325 * \param pv a pointer to the vector
326 * \param items the items array to append
327 * \param count the number of items in the array
328 * \retval true if no allocation failed
329 * \retval false on allocation failure (the vector is left untouched)
330 */
331#define vlc_vector_push_all(pv, items, count) \
332 vlc_vector_push_all_internal_(pv, items, vlc_vector_enforce_size_t_(count))
333
334#define vlc_vector_push_all_internal_(pv, items, count) \
335( \
336 assert(count), \
337 vlc_vector_check_same_ptr_type_((pv)->data, items), \
338 vlc_vector_reserve(pv, (pv)->size + (count)) && \
339 ( \
340 memcpy(&(pv)->data[(pv)->size], items, (count) * sizeof(*(pv)->data)), \
341 (pv)->size += (count), \
342 true \
343 ) \
344)
345
346/**
347 * Insert an hole of size `count` to the given index.
348 *
349 * The items in range [index; size-1] will be moved. The items in the hole are
350 * left uninitialized.
351 *
352 * \param pv a pointer to the vector
353 * \param index the index where the hole is to be inserted
354 * \param count the number of items in the hole
355 * \retval true if no allocation failed
356 * \retval false on allocation failure (the vector is left untouched)
357 */
358#define vlc_vector_insert_hole(pv, index, count) \
359 vlc_vector_insert_hole_internal_(pv, vlc_vector_enforce_size_t_(index), \
360 vlc_vector_enforce_size_t_(count))
361
362#define vlc_vector_insert_hole_internal_(pv, index, count) \
363( \
364 assert(count), \
365 vlc_vector_reserve(pv, (pv)->size + (count)) && \
366 ( \
367 (index) == (pv)->size || \
368 ( \
369 memmove(&(pv)->data[(index) + (count)], \
370 &(pv)->data[index], \
371 ((pv)->size - (index)) * sizeof(*(pv)->data)), \
372 true \
373 ) \
374 ) && \
375 ( \
376 (pv)->size += (count), \
377 true \
378 ) \
379)
380
381/**
382 * Insert an item at the given index.
383 *
384 * The items in range [index; size-1] will be moved.
385 *
386 * \param pv a pointer to the vector
387 * \param index the index where the item is to be inserted
388 * \param item the item to append
389 * \retval true if no allocation failed
390 * \retval false on allocation failure (the vector is left untouched)
391 */
392#define vlc_vector_insert(pv, index, item) \
393( \
394 vlc_vector_insert_hole(pv, index, 1) && \
395 ( \
396 (pv)->data[index] = (item), \
397 true \
398 ) \
399)
400
401/**
402 * Insert `count` items at the given index.
403 *
404 * The items in range [index; size-1] will be moved.
405 *
406 * \param pv a pointer to the vector
407 * \param index the index where the items are to be inserted
408 * \param items the items array to append
409 * \param count the number of items in the array
410 * \retval true if no allocation failed
411 * \retval false on allocation failure (the vector is left untouched)
412 */
413#define vlc_vector_insert_all(pv, index, items, count) \
414( \
415 vlc_vector_check_same_ptr_type_((pv)->data, items), \
416 vlc_vector_insert_hole(pv, index, count) && \
417 ( \
418 memcpy(&(pv)->data[index], items, (count) * sizeof(*(pv)->data)), \
419 true \
420 ) \
421)
422
423/** Reverse a char array in place. */
424static inline void
425vlc_vector_reverse_array_(char *array, size_t len)
427 for (size_t i = 0; i < len / 2; ++i)
428 {
429 char c = array[i];
430 array[i] = array[len - i - 1];
431 array[len - i - 1] = c;
432 }
433}
434
435/**
436 * Right-rotate a (char) array in place.
437 *
438 * For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
439 * distance 4 will result in {5, 6, 1, 2, 3, 4}.
440 *
441 * Private.
442 */
443static inline void
444vlc_vector_rotate_array_left_(char *array, size_t len, size_t distance)
446 vlc_vector_reverse_array_(array, distance);
447 vlc_vector_reverse_array_(&array[distance], len - distance);
448 vlc_vector_reverse_array_(array, len);
449}
450
451/**
452 * Right-rotate a (char) array in place.
453 *
454 * For example, left-rotating a char array containing {1, 2, 3, 4, 5, 6} with
455 * distance 2 will result in {5, 6, 1, 2, 3, 4}.
456 *
457 * Private.
458 */
459static inline void
460vlc_vector_rotate_array_right_(char *array, size_t len, size_t distance)
462 vlc_vector_rotate_array_left_(array, len, len - distance);
463}
464
465/**
466 * Move items in a (char) array in place.
467 *
468 * Move slice [index, count] to target.
469 */
470static inline void
471vlc_vector_move_(char *array, size_t index, size_t count, size_t target)
473 assert(count);
474 if (index < target)
475 vlc_vector_rotate_array_left_(&array[index], target - index + count,
476 count);
477 else
478 vlc_vector_rotate_array_right_(&array[target], index - target + count,
479 count);
480}
481
482/**
483 * Move a slice of items to a given target index.
484 *
485 * The items in range [index; count] will be moved so that the *new* position
486 * of the first item is `target`.
487 *
488 * \param pv a pointer to the vector
489 * \param index the index of the first item to move
490 * \param count the number of items to move
491 * \param target the new index of the moved slice
492 */
493#define vlc_vector_move_slice(pv, index, count, target) \
494 vlc_vector_move_slice_internal_(pv, \
495 vlc_vector_enforce_size_t_(index), \
496 vlc_vector_enforce_size_t_(count), \
497 vlc_vector_enforce_size_t_(target))
498
499#define vlc_vector_move_slice_internal_(pv, index, count, target) \
500 vlc_vector_move_((char *) (pv)->data, \
501 (index) * sizeof((pv)->data[0]), \
502 (count) * sizeof((pv)->data[0]), \
503 (target) * sizeof((pv)->data[0]))
504
505/**
506 * Move an item to a given target index.
507 *
508 * The items will be moved so that its *new* position is `target`.
509 *
510 * \param pv a pointer to the vector
511 * \param index the index of the item to move
512 * \param target the new index of the moved item
513 */
514#define vlc_vector_move(pv, index, target) \
515 vlc_vector_move_slice(pv, index, 1, target)
516
517/**
518 * Remove a slice of items, without shrinking the array.
519 *
520 * If you have no good reason to use the _noshrink() version, use
521 * vlc_vector_remove_slice() instead.
522 *
523 * The items in range [index+count; size-1] will be moved.
524 *
525 * \param pv a pointer to the vector
526 * \param index the index of the first item to remove
527 * \param count the number of items to remove
528 */
529#define vlc_vector_remove_slice_noshrink(pv, index, count) \
530 vlc_vector_remove_slice_noshrink_internal_(pv, \
531 vlc_vector_enforce_size_t_(index), \
532 vlc_vector_enforce_size_t_(count))
533
534#define vlc_vector_remove_slice_noshrink_internal_(pv, index, count) \
535 do { \
536 assert(count); \
537 if ((index) + (count) < (pv)->size) \
538 memmove(&(pv)->data[index], \
539 &(pv)->data[(index) + (count)], \
540 ((pv)->size - (index) - (count)) * sizeof(*(pv)->data)); \
541 (pv)->size -= (count); \
542 } while (0)
543
544/**
545 * Remove a slice of items.
546 *
547 * The items in range [index+count; size-1] will be moved.
548 *
549 * \param pv a pointer to the vector
550 * \param index the index of the first item to remove
551 * \param count the number of items to remove
552 */
553#define vlc_vector_remove_slice(pv, index, count) \
554 do { \
555 vlc_vector_remove_slice_noshrink(pv, index, count); \
556 vlc_vector_autoshrink(pv); \
557 } while (0)
558
559/**
560 * Remove an item, without shrinking the array.
561 *
562 * If you have no good reason to use the _noshrink() version, use
563 * vlc_vector_remove() instead.
564 *
565 * The items in range [index+1; size-1] will be moved.
566 *
567 * \param pv a pointer to the vector
568 * \param index the index of item to remove
569 */
570#define vlc_vector_remove_noshrink(pv, index) \
571 vlc_vector_remove_slice_noshrink(pv, index, 1)
572
573/**
574 * Remove an item.
575 *
576 * The items in range [index+1; size-1] will be moved.
577 *
578 * \param pv a pointer to the vector
579 * \param index the index of item to remove
580 */
581#define vlc_vector_remove(pv, index) \
582 do { \
583 vlc_vector_remove_noshrink(pv, index); \
584 vlc_vector_autoshrink(pv); \
585 } while (0)
586
587/**
588 * Remove an item.
589 *
590 * The removed item is replaced by the last item of the vector.
591 *
592 * This does not preserve ordering, but is O(1). This is useful when the order
593 * of items is not meaningful.
594 *
595 * \param pv a pointer to the vector
596 * \param index the index of item to remove
597 */
598#define vlc_vector_swap_remove(pv, index) \
599 do { \
600 (pv)->data[index] = (pv)->data[(pv)->size-1]; \
601 (pv)->size--; \
602 } while(0)
603
604/**
605 * Return the index of an item.
606 *
607 * Iterate over all items to find a given item.
608 *
609 * Use only for vectors of primitive types or pointers.
610 *
611 * The result is written to `*(pidx)`:
612 * - the index of the item if it is found;
613 * - -1 if it is not found.
614 *
615 * \param pv a pointer to the vector
616 * \param item the item to find (compared with ==)
617 * \param[out] pidx a pointer to the result (ssize_t *)
618 */
619#define vlc_vector_index_of(pv, item, pidx) \
620 do { \
621 ssize_t *out = pidx; /* warning/error on type mismatch */ \
622 size_t vlc_vector_find_idx_; \
623 for (vlc_vector_find_idx_ = 0; \
624 vlc_vector_find_idx_ < (pv)->size; \
625 ++vlc_vector_find_idx_) \
626 if ((pv)->data[vlc_vector_find_idx_] == (item)) \
627 break; \
628 *out = vlc_vector_find_idx_ == (pv)->size ? -1 : \
629 (ssize_t) vlc_vector_find_idx_; \
630 } while (0)
631
632/**
633 * For-each loop.
634 *
635 * Use only for vectors of primitive types or pointers (every struct would be
636 * copied for a vector of structs).
637 *
638 * \param[out] item the iteration variable
639 * \param[out] pv a pointer to the vector
640 */
641#define vlc_vector_foreach(item, pv) \
642 for (size_t vlc_vector_idx_##item = 0; \
643 vlc_vector_idx_##item < (pv)->size && \
644 ((item) = (pv)->data[vlc_vector_idx_##item], true); \
645 ++vlc_vector_idx_##item)
646
647/** @} */
648
649#endif
size_t count
Definition: core.c:403
static size_t vlc_vector_growsize_(size_t value)
Definition: vlc_vector.h:243
static void vlc_vector_reverse_array_(char *array, size_t len)
Reverse a char array in place.
Definition: vlc_vector.h:426
static bool vlc_vector_test_and_reset_failflag_(size_t *pcap)
Test and reset the fail flag.
Definition: vlc_vector.h:193
static void vlc_vector_rotate_array_right_(char *array, size_t len, size_t distance)
Right-rotate a (char) array in place.
Definition: vlc_vector.h:461
static size_t vlc_vector_enforce_size_t_(size_t value)
Definition: vlc_vector.h:135
#define VLC_VECTOR_FAILFLAG_
Definition: vlc_vector.h:140
static size_t vlc_vector_between_(size_t x, size_t min, size_t max)
Definition: vlc_vector.h:129
static void vlc_vector_rotate_array_left_(char *array, size_t len, size_t distance)
Right-rotate a (char) array in place.
Definition: vlc_vector.h:445
static size_t vlc_vector_max_(size_t a, size_t b)
Definition: vlc_vector.h:123
static void vlc_vector_move_(char *array, size_t index, size_t count, size_t target)
Move items in a (char) array in place.
Definition: vlc_vector.h:472
static void * vlc_vector_reallocdata_(void *ptr, size_t count, size_t size, size_t *restrict pcap, size_t *restrict psize)
Realloc data and update vector fields.
Definition: vlc_vector.h:169
static size_t vlc_vector_min_(size_t a, size_t b)
Definition: vlc_vector.h:117
This file is a collection of common definitions and types.
static void * vlc_reallocarray(void *ptr, size_t count, size_t size)
Definition: vlc_common.h:1163