Add wpArrayRemoveSwapEnd
Release / release (push) Successful in 3s

This commit is contained in:
2026-09-19 22:41:10 +01:00
parent 9029e21a4f
commit 3c81feedbb
8 changed files with 56 additions and 3 deletions
+1
View File
@@ -25,6 +25,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `wpPrngSplitmix64Init`, `wpPrngSplitmix64InitWithSeed`, `wpPrngSplitmix64`, `wpPrngSplitmix64InRange`, `wpPrngSplitmix64Choice`
- `wpStr8Clear`
- `wpArrayZero` to zero all elements in an array without setting the count to 0
- `wpArrayRemoveSwapEnd` to remove an array element using its index and swap the last element in
### Changed
+24 -3
View File
@@ -9,7 +9,8 @@
#define arrayHeader(ARRAY) (WpArrayHeader *)(wpMiscUtilsOffsetPointer(ARRAY, (i64)sizeof(WpArrayHeader) * -1))
wp_persist inline void arrayValidate(const WpArray array, u64 item_size);
wp_persist inline void arrayValidate(const WpArray array, u64 item_size);
wp_persist inline void *arrayGetItemWithOffset(const WpArray array, u64 offset);
u64 arrayCount(WpArray array) {
wpDebugAssert(array != NULL, "`array` should not be NULL");
@@ -54,7 +55,7 @@ void *arrayGet(WpArray array, u64 index, u64 item_size) {
WpArrayHeader *header = arrayHeader(array);
wpRuntimeAssert(index < header->count, "`index` is out of bounds");
return wpMiscUtilsOffsetPointer(array, header->item_size * index);
return arrayGetItemWithOffset(array, header->item_size * index);
}
void arraySet(WpArray array, u64 index, void *value, u64 item_size) {
@@ -195,6 +196,22 @@ RETURN_ARRAY_COPY_ALLOC:
return output;
}
void arrayRemoveSwapEnd(WpArray array, u64 index, u64 item_size) {
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
arrayValidate(array, item_size);
WpArrayHeader *header = arrayHeader(array);
if (header->count == 0 || index > header->count) { return; }
u64 last_item_index = header->count - 1;
void *cur_item = arrayGetItemWithOffset(array, header->item_size * index);
void *last_item = arrayGetItemWithOffset(array, header->item_size * last_item_index);
memcpy(cur_item, last_item, header->item_size);
memset(last_item, 0, header->item_size);
--(header->count);
}
void *arrayPop(WpArray array, u64 item_size) {
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
arrayValidate(array, item_size);
@@ -203,7 +220,7 @@ void *arrayPop(WpArray array, u64 item_size) {
if (header->count == 0) { return NULL; }
u64 index = header->count - 1;
void *out = arrayGet(array, index, item_size);
void *out = arrayGetItemWithOffset(array, header->item_size * index);
--(header->count);
return out;
}
@@ -283,3 +300,7 @@ wp_persist inline void arrayValidate(const WpArray array, u64 item_size) {
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
wpRuntimeAssert(item_size == header->item_size, "Invalid item type provided");
}
wp_persist inline void *arrayGetItemWithOffset(const WpArray array, u64 offset) {
return wpMiscUtilsOffsetPointer(array, offset);
}
+2
View File
@@ -168,6 +168,7 @@ typedef enum WpArrayInitFlags {
(WpArray)SRC_ARRAY, \
FLAGS, \
sizeof(TYPE)))
#define wpArrayRemoveSwapEnd(TYPE, ARRAY, INDEX) arrayRemoveSwapEnd(ARRAY, INDEX, sizeof(TYPE))
#define wpArrayZero(TYPE, ARRAY) (arrayZero((WpArray)ARRAY, sizeof(TYPE)))
#define wpArrayClear(TYPE, ARRAY) (arrayClear((WpArray)ARRAY, sizeof(TYPE)))
#define wpArrayCalcAllocSize(TYPE, CAPACITY) arrayCalcAllocSize(CAPACITY, sizeof(TYPE))
@@ -204,6 +205,7 @@ WpArray arrayExtendAlloc(const WpAllocator *allocator, WpArray dst, const WpArra
WpArrayInitFlags flags, u64 item_size);
WpArray arrayCopyAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src,
WpArrayInitFlags flags, u64 item_size);
void arrayRemoveSwapEnd(WpArray array, u64 index, u64 item_size);
void *arrayPop(WpArray array, u64 item_size);
void arrayZero(WpArray array, u64 item_size);
void arrayClear(WpArray array, u64 item_size);
+13
View File
@@ -245,6 +245,19 @@ WpTestFuncResult test_i32_array_copy_alloc(void) {
return wpTesterResult(result);
}
WpTestFuncResult test_i32_array_remove_swap_end(void) {
b8 result;
WpI32Array array = wpArray(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = wpArrayCount(array) == 9 && array[4] == 4;
wpArrayRemoveSwapEnd(i32, array, 4);
result = result && wpArrayCount(array) == 8 && array[4] == 8;
return wpTesterResult(result);
}
WpTestFuncResult test_i32_array_pop(void) {
b8 result;
+13
View File
@@ -261,6 +261,19 @@ WpTestFuncResult test_i32_array_clear(void) {
return wpTesterResult(result);
}
WpTestFuncResult test_i32_array_remove_swap_end(void) {
b8 result;
WpI32Array array = wpArray(i32, 0, 1, 2, 3, 4, 5, 6, 7, 8);
result = wpArrayCount(array) == 9 && array[4] == 4;
wpArrayRemoveSwapEnd(i32, array, 4);
result = result && wpArrayCount(array) == 8 && array[4] == 8;
return wpTesterResult(result);
}
WpTestFuncResult test_i32_array_pop(void) {
b8 result;
+1
View File
@@ -14,6 +14,7 @@ WpTestFuncResult test_i32_array_alloc_capacity(void);
WpTestFuncResult test_i32_array_append_alloc(void);
WpTestFuncResult test_i32_array_extend_alloc(void);
WpTestFuncResult test_i32_array_copy_alloc(void);
WpTestFuncResult test_i32_array_remove_swap_end(void);
WpTestFuncResult test_i32_array_pop(void);
WpTestFuncResult test_i32_array_clear(void);
+1
View File
@@ -44,6 +44,7 @@ int main(void) {
test_i32_array_append_alloc,
test_i32_array_extend_alloc,
test_i32_array_copy_alloc,
test_i32_array_remove_swap_end,
test_i32_array_pop,
test_i32_array_clear,
test_queue_push,
+1
View File
@@ -44,6 +44,7 @@ int main(void) {
test_i32_array_append_alloc,
test_i32_array_extend_alloc,
test_i32_array_copy_alloc,
test_i32_array_remove_swap_end,
test_i32_array_pop,
test_i32_array_clear,
test_queue_push,