green_t refactoring

This commit is contained in:
Manuel Simon Hirsig
2016-08-31 03:56:58 +02:00
parent adb595ba8d
commit 02952ba91a
4 changed files with 43 additions and 86 deletions
+35 -79
View File
@@ -1,6 +1,6 @@
if module 'green_t' then return end
local next, getn, setn, setmetatable, rawget = next, getn, table.setn, setmetatable, rawget
local next, setn, setmetatable = next, table.setn, setmetatable
local wipe, recycle, table
local pool, pool_size = {}, 0
local overflow_pool = setmetatable({}, {__mode='k'})
@@ -93,83 +93,39 @@ do
end
end
do
local function insert_keys(t,k1,k2,k3,k4,k5,k6,k7,k8,k9,k10,k11,k12,k13,k14,k15,k16,k17,k18,k19,k20,overflow)
if k1 == nil then return end; t[k1] = true
if k2 == nil then return end; t[k2] = true
if k3 == nil then return end; t[k3] = true
if k4 == nil then return end; t[k4] = true
if k5 == nil then return end; t[k5] = true
if k6 == nil then return end; t[k6] = true
if k7 == nil then return end; t[k7] = true
if k8 == nil then return end; t[k8] = true
if k9 == nil then return end; t[k9] = true
if k10 == nil then return end; t[k10] = true
if k11 == nil then return end; t[k11] = true
if k12 == nil then return end; t[k12] = true
if k13 == nil then return end; t[k13] = true
if k14 == nil then return end; t[k14] = true
if k15 == nil then return end; t[k15] = true
if k16 == nil then return end; t[k16] = true
if k17 == nil then return end; t[k17] = true
if k18 == nil then return end; t[k18] = true
if k19 == nil then return end; t[k19] = true
if k20 == nil then return end; t[k20] = true
if overflow ~= nil then error('Overflow.') end
local KEYS, VALUES, RETURN_VALUES, PAIRS = 1, 2, 3, 4
local function insert(type)
local code = 'local setn = table.setn; return function('
for i = 1, 99 do
code = code..'a'..i..','
end
code = code..'overflow) local t = t;'
if type == KEYS then
for i = 1, 99 do
code = code..format('if a%d == nil then return t end; t[a%d] = true;', i, i)
end
elseif type == VALUES then
code = code..'setn(t, 99); if a1 == nil then return t end; t[1] = a1;'
for i = 2, 99 do
code = code..format('if a%d == nil then setn(t, %d); return t end; t[%d] = a%d;', i, i - 1, i, i)
end
elseif type == RETURN_VALUES then
code = code..'setn(t, 99);'
for i = 1, 99 do
code = code..format('t[%d] = a%d;', i, i)
end
elseif type == PAIRS then
for i = 1, 97, 2 do
code = code..format('if a%d == nil then return t end; t[a%d] = a%d;', i, i, i + 1)
end
end
code = code..'return overflow ~= nil and error("Overflow.") or t; end;'
local f = loadstring(code)
setfenv(f, M)
return f
end
local function insert_values(t,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15,v16,v17,v18,v19,v20,overflow)
local n = getn(t)
if v1 == nil then return t end; t[n + 1] = v1
if v2 == nil then setn(t, n + 1); return end; t[n + 2] = v2
if v3 == nil then setn(t, n + 2); return end; t[n + 3] = v3
if v4 == nil then setn(t, n + 3); return end; t[n + 4] = v4
if v5 == nil then setn(t, n + 4); return end; t[n + 5] = v5
if v6 == nil then setn(t, n + 5); return end; t[n + 6] = v6
if v7 == nil then setn(t, n + 6); return end; t[n + 7] = v7
if v8 == nil then setn(t, n + 7); return end; t[n + 8] = v8
if v9 == nil then setn(t, n + 8); return end; t[n + 9] = v9
if v10 == nil then setn(t, n + 9); return end; t[n + 10] = v10
if v11 == nil then setn(t, n + 10); return end; t[n + 11] = v11
if v12 == nil then setn(t, n + 11); return end; t[n + 12] = v12
if v13 == nil then setn(t, n + 12); return end; t[n + 13] = v13
if v14 == nil then setn(t, n + 13); return end; t[n + 14] = v14
if v15 == nil then setn(t, n + 14); return end; t[n + 15] = v15
if v16 == nil then setn(t, n + 15); return end; t[n + 16] = v16
if v17 == nil then setn(t, n + 16); return end; t[n + 17] = v17
if v18 == nil then setn(t, n + 17); return end; t[n + 18] = v18
if v19 == nil then setn(t, n + 18); return end; t[n + 19] = v19
if v20 == nil then setn(t, n + 19); return end; t[n + 20] = v20; setn(t, n + 20)
if overflow ~= nil then error('Overflow.') end
end
local function insert_pairs(t,k1,v1,k2,v2,k3,v3,k4,v4,k5,v5,k6,v6,k7,v7,k8,v8,k9,v9,k10,v10,overflow)
if k1 == nil then return end t[k1] = v1
if k2 == nil then return end t[k2] = v2
if k3 == nil then return end t[k3] = v3
if k4 == nil then return end t[k4] = v4
if k5 == nil then return end t[k5] = v5
if k6 == nil then return end t[k6] = v6
if k7 == nil then return end t[k7] = v7
if k8 == nil then return end t[k8] = v8
if k9 == nil then return end t[k9] = v9
if k10 == nil then return end t[k10] = v10
if overflow ~= nil then error('Overflow.') end
end
local function constructor_mt(insert)
return {
__call=function(self,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow)
local t, n = rawget(self, 't') or table(), rawget(self, 'n') or 1
insert(t,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15,a16,a17,a18,a19,a20,overflow)
if n > 1 then
self.t, self.n = t, n - 1
return self
end
recycle(self)
return t
end,
__index=function(self, key) self.n = key; return self end,
}
end
local set_mt, list_mt, table_mt = constructor_mt(insert_keys), constructor_mt(insert_values), constructor_mt(insert_pairs)
function public.set.get() return setmetatable(table(), set_mt) end
function public.list.get() return setmetatable(table(), list_mt) end
function public.T.get() return setmetatable(table(), table_mt) end
public.set.get = insert(KEYS)
public.list.get = insert(VALUES)
public.ret.get = insert(RETURN_VALUES)
public.T.get = insert(PAIRS)
end
+5 -3
View File
@@ -1,9 +1,11 @@
if module then return end
local type, setmetatable, setfenv, unpack, next, pcall, _G = type, setmetatable, setfenv, unpack, next, pcall, getfenv(0)
local type, getmetatable, setmetatable, setfenv, unpack, next, _G = type, getmetatable, setmetatable, setfenv, unpack, next, getfenv(0)
local PUBLIC, PRIVATE = 1, 2
local CALL, INDEX, NEWINDEX = 1, 2, 3
local function debug(...) for i = 1, arg.n do DEFAULT_CHAT_FRAME:AddMessage(RED_FONT_COLOR_CODE..i..') '..tostring(arg[i])) end end
local function error(msg, ...) return _G.error(format(msg or '', unpack(arg))..'\n'..debugstack(), 0) end
local function import_error() error('Import error.') end
local function declaration_error() error('Declaration error.') end
@@ -100,8 +102,8 @@ function module(name)
if name and _G[name] then return true end
local declarator, env, interface = setmetatable({}, declarator_mt), setmetatable({}, env_mt), setmetatable({}, interface_mt)
local self; self = {
access = {_=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE},
[CALL] = {import=function(...) import(self, unpack(arg)) end, error=error, nop=nop},
access = {debug=PRIVATE, _=PRIVATE, error=PRIVATE, nop=PRIVATE, _G=PRIVATE, M=PRIVATE, I=PRIVATE, public=PRIVATE, private=PRIVATE},
[CALL] = {debug=debug, import=function(...) import(self, unpack(arg)) end, error=error, nop=nop},
[INDEX] = {_G=const(_G), M=const(env), I=const(interface), public=function() return declarator.public end, private=function() return declarator.private end},
[NEWINDEX] = {_=nop},
declarator = declarator,
+2 -3
View File
@@ -409,7 +409,7 @@ function update_inventory_records()
availability[i] = 0
end
availability[charge_class] = item_info.count
auctionable_map[item_info.item_key] = T[2](
auctionable_map[item_info.item_key] = T(
'item_id', item_info.item_id,
'suffix_id', item_info.suffix_id,
'key', item_info.item_key,
@@ -419,8 +419,7 @@ function update_inventory_records()
'quality', item_info.quality,
'aux_quantity', item_info.charges or item_info.count,
'max_stack', item_info.max_stack,
'max_charges', item_info.max_charges
)(
'max_charges', item_info.max_charges,
'availability', availability
)
else
+1 -1
View File
@@ -317,7 +317,7 @@ function set_filter_display_offset(x_offset, y_offset)
end
function initialize_filter_dropdown()
for filter in temp-set[2]('and', 'or', 'not', 'min-unit-bid', 'min-unit-buy', 'max-unit-bid', 'max-unit-buy', 'bid-profit', 'buy-profit', 'bid-vend-profit', 'buy-vend-profit', 'bid-dis-profit', 'buy-dis-profit', 'bid-pct', 'buy-pct', 'item', 'tooltip', 'min-lvl', 'max-lvl', 'rarity')('left', 'utilizable', 'discard') do
for filter in temp-set('and', 'or', 'not', 'min-unit-bid', 'min-unit-buy', 'max-unit-bid', 'max-unit-buy', 'bid-profit', 'buy-profit', 'bid-vend-profit', 'buy-vend-profit', 'bid-dis-profit', 'buy-dis-profit', 'bid-pct', 'buy-pct', 'item', 'tooltip', 'min-lvl', 'max-lvl', 'rarity', 'left', 'utilizable', 'discard') do
UIDropDownMenu_AddButton(T(
'text', filter,
'value', filter,