feat(queue): implemented schrink to fit function
This commit is contained in:
parent
349268068b
commit
ca59eec627
5 changed files with 52 additions and 7 deletions
|
|
@ -43,6 +43,7 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
|||
void *fledasty_queue_pop(FledastyQueue *current_queue);
|
||||
|
||||
FledastyError fledasty_queue_clear(FledastyQueue *current_queue);
|
||||
FledastyError fledasty_queue_shrink_to_fit(FledastyQueue *current_queue);
|
||||
|
||||
static inline void *fledasty_queue_peek(const FledastyQueue *current_queue) { return (current_queue == NULL) ? NULL : current_queue->buffer + (current_queue->head * current_queue->element_byte_size); }
|
||||
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue == NULL || current_queue->size == 0; }
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ void *fledasty_stack_pop(FledastyStack *current_stack);
|
|||
FledastyError fledasty_stack_clear(FledastyStack *current_stack);
|
||||
FledastyError fledasty_stack_shrink_to_fit(FledastyStack *current_stack);
|
||||
|
||||
static inline void *fledasty_stack_peek(const FledastyStack *current_stack) { (current_stack == NULL) ? NULL : current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); }
|
||||
static inline void *fledasty_stack_peek(const FledastyStack *current_stack) { return (current_stack == NULL) ? NULL : current_stack->buffer + ((current_stack->size - 1) * current_stack->element_byte_size); }
|
||||
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack == NULL || current_stack->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
* -----------------------------------------------------------------------------
|
||||
*/
|
||||
#include "../../Include/Fledasty/Core/Queue.h"
|
||||
#include "Fledasty/Utils/Error.h"
|
||||
|
||||
#include <Hallocy/Core/Allocator.h>
|
||||
#include <Hallocy/Core/Memory.h>
|
||||
|
|
@ -84,10 +85,17 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
|
|||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
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;
|
||||
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);
|
||||
|
|
@ -125,3 +133,35 @@ FledastyError fledasty_queue_clear(FledastyQueue *current_queue) {
|
|||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -114,12 +114,12 @@ FledastyError fledasty_stack_shrink_to_fit(FledastyStack *current_stack) {
|
|||
}
|
||||
|
||||
current_stack->capacity = (current_stack->size == 0) ? 10 : current_stack->size;
|
||||
unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_stack->capacity);
|
||||
unsigned char *shrinked_buffer = (unsigned char*)hallocy_malloc(current_stack->capacity * current_stack->element_byte_size);
|
||||
if (shrinked_buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
||||
hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size);
|
||||
hallocy_copy_memory(shrinked_buffer, current_stack->buffer, current_stack->size * current_stack->element_byte_size);
|
||||
if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,6 +39,10 @@ int main() {
|
|||
fledasty_queue_push(&test_queue, &i);
|
||||
}
|
||||
|
||||
fledasty_queue_shrink_to_fit(&test_queue);
|
||||
printf("Queue schrinked to fit %s\n", (test_queue.capacity == test_queue.size) ? "succeeded" : "failed");
|
||||
int test = 0;
|
||||
fledasty_queue_push(&test_queue, &test);
|
||||
int *peeked_queue_data = (int*)fledasty_queue_peek(&test_queue);
|
||||
printf("Queue peeked: %d\n", *peeked_queue_data);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue