f2-basic-stack #17
4 changed files with 29 additions and 17 deletions
|
|
@ -35,7 +35,7 @@ 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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -75,7 +75,7 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) {
|
|||
}
|
||||
|
||||
if (current_stack->size == current_stack->capacity) {
|
||||
current_stack->capacity = current_stack->capacity + (current_stack->capacity / 2);
|
||||
current_stack->capacity += current_stack->capacity;
|
||||
current_stack->buffer = (unsigned char*)hallocy_realloc(current_stack->buffer, current_stack->capacity);
|
||||
|
||||
if (current_stack->buffer == NULL) {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue