diff --git a/src/Array.c b/src/Array.c new file mode 100644 index 0000000000000000000000000000000000000000..69729f99f6337b3c1c2d6d76b2a6abb2b265f14c --- /dev/null +++ b/src/Array.c @@ -0,0 +1,137 @@ +#include "Array.h" + +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +int _lower_bound(IntegerArray *array, uint64_t item) { + int low = 0; + int high = array->size - 1; + + while (low <= high) { + int m = (low + high) / 2; + + if (array->items[m] < item) { + low = m + 1; + } else if (array->items[m] > item) { + high = m - 1; + } else { + return m; + } + } + + return low; +} + +IntegerArray *IntegerArray_init(int capacity) { + IntegerArray *array = (IntegerArray *)malloc(sizeof(IntegerArray)); + array->items = (uint64_t *)malloc(sizeof(uint64_t) * capacity); + array->size = 0; + return array; +} + +void IntegerArray_destroy(IntegerArray **array) { + free((*array)->items); + free(*array); + *array = NULL; +} + +int IntegerArray_insert_sorted(IntegerArray *array, uint64_t item) { + int index = _lower_bound(array, item); + + for (int i = array->size - 1; i >= index; i--) { + array[i + 1] = array[i]; + } + + array->items[index] = item; + array->size++; + return index; +} + +void IntegerArray_insert_at_index(IntegerArray *array, int index, uint64_t item) { + for (int i = array->size - 1; i >= index; i--) { + array[i + 1] = array[i]; + } + + array->items[index] = item; + array->size++; +} + +void IntegerArray_append(IntegerArray *array, uint64_t item) { + array->items[array->size] = item; + array->size++; +} + +static _IntegerArray_find_index(IntegerArray *array, uint64_t item) { + int low = 0; + int high = array->size - 1; + + while (low <= high) { + int m = (low + high) / 2; + + if (array->items[m] < item) { + low = m + 1; + } else if (array->items[m] > item) { + high = m - 1; + } else { + return m; + } + } + + return -1; +} + +bool IntegerArray_binary_search(IntegerArray *array, uint64_t item, int *index) { + *index = _IntegerArray_find_index(array, item); + return *index != -1; +} + +void IntegerArray_print(IntegerArray *array) { + printf("["); + + for (int i = 0; i < array->size; i++) { + printf("%ld", array->items[i]); + + if (i < array->size - 1) { + printf(", "); + } + } + + printf("]\n"); +} + +BPTreeNodeArray *BPTreeNodeArray_init(int capacity) { + BPTreeNodeArray *array = (BPTreeNodeArray *)malloc(sizeof(BPTreeNodeArray)); + array->items = (BPTreeNodeArray **)malloc(sizeof(BPTreeNodeArray *) * capacity); + array->size = 0; + return array; +} + +void BPTreeNodeArray_destroy(BPTreeNodeArray **array) { + free((*array)->items); + free(*array); + *array = NULL; +} + +void BPTreeNodeArray_insert_at_index(BPTreeNodeArray *array, int index, BPTreeNode *item) { + for (int i = array->size - 1; i >= index; i--) { + array[i + 1] = array[i]; + } + + array->items[index] = item; + array->size++; +} + +void BPTreeNodeArray_append(BPTreeNodeArray *array, BPTreeNode *item) { + array->items[array->size] = item; + array->size++; +} + +void BPTreeNodeArray_delete_at_index(BPTreeNodeArray *array, int index) { + for (int i = index; i < array->size; i++) { + array[i] = array[i + 1]; + } + + array->size--; +} diff --git a/src/Array.h b/src/Array.h new file mode 100644 index 0000000000000000000000000000000000000000..38f8f81d19d642ded98d1811f7792db33f1b26a2 --- /dev/null +++ b/src/Array.h @@ -0,0 +1,39 @@ +#ifndef ARRAY_H +#define ARRAY_H + +#include <stdbool.h> +#include <stdint.h> + +typedef struct IntegerArray { + uint64_t *items; + int size; +} IntegerArray; + +int _lower_bound(IntegerArray *array, uint64_t item); + +IntegerArray *IntegerArray_init(int capacity); +void IntegerArray_destroy(IntegerArray **array); + +int IntegerArray_insert_sorted(IntegerArray *array, uint64_t item); +void IntegerArray_insert_at_index(IntegerArray *array, int index, uint64_t item); +void IntegerArray_append(IntegerArray *array, uint64_t item); +bool IntegerArray_binary_search(IntegerArray *array, uint64_t item, int *index); +void IntegerArray_delete_sorted(IntegerArray *array, uint64_t item); + +void IntegerArray_print(IntegerArray *array); + +typedef struct BPTreeNode BPTreeNode; + +typedef struct BPTreeNodeArray { + BPTreeNode **items; + int size; +} BPTreeNodeArray; + +BPTreeNodeArray *BPTreeNodeArray_init(int capacity); +void BPTreeNodeArray_destroy(BPTreeNodeArray **array); + +void BPTreeNodeArray_insert_at_index(BPTreeNodeArray *array, int index, BPTreeNode *item); +void BPTreeNodeArray_append(BPTreeNodeArray *array, BPTreeNode *item); +void BPTreeNodeArray_delete_at_index(BPTreeNodeArray *array, int index); + +#endif diff --git a/src/Array.o b/src/Array.o new file mode 100644 index 0000000000000000000000000000000000000000..3ed4c786af04dda387dd90c52d6bb442897ae93b Binary files /dev/null and b/src/Array.o differ diff --git a/src/main.o b/src/main.o index ca2341be399e9bed35f53bf070ec7056618708e1..0de5ffdac02eecf0d672afa2dce4461f9a77d871 100644 Binary files a/src/main.o and b/src/main.o differ diff --git a/src/program b/src/program index c0146df79aba0eb288537ce614d67e08d5b8afbd..52a9237a73e8f0928c5dcb8e03ebb63a1a9f0aa2 100644 Binary files a/src/program and b/src/program differ