-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
73 lines (57 loc) · 1.34 KB
/
vector.c
File metadata and controls
73 lines (57 loc) · 1.34 KB
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "mm.h"
#include "vector.h"
#define VECTOR_INITIAL_SIZE 8
void VInit(Vector_t *v)
{
memset(v, 0, sizeof(*v));
v->capacity = VECTOR_INITIAL_SIZE;
v->storage = (void **)Mmalloc(sizeof(*(v->storage)) * v->capacity);
}
void VDeInit(Vector_t *v)
{
Mfree(v->storage);
memset(v, 0, sizeof(*v));
}
static void _VExpand(Vector_t *v)
{
v->capacity <<= 1;
v->storage = (void **)Mrealloc(v->storage, sizeof(*(v->storage)) * v->capacity);
}
void VAdd(Vector_t *v, void *data)
{
if (v->count == v->capacity)
_VExpand(v);
v->storage[v->count] = data;
v->count += 1;
}
static void _VCheckI(Vector_t *v, size_t i)
{
if (i >= v->count)
{
fprintf(stderr, "_VCheckI Error: Index out of range. Max %u, %u provided\n", (unsigned int)v->count, (unsigned int)i);
abort();
}
}
void VRemoveI(Vector_t *v, size_t i)
{
void **p;
_VCheckI(v, i);
p = v->storage + i;
if (v->count - i - 1 > 0)
memcpy(p, p + 1, sizeof(*p) * (v->count - i - 1));
v->count -= 1;
}
void *VI(Vector_t *v, size_t i)
{
_VCheckI(v, i);
return v->storage[i];
}
void Vforeach(Vector_t *v, void *param, void (*handler)(void *data, void *param))
{
size_t i;
for (i = 0; i < v->count; i += 1)
handler(v->storage[i], param);
}