Fledasty/Src/Core/Queue.c

167 lines
6.2 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: Queue.c
* Description:
* This file contains the functions for modifying the queue. It includes functions
* to Push, Pop, Peek and check if empty.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/Queue.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
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->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;
}
} else {
new_queue->size = values_size;
new_queue->capacity = new_queue->size + new_queue->size;
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->tail = new_queue->size;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
HallocyError result = hallocy_free(current_queue->buffer);
if (result != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_queue->buffer = NULL;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
if (current_queue == NULL || value == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
if (current_queue->size == current_queue->capacity) {
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;
}
if (current_queue->tail < current_queue->head) {
size_t head_size = (current_queue->size - current_queue->tail) * current_queue->element_byte_size;
hallocy_move_memory(current_queue->buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_size);
hallocy_move_memory(current_queue->buffer + head_size, current_queue->buffer, current_queue->tail * current_queue->element_byte_size);
} else {
hallocy_move_memory(current_queue->buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->size * current_queue->element_byte_size);
}
current_queue->head = 0;
current_queue->tail = current_queue->size;
}
hallocy_copy_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), value, current_queue->element_byte_size);
current_queue->size += 1;
current_queue->tail += 1;
if (current_queue->tail >= current_queue->capacity) {
current_queue->tail = 0;
}
return FLEDASTY_ERROR_NONE;
}
void *fledasty_queue_pop(FledastyQueue *current_queue) {
if (current_queue == NULL || current_queue->size == 0) {
return NULL;
}
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;
}
return value_address;
}
FledastyError fledasty_queue_clear(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
current_queue->size = 0;
return FLEDASTY_ERROR_NONE;
}
FledastyError fledasty_queue_shrink_to_fit(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return FLEDASTY_ERROR_INVALID_POINTER;
}
size_t new_capacity = (current_queue->size == 0) ? 10 : current_queue->size;
unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(new_capacity);
if (shrinked_buffer == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
if (current_queue->tail < current_queue->head) {
size_t head_size = (current_queue->size - current_queue->tail) * current_queue->element_byte_size;
hallocy_copy_memory(shrinked_buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_size);
hallocy_copy_memory(shrinked_buffer + head_size, current_queue->buffer, current_queue->tail * current_queue->element_byte_size);
} else {
hallocy_copy_memory(shrinked_buffer, current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->size * current_queue->element_byte_size);
}
if (hallocy_free(current_queue->buffer) != HALLOCY_ERROR_NONE) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
current_queue->head = 0;
current_queue->tail = current_queue->size;
current_queue->capacity = new_capacity;
current_queue->buffer = shrinked_buffer;
return FLEDASTY_ERROR_NONE;
}