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
|
/#
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
#/
;struct Vector (raw type T) {
uint
length,
dataSize,
~T data
}
/; method Vector
# Constructors
/; Vector
;self.length = 0
;self.dataSize = 1
;alloc self.data, size T
;/
/; Vector (T first)
;self.length = 1
;self.dataSize = 1
;alloc self.data, size T
;self.data{0} = first
;/
/; Vector ({}T arr)
;self.length = len arr
;self.data_size = 1
/; loop (self.dataSize < self.length)
;self.dataSize *= 2
;/
;alloc self.data, self.dataSize * size T
/; loop (uint i = 0; i < self.length) [i++]
;self.data{i} = arr{i}
;/
;/
# Operator overloads
/; operator {} (uint index) [~T]
/; if (index !< 0 && index < self.length)
;return self.data + index
;/
# Probably not what errors will look like long term, but
# serves as a proof of concept for now
;throw Error("Bad index", ERROR_CODE.OUT_OF_RANGE)
;/
/; operator len [uint]
;return self.length
;/
/; operator == (Vector(T) vec) [bool]
/; if (vec.length != self.length)
;return false
;/
/; loop (uint i = 0; i < self.length) [i++]
/; if (vec.data{i} != self.data{i})
;return false
;/
;/
;return true
;/
/; operator delete
;delete self.data
;/
# Internal
/; _grow
;realloc self.data, self.dataSize * 2 * size T
;self.dataSize *= 2
;/
/; _shrink
;realloc self.data, self.dataSize / 2 * size T
;self.dataSize /= 2
;/
# Basic operations
/; add (T item, uint index)
/; if (index < 0 || index > self.length)
;throw Error("Bad index", ERROR_CODE.OUT_OF_RANGE)
;/
# If it checks out then we check if we should grow
/; if (self.length >= self.dataSize)
# Might throw a memory allocation error
;self._grow()
;/
/; loop (uint i = self.length; i > index) [i--]
;self.data{i} = self.data{i - 1}
;/
;self{index} = replace
;self.length += 1
;/
/; remove (uint index) [T]
;T out = self{index}
/; loop (uint i = index; i < self.length - 1) [i++]
;self.data{i} = self.data{i + 1}
;/
self.length -= 1
/; if (self.length < self.dataSize / 4)
;self._shrink()
;/
;return out
;/
/; remove_item (T item) [bool]
;bool removed = false
/; loop (uint i = 0; i < self.length) [i++]
/; if (!removed && self.data{i} == item)
;removed = true
;; else if (removed)
;self.data{i - 1} = self.data{i}
;/
;/
/; if (removed)
;self.length -= 1
/; if (self.length < self.dataSize / 4)
;self._shrink()
;/
;/
;return removed
;/
/; remove_all (T item) [uint]
;uint removed = 0
/; loop (uint i = 0; i < self.length - removed) [i++]
/; loop (self.data{i + removed} == item && removed < self.length)
;removed += 1
;; if (removed != 0 && removed < self.length)
;self.data{i} = self.data{i + removed}
;/
;/
;self.length -= removed
/; loop (self.length < self.dataSize / 4)
;self._shrink()
;/
;return removed
;/
/; push (T item) [uint]
;self.add(item, self.length)
;return self.length - 1
;/
/; pop [T]
;return self.remove(self.length - 1)
;/
;/
|