-
Notifications
You must be signed in to change notification settings - Fork 2
/
vector.c
62 lines (54 loc) · 1.13 KB
/
vector.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include <stdlib.h>
#include <string.h>
#include "err.h"
#include "vector.h"
enum nd_err
vector_init(struct vector * v, const size_t s) {
static const size_t INITIAL_CAPACITY = 64;
if (!(v->data = malloc(s * INITIAL_CAPACITY))) {
return ND_ALLOC;
}
v->cap = INITIAL_CAPACITY;
v->size = s;
v->len = 0;
return ND_SUCCESS;
}
static
enum nd_err
vector_resize(struct vector * v) {
if (v->len == v->cap) {
const size_t newcap = v->cap * 3 / 2;
void * newdata = realloc(v->data, newcap * v->size);
if (newdata) {
v->data = newdata;
v->cap = newcap;
} else {
return ND_ALLOC;
}
}
return ND_SUCCESS;
}
enum nd_err
vector_add(struct vector * v, const void * new) {
enum nd_err ret;
if ((ret = vector_resize(v)) == ND_SUCCESS) {
void * item = vector_item(v, v->len);
memcpy(item, new, v->size);
v->len++;
}
return ret;
}
int
vector_search(const struct vector * v, const void * item) {
int i;
for (i = 0; i < v->len; i++)
if (!memcmp(item, vector_item(v, i), v->size))
return i;
return -1;
}
void
vector_free(struct vector * v) {
free(v->data);
v->data = NULL;
}