|
|
@@ -0,0 +1,61 @@
|
|
|
+#include "../headers/kiyomitree.h"
|
|
|
+
|
|
|
+void** KT_new_array(size_t element_count){
|
|
|
+ size_t* current_allocated_elements = malloc(sizeof(size_t));
|
|
|
+ *current_allocated_elements = element_count;
|
|
|
+
|
|
|
+ size_t* current_existing_elements = malloc(sizeof(size_t));
|
|
|
+ *current_existing_elements = 0;
|
|
|
+
|
|
|
+ void** array = malloc(sizeof(void*) * (element_count + 2));
|
|
|
+
|
|
|
+ array[0] = current_allocated_elements;
|
|
|
+ array[1] = current_existing_elements;
|
|
|
+
|
|
|
+ return array + 2;
|
|
|
+}
|
|
|
+
|
|
|
+void check_size(void** array){
|
|
|
+ size_t* current_allocated_elements = array[-2];
|
|
|
+ size_t* current_existing_elements = array[-1];
|
|
|
+
|
|
|
+ if (*current_existing_elements >= *current_allocated_elements)
|
|
|
+ array = (void**)(realloc(array - 2, sizeof(void*) * (*current_allocated_elements * 2 + 2)) + 2);
|
|
|
+}
|
|
|
+
|
|
|
+void KT_append(void** array, void* pointer){
|
|
|
+ check_size(array);
|
|
|
+
|
|
|
+ array[(*(size_t*)array[-1])++] = pointer;
|
|
|
+}
|
|
|
+
|
|
|
+void KT_prepend(void** array, void* pointer){
|
|
|
+ (*(size_t*)array[-1])++;
|
|
|
+
|
|
|
+ check_size(array);
|
|
|
+
|
|
|
+ memcpy(array + 1, array, *(size_t*)array[-1] - 1);
|
|
|
+
|
|
|
+ array[0] = pointer;
|
|
|
+}
|
|
|
+
|
|
|
+void KT_insert_left(void** array, void* pointer, size_t index){
|
|
|
+ (*(size_t*)array[-1])++;
|
|
|
+
|
|
|
+ check_size(array);
|
|
|
+
|
|
|
+ memcpy(array + index + 1, array, *(size_t*)array[-1] - index - 1);
|
|
|
+
|
|
|
+ array[index] = pointer;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void KT_insert_right(void** array, void* pointer, size_t index){
|
|
|
+ (*(size_t*)array[-1])++;
|
|
|
+
|
|
|
+ check_size(array);
|
|
|
+
|
|
|
+ memcpy(array + index + 2, array, *(size_t*)array[-1] - index - 2);
|
|
|
+
|
|
|
+ array[index + 1] = pointer;
|
|
|
+}
|