Merge pull request 'f1-basic-queue' (#16) from f1-basic-queue into main

Reviewed-on: #16
This commit is contained in:
Mineplay 2025-04-22 16:02:39 -05:00
commit ee75c231c7
4 changed files with 219 additions and 1 deletions

View file

@ -0,0 +1,47 @@
/*
* 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.h
* Description:
* This file contains the Queue structure and the functions for modifying it.
* It includes functions to Push, Pop, Peek and check if empty.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef FLEDASTY_QUEUE
#define FLEDASTY_QUEUE
#include <stddef.h>
#include "../Utils/Error.h"
typedef struct {
size_t size, capacity;
size_t head, tail;
size_t element_byte_size;
unsigned char *buffer;
} FledastyQueue;
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, 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_pop(FledastyQueue *current_queue);
static inline size_t fledasty_queue_is_empty(FledastyQueue *current_queue) { return current_queue->size == 0; }
#endif

View file

@ -0,0 +1,32 @@
/*
* 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: Error.h
* Description:
* This file contains the ErrorCode enum that defines the error codes for the
* library.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef FLEDASTY_ERROR
#define FLEDASTY_ERROR
typedef enum {
FLEDASTY_ERROR_NONE = 0,
FLEDASTY_ERROR_FAILED_ALLOCATION = 1,
FLEDASTY_ERROR_INVALID_POINTER = 2,
} FledastyError;
#endif

117
Src/Core/Queue.c Normal file
View file

@ -0,0 +1,117 @@
/*
* 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 <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
#include <string.h>
FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, 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->element_byte_size = element_byte_size;
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->head = 0;
new_queue->tail = 0;
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;
}
#include <stdio.h>
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->capacity / 2);
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;
}
printf("Allocated\n");
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);
current_queue->head = new_head;
}
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) {
printf("To 0\n");
current_queue->tail = 0;
}
return FLEDASTY_ERROR_NONE;
}
void *fledasty_queue_peek(FledastyQueue *current_queue) {
if (current_queue == NULL) {
return NULL;
}
return current_queue->buffer + (current_queue->head * current_queue->element_byte_size);
}
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;
if (current_queue->head == current_queue->capacity) {
current_queue->head = 0;
} else {
current_queue->head += 1;
}
return value_address;
}

View file

@ -20,8 +20,30 @@
* -----------------------------------------------------------------------------
*/
#include <stdio.h>
#include <Fledasty/Core/Queue.h>
int main() {
printf("Test\n");
FledastyQueue test_queue;
fledasty_queue_initialize(&test_queue, 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);
for (int i = test_queue.size; i > 0; i -= 1) {
int *popped_data = (int*)fledasty_queue_pop(&test_queue);
printf("Popped: %d\n", *popped_data);
}
if (fledasty_queue_is_empty(&test_queue)) {
printf("Queue is empty\n");
}
fledasty_queue_destroy(&test_queue);
printf("Done\n");
return 0;
}