Compare commits

...

2 commits

3 changed files with 57 additions and 60 deletions

View file

@ -27,19 +27,18 @@
#include "../Utils/Error.h" #include "../Utils/Error.h"
typedef struct FledastyQueueNode {
void *value;
struct FledastyQueueNode *previous, *next;
} FledastyQueueNode;
typedef struct { typedef struct {
size_t size; size_t size, capacity;
FledastyQueueNode *start, *end; size_t head, tail;
size_t element_byte_size;
unsigned char *buffer;
} FledastyQueue; } FledastyQueue;
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue); FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size);
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); FledastyError fledasty_queue_destroy(FledastyQueue *current_queue);
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size); FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
void *fledasty_queue_peek(FledastyQueue *current_queue);
#endif #endif

View file

@ -12,9 +12,10 @@
* limitations under the License. * limitations under the License.
* *
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
* File: Main.c * File: Queue.c
* Description: * Description:
* Executes all tests. * This file contains the functions for modifying the queue. It includes functions
* to Push, Pop, Peek and check if empty.
* *
* Author: Mineplay * Author: Mineplay
* ----------------------------------------------------------------------------- * -----------------------------------------------------------------------------
@ -22,17 +23,26 @@
#include "../../Include/Fledasty/Core/Queue.h" #include "../../Include/Fledasty/Core/Queue.h"
#include <Hallocy/Core/Allocator.h> #include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h> #include <Hallocy/Utils/Error.h>
#include <string.h> #include <string.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) { FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size) {
if (new_queue == NULL) { if (new_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER; return FLEDASTY_ERROR_INVALID_POINTER;
} }
new_queue->size = 0; new_queue->size = 0;
new_queue->start = NULL; new_queue->capacity = 10;
new_queue->end = NULL; new_queue->element_byte_size = element_byte_size;
new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
if (new_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_queue->head = 0;
new_queue->tail = 0;
return FLEDASTY_ERROR_NONE; return FLEDASTY_ERROR_NONE;
} }
@ -42,56 +52,48 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
return FLEDASTY_ERROR_INVALID_POINTER; return FLEDASTY_ERROR_INVALID_POINTER;
} }
FledastyQueueNode *previousNode = NULL; HallocyError result = hallocy_free(current_queue->buffer);
FledastyQueueNode *current_node = current_queue->start; if (result != HALLOCY_ERROR_NONE) {
while (current_node != NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION;
previousNode = current_node;
current_node = current_node->next;
HallocyError error = hallocy_free(previousNode->value);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
error = hallocy_free(previousNode);
if (error != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
} }
current_queue->size = 0; current_queue->buffer = NULL;
current_queue->start = NULL;
current_queue->end = NULL;
return FLEDASTY_ERROR_NONE; return FLEDASTY_ERROR_NONE;
} }
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size) { FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
if (current_queue == NULL || value == NULL) { if (current_queue == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER; return FLEDASTY_ERROR_INVALID_POINTER;
} }
FledastyQueueNode *new_queue_node = (FledastyQueueNode*)hallocy_malloc(sizeof(FledastyQueueNode)); if (current_queue->size == current_queue->capacity) {
if (new_queue_node == NULL) { current_queue->capacity = current_queue->capacity + (current_queue->capacity / 2);
return FLEDASTY_ERROR_FAILED_ALLOCATION; current_queue->buffer = (unsigned char*)hallocy_realloc(current_queue->buffer, current_queue->capacity * current_queue->element_byte_size);
if (current_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
size_t new_tail = current_queue->capacity - current_queue->tail;
hallocy_move_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), current_queue->buffer + (new_tail * current_queue->element_byte_size), current_queue->size - current_queue->tail);
current_queue->tail = new_tail;
} }
new_queue_node->value = (FledastyQueueNode*)hallocy_malloc(size); hallocy_copy_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), value, current_queue->element_byte_size);
hallocy_copy_memory(new_queue_node->value, value, size);
new_queue_node->next = NULL;
if (current_queue->start == NULL) {
current_queue->start = new_queue_node;
current_queue->end = new_queue_node;
new_queue_node->previous = NULL;
} else {
new_queue_node->previous = current_queue->end;
current_queue->end->next = new_queue_node;
current_queue->end = new_queue_node;
}
current_queue->size += 1; current_queue->size += 1;
current_queue->head += 1;
if (current_queue->head >= current_queue->capacity) {
current_queue->head = 0;
}
return FLEDASTY_ERROR_NONE; return FLEDASTY_ERROR_NONE;
} }
void *fledasty_queue_peek(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return NULL;
}
return current_queue->buffer + (current_queue->tail * current_queue->element_byte_size);
}

View file

@ -24,18 +24,14 @@
int main() { int main() {
FledastyQueue test_queue; FledastyQueue test_queue;
fledasty_queue_initialize(&test_queue); fledasty_queue_initialize(&test_queue, sizeof(int));
int value = 10;
for (int i = 0; i < 10; i += 1) { for (int i = 0; i < 10; i += 1) {
fledasty_queue_push(&test_queue, &i, sizeof(int)); fledasty_queue_push(&test_queue, &i);
} }
FledastyQueueNode *current_node = test_queue.start; int *peek_data = (int*)fledasty_queue_peek(&test_queue);
for (int i = 0; i < test_queue.size; i++) { printf("Peeked: %d\n", *peek_data);
printf("Element %d\n", *((int*)current_node->value));
current_node = current_node->next;
}
fledasty_queue_destroy(&test_queue); fledasty_queue_destroy(&test_queue);