From f3e23ea64260d436689d08fd92e947edac171aa7 Mon Sep 17 00:00:00 2001 From: Manuel Simon Hirsig Date: Mon, 29 Aug 2016 16:37:28 +0200 Subject: [PATCH] refactoring --- aux-addon.lua | 97 ++++++++++++++++++++++++++--------------- tabs/search/frame.lua | 8 ++-- tabs/search/results.lua | 2 +- util/core.lua | 5 +-- util/persistence.lua | 5 +-- 5 files changed, 70 insertions(+), 47 deletions(-) diff --git a/aux-addon.lua b/aux-addon.lua index cf965f5..25592cf 100644 --- a/aux-addon.lua +++ b/aux-addon.lua @@ -5,7 +5,7 @@ module() public.version = '5.0.0' -function public.log(...) temp=arg +function public.log(...) local msg = '[aux]' for i = 1, arg.n do msg = msg..' '..tostring(arg[i]) end DEFAULT_CHAT_FRAME:AddMessage(LIGHTYELLOW_FONT_COLOR_CODE..msg) @@ -42,36 +42,62 @@ function public.current_owner_page.get() return current_owner_page end --end do - local pool, overflow_pool, tmp = {}, setmetatable({}, {__mode='v'}), {} + local next, getn, setn, setmetatable, rawget = next, getn, table.setn, setmetatable, rawget + local wipe, recycle + local pool, pool_size = {}, 0 + local overflow_pool = setmetatable({}, {__mode='k'}) + local tmp = {} - CreateFrame'Frame':SetScript('OnUpdate', function() + CreateFrame('Frame'):SetScript('OnUpdate', function() for t in tmp do recycle(t) end wipe(tmp) end) - function public.wipe(t) -- like with a cloth or something + function wipe(t) -- like with a cloth or something for k in t do t[k] = nil end t.reset = 1 t.reset = nil - table.setn(t, 0) + setn(t, 0) return setmetatable(t, nil) end + public.wipe = wipe - function public.recycle(t) + function recycle(t) wipe(t) - if getn(pool) < 50 then - tinsert(pool, t) + if pool_size < 50 then + pool_size = pool_size + 1 + pool[pool_size] = t else - tinsert(overflow_pool, t) + overflow_pool[t] = true end --- log(getn(pool), '-', getn(overflow_pool)) end + public.recycle = recycle function public.t.get() - return tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} + if pool_size > 0 then + pool_size = pool_size - 1 + return pool[pool_size + 1] + end + local t = next(overflow_pool) + if t then + overflow_pool[t] = nil + return t + end + return {} end function public.tt.get() - local t = tremove(pool) or tremove(overflow_pool, next(overflow_pool)) or {} + local t + if pool_size > 0 then + t = pool[pool_size] + pool_size = pool_size - 1 + else + t = next(overflow_pool) + if t then + overflow_pool[t] = nil + else + t = {} + end + end tmp[t] = true return t end @@ -126,25 +152,25 @@ do 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 table.setn(t, n + 1); return end; t[n + 2] = v2 - if v3 == nil then table.setn(t, n + 2); return end; t[n + 3] = v3 - if v4 == nil then table.setn(t, n + 3); return end; t[n + 4] = v4 - if v5 == nil then table.setn(t, n + 4); return end; t[n + 5] = v5 - if v6 == nil then table.setn(t, n + 5); return end; t[n + 6] = v6 - if v7 == nil then table.setn(t, n + 6); return end; t[n + 7] = v7 - if v8 == nil then table.setn(t, n + 7); return end; t[n + 8] = v8 - if v9 == nil then table.setn(t, n + 8); return end; t[n + 9] = v9 - if v10 == nil then table.setn(t, n + 9); return end; t[n + 10] = v10 - if v11 == nil then table.setn(t, n + 10); return end; t[n + 11] = v11 - if v12 == nil then table.setn(t, n + 11); return end; t[n + 12] = v12 - if v13 == nil then table.setn(t, n + 12); return end; t[n + 13] = v13 - if v14 == nil then table.setn(t, n + 13); return end; t[n + 14] = v14 - if v15 == nil then table.setn(t, n + 14); return end; t[n + 15] = v15 - if v16 == nil then table.setn(t, n + 15); return end; t[n + 16] = v16 - if v17 == nil then table.setn(t, n + 16); return end; t[n + 17] = v17 - if v18 == nil then table.setn(t, n + 17); return end; t[n + 18] = v18 - if v19 == nil then table.setn(t, n + 18); return end; t[n + 19] = v19 - if v20 == nil then table.setn(t, n + 19); return end; t[n + 20] = v20; table.setn(t, n + 20) + 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) @@ -169,17 +195,16 @@ do 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) - public() - function set.get() return setmetatable(tt, set_mt) end - function list.get() return setmetatable(tt, list_mt) end - function T.get() return setmetatable(tt, table_mt) end - private() + function public.set.get() return setmetatable(t, set_mt) end + function public.list.get() return setmetatable(t, list_mt) end + function public.T.get() return setmetatable(t, table_mt) end end local event_frame = CreateFrame('Frame') diff --git a/tabs/search/frame.lua b/tabs/search/frame.lua index 75b6c9d..8cce903 100644 --- a/tabs/search/frame.lua +++ b/tabs/search/frame.lua @@ -313,10 +313,10 @@ function create_frames() btn:SetScript('OnClick', function() local filters = filter_util.queries(search_box:GetText()) if filters then - tinsert(_G.aux_favorite_searches, 1, { - filter_string = search_box:GetText(), - prettified = join(map(filters, function(filter) return filter.prettified end), ';'), - }) + tinsert(_G.aux_favorite_searches, 1, T( + 'filter_string', search_box:GetText(), + 'prettified', join(map(filters, function(filter) return filter.prettified end), ';') + )) end update_search_listings() end) diff --git a/tabs/search/results.lua b/tabs/search/results.lua index 5c60030..19d8ee0 100644 --- a/tabs/search/results.lua +++ b/tabs/search/results.lua @@ -344,7 +344,7 @@ function public.execute(resume, real_time) else new_search(filter_string) end - new_recent_search(filter_string, join(map(queries, function(filter) return filter.prettified end), ';')) + new_recent_search(filter_string, join(map(copy(queries), function(filter) return filter.prettified end), ';')) else current_search.records = {} current_search.table:SetDatabase(current_search.records) diff --git a/util/core.lua b/util/core.lua index 1ce01e8..c0b342d 100644 --- a/util/core.lua +++ b/util/core.lua @@ -199,9 +199,8 @@ function public.filter(t, p) end function public.map(t, f) - local mapped = M.t - for k, v in t do mapped[k] = f(v, k) end - return mapped + for k, v in t do t[k] = f(v, k) end + return t end function public.trim(str) diff --git a/util/persistence.lua b/util/persistence.lua index cc76bd3..d2bdc9a 100644 --- a/util/persistence.lua +++ b/util/persistence.lua @@ -50,8 +50,7 @@ function public.read_list(schema, str) if str == '' then return t end local separator = schema[2] local element_type = schema[3] - local parts = split(str, separator) - return map(parts, function(part) + return map(split(str, separator), function(part) return read(element_type, part) end) end @@ -59,7 +58,7 @@ end function public.write_list(schema, list) local separator = schema[2] local element_type = schema[3] - local parts = map(list, function(element) + local parts = map(copy(list), function(element) return write(element_type, element) end) return join(parts, separator)