Add function array_splice just like function list_splice

as it can be done faster at low level.
This commit is contained in:
Patrick Pelissier committed 2018-04-15 11:41:32 +02:00
1 parent 2c9169308b
commit 12b15e0037
2 files changed
+35

No files matched your search

+19
View File
@@ -820,5 +820,24 @@
return M_HASH_FINAL (hash); \
} \
, /* no HASH */ ) \
\
static inline void \
M_C(name, _splice)(array_t a1, array_t a2) \
{ \
ARRAYI_CONTRACT(a1); \
ARRAYI_CONTRACT(a2); \
size_t newSize = a1->size + a2->size; \
if (newSize > a1->alloc) { \
type *ptr = M_GET_REALLOC oplist (type, a1->ptr, newSize); \
if (M_UNLIKELY (ptr == NULL) ) { \
M_MEMORY_FULL(sizeof (type) * newSize); \
} \
a1->ptr = ptr; \
a1->alloc = newSize; \
} \
memcpy(&a1->ptr[a1->size], &a2->ptr[0], a2->size * sizeof (type)); \
a2->size = 0; \
a1->size = newSize; \
} \
#endif
+16
View File
@@ -146,6 +146,22 @@ static void test_uint(void)
assert (array_uint_capacity(v) > 0);
array_uint_reserve(v, 0);
assert (array_uint_capacity(v) == 0);
array_uint_clean(v2);
array_uint_splice(v, v2);
assert (array_uint_size(v) == 0);
assert (array_uint_size(v2) == 0);
for(unsigned i = 0; i < 100; i++) {
array_uint_push_back(v, i);
array_uint_push_back(v2, 100+i);
}
array_uint_splice(v, v2);
assert (array_uint_size(v) == 200);
assert (array_uint_size(v2) == 0);
for(unsigned i = 0; i < 200; i++) {
assert (i == *array_uint_get(v, i));
}
array_uint_clear(v);
array_uint_clear(v2);