12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #define IMPORT_CORE
- #include "core/Queue.h"
- #include <assert.h>
- #include <stdio.h>
- #include <string.h>
- #include "core/ToString.h"
- #include "core/Utility.h"
- void initQueue(Queue* r, size_t capacity, size_t dataSize) {
- *r = ((Queue){0, 0, 0, capacity, dataSize, nullptr});
- }
- void destroyQueue(Queue* r) {
- cFree(r->data);
- *r = (Queue){0};
- }
- static void* getPointer(const Queue* r, size_t index) {
- return (char*)r->data + r->dataSize * index;
- }
- void pushQueueData(Queue* r, const void* data) {
- if(r->length >= r->capacity) {
- return;
- } else if(r->data == nullptr) {
- r->data = cAllocate(r->capacity * r->dataSize);
- }
- memcpy(getPointer(r, r->writeIndex), data, r->dataSize);
- r->writeIndex = (r->writeIndex + 1) % r->capacity;
- r->length++;
- }
- void* getQueueIndex(const Queue* r, size_t index) {
- return getPointer(r, (index + r->readIndex) % r->capacity);
- }
- void clearQueue(Queue* r) {
- r->writeIndex = 0;
- r->readIndex = 0;
- r->length = 0;
- }
- void popQueueData(Queue* r) {
- assert(r->length > 0);
- r->length--;
- r->readIndex = (r->readIndex + 1) % r->capacity;
- }
|