-
Notifications
You must be signed in to change notification settings - Fork 2
/
list.cpp
242 lines (210 loc) · 6.84 KB
/
list.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <stdexcept>
#include <algorithm>
#include "list.h"
#include "int.h"
#include "bool.h"
#include "func.h"
#include "assert.h"
#include "none.h"
#include "list_iterator.h"
value make$list() {
value ret;
ret.type = value::LIST;
ret.listval = new list_t();
return ret;
}
value append$list$(value self, value v) {
assert(self.type == value::LIST);
self.listval->push_back(v);
return make$none();
}
value pop$list(value self) {
assert(self.type == value::LIST);
value ret = self.listval->back();
self.listval->pop_back();
return ret;
}
value __getitem__$list$(value self, value k) {
assert(self.type == value::LIST);
switch (k.type) {
case value::INT:
return __getitem__$list$int_(self, k);
case value::SLICE:
return __getitem__$list$slice(self, k);
}
throw std::runtime_error("Invalid argument type for list::__getitem__");
}
value __setitem__$list$$(value self, value k, value v) {
assert(self.type == value::LIST);
switch (k.type) {
case value::INT:
return __setitem__$list$int_$(self, k, v);
case value::SLICE:
return __setitem__$list$slice$(self, k, v);
}
return make$none();
}
value __add__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
list_t *merged = new list_t();
merged->reserve(x.listval->size() + y.listval->size());
merged->insert(merged->end(), x.listval->begin(), x.listval->end());
merged->insert(merged->end(), y.listval->begin(), y.listval->end());
value ret;
ret.type = value::LIST;
ret.listval = merged;
return ret;
}
value __mul__$list$bool_(value self, value n) {
assert(self.type == value::LIST && n.type == value::BOOL);
return __mul__$list$int_(self, __int__$bool_(n));
}
value __mul__$bool_$list(value n, value self) {
assert(self.type == value::LIST && n.type == value::BOOL);
return __mul__$int_$list(__int__$bool_(n), self);
}
value __mul__$int_$list(value n, value self) {
assert(self.type == value::LIST && n.type == value::INT);
return __mul__$list$int_(self, n);
}
value __mul__$list$int_(value self, value n) {
assert(self.type == value::LIST && n.type == value::INT);
list_t *duped = new list_t();
if (n.intval > 0) {
duped->reserve(self.listval->size() * n.intval);
for (long i = 0; i < n.intval; i++)
duped->insert(duped->end(), self.listval->begin(), self.listval->end());
}
value ret;
ret.type = value::LIST;
ret.listval = duped;
return ret;
}
value __len__$list(value self) {
assert(self.type == value::LIST);
return make$int_(self.listval->size());
}
value __eq__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval == *y.listval);
}
value __ne__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval != *y.listval);
}
value __lt__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval < *y.listval);
}
value __le__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval <= *y.listval);
}
value __ge__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval >= *y.listval);
}
value __gt__$list$list(value x, value y) {
assert(x.type == value::LIST && y.type == value::LIST);
return make$bool_(*x.listval > *y.listval);
}
value __contains__$list$(value self, value v) {
assert(self.type == value::LIST);
for (size_t i = 0; i < self.listval->size(); i++)
if ((*self.listval)[i] == v)
return make$bool_(true);
return make$bool_(false);
}
value __delitem__$list$int_(value self, value k) {
assert(self.type == value::LIST && k.type == value::INT);
self.listval->erase(self.listval->begin() + k.intval);
return make$none();
}
value __delitem__$list$slice(value self, value k) {
assert(false);
return make$none();
}
value __bool__$list(value self) {
assert(self.type == value::LIST);
return make$bool_(!self.listval->empty());
}
value __getitem__$list$int_(value self, value k) {
return self.listval->at(k.intval);
}
value __getitem__$list$slice(value self, value k) {
long start = k.sliceval->start;
long stop = k.sliceval->stop;
long step = k.sliceval->step;
if (start < 0)
start = (long)self.listval->size() + start;
if (stop < 0)
stop = (long)self.listval->size() + stop;
list_t *ret = new list_t();
if (step > 0) {
start = std::max(0L, start);
stop = std::min((long)self.listval->size(), stop);
for (long i = start; i < stop; i += step) {
ret->push_back(self.listval->at(i));
}
} else {
start = std::min((long)self.listval->size() - 1L, start);
stop = std::max(-1L, stop);
for (long i = start; i > stop; i += step) {
ret->push_back(self.listval->at(i));
}
}
value val;
val.type = value::LIST;
val.listval = ret;
return val;
}
value __setitem__$list$int_$(value self, value k, value v) {
assert(self.type == value::LIST && k.type == value::INT);
self.listval->at(k.intval) = v;
return make$none();
}
value __setitem__$list$slice$(value self, value k, value v) {
assert(self.type == value::LIST && k.type == value::SLICE);
if (v.type != value::LIST)
throw std::runtime_error("list::__setitem__ must pass in a list");
long start = k.sliceval->start;
long stop = k.sliceval->stop;
long step = k.sliceval->step;
if (start < 0)
start = (long)self.listval->size() + start;
if (stop < 0)
stop = (long)self.listval->size() + stop;
if (step > 0) {
start = std::min((long)self.listval->size(), std::max(0L, start));
stop = std::max(start, std::min((long)self.listval->size(), stop));
if (step == 1) {
self.listval->erase(self.listval->begin() + start,
self.listval->begin() + stop);
self.listval->insert(self.listval->begin() + start, v.listval->begin(),
v.listval->end());
} else {
long count = (stop - start + step - 1) / step;
if (count != v.listval->size())
throw std::runtime_error(
"list::__setitem__ slice length not match value length");
for (long i = 0; i < count; i++) {
self.listval->at(start + step * i) = v.listval->at(i);
}
}
} else {
start = std::min((long)self.listval->size() - 1L, std::max(-1L, start));
stop = std::min(start, std::max(-1L, stop));
long count = (start - stop + -step - 1) / -step;
if (count != v.listval->size())
throw std::runtime_error(
"list::__setitem__ slice length not match value length");
for (long i = 0; i < count; i++) {
self.listval->at(start + i * step) = v.listval->at(i);
}
}
return make$none();
}
value __iter__$list(value self) {
assert(self.type == value::LIST);
return make$list_iterator(self.listval);
}