From 3e04e24b1cacb777c089dfb3af4e25daf2902aed Mon Sep 17 00:00:00 2001 From: Mineplay Date: Sat, 19 Apr 2025 15:14:30 -0500 Subject: [PATCH] feat(queue): added queue datastructure with init and destroy function --- Include/Fledasty/Core/Queue.h | 43 ++++++++++++++++++++++ Include/Fledasty/Utils/Error.h | 32 +++++++++++++++++ Src/Core/Queue.c | 66 ++++++++++++++++++++++++++++++++++ Tests/Main.c | 7 +++- 4 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 Include/Fledasty/Core/Queue.h create mode 100644 Include/Fledasty/Utils/Error.h create mode 100644 Src/Core/Queue.c diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h new file mode 100644 index 0000000..70a3c9e --- /dev/null +++ b/Include/Fledasty/Core/Queue.h @@ -0,0 +1,43 @@ +/* + * 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 FledastyQueueNode { + void *value; + struct FledastyQueueNode *previous, *next; +} FledastyQueueNode; + +typedef struct { + size_t size; + FledastyQueueNode *start, *end; +} FledastyQueue; + +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue); +FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); + +#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..c981843 --- /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_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..b9108af --- /dev/null +++ b/Src/Core/Queue.c @@ -0,0 +1,66 @@ +/* + * 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: Main.c + * Description: + * Executes all tests. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/Queue.h" + +#include +#include + +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) { + if (new_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_queue->size = 0; + new_queue->start = NULL; + new_queue->end = NULL; + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { + if (current_queue == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyQueueNode *previousNode = NULL; + FledastyQueueNode *current_node = current_queue->start; + while (current_node != NULL) { + previousNode = current_node; + current_node = current_node->next; + + HallocyError error = hallocy_free(previousNode->value); + if (error != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_ALLOCATION; + } + + error = hallocy_free(previousNode); + if (error != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_ALLOCATION; + } + } + + current_queue->size = 0; + current_queue->start = NULL; + current_queue->end = NULL; + + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index cde1df7..d2da10b 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -20,8 +20,13 @@ * ----------------------------------------------------------------------------- */ #include +#include int main() { - printf("Test\n"); + FledastyQueue test_queue; + fledasty_queue_initialize(&test_queue); + fledasty_queue_destroy(&test_queue); + + printf("Done\n"); return 0; } \ No newline at end of file