-
Notifications
You must be signed in to change notification settings - Fork 10
/
MalVector.HC
121 lines (107 loc) · 2.07 KB
/
MalVector.HC
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
111
112
113
114
115
116
117
118
119
120
121
// Functions for working with vector Malvals
#ifndef MALVECTOR_HC
#define MALVECTOR_HC
#include "MalList.HC"
#include "PArray.HC"
#include "Types.HC"
#include "Util.HC"
BOOL MalIsVector(Malval *m)
{
NullCheck(m, "malval");
return m->_tag == VEC;
}
Malval *MalVectorMk(PArray *pa)
{
NullCheck(pa, "list array");
Malval *m = _MalMk;
m->_tag = VEC;
m->_array = pa;
m->_metadata = MalNilMk;
return m;
}
Malval *MalVectorEmpty()
{
return MalVectorMk(PArrayEmpty);
}
Malval *MalVectorCopy(Malval *l)
{
if (MalIsVector(l)) {
return MalVectorMk(PArrayCopy(l->_array));
}
else {
return throws("MalVectorCopy expects vector argument");
}
}
INT MalVectorSize(Malval *l)
{
if (MalIsVector(l)) {
return PArraySize(l->_array);
}
else {
throws("MalVectorSize expects vector argument");
return 0; // make compiler stfu
}
}
Malval *MalVectorNth(Malval *l, INT i)
{
if (MalIsVector(l)) {
return PArrayGet(l->_array, i);
}
else {
return throws("MalVectorGet expects vector argument");
}
}
Malval *MalVectorHead(Malval *l)
{
if (MalIsVector(l)) {
if (MalVectorSize(l)) {
return PArrayFront(l->_array);
}
else {
return MalNilMk;
}
}
else {
return throws("MalVectorHead expects vector argument");
}
}
Malval *MalVectorTail(Malval *l)
{
if (MalIsVector(l)) {
if (MalVectorSize(l)) {
return MalVectorMk(PArrayTail(l->_array));
}
else {
return MalVectorEmpty;
}
}
else {
return throws("MalVectorTail expects vector argument");
}
}
Malval *MalVectorPush(Malval *l, Malval *x)
{
Malval *v;
if (MalIsVector(l)) {
v = MalVectorMk(l->_array);
PArrayPush(v->_array, x);
return v;
}
else {
return throws("MalVectorPush expects vector argument");
}
}
Malval *MalVectorConcat(Malval *l1, Malval *l2)
{
PArray *pa;
if (MalIsVector(l1) && MalIsVector(l2)) {
pa = PArrayEmpty;
PArrayConcat(pa, l1->_array);
PArrayConcat(pa, l2->_array);
return MalVectorMk(pa);
}
else {
return throws("MalVectorConcat expects vector arguments");
}
}
#endif