Merge pull request 'f2-basic-stack' (#17) from f2-basic-stack into main

Reviewed-on: #17
This commit is contained in:
Mineplay 2025-04-24 04:51:22 -05:00
commit 48f402708c
5 changed files with 210 additions and 21 deletions

View file

@ -35,13 +35,13 @@ typedef struct {
unsigned char *buffer;
} FledastyQueue;
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size);
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, 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_peek(const 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; }
static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
#endif

View file

@ -0,0 +1,46 @@
/*
* 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 <stddef.h>
#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, 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);
void *fledasty_stack_peek(const FledastyStack *current_stack);
void *fledasty_stack_pop(FledastyStack *current_stack);
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
#endif

View file

@ -26,23 +26,35 @@
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size) {
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, 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->head = 0;
new_queue->element_byte_size = element_byte_size;
if (values == NULL || values_size == 0) {
new_queue->size = 0;
new_queue->capacity = 10;
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->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
if (new_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
} else {
new_queue->size = values_size;
new_queue->capacity = values_size + (values_size / 2);
new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size);
if (new_queue->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_queue->buffer, values, values_size * element_byte_size);
}
new_queue->head = 0;
new_queue->tail = 0;
new_queue->tail = new_queue->size;
return FLEDASTY_ERROR_NONE;
}
@ -66,15 +78,16 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
}
if (current_queue->size == current_queue->capacity) {
current_queue->capacity = current_queue->capacity + (current_queue->capacity / 2);
current_queue->capacity += current_queue->capacity;
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_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);
size_t head_length = current_queue->size - current_queue->head;
size_t new_head = current_queue->capacity - head_length;
hallocy_move_memory(current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_length * current_queue->element_byte_size);
current_queue->head = new_head;
}
@ -89,7 +102,7 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
return FLEDASTY_ERROR_NONE;
}
void *fledasty_queue_peek(FledastyQueue *current_queue) {
void *fledasty_queue_peek(const FledastyQueue *current_queue) {
if (current_queue == NULL) {
return NULL;
}
@ -105,10 +118,9 @@ void *fledasty_queue_pop(FledastyQueue *current_queue) {
void *value_address = current_queue->buffer + (current_queue->head * current_queue->element_byte_size);
current_queue->size -= 1;
current_queue->head += 1;
if (current_queue->head == current_queue->capacity) {
current_queue->head = 0;
} else {
current_queue->head += 1;
}
return value_address;

View file

@ -0,0 +1,109 @@
/*
* 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 <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
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->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;
}
} 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;
}
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;
}
FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
if (current_stack == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_stack->size == current_stack->capacity) {
current_stack->capacity += current_stack->capacity;
current_stack->buffer = (unsigned char*)hallocy_realloc(current_stack->buffer, current_stack->capacity);
if (current_stack->buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
}
hallocy_copy_memory(current_stack->buffer + (current_stack->size * current_stack->element_byte_size), value, current_stack->element_byte_size);
current_stack->size += 1;
return FLEDASTY_ERROR_NONE;
}
void *fledasty_stack_peek(const FledastyStack *current_stack) {
if (current_stack == NULL) {
return NULL;
}
return current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
}
void *fledasty_stack_pop(FledastyStack *current_stack) {
if (current_stack == NULL) {
return NULL;
}
void *value_address = current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size);
current_stack->size -= 1;
return value_address;
}

View file

@ -21,17 +21,18 @@
*/
#include <stdio.h>
#include <Fledasty/Core/Queue.h>
#include <Fledasty/Core/Stack.h>
int main() {
FledastyQueue test_queue;
fledasty_queue_initialize(&test_queue, sizeof(int));
fledasty_queue_initialize(&test_queue, (int[]){11, 12, 13, 14, 15}, 5, 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);
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
printf("Peeked: %d\n", *peeked_queue_data);
for (int i = test_queue.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
@ -44,6 +45,27 @@ int main() {
fledasty_queue_destroy(&test_queue);
FledastyStack test_stack;
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);
}
int *peeked_stack_data = (int*)fledasty_stack_peek(&test_stack);
printf("Peeked: %d\n", *peeked_stack_data);
for (int i = test_stack.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_stack_pop(&test_stack);
printf("Popped: %d\n", *popped_data);
}
if (fledasty_stack_is_empty(&test_stack)) {
printf("Stack is empty\n");
}
fledasty_stack_destroy(&test_stack);
printf("Done\n");
return 0;
}