Browse Source

vector: add vlc_vector_push_hole()

pull/162/head
Thomas Guillem 3 years ago
committed by Felix Paul Kühne
parent
commit
c44abd8199
  1. 20
      include/vlc_vector.h
  2. 16
      src/test/vector.c

20
include/vlc_vector.h

@ -319,6 +319,26 @@ vlc_vector_growsize_(size_t value)
) \
)
/**
* Push a hole at the end of the vector.
*
* The amortized complexity is O(1). The items in the hole are left
* uninitialized and can be accessed in the range [size-count; size-1].
*
* \param pv a pointer to the vector
* \param count the number of items in the hole
* \retval true if no allocation failed
* \retval false on allocation failure (the vector is left untouched)
*/
#define vlc_vector_push_hole(pv, count) \
( \
vlc_vector_reserve(pv, (pv)->size + vlc_vector_enforce_size_t_(count)) && \
( \
(pv)->size += vlc_vector_enforce_size_t_(count), \
true \
) \
)
/**
* Append `count` items at the end of the vector.
*

16
src/test/vector.c

@ -87,9 +87,15 @@ static void test_vector_push_array(void)
int items[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
ok = vlc_vector_push_all(&vec, items, 8);
assert(ok);
int items_hole[] = { 7, 6, 5, 4, 3, 2, 1, 0 };
ok = vlc_vector_push_hole(&vec, 8);
assert(ok);
assert(vec.size == 13);
for (size_t i = 0; i < 8; ++i)
vec.data[vec.size-8+i] = items_hole[i];
assert(vec.size == 21);
assert(vec.data[0] == 3);
assert(vec.data[1] == 14);
assert(vec.data[2] == 15);
@ -103,6 +109,14 @@ static void test_vector_push_array(void)
assert(vec.data[10] == 6);
assert(vec.data[11] == 7);
assert(vec.data[12] == 8);
assert(vec.data[13] == 7);
assert(vec.data[14] == 6);
assert(vec.data[15] == 5);
assert(vec.data[16] == 4);
assert(vec.data[17] == 3);
assert(vec.data[18] == 2);
assert(vec.data[19] == 1);
assert(vec.data[20] == 0);
vlc_vector_destroy(&vec);
}

Loading…
Cancel
Save