diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h new file mode 100644 index 0000000..8b168fc --- /dev/null +++ b/Include/Fledasty/Core/Queue.h @@ -0,0 +1,47 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Queue.h + * Description: + * This file contains the Queue structure and the functions for modifying it. + * It includes functions to Push, Pop, Peek and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_QUEUE +#define FLEDASTY_QUEUE + +#include + +#include "../Utils/Error.h" + +typedef struct { + size_t size, capacity; + size_t head, tail; + + size_t element_byte_size; + unsigned char *buffer; +} FledastyQueue; + +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size); +FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); + +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); +void *fledasty_queue_peek(FledastyQueue *current_queue); +void *fledasty_queue_pop(FledastyQueue *current_queue); + +static inline size_t fledasty_queue_is_empty(FledastyQueue *current_queue) { return current_queue->size == 0; } + +#endif \ No newline at end of file diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h new file mode 100644 index 0000000..91f5dc7 --- /dev/null +++ b/Include/Fledasty/Utils/Error.h @@ -0,0 +1,32 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Error.h + * Description: + * This file contains the ErrorCode enum that defines the error codes for the + * library. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_ERROR +#define FLEDASTY_ERROR + +typedef enum { + FLEDASTY_ERROR_NONE = 0, + FLEDASTY_ERROR_FAILED_ALLOCATION = 1, + FLEDASTY_ERROR_INVALID_POINTER = 2, +} FledastyError; + +#endif \ No newline at end of file diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c new file mode 100644 index 0000000..1711c8d --- /dev/null +++ b/Src/Core/Queue.c @@ -0,0 +1,117 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Queue.c + * Description: + * This file contains the functions for modifying the queue. It includes functions + * to Push, Pop, Peek and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/Queue.h" + +#include +#include +#include +#include + +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size) { + if (new_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_queue->size = 0; + new_queue->capacity = 10; + 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; +} + +FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { + if (current_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + HallocyError result = hallocy_free(current_queue->buffer); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + current_queue->buffer = NULL; + return FLEDASTY_ERROR_NONE; +} +#include +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { + if (current_queue == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + if (current_queue->size == current_queue->capacity) { + current_queue->capacity = current_queue->capacity + (current_queue->capacity / 2); + 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; + } + printf("Allocated\n"); + size_t new_head = current_queue->capacity - current_queue->head; + hallocy_move_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->size - current_queue->tail); + current_queue->head = new_head; + } + + hallocy_copy_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), value, current_queue->element_byte_size); + + current_queue->size += 1; + current_queue->tail += 1; + if (current_queue->tail >= current_queue->capacity) { + printf("To 0\n"); + current_queue->tail = 0; + } + + return FLEDASTY_ERROR_NONE; +} + +void *fledasty_queue_peek(FledastyQueue *current_queue) { + if (current_queue == NULL) { + return NULL; + } + + return current_queue->buffer + (current_queue->head * current_queue->element_byte_size); +} + +void *fledasty_queue_pop(FledastyQueue *current_queue) { + if (current_queue == NULL || current_queue->size == 0) { + return NULL; + } + + void *value_address = current_queue->buffer + (current_queue->head * current_queue->element_byte_size); + + current_queue->size -= 1; + if (current_queue->head == current_queue->capacity) { + current_queue->head = 0; + } else { + current_queue->head += 1; + } + + return value_address; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index cde1df7..1097b69 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -20,8 +20,30 @@ * ----------------------------------------------------------------------------- */ #include +#include int main() { - printf("Test\n"); + FledastyQueue test_queue; + fledasty_queue_initialize(&test_queue, sizeof(int)); + + for (int i = 0; i < 10; i += 1) { + fledasty_queue_push(&test_queue, &i); + } + + int *peek_data = (int*)fledasty_queue_peek(&test_queue); + printf("Peeked: %d\n", *peek_data); + + for (int i = test_queue.size; i > 0; i -= 1) { + int *popped_data = (int*)fledasty_queue_pop(&test_queue); + printf("Popped: %d\n", *popped_data); + } + + if (fledasty_queue_is_empty(&test_queue)) { + printf("Queue is empty\n"); + } + + fledasty_queue_destroy(&test_queue); + + printf("Done\n"); return 0; } \ No newline at end of file