summaryrefslogtreecommitdiff
path: root/src/array.cpp
blob: 88eb492215a5b7a1692c1310e0ba91fb2540368a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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;
}