-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.t
183 lines (145 loc) · 4.27 KB
/
stack.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
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
require("terralibext")
local base = require("base")
local alloc = require("alloc")
local err = require("assert")
local concepts = require("concepts")
local range = require("range")
local Allocator = alloc.Allocator
local size_t = uint64
local StackBase = terralib.memoize(function(stack)
local T = stack.eltype
terra stack:reverse()
var size = self:size()
for i = 0, size / 2 do
var a, b = self:get(i), self:get(size -1 - i)
self:set(i, b)
self:set(size - 1 - i, a)
end
end
--iterator - behaves like a pointer and can be passed
--around like a value, convenient for use in ranges.
local struct iterator{
parent : &stack
ptr : &T
}
terra stack:getiterator()
return iterator{self, self:getdataptr()}
end
terra iterator:getvalue()
return @self.ptr
end
terra iterator:next()
self.ptr = self.ptr + 1
end
terra iterator:isvalid()
return self.ptr - self.parent:getdataptr() < self.parent:size()
end
stack.iterator = iterator
range.Base(stack, iterator)
end)
local DynamicStack = terralib.memoize(function(T)
local S = alloc.SmartBlock(T) --typed memory block
S:complete() --always complete the implementation of SmartBlock
local struct stack{
data : S
size : size_t
}
stack.eltype = T
stack.metamethods.__typename = function(self)
return ("DynamicStack(%s)"):format(tostring(T))
end
--add methods, staticmethods and templates tablet and template fallback mechanism
--allowing concepts-based function overloading at compile-time
base.AbstractBase(stack)
stack.staticmethods.new = terra(alloc : Allocator, capacity: size_t)
var s : stack
s.data = alloc:allocate(sizeof(T), capacity)
s.size = 0
return s
end
stack.staticmethods.frombuffer = terra(n: size_t, ptr: &T)
var s: stack
s.data = S.frombuffer(n, ptr)
s.size = n
return s
end
terra stack:getdataptr()
return self.data:getdataptr()
end
terra stack:size()
return self.size
end
terra stack:capacity()
return self.data:size()
end
terra stack:push(v : T)
--we don't allow pushing when 'data' is empty
err.assert(self.data:isempty() == false)
if self:size() == self:capacity() then
self.data:reallocate(1 + 2 * self:capacity())
end
self.size = self.size + 1
self:set(self.size - 1, v)
end
terra stack:pop()
if self:size() > 0 then
var tmp = self:get(self.size - 1)
self.size = self.size - 1
return tmp
end
end
terra stack:get(i : size_t)
err.assert(i < self:size())
return self.data:get(i)
end
terra stack:set(i : size_t, v : T)
err.assert(i < self:size())
self.data:set(i, v)
end
stack.metamethods.__apply = macro(function(self, i)
return quote
err.assert(i < self:size())
in
self.data(i)
end
end)
terra stack:insert(i: size_t, v: T)
var sz = self:size()
err.assert(i <= sz)
self:push(v)
if i < sz then
for jj = 0, sz - i do
var j = sz - 1 - jj
self(j + 1) = self(j)
end
self(i) = v
end
end
--add all methods from stack-base
StackBase(stack)
--initialize to empty block
stack.methods.__init = terra(self : &stack)
self.data:__init()
self.size = 0
end
--behavior w.r.t memory allocation, etc
terralib.ext.addmissing.__move(stack)
terralib.ext.addmissing.__forward(stack)
--specialized copy-assignment - the resource is always moved from
stack.methods.__copy = terra(from : &stack, to : &stack)
to.data = from.data:__move()
to.size = from.size
from:__init()
end
--sanity check
assert(concepts.DStack(stack), "Stack type does not satisfy the DStack concepts.")
return stack
end)
return {
StackBase = StackBase,
DynamicStack = DynamicStack
}