+24
-3
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user