-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNodeVector.c
37 lines (33 loc) · 955 Bytes
/
NodeVector.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
#include "NodeVector.h"
#include "debugmalloc.h"
NodeVector nodev_create(size_t count) {
NodeVector result;
result.capacity = count * 2 + 1;
result.count = count;
result.nodes = (Node *)malloc(sizeof(Node) * result.capacity);
return result;
}
void nodev_push_back(NodeVector *vector, Node node) {
if (vector->count >= vector->capacity) {
vector->capacity *= 2;
Node *newmem = (Node *)realloc(vector->nodes, sizeof(Node) * vector->capacity);
if (newmem == NULL) {
log_error("Couldn't reallocate vector!\n");
return;
}
vector->nodes = newmem;
}
memcpy(&(vector->nodes[vector->count]), &node, sizeof(node));
vector->count++;
}
void nodev_erase(NodeVector *vector, int index) {
for (int i = index + 1; i < vector->count; i++) {
vector->nodes[i - 1] = vector->nodes[i];
}
vector->count--;
}
void nodev_free(NodeVector *vector) {
for (int i = 0; i < vector->count; i++)
node_free(&vector->nodes[i]);
free(vector->nodes);
}