-
Notifications
You must be signed in to change notification settings - Fork 1
/
DArray.c
46 lines (33 loc) · 918 Bytes
/
DArray.c
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
#include <stdlib.h>
#include "DArray.h"
#include "Error.h"
DArray* initDArray(byte size) {
DArray* arr = (DArray*) malloc(sizeof(DArray));
if (!arr) handleError(ERR_MEM, FATAL, "Could not allocate space for the dynamic array structure!\n");
arr->ids = (char*) malloc(size);
if (!arr->ids) handleError(ERR_MEM, FATAL, "Could not allocate space for the dyanmic array contents!\n");
arr->size = size;
for (int i = 0; i < arr->size; i++) arr->ids[i] = -1;
return arr;
}
void dArrayAdd(DArray* arr, byte id) {
if (!arr) return;
for (int i = 0; i < arr->size; i++) {
if (arr->ids[i] == -1) {
arr->ids[i] = id;
return;
}
}
}
bool dArrayExists(DArray* arr, byte id) {
if (!arr) return false;
for (int i = 0; i < arr->size; i++) {
if (arr->ids[i] == id) return true;
}
return false;
}
void dArrayFree(DArray* arr) {
free(arr->ids);
free(arr);
arr = NULL;
}