Rename queue
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
#include "../../common/misc/misc_utils.h"
|
||||
#include <string.h>
|
||||
|
||||
void _queue_push(GenericQueue *queue, void *item, u64 item_size) {
|
||||
void _queuePush(WpQueue *queue, void *item, u64 item_size) {
|
||||
wpDebugAssert(queue != NULL, "`queue` should not be NULL");
|
||||
wpRuntimeAssert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
@@ -22,12 +22,12 @@ void _queue_push(GenericQueue *queue, void *item, u64 item_size) {
|
||||
}
|
||||
}
|
||||
|
||||
GenericQueue *_queue_push_alloc(const WpAllocator *allocator, GenericQueue *queue, void *item, u64 item_size) {
|
||||
WpQueue *_queuePushAlloc(const WpAllocator *allocator, WpQueue *queue, void *item, u64 item_size) {
|
||||
wpDebugAssert(allocator != NULL && queue != NULL && item != NULL,
|
||||
"`allocator`, `queue` and `item` should not be NULL");
|
||||
wpRuntimeAssert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
GenericQueue *output = queue;
|
||||
WpQueue *output = queue;
|
||||
|
||||
u64 capacity = wapp_array_capacity(queue->items);
|
||||
|
||||
@@ -36,7 +36,7 @@ GenericQueue *_queue_push_alloc(const WpAllocator *allocator, GenericQueue *queu
|
||||
if (queue_full) {
|
||||
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(capacity * 2);
|
||||
u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
|
||||
u64 alloc_size = sizeof(GenericQueue) + array_size;
|
||||
u64 alloc_size = sizeof(WpQueue) + array_size;
|
||||
void *buffer = wpMemAllocatorAlloc(allocator, alloc_size);
|
||||
if (!buffer) {
|
||||
goto RETURN_QUEUE_PUSH_ALLOC;
|
||||
@@ -44,7 +44,7 @@ GenericQueue *_queue_push_alloc(const WpAllocator *allocator, GenericQueue *queu
|
||||
|
||||
memset((void *)buffer, 0, alloc_size);
|
||||
|
||||
output = (GenericQueue *)buffer;
|
||||
output = (WpQueue *)buffer;
|
||||
output->items = _array_from_preallocated_buffer((void *)(output + 1), array_size, ARRAY_INIT_FILLED, item_size);
|
||||
|
||||
// NOTE (Abdelrahman): When the queue is full, the front and back indices should
|
||||
@@ -84,13 +84,13 @@ GenericQueue *_queue_push_alloc(const WpAllocator *allocator, GenericQueue *queu
|
||||
output->count = queue->count;
|
||||
}
|
||||
|
||||
_queue_push(output, item, item_size);
|
||||
_queuePush(output, item, item_size);
|
||||
|
||||
RETURN_QUEUE_PUSH_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
void *_queue_pop(GenericQueue *queue, u64 item_size) {
|
||||
void *_queuePop(WpQueue *queue, u64 item_size) {
|
||||
wpDebugAssert(queue != NULL, "`queue` should not be NULL");
|
||||
wpRuntimeAssert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
|
||||
+39
-39
@@ -17,33 +17,33 @@ typedef struct {
|
||||
u64 front;
|
||||
u64 back;
|
||||
u64 count;
|
||||
} GenericQueue;
|
||||
} WpQueue;
|
||||
|
||||
// NOTE (Abdelrahman): GenericQueue typedefs for readability
|
||||
typedef GenericQueue VoidPtrQueue;
|
||||
typedef GenericQueue C8Queue;
|
||||
typedef GenericQueue C16Queue;
|
||||
typedef GenericQueue C32Queue;
|
||||
typedef GenericQueue U8Queue;
|
||||
typedef GenericQueue U16Queue;
|
||||
typedef GenericQueue U32Queue;
|
||||
typedef GenericQueue U64Queue;
|
||||
typedef GenericQueue B8Queue;
|
||||
typedef GenericQueue I8Queue;
|
||||
typedef GenericQueue I16Queue;
|
||||
typedef GenericQueue I32Queue;
|
||||
typedef GenericQueue I64Queue;
|
||||
typedef GenericQueue F32Queue;
|
||||
typedef GenericQueue F64Queue;
|
||||
typedef GenericQueue F128Queue;
|
||||
typedef GenericQueue UptrQueue;
|
||||
typedef GenericQueue IptrQueue;
|
||||
typedef GenericQueue WpStr8Queue;
|
||||
// NOTE (Abdelrahman): WpQueue typedefs for readability
|
||||
typedef WpQueue WpVoidPtrQueue;
|
||||
typedef WpQueue WpC8Queue;
|
||||
typedef WpQueue WpC16Queue;
|
||||
typedef WpQueue WpC32Queue;
|
||||
typedef WpQueue WpU8Queue;
|
||||
typedef WpQueue WpU16Queue;
|
||||
typedef WpQueue WpU32Queue;
|
||||
typedef WpQueue WpU64Queue;
|
||||
typedef WpQueue WpB8Queue;
|
||||
typedef WpQueue WpI8Queue;
|
||||
typedef WpQueue WpI16Queue;
|
||||
typedef WpQueue WpI32Queue;
|
||||
typedef WpQueue WpI64Queue;
|
||||
typedef WpQueue WpF32Queue;
|
||||
typedef WpQueue WpF64Queue;
|
||||
typedef WpQueue WpF128Queue;
|
||||
typedef WpQueue WpUptrQueue;
|
||||
typedef WpQueue WpIptrQueue;
|
||||
typedef WpQueue WpStr8Queue;
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
#define wapp_queue(TYPE, CAPACITY) ([&]() { \
|
||||
wp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED); \
|
||||
wp_persist GenericQueue queue = { \
|
||||
#define wpQueue(TYPE, CAPACITY) ([&]() { \
|
||||
wp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED); \
|
||||
wp_persist WpQueue queue = { \
|
||||
arr, \
|
||||
0, \
|
||||
0, \
|
||||
@@ -52,8 +52,8 @@ typedef GenericQueue WpStr8Queue;
|
||||
\
|
||||
return queue; \
|
||||
}())
|
||||
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
||||
wp_persist GenericQueue queue = { \
|
||||
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
||||
wp_persist WpQueue queue = { \
|
||||
wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
|
||||
0, \
|
||||
0, \
|
||||
@@ -63,13 +63,13 @@ typedef GenericQueue WpStr8Queue;
|
||||
return queue; \
|
||||
}())
|
||||
#else
|
||||
#define wapp_queue(TYPE, CAPACITY) ((GenericQueue){ \
|
||||
#define wpQueue(TYPE, CAPACITY) ((WpQueue){ \
|
||||
.items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED), \
|
||||
.front = 0, \
|
||||
.back = 0, \
|
||||
.count = 0, \
|
||||
})
|
||||
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((GenericQueue){ \
|
||||
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((WpQueue){ \
|
||||
.items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
|
||||
.front = 0, \
|
||||
.back = 0, \
|
||||
@@ -77,21 +77,21 @@ typedef GenericQueue WpStr8Queue;
|
||||
})
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
#define wapp_queue_capacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items))
|
||||
#define wapp_queue_item_size(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items))
|
||||
#define wapp_queue_push(TYPE, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queue_push(QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
#define wpQueueCapacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items))
|
||||
#define wpQueueItemSize(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items))
|
||||
#define wpQueuePush(TYPE, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queuePush(QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
#define wapp_queue_push_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queue_push_alloc(ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
#define wpQueuePushAlloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queuePushAlloc(ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
#define wapp_queue_pop(TYPE, QUEUE_PTR) ( \
|
||||
(TYPE *)_queue_pop(QUEUE_PTR, sizeof(TYPE)) \
|
||||
#define wpQueuePop(TYPE, QUEUE_PTR) ( \
|
||||
(TYPE *)_queuePop(QUEUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
|
||||
void _queue_push(GenericQueue *queue, void *item, u64 item_size);
|
||||
GenericQueue *_queue_push_alloc(const WpAllocator *allocator, GenericQueue *queue, void *item, u64 item_size);
|
||||
void *_queue_pop(GenericQueue *queue, u64 item_size);
|
||||
void _queuePush(WpQueue *queue, void *item, u64 item_size);
|
||||
WpQueue *_queuePushAlloc(const WpAllocator *allocator, WpQueue *queue, void *item, u64 item_size);
|
||||
void *_queuePop(WpQueue *queue, u64 item_size);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
+16
-16
@@ -6,11 +6,11 @@
|
||||
#define CAPACITY 8
|
||||
|
||||
WpTestFuncResult test_queue_push(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
@@ -24,40 +24,40 @@ WpTestFuncResult test_queue_push(void) {
|
||||
|
||||
WpTestFuncResult test_queue_push_alloc(void) {
|
||||
WpAllocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
i32 item = 8;
|
||||
u64 new_capacity = CAPACITY * 2;
|
||||
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
WpI32Queue *new_queue = wpQueuePushAlloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
u64 capacity = wapp_queue_capacity(&queue);
|
||||
u64 capacity = wpQueueCapacity(&queue);
|
||||
result = result && capacity == new_capacity;
|
||||
|
||||
for (u64 i = 0; i < 2; ++i) {
|
||||
wapp_queue_pop(i32, &queue);
|
||||
wpQueuePop(i32, &queue);
|
||||
}
|
||||
|
||||
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||
u64 remaining = wpQueueCapacity(&queue) - queue.count;
|
||||
for (u64 i = 0; i < remaining; ++i) {
|
||||
item = (i32)(remaining + i);
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
++item;
|
||||
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
new_queue = wpQueuePushAlloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
|
||||
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||
result = result && wpQueueCapacity(&queue) == new_capacity * 2;
|
||||
|
||||
i32 *arr = (i32 *)queue.items;
|
||||
for (u64 i = 0; i < queue.count; ++i) {
|
||||
@@ -71,26 +71,26 @@ WpTestFuncResult test_queue_push_alloc(void) {
|
||||
}
|
||||
|
||||
WpTestFuncResult test_queue_pop(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
u64 half_count = queue.count / 2;
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
i32 *value = wpQueuePop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 item = (i32)i + CAPACITY;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
i32 *value = wpQueuePop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||
}
|
||||
|
||||
|
||||
+16
-16
@@ -4,11 +4,11 @@
|
||||
#define CAPACITY 8
|
||||
|
||||
WpTestFuncResult test_queue_push(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
@@ -22,40 +22,40 @@ WpTestFuncResult test_queue_push(void) {
|
||||
|
||||
WpTestFuncResult test_queue_push_alloc(void) {
|
||||
WpAllocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
i32 item = 8;
|
||||
u64 new_capacity = CAPACITY * 2;
|
||||
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
WpI32Queue *new_queue = wpQueuePushAlloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
u64 capacity = wapp_queue_capacity(&queue);
|
||||
u64 capacity = wpQueueCapacity(&queue);
|
||||
result = result && capacity == new_capacity;
|
||||
|
||||
for (u64 i = 0; i < 2; ++i) {
|
||||
wapp_queue_pop(i32, &queue);
|
||||
wpQueuePop(i32, &queue);
|
||||
}
|
||||
|
||||
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||
u64 remaining = wpQueueCapacity(&queue) - queue.count;
|
||||
for (u64 i = 0; i < remaining; ++i) {
|
||||
item = (i32)(remaining + i);
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
++item;
|
||||
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
new_queue = wpQueuePushAlloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
|
||||
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||
result = result && wpQueueCapacity(&queue) == new_capacity * 2;
|
||||
|
||||
i32 *arr = (i32 *)queue.items;
|
||||
for (u64 i = 0; i < queue.count; ++i) {
|
||||
@@ -69,26 +69,26 @@ WpTestFuncResult test_queue_push_alloc(void) {
|
||||
}
|
||||
|
||||
WpTestFuncResult test_queue_pop(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
WpI32Queue queue = wpQueue(i32, CAPACITY);
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
u64 half_count = queue.count / 2;
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
i32 *value = wpQueuePop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 item = (i32)i + CAPACITY;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
wpQueuePush(i32, &queue, &item);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
i32 *value = wpQueuePop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user