-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.lua
238 lines (215 loc) · 4.96 KB
/
helpers.lua
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
local os = require("os")
local math = require("math")
module(..., package.seeall)
operator = {
mod = math.mod;
pow = math.pow;
add = function(n,m) return n + m end;
sub = function(n,m) return n - m end;
mul = function(n,m) return n * m end;
div = function(n,m) return n / m end;
gt = function(n,m) return n > m end;
lt = function(n,m) return n < m end;
eq = function(n,m) return n == m end;
le = function(n,m) return n <= m end;
ge = function(n,m) return n >= m end;
ne = function(n,m) return n ~= m end;
assign = function(a,b) return b end;
}
isarray = function(a)
if type(a) == "table" and (a.__metatable == Array or a._type == "array") then
return true
else
return false
end
end
zeros = function(length)
local temp = {}
for i = 0,length-1,1 do
temp[i] = 0
end
return temp
end
equal = function(tbl1, tbl2)
local answer = true
for i=0,#tbl1 do
if tbl1[i] ~= tbl2[i] then
answer = false
break
end
end
return answer
end
-- benchmarking function
benchmark = function(f, count, name)
local t1 = os.clock()
for i = 1, count, 1 do
f()
end
local t2 = os.clock()
print("Benchmark "..name.." took ".. (t2-t1)/(1.0*count) .." seconds/iteration")
end
zerobased = function(tbl)
if tbl[0] == nil then
local result = {}
for i=1,#tbl,1 do
result[i-1] = tbl[i]
end
return result
end
return tbl
end
onebased = function(tbl)
if tbl[0] ~= nil then
local result = {}
for i=1,#tbl+1,1 do
result[i] = tbl[i-1]
end
return result
end
return tbl
end
binmap = function(func, tbl1, tbl2)
local newtbl = {}
local start = 0
local stop = #tbl1
assert(tbl1[0] ~= nil)
if tbl1[0] == nil then
assert(tbl2[0] == nil)
start = 1
stop = #tbl1
end
for i=start,stop,1 do
newtbl[i] = func(tbl1[i], tbl2[i])
end
return newtbl
end
copy = function(tbl)
local newtbl = {}
local start = 0
local stop = #tbl
if tbl[0] == nil then
start = 1
stop = #tbl
end
for i=start,stop,1 do
newtbl[i] = tbl[i]
end
return newtbl
end
reduce = function(func, tbl, initial)
local result = initial
local start = 0
local stop = #tbl
if tbl[0] == nil then
start = 1
end
for i=start,stop do
result = func(result, tbl[i])
end
return result
end
cumreduce = function(func, tbl, initial)
local newtbl = {}
local result = initial
local start = 0
local stop = #tbl+1
assert(tbl[0] ~= nil)
if tbl[0] == nil then
start = 1
stop = #tbl
end
for i=start,stop,1 do
newtbl[i] = func(result, tbl[i])
end
return newtbl
end
reverse = function(tbl)
local newtbl = {}
local length = #tbl
local start = 0
local stop = #tbl
assert(tbl[0] ~= nil)
if tbl[0] == nil then
start = 1
stop = #tbl
length = #tbl + 1
end
for i=start,stop,1 do
newtbl[length - i] = tbl[i]
end
return newtbl
end
-- table to formatted string
local function table_print (tt, indent, done)
done = done or {}
indent = indent or 0
if type(tt) == "table" then
local sb = {}
for key, value in pairs (tt) do
table.insert(sb, string.rep (" ", indent)) -- indent it
if type (value) == "table" and not done [value] then
done [value] = true
table.insert(sb, "{\n");
table.insert(sb, table_print (value, indent + 2, done))
table.insert(sb, string.rep (" ", indent)) -- indent it
table.insert(sb, "}\n");
elseif "number" == type(key) then
table.insert(sb, string.format("%s ", tostring(value)))
else
table.insert(sb, string.format(
"%s = \"%s\"\n", tostring (key), tostring(value)))
end
end
return table.concat(sb)
else
return tt .. "\n"
end
end
-- pretty printing helper
function to_string( tbl )
if "nil" == type( tbl ) then
return tostring(nil)
elseif "table" == type( tbl ) then
return table_print(tbl)
elseif "string" == type( tbl ) then
return tbl
else
return tostring(tbl)
end
end
-- printf helper
function printf(s,...)
return io.write(s:format(...))
end -- function
-- helpers to protect agains undefined globals, or any globals at all
local mt = getmetatable(_G)
if mt == nil then
mt = {}
setmetatable(_G, mt)
end
__STRICT = true
__FORBID_GLOBALS = false
mt.__declared = {}
mt.__newindex = function (t, n, v)
if __FORBID_GLOBALS then
error("declared global " .. n .. " with value ".. tostring(v))
end
if __STRICT and not mt.__declared[n] then
local w = debug.getinfo(2, "S").what
if w ~= "main" and w ~= "C" then
error("assign to undeclared variable '"..n.."'", 2)
end
mt.__declared[n] = true
end
rawset(t, n, v)
end
mt.__index = function (t, n)
if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then
error("variable '"..n.."' is not declared", 2)
end
return rawget(t, n)
end
function global(...)
for _, v in ipairs{...} do mt.__declared[v] = true end
end