feat(stack): made it possible to initialize stack with list

This commit is contained in:
Mineplay 2025-04-23 17:40:38 -05:00
parent 8b0a617cb8
commit 6e8b2eade3
3 changed files with 20 additions and 8 deletions

View file

@ -34,7 +34,7 @@ typedef struct {
unsigned char *buffer;
} FledastyStack;
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size);
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, const size_t element_byte_size);
FledastyError fledasty_stack_destroy(FledastyStack *current_stack);
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);

View file

@ -26,18 +26,30 @@
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size) {
FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, 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;
if (values == NULL || values_size == 0) {
new_stack->size = 0;
new_stack->capacity = 10;
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
if (new_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
if (new_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
} else {
new_stack->size = values_size;
new_stack->capacity = values_size + (values_size / 2);
new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size);
if (new_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_stack->buffer, values, values_size * element_byte_size);
}
return FLEDASTY_ERROR_NONE;

View file

@ -46,7 +46,7 @@ int main() {
fledasty_queue_destroy(&test_queue);
FledastyStack test_stack;
fledasty_stack_initialize(&test_stack, sizeof(int));
fledasty_stack_initialize(&test_stack, (int[]){ 11, 12, 13, 14, 15 }, 5, sizeof(int));
for (int i = 0; i < 10; i += 1) {
fledasty_stack_push(&test_stack, &i);