-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.c
69 lines (61 loc) · 1.17 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
63
64
65
66
67
68
69
/*
resizable array for C
Robert Oliveira
*/
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct {
int *items;
int n_items;
} vector;
vector vector_new(int n_items)
{
// returns NULL on failure
vector v = { NULL, 0};
v.n_items = n_items;
v.items = malloc(n_items * sizeof(*v.items));
return v;
}
void vector_delete(vector *v)
{
assert(v->items);
free(v->items);
v->items=NULL;
}
int vector_resize(vector *v, int n_items)
{
void *new_items;
assert(v!=NULL);
assert(n_items>0);
new_items = realloc(v->items, n_items * sizeof(*v->items));
if ( new_items!=NULL ) {
v->items = new_items;
v->n_items = n_items;
}
return new_items!=NULL;
}
int vector_set(vector *v, int index, int item)
{
assert(v!=NULL);
if ( index >= v->n_items ) {
vector_resize(v, index+8);
}
if ( index < v->n_items ) {
v->items[index] = item;
}
return index < v->n_items;
}
int vector_test(void)
{
vector v = vector_new(5);
int i;
for (i=0; i<5; i++) assert(vector_set(&v, i, i*10));
for (i=5; i<10; i++) assert(vector_set(&v, i, i*10));
for (i=0; i<10; i++) printf("item[%i] = %i\n", i, v.items[i]);
vector_delete(&v);
}
main()
{
vector_test();
}