This commit is contained in:
Manuel Simon Hirsig
2016-09-04 01:29:57 +02:00
parent 05fd1587cd
commit 8be659f4b4
3 changed files with 37 additions and 36 deletions
+13 -12
View File
@@ -13,11 +13,12 @@ function wipe(t)
setmetatable(t, nil); setn(t, 0)
for k in t do t[k] = nil end
t.reset, t.reset = nil, 1
return t
end
public.wipe = wipe
function recycle(t)
wipe(t); tmp[wipe(t)] = nil
wipe(t); tmp[t] = nil
if pool_size < 50 then
pool_size = pool_size + 1
pool[pool_size] = t
@@ -41,38 +42,38 @@ public.t.get = table
function temp_table() local t = table(); tmp[t] = true; return t end
public.tt.get = temp_table
do local mt = {__metatable=false, __newindex=error}
function public.empty.get() return setmetatable(temp_table, mt) end
do local mt = {__newindex=nop}
function public.empty.get() return setmetatable(temp_table(), mt) end
end
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = true end return v end
mt = {__metatable=false, __call=f, __sub=f}
mt = {__call=f, __sub=f}
public.temp {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = true end}
end
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = nil end return v end
mt = {__metatable=false, __call=f, __sub=f}
mt = {__call=f, __sub=f}
public.perm {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = nil end}
end
do
local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4
local function insert(type)
local chunk = 'setmetatable(t, nil); local setn, error = table.setn, error; return function(t'
local chunk = 'local setmetatable, setn, error = setmetatable, table.setn, error; return function(t'
for i = 1, 98 do chunk = chunk..',a'..i end
chunk = chunk..',overflow)'
chunk = chunk..',overflow) setmetatable(t, nil);'
if type == SET then
for i = 1, 98 do
chunk = chunk..format('if a%d == nil then return t end t[a%d] = true;', i, i)
end
elseif type == ARRAY then
chunk = chunk..'setn(t, 99); if a1 == nil then return t end t[1] = a1;'
chunk = chunk..'setn(t, 98); if a1 == nil then return t end t[1] = a1;'
for i = 2, 98 do
chunk = chunk..format('if a%d == nil then setn(t, %d); return t end t[%d] = a%d;', i, i - 1, i, i)
end
elseif type == ARRAY0 then
chunk = chunk..'setn(t, 99);'
chunk = chunk..'setn(t, 98);'
for i = 1, 98 do chunk = chunk..format('t[%d] = a%d;', i, i) end
elseif type == TABLE then
for i = 1, 97, 2 do
@@ -83,9 +84,9 @@ do
local f = loadstring(chunk); setfenv(f, M); return f()
end
local function getter(type)
local mt = {__metatable=false, __newindex=error, __call=insert(type)}
function mt:__index(t) recycle(self); return setmetatable(mt, t) end
local mt = {__newindex=nop, __call=insert(type)}
function mt:__index(t) recycle(self); return setmetatable(t, mt) end
return function() return setmetatable(t, mt) end
end
public(); S, A, A0, T = {get=getter(SET)}, {get=getter(ARRAY)}, {get=getter(ARRAY0)}, {get=getter(TABLE)}
public(); S.get = getter(SET); A.get = getter(ARRAY); A0.get = getter(ARRAY0); T.get = getter(TABLE)
end