|
|
@@ -15,47 +15,77 @@ void** KT_new_array(size_t element_count){
|
|
|
return array + 2;
|
|
|
}
|
|
|
|
|
|
-void check_size(void** array){
|
|
|
- size_t* current_allocated_elements = array[-2];
|
|
|
- size_t* current_existing_elements = array[-1];
|
|
|
+#include <stdio.h>
|
|
|
+
|
|
|
+void check_size(void*** array){
|
|
|
+ size_t* current_allocated_elements = (size_t*)((*array)[-2]);
|
|
|
+ size_t* current_existing_elements = (size_t*)((*array)[-1]);
|
|
|
|
|
|
- if (*current_existing_elements >= *current_allocated_elements)
|
|
|
- array = (void**)(realloc(array - 2, sizeof(void*) * (*current_allocated_elements * 2 + 2)) + 2);
|
|
|
-}
|
|
|
+ if (*current_existing_elements >= *current_allocated_elements){
|
|
|
+ *current_allocated_elements += *current_allocated_elements ? *current_allocated_elements : 1;
|
|
|
+
|
|
|
+ void* temp = realloc(*array - 2, sizeof(void*) * (*current_allocated_elements + 2));
|
|
|
+
|
|
|
+ if (!temp)
|
|
|
+ printf("realloc failed\n");
|
|
|
+
|
|
|
+ *array = (void**)(temp) + 2;
|
|
|
+
|
|
|
+ printf("%ld elements now in array \n", *((size_t*)(*array)[-1]));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void KT_append(void*** array, void* pointer){
|
|
|
+ size_t* existing_elements = (size_t*)(*array)[-1];
|
|
|
+
|
|
|
+ (*existing_elements)++;
|
|
|
|
|
|
-void KT_append(void** array, void* pointer){
|
|
|
check_size(array);
|
|
|
|
|
|
- array[(*(size_t*)array[-1])++] = pointer;
|
|
|
+ (*array)[*existing_elements - 1] = pointer;
|
|
|
}
|
|
|
|
|
|
-void KT_prepend(void** array, void* pointer){
|
|
|
- (*(size_t*)array[-1])++;
|
|
|
+void KT_prepend(void*** array, void* pointer){
|
|
|
+ size_t* existing_elements = (size_t*)(*array)[-1];
|
|
|
+
|
|
|
+ (*existing_elements)++;
|
|
|
|
|
|
check_size(array);
|
|
|
|
|
|
- memcpy(array + 1, array, *(size_t*)array[-1] - 1);
|
|
|
+ memmove(*array + 1, *array, sizeof(void*) * (*existing_elements - 1));
|
|
|
|
|
|
- array[0] = pointer;
|
|
|
+ (*array)[0] = pointer;
|
|
|
}
|
|
|
|
|
|
-void KT_insert_left(void** array, void* pointer, size_t index){
|
|
|
- (*(size_t*)array[-1])++;
|
|
|
+void KT_insert_left(void*** array, void* pointer, size_t index){
|
|
|
+ size_t* existing_elements = (size_t*)(*array)[-1];
|
|
|
+
|
|
|
+ (*existing_elements)++;
|
|
|
|
|
|
check_size(array);
|
|
|
|
|
|
- memcpy(array + index + 1, array, *(size_t*)array[-1] - index - 1);
|
|
|
+ memmove(*array + index + 1, *array + index, sizeof(void*) * (*existing_elements - index - 1));
|
|
|
|
|
|
- array[index] = pointer;
|
|
|
+ (*array)[index] = pointer;
|
|
|
}
|
|
|
|
|
|
|
|
|
-void KT_insert_right(void** array, void* pointer, size_t index){
|
|
|
- (*(size_t*)array[-1])++;
|
|
|
+void KT_insert_right(void*** array, void* pointer, size_t index){
|
|
|
+ size_t* existing_elements = (size_t*)(*array)[-1];
|
|
|
+
|
|
|
+ (*existing_elements)++;
|
|
|
|
|
|
check_size(array);
|
|
|
|
|
|
- memcpy(array + index + 2, array, *(size_t*)array[-1] - index - 2);
|
|
|
+ memmove(*array + index + 2, *array, sizeof(void*) * (*existing_elements - index - 2));
|
|
|
+
|
|
|
+ (*array)[index + 1] = pointer;
|
|
|
+}
|
|
|
+
|
|
|
+void KT_drop(void*** array, size_t index){
|
|
|
+ size_t* existing_elements = (size_t*)(*array)[-1];
|
|
|
+
|
|
|
+ (*existing_elements)--;
|
|
|
|
|
|
- array[index + 1] = pointer;
|
|
|
+ memmove(*array + index, *array + index + 1, sizeof(void*) * (*existing_elements - index - 1));
|
|
|
}
|