137 lines
4.6 KiB
C
137 lines
4.6 KiB
C
/*
|
|
* 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 "Fledasty/Utils/Error.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 = new_stack->size + new_stack->size;
|
|
|
|
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 * current_stack->element_byte_size);
|
|
|
|
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;
|
|
}
|
|
|
|
current_stack->size -= 1;
|
|
return current_stack->buffer + (current_stack->size * current_stack->element_byte_size);
|
|
}
|
|
|
|
FledastyError fledasty_stack_clear(FledastyStack *current_stack) {
|
|
if (current_stack == NULL) {
|
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
|
}
|
|
|
|
current_stack->size = 0;
|
|
return FLEDASTY_ERROR_NONE;
|
|
}
|
|
|
|
FledastyError fledasty_stack_shrink_to_fit(FledastyStack *current_stack) {
|
|
if (current_stack == NULL) {
|
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
|
}
|
|
|
|
current_stack->capacity = (current_stack->size == 0) ? 10 : current_stack->size;
|
|
unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_stack->capacity);
|
|
if (shrinked_buffer == NULL) {
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
}
|
|
|
|
hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size);
|
|
if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) {
|
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
|
}
|
|
|
|
current_stack->buffer = shrinked_buffer;
|
|
return FLEDASTY_ERROR_NONE;
|
|
}
|