extracted sorting into a separate file. may later replace the unstable sort in the TSM table by a stable sort

This commit is contained in:
Manuel Simon Hirsig
2016-02-02 15:59:26 +01:00
parent f32e188cec
commit d7af47d1b2
11 changed files with 191 additions and 147 deletions
+1
View File
@@ -10,6 +10,7 @@ gui.lua
control.xml
persistence.lua
util.lua
sort.lua
money.lua
scan.lua
+16 -3
View File
@@ -325,7 +325,6 @@ local methods = {
-- sort by percent
return SortHelperFunc(a, b, 'percent')
end
-- sort arbitrarily, but make sure the sort is stable
return tostring(a) < tostring(b)
end
if self.sortInfo.descending then
@@ -400,7 +399,7 @@ local methods = {
row.cells[3]:SetText(numAuctionsText)
row.cells[4]:SetText(record.aux_quantity)
row.cells[5]:SetText(TIME_LEFT_STRINGS[record.duration or 0] or '---')
row.cells[6]:SetText(Aux.is_player(record.owner) and ('|cffffff00'..record.owner..'|r') or record.owner)
row.cells[6]:SetText(Aux.is_player(record.owner) and ('|cffffff00'..record.owner..'|r') or (record.owner or '---'))
local bid, buyout, colorBid, colorBuyout = self.GetRowPrices(record, aux_price_per_unit)
row.cells[7]:SetText(bid > 0 and Aux.money.to_string(bid, true, false, colorBid) or '---')
row.cells[8]:SetText(buyout > 0 and Aux.money.to_string(buyout, true, false, colorBuyout) or '---')
@@ -500,6 +499,14 @@ local methods = {
end
end,
RemoveAuctionRecord = function(rt, record)
local index = Aux.util.index_of(record, rt.records)
if index then
tremove(rt.records, index)
end
rt:SetDatabase()
end,
RemoveSelectedRecord = function(rt, count)
count = count or 1
for i=1, count do
@@ -519,12 +526,18 @@ local methods = {
rt:SetDatabase()
end,
ContainsRecord = function(rt, record)
if Aux.util.index_of(record, rt.records) then
return true
end
end,
SetSort = function(rt, sortIndex)
local sortIndexLookup
if aux_price_per_unit then
sortIndexLookup = {'name', 'level', 'numAuctions', 'aux_quantity', 'duration', 'owner', 'unit_bid_price', 'buyout_price', 'percent'}
else
sortIndexLookup = {'name', 'level', 'numAuctions', 'aux_quantity', 'duration', 'owner', 'bid_price', 'buyout', 'percent'}
sortIndexLookup = {'name', 'level', 'numAuctions', 'aux_quantity', 'duration', 'owner', 'bid_price', 'buyout_price', 'percent'}
end
if sortIndex then
if sortIndex == rt.sortInfo.index then return end
+3 -3
View File
@@ -50,7 +50,7 @@ function public.on_load()
{
title = 'Auction Item',
width = 280,
comparator = function(row1, row2) return Aux.util.compare(row1.tooltip[1][1].text, row2.tooltip[1][1].text, Aux.util.GT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.tooltip[1][1].text, row2.tooltip[1][1].text, Aux.sort.GT) end,
cell_initializer = function(cell)
local icon = CreateFrame('Button', nil, cell)
icon:EnableMouse(false)
@@ -91,7 +91,7 @@ function public.on_load()
{
title = 'Qty',
width = 25,
comparator = function(row1, row2) return Aux.util.compare(row1.stack_size, row2.stack_size, Aux.util.LT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.stack_size, row2.stack_size, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, datum)
cell.text:SetText(datum.aux_quantity)
@@ -101,7 +101,7 @@ function public.on_load()
{
title = 'Current Bid',
width = 80,
comparator = function(auction1, auction2) return Aux.util.compare(auction1.high_bid, auction2.high_bid, Aux.util.GT) end,
comparator = function(auction1, auction2) return Aux.sort.compare(auction1.high_bid, auction2.high_bid, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('CENTER'),
cell_setter = function(cell, auction)
cell.text:SetText(auction.high_bid and Aux.util.money_string(auction.high_bid) or RED_FONT_COLOR_CODE..'No Bids'..FONT_COLOR_CODE_CLOSE)
+3 -3
View File
@@ -54,7 +54,7 @@ function public.on_load()
{
title = 'Auction Item',
width = 280,
comparator = function(row1, row2) return Aux.util.compare(row1.tooltip[1][1].text, row2.tooltip[1][1].text, Aux.util.GT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.tooltip[1][1].text, row2.tooltip[1][1].text, Aux.sort.GT) end,
cell_initializer = function(cell)
local icon = CreateFrame('Button', nil, cell)
icon:EnableMouse(false)
@@ -95,7 +95,7 @@ function public.on_load()
{
title = 'Qty',
width = 25,
comparator = function(row1, row2) return Aux.util.compare(row1.stack_size, row2.stack_size, Aux.util.LT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.stack_size, row2.stack_size, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, datum)
cell.text:SetText(datum.aux_quantity)
@@ -104,7 +104,7 @@ function public.on_load()
{
title = 'Status',
width = 70,
comparator = function(auction1, auction2) return Aux.util.compare(auction1.status, auction2.status, Aux.util.GT) end,
comparator = function(auction1, auction2) return Aux.sort.compare(auction1.status, auction2.status, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('CENTER'),
cell_setter = function(cell, auction)
cell.text:SetText(auction.status)
+5 -5
View File
@@ -49,7 +49,7 @@ function public.on_load()
title = 'Item',
width = 163,
comparator = function(datum1, datum2)
return Aux.util.compare(datum1.time, datum2.time, Aux.util.GT)
return Aux.sort.compare(datum1.time, datum2.time, Aux.sort.GT)
end,
cell_initializer = function(cell)
local icon = CreateFrame('Button', nil, cell)
@@ -121,7 +121,7 @@ function public.on_load()
{
title = 'Qty',
width = 25,
comparator = function(group1, group2) return Aux.util.compare(group1[1].aux_quantity, group2[1].aux_quantity, Aux.util.LT) end,
comparator = function(group1, group2) return Aux.sort.compare(group1[1].aux_quantity, group2[1].aux_quantity, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, group)
cell.text:SetText(group[1].aux_quantity)
@@ -134,7 +134,7 @@ function public.on_load()
{
title = 'Lvl',
width = 25,
comparator = function(group1, group2) return Aux.util.compare(group1[1].level, group2[1].level, Aux.util.GT) end,
comparator = function(group1, group2) return Aux.sort.compare(group1[1].level, group2[1].level, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, group)
local level = max(1, group[1].level)
@@ -150,7 +150,7 @@ function public.on_load()
{
title = 'Status',
width = 70,
comparator = function(group1, group2) return Aux.util.compare(group1[1].status, group2[1].status, Aux.util.GT) end,
comparator = function(group1, group2) return Aux.sort.compare(group1[1].status, group2[1].status, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('CENTER'),
cell_setter = function(cell, group)
cell.text:SetText(group[1].status)
@@ -161,7 +161,7 @@ function public.on_load()
{
title = 'Avail',
width = 40,
comparator = function(group1, group2) return Aux.util.compare(getn(Aux.util.filter(group1, function(auction) return not auction.gone end)), getn(Aux.util.filter(group2, function(auction) return not auction.gone end)), Aux.util.LT) end,
comparator = function(group1, group2) return Aux.sort.compare(getn(Aux.util.filter(group1, function(auction) return not auction.gone end)), getn(Aux.util.filter(group2, function(auction) return not auction.gone end)), Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, group)
cell.text:SetText(getn(Aux.util.filter(group, function(auction) return not auction.gone end)))
+7 -7
View File
@@ -5,7 +5,7 @@ function m.money_column(title, getter)
return {
title = title,
width = 80,
comparator = function(datum1, datum2) return Aux.util.compare(getter(datum1), getter(datum2), Aux.util.GT) end,
comparator = function(datum1, datum2) return Aux.sort.compare(getter(datum1), getter(datum2), Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, datum)
cell.text:SetText(getter(datum) and Aux.util.money_string(getter(datum)) or 'N/A')
@@ -19,13 +19,13 @@ function m.owner_column(getter)
width = 90,
comparator = function(datum1, datum2)
if getter(datum1) == UnitName('player') and getter(datum2) == UnitName('player') then
return Aux.util.EQ
return Aux.sort.EQ
elseif getter(datum1) == UnitName('player') then
return Aux.util.LT
return Aux.sort.LT
elseif getter(datum2) == UnitName('player') then
return Aux.util.GT
return Aux.sort.GT
else
return Aux.util.compare(getter(datum1), getter(datum2), Aux.util.GT)
return Aux.sort.compare(getter(datum1), getter(datum2), Aux.sort.GT)
end
end,
cell_initializer = Aux.sheet.default_cell_initializer('LEFT'),
@@ -44,7 +44,7 @@ function m.percentage_market_column(item_key_getter, value_getter)
local market_price2 = Aux.history.market_value(item_key_getter(datum2))
local factor1 = value_getter(datum1) and market_price1 and market_price1 > 0 and value_getter(datum1) / market_price1
local factor2 = value_getter(datum2) and market_price2 and market_price2 > 0 and value_getter(datum2) / market_price2
return Aux.util.compare(factor1, factor2, Aux.util.GT)
return Aux.sort.compare(factor1, factor2, Aux.sort.GT)
end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, datum)
@@ -70,7 +70,7 @@ function m.duration_column(getter)
return {
title = 'Left',
width = 30,
comparator = function(datum1, datum2) return Aux.util.compare(getter(datum1), getter(datum2), Aux.util.GT) end,
comparator = function(datum1, datum2) return Aux.sort.compare(getter(datum1), getter(datum2), Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('CENTER'),
cell_setter = function(cell, datum)
local text
+7 -7
View File
@@ -54,7 +54,7 @@ function public.on_load()
{
title = 'Qty',
width = 25,
comparator = function(datum1, datum2) return Aux.util.compare(datum1.aux_quantity, datum2.aux_quantity, Aux.util.LT) end,
comparator = function(datum1, datum2) return Aux.sort.compare(datum1.aux_quantity, datum2.aux_quantity, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, datum)
cell.text:SetText(datum.aux_quantity)
@@ -63,7 +63,7 @@ function public.on_load()
{
title = 'Item',
width = 186,
comparator = function(row1, row2) return Aux.util.compare(row1.name, row2.name, Aux.util.GT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.name, row2.name, Aux.sort.GT) end,
cell_initializer = function(cell)
local icon = CreateFrame('Button', nil, cell)
icon:EnableMouse(false)
@@ -113,7 +113,7 @@ function public.on_load()
{
title = 'Avail',
width = 40,
comparator = function(row1, row2) return Aux.util.compare(row1.count, row2.count, Aux.util.LT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.count, row2.count, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, row)
cell.text:SetText(row.count)
@@ -122,7 +122,7 @@ function public.on_load()
{
title = 'Yours',
width = 40,
comparator = function(row1, row2) return Aux.util.compare(row1.yours, row2.yours, Aux.util.LT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.yours, row2.yours, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, row)
cell.text:SetText(row.yours)
@@ -131,7 +131,7 @@ function public.on_load()
{
title = 'Max Left',
width = 55,
comparator = function(row1, row2) return Aux.util.compare(row1.max_time_left, row2.max_time_left, Aux.util.GT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.max_time_left, row2.max_time_left, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('CENTER'),
cell_setter = function(cell, datum)
local text
@@ -150,7 +150,7 @@ function public.on_load()
{
title = 'Qty',
width = 25,
comparator = function(row1, row2) return Aux.util.compare(row1.stack_size, row2.stack_size, Aux.util.LT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.stack_size, row2.stack_size, Aux.sort.LT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, row)
cell.text:SetText(row.stack_size == private.get_stack_size_slider_value() and GREEN_FONT_COLOR_CODE..row.stack_size..FONT_COLOR_CODE_CLOSE or row.stack_size)
@@ -159,7 +159,7 @@ function public.on_load()
{
title = 'Buy/ea',
width = 80,
comparator = function(row1, row2) return Aux.util.compare(row1.unit_buyout_price, row2.unit_buyout_price, Aux.util.GT) end,
comparator = function(row1, row2) return Aux.sort.compare(row1.unit_buyout_price, row2.unit_buyout_price, Aux.sort.GT) end,
cell_initializer = Aux.sheet.default_cell_initializer('RIGHT'),
cell_setter = function(cell, row)
cell.text:SetText(Aux.util.money_string(row.unit_buyout_price))
+60 -45
View File
@@ -633,7 +633,7 @@ end
private.results_listing:SetSort(9)
private.results_listing:Clear()
private.results_listing:SetHandler('OnCellAltClick', function(cell, button)
private.find_auction(cell.row.data.record, true, button == 'LeftButton')
private.find_auction_and_bid(cell.row.data.record, button == 'LeftButton')
end)
private.results_listing:SetHandler('OnSelectionChanged', function(rt, datum)
if not datum then return end
@@ -707,6 +707,7 @@ end
}
private.recent_searches_listing = Aux.listing.CreateScrollingTable(AuxFilterSearchFrameSavedRecent)
private.recent_searches_listing:DisableSelection(true)
private.recent_searches_listing:SetColInfo({{name='Recent Searches', width=1}})
private.recent_searches_listing:SetHandler('OnClick', handlers.OnClick)
private.recent_searches_listing:SetHandler('OnEnter', handlers.OnEnter)
@@ -715,6 +716,7 @@ end
Aux.gui.vertical_line(AuxFilterSearchFrameSaved, 379)
private.favorite_searches_listing = Aux.listing.CreateScrollingTable(AuxFilterSearchFrameSavedFavorite)
private.favorite_searches_listing:DisableSelection(true)
private.favorite_searches_listing:SetColInfo({{name='Favorite Searches', width=1}})
private.favorite_searches_listing:SetHandler('OnClick', handlers.OnClick)
private.favorite_searches_listing:SetHandler('OnEnter', handlers.OnEnter)
@@ -801,62 +803,75 @@ function public.start_search()
end)
end
function private.test(record)
return function(index)
return Aux.info.auction(index).search_signature == record.search_signature
end
end
function private.record_remover(record)
return function()
private.results_listing:RemoveAuctionRecord(record)
end
end
function private.find_auction_and_bid(record, buyout_mode)
if not private.results_listing:ContainsRecord(record) or (buyout_mode and not record.buyout_price) or (not buyout_mode and record.high_bidder) or Aux.is_player(record.owner) then
return
end
Aux.scan_util.find(private.test(record), record.query, record.page, private.status_bar, private.record_remover(record), function(index)
if private.results_listing:ContainsRecord(record) then
Aux.place_bid('list', index, buyout_mode and record.buyout_price or record.bid_price, private.record_remover(record))
end
end)
end
do
local found_index
function private.find_auction(entry, express_mode, buyout_mode)
if entry.gone or (buyout_mode and not entry.buyout_price) or (express_mode and not buyout_mode and entry.high_bidder) or entry.owner == UnitName('player') then
function private.find_auction(record)
if not private.results_listing:ContainsRecord(record) or Aux.is_player(record.owner) then
return
end
local function test(index)
return Aux.info.auction(index).search_signature == entry.search_signature
end
found_index = nil
local function remove_entry()
private.results_listing:RemoveSelectedRecord()
entry.gone = true
end
Aux.scan_util.find(private.test(record), record.query, record.page, private.status_bar, private.record_remover(record), function(index)
if express_mode then
Aux.scan_util.find(test, entry.query, entry.page, private.status_bar, remove_entry, function(index)
if not entry.gone then
Aux.place_bid('list', index, buyout_mode and entry.buyout_price or entry.bid_price, remove_entry)
end
end)
else
found_index = nil
found_index = index
Aux.scan_util.find(test, entry.query, entry.page, private.status_bar, remove_entry, function(index)
if not record.high_bidder then
private.bid_button:SetScript('OnClick', function()
if private.test(record)(index) and private.results_listing:ContainsRecord(record) then
Aux.place_bid('list', index, record.bid_price, private.record_remover(record))
end
end)
private.bid_button:Enable()
end
found_index = index
if not entry.high_bidder then
private.bid_button:SetScript('OnClick', function()
if test(index) and not entry.gone then
Aux.place_bid('list', index, entry.bid_price, remove_entry)
end
end)
private.bid_button:Enable()
end
if entry.buyout_price > 0 then
RESULTS.buyout_button:SetScript('OnClick', function()
if test(index) and not entry.gone then
Aux.place_bid('list', index, entry.buyout_price, remove_entry)
end
end)
RESULTS.buyout_button:Enable()
end
end)
end
if record.buyout_price > 0 then
RESULTS.buyout_button:SetScript('OnClick', function()
if private.test(record)(index) and private.results_listing:ContainsRecord(record) then
Aux.place_bid('list', index, record.buyout_price, private.record_remover(record))
end
end)
RESULTS.buyout_button:Enable()
end
end)
end
function public.on_update()
-- if not (RESULTS.buyout_button:IsEnabled() or private.bid_button:IsEnabled()) then
-- return
-- end
if not (RESULTS.buyout_button:IsEnabled() or private.bid_button:IsEnabled()) then
return
end
if not found_index then
RESULTS.buyout_button:Disable()
private.bid_button:Disable()
return
end
local selection = private.results_listing:GetSelection()
if not selection then
@@ -865,7 +880,7 @@ do
return
end
if found_index and selection.record.search_signature ~= Aux.info.auction(found_index).search_signature then
if found_index and (not Aux.info.auction(found_index) or selection.record.search_signature ~= Aux.info.auction(found_index).search_signature) then
RESULTS.buyout_button:Disable()
private.bid_button:Disable()
private.find_auction(selection.record)
+5 -5
View File
@@ -271,12 +271,12 @@ function private.row_comparator(sheet)
local column = sheet.columns[sort_info.column]
if column.comparator then
local ordering = column.comparator(row1, row2)
if ordering ~= Aux.util.EQ then
return sort_info.order == 'ascending' and ordering or Aux.util.invert_order(ordering)
if ordering ~= Aux.sort.EQ then
return sort_info.order == 'ascending' and ordering or Aux.sort.invert_order(ordering)
end
end
end
return Aux.util.EQ
return Aux.sort.EQ
end
end
@@ -295,7 +295,7 @@ function public.populate(sheet, data)
sheet.data = data
-- sheet.selected = {}
Aux.util.merge_sort(sheet.data, private.row_comparator(sheet))
Aux.sort.merge_sort(sheet.data, private.row_comparator(sheet))
public.render(sheet)
end
@@ -309,7 +309,7 @@ function Aux.sheet.sort(sheet, column_index)
tinsert(sheet.sort_order, 1, {column=column_index, order = 'ascending'})
end
Aux.util.merge_sort(sheet.data, private.row_comparator(sheet))
Aux.sort.merge_sort(sheet.data, private.row_comparator(sheet))
public.render(sheet)
end
+84
View File
@@ -0,0 +1,84 @@
local private, public = {}, {}
Aux.sort = public
public.LT = {}
public.EQ = {}
public.GT = {}
-- stable sorting
function public.merge_sort(A, comp)
local n = getn(A)
local B = {}
local width = 1
while width <= n do
for i=1, n, 2 * width do
private.merge(A, i, min(i + width, n), min(i + 2 * width - 1, n), B, comp)
end
private.copy_array(B, A, n)
width = 2 * width
end
end
function private.merge(A, start1, start2, last, B, comp)
local i1 = start1
local i2 = start2
for i=start1,last do
if i1 < start2 and (i2 > last or comp(A[i1], A[i2]) == public.LT or comp(A[i1], A[i2]) == public.EQ) then
B[i] = A[i1]
i1 = i1 + 1
else
B[i] = A[i2]
i2 = i2 + 1
end
end
end
function private.copy_array(A, B, n)
for i=1,n do
B[i] = A[i]
end
end
function public.invert_order(ordering)
if ordering == public.LT then
return public.GT
elseif ordering == public.GT then
return public.LT
else
return public.EQ
end
end
function public.compare(a, b, nil_ordering)
nil_ordering = nil_ordering or public.EQ
if not a and b then
return nil_ordering
elseif a and not b then
return public.invert_order(nil_ordering)
elseif not a and not b then
return public.EQ
elseif a < b then
return public.LT
elseif a > b then
return public.GT
else
return public.EQ
end
end
function public.compare_from_lt(comp)
return function(a, b)
if comp(a, b) then
return public.LT
elseif comp(b, a) then
return public.GT
else
return public.EQ
end
end
end
-69
View File
@@ -1,10 +1,6 @@
local private, public = {}, {}
Aux.util = public
Aux.util.LT = {}
Aux.util.EQ = {}
Aux.util.GT = {}
local merge, copy_array
function Aux.util.pass()
@@ -179,71 +175,6 @@ function Aux.util.take(n, xs)
return ys
end
function Aux.util.merge_sort(A, comp)
local n = getn(A)
local B = {}
local width = 1
while width <= n do
for i=1, n, 2 * width do
merge(A, i, min(i + width, n), min(i + 2 * width - 1, n), B, comp)
end
copy_array(B, A, n)
width = 2 * width
end
end
function merge(A, start1, start2, last, B, comp)
local i1 = start1
local i2 = start2
for i=start1,last do
if i1 < start2 and (i2 > last or comp(A[i1], A[i2]) == Aux.util.LT or comp(A[i1], A[i2]) == Aux.util.EQ) then
B[i] = A[i1]
i1 = i1 + 1
else
B[i] = A[i2]
i2 = i2 + 1
end
end
end
function copy_array(A, B, n)
for i=1,n do
B[i] = A[i]
end
end
function Aux.util.invert_order(ordering)
if ordering == Aux.util.LT then
return Aux.util.GT
elseif ordering == Aux.util.GT then
return Aux.util.LT
else
return Aux.util.EQ
end
end
function Aux.util.compare(a, b, nil_ordering)
nil_ordering = nil_ordering or Aux.util.EQ
if not a and b then
return nil_ordering
elseif a and not b then
return Aux.util.invert_order(nil_ordering)
elseif not a and not b then
return Aux.util.EQ
elseif a < b then
return Aux.util.LT
elseif a > b then
return Aux.util.GT
else
return Aux.util.EQ
end
end
function Aux.util.index_of(value, array)
for i, item in ipairs(array) do
if item == value then