f1-basic-queue #16
3 changed files with 34 additions and 10 deletions
|
|
@ -40,5 +40,6 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue);
|
||||||
|
|
||||||
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
||||||
void *fledasty_queue_peek(FledastyQueue *current_queue);
|
void *fledasty_queue_peek(FledastyQueue *current_queue);
|
||||||
|
void *fledasty_queue_pop(FledastyQueue *current_queue);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -60,7 +60,7 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) {
|
||||||
current_queue->buffer = NULL;
|
current_queue->buffer = NULL;
|
||||||
return FLEDASTY_ERROR_NONE;
|
return FLEDASTY_ERROR_NONE;
|
||||||
}
|
}
|
||||||
|
#include <stdio.h>
|
||||||
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
|
FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
|
||||||
if (current_queue == NULL || value == NULL) {
|
if (current_queue == NULL || value == NULL) {
|
||||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||||
|
|
@ -73,18 +73,19 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) {
|
||||||
if (current_queue->buffer == NULL) {
|
if (current_queue->buffer == NULL) {
|
||||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||||
}
|
}
|
||||||
|
printf("Allocated\n");
|
||||||
size_t new_tail = current_queue->capacity - current_queue->tail;
|
size_t new_head = current_queue->capacity - current_queue->head;
|
||||||
hallocy_move_memory(current_queue->buffer + (current_queue->tail * current_queue->element_byte_size), current_queue->buffer + (new_tail * current_queue->element_byte_size), current_queue->size - current_queue->tail);
|
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->tail = new_tail;
|
current_queue->head = new_head;
|
||||||
}
|
}
|
||||||
|
|
||||||
hallocy_copy_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), value, current_queue->element_byte_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->size += 1;
|
||||||
current_queue->head += 1;
|
current_queue->tail += 1;
|
||||||
if (current_queue->head >= current_queue->capacity) {
|
if (current_queue->tail >= current_queue->capacity) {
|
||||||
current_queue->head = 0;
|
printf("To 0\n");
|
||||||
|
current_queue->tail = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return FLEDASTY_ERROR_NONE;
|
return FLEDASTY_ERROR_NONE;
|
||||||
|
|
@ -95,5 +96,22 @@ void *fledasty_queue_peek(FledastyQueue *current_queue) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return current_queue->buffer + (current_queue->tail * current_queue->element_byte_size);
|
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;
|
||||||
}
|
}
|
||||||
|
|
@ -33,6 +33,11 @@ int main() {
|
||||||
int *peek_data = (int*)fledasty_queue_peek(&test_queue);
|
int *peek_data = (int*)fledasty_queue_peek(&test_queue);
|
||||||
printf("Peeked: %d\n", *peek_data);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
fledasty_queue_destroy(&test_queue);
|
fledasty_queue_destroy(&test_queue);
|
||||||
|
|
||||||
printf("Done\n");
|
printf("Done\n");
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue