summaryrefslogtreecommitdiff
path: root/src/array.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/array.cpp')
-rw-r--r--src/array.cpp113
1 files changed, 113 insertions, 0 deletions
diff --git a/src/array.cpp b/src/array.cpp
new file mode 100644
index 0000000..88eb492
--- /dev/null
+++ b/src/array.cpp
@@ -0,0 +1,113 @@
+#include "array.h"
+
+struct EntityArr entity_arr_init(struct Arena *arena, unsigned int capacity) {
+ struct EntityArr arr = {0};
+ void *allocation = arena_alloc(arena, sizeof(struct EntityArr) * capacity);
+
+ // @todo: check if allocation was not possible, log in case of that
+ assert(allocation != NULL);
+ arr.buffer = (struct Entity *)allocation;
+ arr.capacity = capacity;
+ arr.size = 0;
+
+ return arr;
+}
+
+void entity_arr_insert(struct EntityArr *arr, struct Entity to_insert,
+ unsigned int index) {
+ if (index >= arr->capacity) {
+ return;
+ }
+
+ arr->buffer[index] = to_insert;
+}
+
+struct Entity entity_arr_remove(struct EntityArr *arr, struct Entity to_remove,
+ unsigned int index) {
+ if (index >= arr->capacity) {
+ return EntityNone;
+ }
+
+ struct Entity removed = arr->buffer[index];
+ arr->buffer[index] = EntityNone;
+
+ return removed;
+}
+
+void entity_arr_push_head(struct EntityArr *arr, struct Entity value) {
+ if (arr->size >= arr->capacity) {
+ return;
+ }
+
+ for (int i = arr->size - 1; i >= 0; i--) {
+ arr->buffer[i + 1] = arr->buffer[i];
+ }
+
+ arr->buffer[0] = value;
+ arr->size++;
+}
+
+void entity_arr_push_tail(struct EntityArr *arr, struct Entity value) {
+ if (arr->size >= arr->capacity) {
+ return;
+ }
+
+ arr->buffer[arr->size] = value;
+ arr->size++;
+}
+
+struct Entity entity_arr_pop_head(struct EntityArr *arr) {
+ if (arr->size == 0)
+ return EntityNone;
+
+ struct Entity popped = arr->buffer[0];
+ for (int i = 1; i < arr->size; i++) {
+ arr->buffer[i - 1] = arr->buffer[i];
+ }
+ arr->size--;
+
+ return popped;
+}
+
+struct Entity entity_arr_pop_tail(struct EntityArr *arr) {
+ if (arr->size == 0)
+ return EntityNone;
+
+ struct Entity popped = arr->buffer[arr->size - 1];
+ arr->size--;
+
+ return popped;
+}
+
+void entity_qpush(EntityQueue *queue, struct Entity value) {
+ entity_arr_push_tail(queue, value);
+}
+
+struct Entity entity_qpop(EntityQueue *queue) {
+ return entity_arr_pop_head(queue);
+}
+
+void entity_spush(EntityStack *stack, struct Entity value) {
+ entity_arr_push_tail(stack, value);
+}
+
+struct Entity entity_spop(EntityStack *stack) {
+ return entity_arr_pop_tail(stack);
+}
+
+int entity_arr_find(struct EntityArr *arr, struct Entity value) {
+ /*
+ * finds value in arr
+ * returns index of value
+ */
+ int find_index = -1;
+ for (int i = 0; i < arr->capacity; i++) {
+ struct Entity entry = arr->buffer[i];
+ if (entry.type == value.type && entry.id == value.id) {
+ find_index = i;
+ break;
+ }
+ }
+
+ return find_index;
+}