refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-09-04 15:46:35 +02:00
parent cfa7ed5d14
commit 0af41b1c23
2 changed files with 27 additions and 64 deletions
+17
View File
@@ -25,9 +25,14 @@ function release(t)
else else
overflow_pool[t] = true overflow_pool[t] = true
end end
return
end end
public.release = release public.release = release
function public.bk(t)
if getn(t) > 0 then return tremove(t, 1), bk(t) else release(t) end
end
function acquire() function acquire()
if pool_size > 0 then if pool_size > 0 then
pool_size = pool_size - 1 pool_size = pool_size - 1
@@ -84,4 +89,16 @@ do local SET, ARRAY, ARRAY0, TABLE = 1, 2, 3, 4
return function() return setmetatable(t, mt) end return function() return setmetatable(t, mt) end
end end
public(); S.get = getter(SET); A.get = getter(ARRAY); A0.get = getter(ARRAY0); T.get = getter(TABLE) public(); S.get = getter(SET); A.get = getter(ARRAY); A0.get = getter(ARRAY0); T.get = getter(TABLE)
end
do local chunk = 'return function(i'
for i = 1, 99 do chunk = chunk..',a'..i end
chunk = chunk..')'
for i = 1, 99 do
chunk = format('%s if i == %d then return a%i', chunk, i, i)
for j = i + 1, 99 do chunk = chunk..',a'..j end
chunk = chunk..' end'
end
chunk = chunk..' end'
public.select = loadstring(chunk)()
end end
+10 -64
View File
@@ -22,9 +22,7 @@ do
state = nil state = nil
return temp return temp
end end
function public.present(v) function public.present(v) state = v; return f end
state = v; return f
end
end end
do do
@@ -67,18 +65,6 @@ public.huge = 1.8*10^308
function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end function public.modified.get() return IsShiftKeyDown() or IsControlKeyDown() or IsAltKeyDown() end
--do TODO
-- local _state = setmetatable({}, {__mode='kv'})
-- local __index = function(self, key)
-- return _state[self].handler({public=self, private=_state[self].state}, key)
-- end
-- function public.class(state, handler)
-- local state, self = {handler=handler, state=state}, {}
-- _state[self] = state
-- return setmetatable(self, {__metatable=false, __index=__index, state=state})
-- end
--end
do do
local _state = setmetatable(t, T('__mode', 'kv')) local _state = setmetatable(t, T('__mode', 'kv'))
local __index = function(self, key) local __index = function(self, key)
@@ -91,12 +77,6 @@ do
end end
end end
function public.expand(array, ...) temp=arg
local t = t
for i = 1, arg.n do t[arg[i]] = array[i] end
return t
end
function public.copy(t) function public.copy(t)
local copy = M.t local copy = M.t
for k, v in t do copy[k] = v end for k, v in t do copy[k] = v end
@@ -104,27 +84,6 @@ function public.copy(t)
return setmetatable(copy, getmetatable(t)) return setmetatable(copy, getmetatable(t))
end end
function public.select(i, ...) temp=arg
while i > 1 do i = i - 1; tremove(arg, i) end
return tremove(arg, 1), unpack(arg)
end
function public.collect(indices, ...) temp=arg
local t = M.t
for _, v in indices do
tinsert(t, arg[v])
end
return t
end
function public.hashset(t)
local set = M.t
for _, v in t do
set[v] = true
end
return set
end
function public.size(t) function public.size(t)
local size = 0 local size = 0
for _ in t do size = size + 1 end for _ in t do size = size + 1 end
@@ -132,12 +91,10 @@ function public.size(t)
end end
function public.key(value, t) function public.key(value, t)
for k, v in t do for k, v in t do if v == value then return k end end
if v == value then return k end
end
end end
function public.keys(t) function public.keys(t) -- TODO recursive return instead of new table
local keys = M.t local keys = M.t
for k in t do tinsert(keys, k) end for k in t do tinsert(keys, k) end
return keys return keys
@@ -152,14 +109,10 @@ end
function public.eq(t1, t2) function public.eq(t1, t2)
if not t1 or not t2 then return false end if not t1 or not t2 then return false end
for key, value in t1 do for key, value in t1 do
if t2[key] ~= value then if t2[key] ~= value then return false end
return false
end
end end
for key, value in t2 do for key, value in t2 do
if t1[key] ~= value then if t1[key] ~= value then return false end
return false
end
end end
return true return true
end end
@@ -178,9 +131,7 @@ end
function public.all(t, p) function public.all(t, p)
for _, v in t do for _, v in t do
if p then if p then
if not p(v) then if not p(v) then return false end
return false
end
elseif not v then elseif not v then
return false return false
end end
@@ -190,9 +141,7 @@ end
function public.filter(t, p) function public.filter(t, p)
for k, v in t do for k, v in t do
if not p(v, k) then if not p(v, k) then t[k] = nil end
t[k] = nil
end
end end
return t return t
end end
@@ -202,14 +151,12 @@ function public.map(t, f)
return t return t
end end
function public.trim(str) function public.trim(str) return gsub(str, '^%s*(.-)%s*$', '%1') end
return gsub(str, '^%s*(.-)%s*$', '%1')
end
function public.split(str, separator) function public.split(str, separator)
local parts = t local parts = t
while true do while true do
local start_index, _ = strfind(str, separator, 1, true) local start_index = strfind(str, separator, 1, true)
if start_index then if start_index then
local part = strsub(str, 1, start_index - 1) local part = strsub(str, 1, start_index - 1)
tinsert(parts, part) tinsert(parts, part)
@@ -238,7 +185,6 @@ function public.later(t0, t)
return function() return GetTime() - t0 > t end return function() return GetTime() - t0 > t end
end end
function public.signal() function public.signal() local params
local params
return function(...) params = arg end, function() return params end return function(...) params = arg end, function() return params end
end end