feat(dynamic array): implemented insert before value function for dynamic array
This commit is contained in:
parent
4388756371
commit
c51fd33b50
6 changed files with 54 additions and 4 deletions
|
|
@ -25,6 +25,7 @@
|
|||
#define FLEDASTY_DYNAMIC_ARRAY
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
|
|
@ -40,7 +41,10 @@ FledastyError fledasty_dynamic_array_destroy(FledastyDynamicArray *current_dynam
|
|||
|
||||
FledastyError fledasty_dynamic_array_append(FledastyDynamicArray *current_dynamic_array, void *value);
|
||||
FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *current_dynamic_array, const size_t index, void *value);
|
||||
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value);
|
||||
|
||||
void *fledasty_dynamic_array_get(FledastyDynamicArray *current_dynamic_array, const size_t index);
|
||||
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index);
|
||||
|
||||
inline static bool fledasty_dynamic_array_is_empty(const FledastyDynamicArray *current_dynamic_array) { return current_dynamic_array->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#define FLEDASTY_QUEUE
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
|
|
@ -42,6 +43,6 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value);
|
|||
void *fledasty_queue_peek(const FledastyQueue *current_queue);
|
||||
void *fledasty_queue_pop(FledastyQueue *current_queue);
|
||||
|
||||
static inline size_t fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
||||
static inline bool fledasty_queue_is_empty(const FledastyQueue *current_queue) { return current_queue->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -24,6 +24,7 @@
|
|||
#define FLEDASTY_STACK
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "../Utils/Error.h"
|
||||
|
||||
|
|
@ -41,6 +42,6 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value);
|
|||
void *fledasty_stack_peek(const FledastyStack *current_stack);
|
||||
void *fledasty_stack_pop(FledastyStack *current_stack);
|
||||
|
||||
static inline size_t fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||
static inline bool fledasty_stack_is_empty(const FledastyStack *current_stack) { return current_stack->size == 0; }
|
||||
|
||||
#endif
|
||||
|
|
@ -28,6 +28,7 @@ typedef enum {
|
|||
FLEDASTY_ERROR_FAILED_ALLOCATION = 1,
|
||||
FLEDASTY_ERROR_INVALID_POINTER = 2,
|
||||
FLEDASTY_ERROR_INDEX_OUT_OF_RANGE = 3,
|
||||
FLEDASTY_ERROR_VALUE_NOT_FOUND = 4,
|
||||
} FledastyError;
|
||||
|
||||
#endif
|
||||
|
|
@ -98,6 +98,10 @@ FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *curre
|
|||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *insert_pointer = current_dynamic_array->buffer + (index * current_dynamic_array->element_byte_size);
|
||||
|
|
@ -108,7 +112,38 @@ FledastyError fledasty_dynamic_array_insert_at_index(FledastyDynamicArray *curre
|
|||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
void *fledasty_dynamic_array_get(FledastyDynamicArray *current_dynamic_array, const size_t index) {
|
||||
FledastyError fledasty_dynamic_array_insert_before_value(FledastyDynamicArray *current_dynamic_array, void *before_value, void *value) {
|
||||
if (current_dynamic_array == NULL || before_value == NULL || value == NULL) {
|
||||
return FLEDASTY_ERROR_INVALID_POINTER;
|
||||
}
|
||||
|
||||
size_t byte_index = 0;
|
||||
size_t byte_size = current_dynamic_array->size * current_dynamic_array->element_byte_size;
|
||||
while (byte_index < byte_size && !hallocy_compare_memory(current_dynamic_array->buffer + byte_index, before_value, current_dynamic_array->element_byte_size)) {
|
||||
byte_index += current_dynamic_array->element_byte_size;
|
||||
}
|
||||
|
||||
if (byte_index == byte_size) {
|
||||
return FLEDASTY_ERROR_VALUE_NOT_FOUND;
|
||||
}
|
||||
|
||||
if (current_dynamic_array->size == current_dynamic_array->capacity) {
|
||||
current_dynamic_array->capacity += current_dynamic_array->capacity;
|
||||
current_dynamic_array->buffer = (unsigned char*)hallocy_realloc(current_dynamic_array->buffer, current_dynamic_array->capacity * current_dynamic_array->element_byte_size);
|
||||
|
||||
if (current_dynamic_array->buffer == NULL) {
|
||||
return FLEDASTY_ERROR_FAILED_ALLOCATION;
|
||||
}
|
||||
}
|
||||
|
||||
hallocy_move_memory(current_dynamic_array->buffer + (byte_index + current_dynamic_array->element_byte_size), current_dynamic_array->buffer + byte_index, byte_size - byte_index);
|
||||
hallocy_copy_memory(current_dynamic_array->buffer + byte_index, value, current_dynamic_array->element_byte_size);
|
||||
current_dynamic_array->size += 1;
|
||||
|
||||
return FLEDASTY_ERROR_NONE;
|
||||
}
|
||||
|
||||
void *fledasty_dynamic_array_get(const FledastyDynamicArray *current_dynamic_array, const size_t index) {
|
||||
if (current_dynamic_array == NULL || index >= current_dynamic_array->size) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,11 +76,19 @@ int main() {
|
|||
int insert_value = 18;
|
||||
fledasty_dynamic_array_insert_at_index(&test_dynamic_array, 1, &insert_value);
|
||||
|
||||
insert_value = 35;
|
||||
int before_value = 11;
|
||||
fledasty_dynamic_array_insert_before_value(&test_dynamic_array, &before_value, &insert_value);
|
||||
|
||||
for (int i = 0; i < test_dynamic_array.size; i += 1) {
|
||||
int *dynamic_array_data = (int*)fledasty_dynamic_array_get(&test_dynamic_array, i);
|
||||
printf("Dynamic array get: %d\n", *dynamic_array_data);
|
||||
}
|
||||
|
||||
if (fledasty_dynamic_array_is_empty(&test_dynamic_array)) {
|
||||
printf("Dynamic array is empty\n");
|
||||
}
|
||||
|
||||
fledasty_dynamic_array_destroy(&test_dynamic_array);
|
||||
printf("Done\n");
|
||||
return 0;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue