-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.t
353 lines (296 loc) · 8.09 KB
/
vector.t
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
local mem = require("mem")
local util = require("util")
local templatize = require("templatize")
local hash = require("hash")
local cstdlib = terralib.includec("stdlib.h")
local cstring = terralib.includec("string.h")
local cstdio = terralib.includec("stdio.h")
local cmath = terralib.includec("math.h")
local expandFactor = 1.5
local minCapacity = 2
local V
V = templatize(function(T)
-- if T:isstruct() and (T:getmethod("__destruct") or T:getmethod("__copy")) then
-- error("vector.t: cannot templatize on struct types with non-trivial destructors and/or copy constructors")
-- end
local st = terralib.sizeof(T)
local struct Vector
{
__data : &T,
__capacity : uint,
size : uint
}
Vector.metamethods.__typename = function(self)
return string.format("Vector(%s)", tostring(T))
end
Vector.ValueType = T
Vector.methods.fill = macro(function(self, ...)
local numargs = select("#",...)
local args = {}
for i=1,numargs do
local a = (select(i,...))
table.insert(args, `mem.copy(a))
end
local function buildLHS(vec)
local arrayelems = {}
for i=1,numargs do
local index = i-1
table.insert(arrayelems, `vec.__data[index])
end
return arrayelems
end
return quote
var vec = self
vec:__resize(numargs)
vec.size = numargs
[buildLHS(vec)] = [args]
in
vec
end
end)
terra Vector:__construct()
self.size = 0
self.__data = nil
self:__resize(minCapacity)
end
terra Vector:__construct(initialSize: uint, val: T)
self.__data = nil
var initCap = initialSize
if initCap < minCapacity then initCap = minCapacity end
self:__resize(initCap)
self.size = initialSize
for i=0,initialSize do
self.__data[i] = mem.copy(val)
end
end
terra Vector:__construct(initialSize: uint)
self.__data = nil
var initCap = initialSize
if initCap < minCapacity then initCap = minCapacity end
self:__resize(initCap)
self.size = initialSize
for i=0,initialSize do
mem.init(self.__data[i])
end
end
terra Vector:__copy(v: &Vector)
self.__data = nil
self:__resize(v.size)
self.size = v.size
self.__capacity = v.__capacity
for i=0,self.size do
self.__data[i] = mem.copy(v.__data[i])
end
end
Vector.__templatecopy = templatize(function(T2)
return terra(self: &Vector, v: &V(T2))
self.__data = nil
self:__resize(v.size)
self.size = v.size
self.__capacity = v.__capacity
for i=0,self.size do
self.__data[i] = [mem.templatecopy(T)](v.__data[i])
end
end
end)
terra Vector:__destruct()
self:clear()
self.__capacity = 0
cstdlib.free(self.__data)
self.__data = nil
end
Vector.metamethods.__eq = terra(self: &Vector, other: Vector)
if self.size ~= other.size then return false
else
for i=0,self.size do
if not (self.__data[i] == other.__data[i]) then return false end
end
return true
end
end
terra Vector:__hash()
return hash.rawhash([&int8](self.__data), self.size*st)
end
util.inline(Vector.methods.__hash)
terra Vector:__resize(size: uint)
self.__capacity = size
if self.__data == nil then
self.__data = [&T](cstdlib.malloc(size*st))
else
self.__data = [&T](cstdlib.realloc(self.__data, size*st))
end
end
terra Vector:__expand()
self:__resize(cmath.ceil(self.__capacity*expandFactor))
end
terra Vector:__maybeContract()
-- I'm a little uncomfortable with this, actually...
-- if self.__capacity > expandFactor*self.size then
-- self:__resize(self.size)
-- end
end
terra Vector:reserve(cap: uint)
while self.__capacity < cap do
self:__expand()
end
end
terra Vector:resize(size: uint)
var oldsize = self.size
self.size = size
while self.size > self.__capacity do
self:__expand()
end
-- If new size is greater, then we need to initialize new memory
for i=oldsize,self.size do
mem.init(self.__data[i])
end
-- If new size is smaller, then we need to destruct old memory
for i=self.size,oldsize do
mem.destruct(self.__data[i])
end
self:__maybeContract()
end
terra Vector:incrementSize()
self.size = self.size + 1
if self.size > self.__capacity then
self:__expand()
end
end
terra Vector:decrementSize()
self.size = self.size - 1
self:__maybeContract()
end
-- IMPORTANT: Client code must capture the return value of this function
-- and eventually destruct it. Otherwise, a memory leak may result
terra Vector:get(index: uint)
return mem.copy(self.__data[index])
end
util.inline(Vector.methods.get)
terra Vector:getPointer(index: uint)
return &(self.__data[index])
end
util.inline(Vector.methods.getPointer)
Vector.metamethods.__apply = macro(function(self, index)
return `self.__data[index]
end)
terra Vector:set(index: uint, val: T)
mem.destruct(self.__data[index])
self.__data[index] = mem.copy(val)
end
util.inline(Vector.methods.set)
terra Vector:push(val: T)
self:incrementSize()
self.__data[self.size-1] = mem.copy(val)
end
terra Vector:pushNoCopy(val: T)
self:incrementSize()
self.__data[self.size-1] = val
end
terra Vector:pop()
if self.size > 0 then
mem.destruct(self.__data[self.size-1])
self:decrementSize()
end
end
-- IMPORTANT: Client code must capture the return value of this function
-- and eventually destruct it. Otherwise, a memory leak may result
terra Vector:back()
return mem.copy(self.__data[self.size-1])
end
util.inline(Vector.methods.back)
terra Vector:backPointer()
return &(self.__data[self.size-1])
end
util.inline(Vector.methods.backPointer)
terra Vector:insert(index: uint, val: T)
if index <= self.size then
if index == self.size then
self:push(val)
else
if self.size+1 > self.__capacity then
self:__expand()
end
cstring.memmove(self.__data+(index+1), self.__data+index, (self.size-index)*st)
self.__data[index] = mem.copy(val)
self.size = self.size + 1
end
else
cstdio.printf("Vector:insert - index out of range.\n")
cstdlib.exit(1)
end
end
terra Vector:remove(index: uint)
if index < self.size then
mem.destruct(self.__data[index])
if index < self.size - 1 then
cstring.memmove(self.__data+index, self.__data+(index+1), (self.size-index-1)*st)
end
self:decrementSize()
else
cstdio.printf("Vector:remove - index out of range.\n")
cstdlib.exit(1)
end
end
terra Vector:clear()
for i=0,self.size do mem.destruct(self.__data[i]) end
self.size = 0
end
terra Vector:clearAndDelete()
for i=0,self.size do mem.delete(self.__data[i]) end
self.size = 0
end
terra Vector:clearAndReclaimMemory()
self:clear()
self:__resize(minCapacity)
end
-- I really should get around to writing a dynamically-sized linalg Vec class,
-- but for now, I'm just adding the things I absolutely need (i.e. for
-- autocorrelation) here.
Vector.metamethods.__add = terra(self: Vector, other: Vector)
util.assert(self.size == other.size, "Attempt to add Vectors of different size.\n")
var v = Vector.stackAlloc(self.size)
for i=0,self.size do v(i) = self(i) + other(i) end
return v
end
Vector.metamethods.__sub = terra(self: Vector, other: Vector)
util.assert(self.size == other.size, "Attempt to subtract Vectors of different size.\n")
var v = Vector.stackAlloc(self.size)
for i=0,self.size do v(i) = self(i) - other(i) end
return v
end
Vector.metamethods.__div = terra(self: Vector, scalar: T)
var v = Vector.stackAlloc(self.size)
for i=0,self.size do v(i) = self(i) / scalar end
return v
end
terra Vector:dot(other: Vector)
util.assert(self.size == other.size, "Attempt to dot Vectors of different size.\n")
var sum = T(0.0)
for i=0,self.size do
sum = self(i) * other(i)
end
return sum
end
mem.addConstructors(Vector)
return Vector
end)
V.fromItems = macro(function(...)
local T = (select(1,...)):gettype()
local args = {}
for i=1,select("#",...) do
table.insert(args, (select(i,...)))
end
return `[V(T)].stackAlloc():fill([args])
end)
-- Like fromItems, but callable from Lua code, and gc's the resulting object
function V.fromNums(...)
local v = terralib.new(V(double))
V(double).methods.__construct(v)
V(double).methods.resize(v, select("#",...))
for i=1,select("#",...) do
V(double).methods.set(v, i-1, (select(i,...)))
end
mem.gc(v)
return v
end
return V