65#define VLC_VECTOR(type) \
75#define VLC_VECTOR_INITIALIZER { 0, 0, NULL }
80#define vlc_vector_init(pv) (void) \
93#define vlc_vector_destroy(pv) \
101#define vlc_vector_clear(pv) \
104 vlc_vector_destroy(pv), \
105 vlc_vector_init(pv) \
113#define VLC_VECTOR_MINCAP_ ((size_t) 10)
118 return a < b ? a : b;
124 return a > b ? a : b;
139#define VLC_VECTOR_FAILFLAG_ (~(((size_t) -1) >> 1))
169 size_t *restrict pcap,
size_t *restrict psize)
196 *pcap &= ~VLC_VECTOR_FAILFLAG_;
212#define vlc_vector_realloc_(pv, newcap) \
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) \
232#define vlc_vector_resize_(pv, newcap) \
234 (pv)->cap == (newcap) || \
236 (newcap) > 0 ? vlc_vector_realloc_(pv, newcap) \
237 : (vlc_vector_clear(pv), true) \
245 return value + (value >> 1);
249#define vlc_vector_max_cap_(pv) (SIZE_MAX / 2 / sizeof(*(pv)->data))
259#define vlc_vector_reserve(pv, mincap) \
261 vlc_vector_reserve_internal_(pv, \
262 vlc_vector_max_(mincap, VLC_VECTOR_MINCAP_))
264#define vlc_vector_reserve_internal_(pv, mincap) \
266 (mincap) <= (pv)->cap || \
268 (mincap) <= vlc_vector_max_cap_(pv) && \
269 vlc_vector_realloc_(pv, \
271 vlc_vector_between_(vlc_vector_growsize_((pv)->cap), \
273 vlc_vector_max_cap_(pv))) \
282#define vlc_vector_shrink_to_fit(pv) \
284 vlc_vector_resize_(pv, (pv)->size)
293#define vlc_vector_autoshrink(pv) (void) \
295 (pv)->cap <= VLC_VECTOR_MINCAP_ || \
296 (pv)->cap < vlc_vector_growsize_((pv)->size+5) || \
297 vlc_vector_resize_(pv, vlc_vector_max_((pv)->size+5, VLC_VECTOR_MINCAP_)) \
300#define vlc_vector_check_same_ptr_type_(a, b) \
313#define vlc_vector_push(pv, item) \
315 vlc_vector_reserve(pv, (pv)->size + 1) && \
317 (pv)->data[(pv)->size++] = (item), \
333#define vlc_vector_push_hole(pv, count) \
335 vlc_vector_reserve(pv, (pv)->size + vlc_vector_enforce_size_t_(count)) && \
337 (pv)->size += vlc_vector_enforce_size_t_(count), \
351#define vlc_vector_push_all(pv, items, count) \
352 vlc_vector_push_all_internal_(pv, items, vlc_vector_enforce_size_t_(count))
354#define vlc_vector_push_all_internal_(pv, items, count) \
357 vlc_vector_check_same_ptr_type_((pv)->data, items), \
358 vlc_vector_reserve(pv, (pv)->size + (count)) && \
360 memcpy(&(pv)->data[(pv)->size], items, (count) * sizeof(*(pv)->data)), \
361 (pv)->size += (count), \
378#define vlc_vector_insert_hole(pv, index, count) \
379 vlc_vector_insert_hole_internal_(pv, vlc_vector_enforce_size_t_(index), \
380 vlc_vector_enforce_size_t_(count))
382#define vlc_vector_insert_hole_internal_(pv, index, count) \
385 vlc_vector_reserve(pv, (pv)->size + (count)) && \
387 (index) == (pv)->size || \
389 memmove(&(pv)->data[(index) + (count)], \
390 &(pv)->data[index], \
391 ((pv)->size - (index)) * sizeof(*(pv)->data)), \
396 (pv)->size += (count), \
412#define vlc_vector_insert(pv, index, item) \
414 vlc_vector_insert_hole(pv, index, 1) && \
416 (pv)->data[index] = (item), \
433#define vlc_vector_insert_all(pv, index, items, count) \
435 vlc_vector_check_same_ptr_type_((pv)->data, items), \
436 vlc_vector_insert_hole(pv, index, count) && \
438 memcpy(&(pv)->data[index], items, (count) * sizeof(*(pv)->data)), \
447 for (
size_t i = 0; i < len / 2; ++i)
450 array[i] = array[len - i - 1];
451 array[len - i - 1] = c;
513#define vlc_vector_move_slice(pv, index, count, target) \
514 vlc_vector_move_slice_internal_(pv, \
515 vlc_vector_enforce_size_t_(index), \
516 vlc_vector_enforce_size_t_(count), \
517 vlc_vector_enforce_size_t_(target))
519#define vlc_vector_move_slice_internal_(pv, index, count, target) \
520 vlc_vector_move_((char *) (pv)->data, \
521 (index) * sizeof((pv)->data[0]), \
522 (count) * sizeof((pv)->data[0]), \
523 (target) * sizeof((pv)->data[0]))
534#define vlc_vector_move(pv, index, target) \
535 vlc_vector_move_slice(pv, index, 1, target)
549#define vlc_vector_remove_slice_noshrink(pv, index, count) \
550 vlc_vector_remove_slice_noshrink_internal_(pv, \
551 vlc_vector_enforce_size_t_(index), \
552 vlc_vector_enforce_size_t_(count))
554#define vlc_vector_remove_slice_noshrink_internal_(pv, index, count) \
557 if ((index) + (count) < (pv)->size) \
558 memmove(&(pv)->data[index], \
559 &(pv)->data[(index) + (count)], \
560 ((pv)->size - (index) - (count)) * sizeof(*(pv)->data)); \
561 (pv)->size -= (count); \
573#define vlc_vector_remove_slice(pv, index, count) \
575 vlc_vector_remove_slice_noshrink(pv, index, count); \
576 vlc_vector_autoshrink(pv); \
590#define vlc_vector_remove_noshrink(pv, index) \
591 vlc_vector_remove_slice_noshrink(pv, index, 1)
601#define vlc_vector_remove(pv, index) \
603 vlc_vector_remove_noshrink(pv, index); \
604 vlc_vector_autoshrink(pv); \
618#define vlc_vector_swap_remove(pv, index) \
620 (pv)->data[index] = (pv)->data[(pv)->size-1]; \
639#define vlc_vector_index_of(pv, item, pidx) \
641 ssize_t *out = pidx; \
642 size_t vlc_vector_find_idx_; \
643 for (vlc_vector_find_idx_ = 0; \
644 vlc_vector_find_idx_ < (pv)->size; \
645 ++vlc_vector_find_idx_) \
646 if ((pv)->data[vlc_vector_find_idx_] == (item)) \
648 *out = vlc_vector_find_idx_ == (pv)->size ? -1 : \
649 (ssize_t) vlc_vector_find_idx_; \
661#define vlc_vector_foreach(item, pv) \
662 for (size_t vlc_vector_idx_##item = 0; \
663 vlc_vector_idx_##item < (pv)->size && \
664 ((item) = (pv)->data[vlc_vector_idx_##item], true); \
665 ++vlc_vector_idx_##item)
675#define vlc_vector_foreach_ref(ref, pv) \
676 for (size_t vlc_vector_idx_##ref = 0; \
677 vlc_vector_idx_##ref < (pv)->size && \
678 ((ref) = &(pv)->data[vlc_vector_idx_##ref], true); \
679 ++vlc_vector_idx_##ref)
684#define vlc_vector_last(pv) \
686 assert((pv)->size != 0), \
687 (pv)->data[(pv)->size - 1] \
693#define vlc_vector_last_ref(pv) \
695 assert((pv)->size != 0), \
696 &(pv)->data[(pv)->size - 1] \
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:446
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:481
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:465
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:492
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:1077