diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index e69de29..78805bf 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -0,0 +1,40 @@ +/* + * 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: Stack.h + * Description: + * This file contains the Stack structure and the functions for modifying it. + * It includes functions to Push, Pop, Peek and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef FLEDASTY_STACK +#define FLEDASTY_STACK + +#include + +#include "../Utils/Error.h" + +typedef struct { + size_t size, capacity; + + size_t element_byte_size; + unsigned char *buffer; +} FledastyStack; + +FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size); +FledastyError fledasty_stack_destroy(FledastyStack *current_stack); + +#endif \ No newline at end of file diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index e69de29..1938d7a 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -0,0 +1,58 @@ +/* + * 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: Stack.c + * Description: + * This file contains the functions for modifying the stack. It includes functions + * to Push, Pop, Peek and check if empty. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Fledasty/Core/Stack.h" + +#include +#include +#include + +FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size) { + if (new_stack == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + new_stack->size = 0; + new_stack->capacity = 10; + new_stack->element_byte_size = element_byte_size; + + new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size); + if (new_stack->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_stack_destroy(FledastyStack *current_stack) { + if (current_stack == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + HallocyError result = hallocy_free(current_stack->buffer); + if (result != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + current_stack->buffer = NULL; + return FLEDASTY_ERROR_NONE; +} \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index 1097b69..33ab88a 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -21,6 +21,7 @@ */ #include #include +#include int main() { FledastyQueue test_queue; @@ -44,6 +45,11 @@ int main() { fledasty_queue_destroy(&test_queue); + FledastyStack test_stack; + fledasty_stack_initialize(&test_stack, sizeof(int)); + + fledasty_stack_destroy(&test_stack); + printf("Done\n"); return 0; } \ No newline at end of file