Skip to content
Snippets Groups Projects
Commit 2bb9bc1f authored by florian.burgener's avatar florian.burgener
Browse files

Rewrite arrays

parent acfbaaba
Branches
No related tags found
No related merge requests found
#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--;
}
#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
File added
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment