From 91b057d2fd7f684fe7e43f69baee5c67ff154303 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Sun, 4 Sep 2016 19:24:58 +0200 Subject: [PATCH] refactoring --- libs/green_t.lua | 41 ++++++++++++++++++----------------------- util/core.lua | 4 ---- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/libs/green_t.lua b/libs/green_t.lua index 61583a1..e15d968 100644 --- a/libs/green_t.lua +++ b/libs/green_t.lua @@ -29,8 +29,8 @@ function release(t) end public.release = release -function public.bk(t) - if getn(t) > 0 then return tremove(t, 1), bk(t) else release(t) end +function public.strip(t) + if getn(t) > 0 then return tremove(t, 1), strip(t) else release(t) end end function acquire() @@ -53,47 +53,42 @@ end do local f, mt f = function(_, v) if type(v) == 'table' then auto_release[v] = true end return v end - mt = {__call=f, __sub=f, __exp=f} + mt = {__call=f, __sub=f} public.temp {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = true end} end do local f, mt f = function(_, v) if type(v) == 'table' then auto_release[v] = nil end return v end - mt = {__call=f, __sub=f, __exp=f} + mt = {__call=f, __sub=f} public.perm {get=function() return setmetatable(acquire_temp(), mt) end, set=function(t) auto_release[t] = nil end} end -do local mt, object, key = {__newindex=nop}, {}, nil - function mt:__unm() local temp = object; object = acquire(); return temp end +do local mt, key = {}, nil + function mt:__unm() local temp = mt.__index; mt.__index = nil; return temp end function mt:__index(k) key = k; return self end - function mt:__call(v) object[key] = v; key = nil return self end - function public.object.get() return setmetatable(acquire_temp(), mt) end + function mt:__call(v) self[key] = v; key = nil return self end + function public.__(t) mt.__newindex = wipe(t); return setmetatable(acquire_temp(), mt) end end 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)' + local function constructor(type) + local chunk = 'local setn, error = table.setn, error; return function(' + for i = 1, 99 do chunk = chunk..'a'..i..',' end + chunk = chunk..'overflow) local t = t' if type == SET then - for i = 1, 98 do chunk = format('%s; if a%d == nil then return t end; t[a%d] = true', chunk, i, i) end + for i = 1, 99 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 = format('%s; if a%d == nil then setn(t, %d); return t end; t[%d] = a%d', chunk, i, i - 1, i, i) end + chunk = chunk..'; setn(t, 99); if a1 == nil then return t end; t[1] = a1' + for i = 2, 99 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 = format('%s; t[%d] = a%d', chunk, i, i) end + chunk = chunk..'; setn(t, 99)' + for i = 1, 99 do chunk = format('%s; t[%d] = a%d', chunk, i, i) end elseif type == TABLE then 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 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) 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) + public(); S, A, A0, T = constructor(SET), constructor(ARRAY), constructor(ARRAY0), constructor(TABLE) end do local chunk = 'return function(i' diff --git a/util/core.lua b/util/core.lua index 655d89e..d4267cd 100644 --- a/util/core.lua +++ b/util/core.lua @@ -2,10 +2,6 @@ aux 'core' public.join = _G.table.concat -do local mt = {__index=function(self, key) setmetatable(self, nil); return getfenv(2)[key] end} - function public.__(t) return setmetatable(t, mt) end -end - function public.range(arg1, arg2) local i, n = arg2 and arg1 or 1, arg2 or arg1 if i <= n then return first, range(i + 1, n) end