-
Notifications
You must be signed in to change notification settings - Fork 1
/
pile_vector.c
110 lines (99 loc) · 2.52 KB
/
pile_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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef _PILE_VECTOR_H_
#include "pile_vector.h"
#endif
pile_int_t impl_fn(pile_vector, resize, pile_int_t size)
{
self->size = size;
PILE_REALLOC(self->data, size * sizeof(pile_container_t));
return self->size;
}
pile_int_t impl_fn(pile_vector, push, pile_container_t item)
{
if (self->end >= self->size)
{
self->resize(self, self->end + 1);
}
self->data[self->end] = item;
self->end++;
return self->end;
}
pile_int_t impl_fn(pile_vector, pop)
{
if (self->end == self->size)
{
self->resize(self, self->size - 1);
}
self->end--;
return self->end;
}
pile_container_t impl_fn(pile_vector, top)
{
return self->data[self->end - 1];
}
pile_container_t impl_fn(pile_vector, bottom)
{
return self->data[0];
}
pile_int_t impl_fn(pile_vector, append, pile_vector_ptr_t vec)
{
pile_int_t new_size = self->end + vec->end;
if (new_size > self->size)
{
self->resize(self, new_size);
}
PILE_MEMCPY(self->data + self->end, vec->data,
vec->end * sizeof(pile_container_t));
self->end = new_size;
return self->end;
}
pile_bool_t impl_fn(pile_vector, is_empty)
{
return (self->end == 0);
}
pile_int_t impl_fn(pile_vector, slice,
pile_int_t start, pile_int_t length)
{
pile_container_ptr_t origin = self->data;
self->data = (pile_container_ptr_t)
PILE_MALLOC(length * sizeof(pile_container_t));
PILE_MEMCPY(self->data, origin + start,
length * sizeof(pile_container_t));
self->size -= start;
self->end = length;
PILE_FREE(origin);
return length;
}
pile_int_t impl_fn(pile_vector, insert,
pile_int_t pos, pile_container_t val)
{
if (self->end >= self->size)
{
self->resize(self, self->size + 1);
}
PILE_MEMCPY(self->data + pos + 1,
self->data + pos,
(self->end - pos) * sizeof(pile_container_t));
self->data[pos] = val;
self->end++;
return self->end;
}
impl_construct(pile_vector, pile_int_t size)
{
self->size = size;
self->end = 0;
self->data = (pile_container_ptr_t)
PILE_MALLOC(size * sizeof(pile_container_t));
_assign(pile_vector, push);
_assign(pile_vector, resize);
_assign(pile_vector, pop);
_assign(pile_vector, top);
_assign(pile_vector, bottom);
_assign(pile_vector, append);
_assign(pile_vector, is_empty);
_assign(pile_vector, slice);
_assign(pile_vector, insert);
}
impl_destruct(pile_vector)
{
PILE_FREE(self->data);
}