Update array remove swap to handle deleting last index
This commit is contained in:
@@ -204,11 +204,15 @@ void arrayRemoveSwapEnd(WpArray array, u64 index, u64 item_size) {
|
|||||||
if (header->count == 0 || index > header->count) { return; }
|
if (header->count == 0 || index > header->count) { return; }
|
||||||
|
|
||||||
u64 last_item_index = header->count - 1;
|
u64 last_item_index = header->count - 1;
|
||||||
|
|
||||||
|
if (index < last_item_index) {
|
||||||
void *cur_item = arrayGetItemWithOffset(array, header->item_size * index);
|
void *cur_item = arrayGetItemWithOffset(array, header->item_size * index);
|
||||||
void *last_item = arrayGetItemWithOffset(array, header->item_size * last_item_index);
|
void *last_item = arrayGetItemWithOffset(array, header->item_size * last_item_index);
|
||||||
|
|
||||||
memcpy(cur_item, last_item, header->item_size);
|
memcpy(cur_item, last_item, header->item_size);
|
||||||
memset(last_item, 0, header->item_size);
|
memset(last_item, 0, header->item_size);
|
||||||
|
}
|
||||||
|
|
||||||
--(header->count);
|
--(header->count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user