forked from robertapengelly/ld86
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
42 lines (29 loc) · 897 Bytes
/
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
/******************************************************************************
* @file vector.c
*****************************************************************************/
#include <stddef.h>
#include <stdlib.h>
#include "vector.h"
void *vec_pop (struct vector *vec) {
if (!vec || vec == NULL) {
return NULL;
}
if (vec->length <= 0) {
return NULL;
}
return vec->data[--vec->length];
}
int vec_push (struct vector *vec, void *elem) {
if (vec->capacity <= vec->length) {
if (vec->capacity <= 0) {
vec->capacity = 16;
} else {
vec->capacity <<= 1;
}
if ((vec->data = realloc (vec->data, sizeof (*vec->data) * vec->capacity)) == NULL) {
return -2;
}
}
vec->data[vec->length++] = (void *) elem;
return 0;
}