This commit is contained in:
Manuel Simon Hirsig
2016-09-04 03:50:42 +02:00
parent a9cce523b1
commit 10f8890e78
2 changed files with 38 additions and 44 deletions
+23 -28
View File
@@ -1,12 +1,12 @@
green_t = module
local next, setn, type, setmetatable = next, table.setn, type, setmetatable
local wipe, recycle, table, temp_table
local wipe, release, acquire, acquire_temp
local pool, pool_size, overflow_pool, tmp = {}, 0, setmetatable({}, {__mode='k'}), {}
CreateFrame('Frame'):SetScript('OnUpdate', function()
for t in tmp do recycle(t) end; wipe(tmp)
for t in tmp do release(t) end; wipe(tmp)
end)
function wipe(t)
@@ -17,7 +17,7 @@ function wipe(t)
end
public.wipe = wipe
function recycle(t)
function release(t)
wipe(t); tmp[t] = nil
if pool_size < 50 then
pool_size = pool_size + 1
@@ -26,9 +26,9 @@ function recycle(t)
overflow_pool[t] = true
end
end
public.recycle = recycle
public.release = release
function table()
function acquire()
if pool_size > 0 then
pool_size = pool_size - 1
return pool[pool_size + 1]
@@ -37,55 +37,50 @@ function table()
if t then overflow_pool[t] = nil; return t end
return {}
end
public.t.get = table
public.t.get = acquire
function temp_table() local t = table(); tmp[t] = true; return t end
public.tt.get = temp_table
function acquire_temp() local t = acquire(); tmp[t] = true; return t end
public.tt.get = acquire_temp
do local mt = {__newindex=nop}
function public.empty.get() return setmetatable(temp_table(), mt) end
function public.empty.get() return setmetatable(acquire_temp(), mt) end
end
do local f, mt
f = function(_, v) if type(v) == 'table' then tmp[v] = true end return v end
mt = {__call=f, __sub=f}
public.temp {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = true end}
public.temp {get=function() return setmetatable(acquire_temp(), 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 = {__call=f, __sub=f}
public.perm {get=function() return setmetatable(temp_table(), mt) end, set=function(t) tmp[t] = nil end}
public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) tmp[t] = nil end}
end
do
local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4
--T :kek(5) :foo(1) :bar{} :moo 'trump' TODO possible alternative constructor syntax, but high overhead, more elegant because not limited, sometimes better looking.
do local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4
local function insert(type)
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) setmetatable(t, nil);'
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
for i = 1, 98 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = true', chunk, i, i) end
elseif type == ARRAY then
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
chunk = chunk..'; setn(t, 98); if a1 == nil then return t end; t[1] = a1'
for i = 2, 98 do chunk = format('%s; if a%d == nil then setn(t, %d); return t end; t[%d] = a%d', chunk, i, i - 1, i, i) end
elseif type == ARRAY0 then
chunk = chunk..'setn(t, 98);'
for i = 1, 98 do chunk = chunk..format('t[%d] = a%d;', i, i) end
chunk = chunk..'; setn(t, 98)'
for i = 1, 98 do chunk = format('%s; t[%d] = a%d', chunk, i, i) end
elseif type == TABLE then
for i = 1, 97, 2 do
chunk = chunk..format('if a%d == nil then return t end t[a%d] = a%d;', i, i, i + 1)
end
for i = 1, 97, 2 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = a%d', chunk, i, i, i + 1) end
end
chunk = chunk..'return overflow ~= nil and error("Overflow.") or t end'
chunk = chunk..'; return (overflow == nil or error "Overflow.") and t end'
local f = loadstring(chunk); setfenv(f, M); return f()
end
local function getter(type)
local mt = {__newindex=nop, __call=insert(type)}
function mt:__index(t) recycle(self); return setmetatable(t, mt) end
function mt:__index(t) release(self); return setmetatable(t, mt) end
return function() return setmetatable(t, mt) end
end
public(); S.get = getter(SET); A.get = getter(ARRAY); A0.get = getter(ARRAY0); T.get = getter(TABLE)
+15 -16
View File
@@ -5,21 +5,20 @@ local PUBLIC, PRIVATE = 1, 2
local CALL, INDEX, NEWINDEX = 1, 2, 3
local inspect
do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end
inspect = setmetatable({}, {
__call=function(_, ...)
for i = 1, arg.n do
print('arg'..i..' = '..(type(arg[i]) == 'string' and '"'..arg[i]..'"' or tostring(arg[i])))
if type(arg[i]) == 'table' and next(arg[i]) then
print('{')
for k, v in arg[i] do print(' '..(type(k) == 'string' and k or '['..tostring(k)..']')..' = '..(type(v) == 'string' and '"'..v..'"' or tostring(v))) end
print('}')
end
do local function print(msg) DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..msg) end
local mt = {__metatable=false, __pow=function(self, v) self(v); return v end}
function mt:__call(...)
for i = 1, arg.n do
print('arg'..i..' = '..(type(arg[i]) == 'string' and '"'..arg[i]..'"' or tostring(arg[i])))
if type(arg[i]) == 'table' and next(arg[i]) then
print('{')
for k, v in arg[i] do print(' '..(type(k) == 'string' and k or '['..tostring(k)..']')..' = '..(type(v) == 'string' and '"'..v..'"' or tostring(v))) end
print('}')
end
return unpack(arg)
end,
__pow=function(self, v) self(v); return v end,
})
end
return unpack(arg)
end
inspect = setmetatable({}, mt)
end
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
@@ -103,8 +102,8 @@ setmetatable(_G, {
if key ~= 'module' then return nil end
local interface, environment, declarator = INTERFACE {}, ENVIRONMENT {}, DECLARATOR {}
local self; self = {
access = {pp=PRIVATE, _G=PRIVATE, I=PRIVATE, M=PRIVATE, public=PRIVATE, private=PRIVATE, import=PRIVATE, _=PRIVATE, p=PRIVATE, error=PRIVATE, nop=PRIVATE},
[CALL] = {pp=function(o) inspect(o == ENVIRONMENT) end, _G=_G, I=interface, M=environment, import=function(interface) import(self, interface) end, p=inspect, error=error, nop=nop},
access = {_G=PRIVATE, I=PRIVATE, M=PRIVATE, public=PRIVATE, private=PRIVATE, import=PRIVATE, _=PRIVATE, p=PRIVATE, error=PRIVATE, nop=PRIVATE},
[CALL] = {_G=_G, I=interface, M=environment, import=function(interface) import(self, interface) end, p=inspect, error=error, nop=nop},
[INDEX] = {public=function() return declarator.public end, private=function() return declarator.private end},
[NEWINDEX] = {p=inspect, _=nop},
declarator = declarator,