-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivec.h
executable file
·66 lines (55 loc) · 902 Bytes
/
ivec.h
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
#ifndef IVEC_H
#define IVEC_H
#include <assert.h>
#include "xmalloc.h"
typedef struct ivec {
long cap;
long size;
long* data;
} ivec;
static
ivec*
make_ivec(int cap0)
{
assert(cap0 > 0);
ivec* xs = xmalloc(sizeof(ivec));
xs->cap = cap0;
xs->size = 0;
xs->data = xmalloc(xs->cap * sizeof(long));
return xs;
}
static
void
free_ivec(ivec* xs)
{
xfree(xs->data);
xfree(xs);
}
static
void
ivec_push(ivec* xs, long item)
{
if (xs->size >= xs->cap) {
xs->cap *= 2;
xs->data = xrealloc(xs->data, xs->cap * sizeof(long));
}
xs->data[xs->size] = item;
xs->size += 1;
}
static
long
ivec_last(ivec* xs)
{
return xs->data[xs->size - 1];
}
static
ivec*
ivec_copy(ivec* xs)
{
ivec* ys = make_ivec(xs->cap);
for (long ii = 0; ii < xs->size; ++ii) {
ivec_push(ys, xs->data[ii]);
}
return ys;
}
#endif