Thread Tools Display Modes
08-25-10, 08:39 AM   #21
sacrife
An Onyxian Warder
 
sacrife's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 384
Both c1 and c2 returns 'nil'.
__________________

  Reply With Quote
08-25-10, 09:47 AM   #22
Luzzifus
A Warpwood Thunder Caller
 
Luzzifus's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 94
Originally Posted by sacrife View Post
I can't see the difference between this code and the one above from SDPhantom. What did you change?
I replaced the "<=" with "<" in line 8.

Originally Posted by sacrife View Post
And also, how would I go about reversing the quality sort? Now its green > blue > epic. I want to see how it looks the other way.
Oh that has already been answered. ^^
  Reply With Quote
08-25-10, 09:55 AM   #23
Saiket
A Chromatic Dragonspawn
 
Saiket's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 154
Originally Posted by sacrife View Post
Both c1 and c2 returns 'nil'.
Oops, I think that would happen if it tried to sort empty bag slots. This version should just put them at the end instead of comparing the quality/IDs of empty slots.
lua Code:
  1. local QuickSort;
  2. do
  3.     local func = function(v1, v2)
  4.         if v1[1] == 0 or v2[1] == 0 then -- Empty bag slot
  5.             return v1[1] > v2[1]; -- Put empty slots last
  6.         elseif v1[2] ~= v2[2] then
  7.             return v1[2] > v2[2]; -- Higher quality first
  8.         elseif v1[1] ~= v2[1] then
  9.             return v1[1] < v2[1];
  10.         else -- Compare stack counts
  11.             local _, c1 = GetContainerItemInfo(v1[3].bagID, v1[3].slotID);
  12.             local _, c2 = GetContainerItemInfo(v2[3].bagID, v2[3].slotID);
  13.             return c1 < c2;
  14.         end
  15.     end;
  16.     QuickSort = function(tbl) table.sort(tbl, func); end
  17. end
  Reply With Quote
08-25-10, 12:56 PM   #24
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Luzzifus View Post
I wrote that recursive sorting function because I didn't find a way to use table.sort with my data structure. I was also aware of the stack overflow errors it produced but I didn't find a way around it. So I hope it's ok if I use your code as a replacement in cargbags_Nivaya too?
Go ahead, I had written my own sorting function before that worked perfectly for the only exception that it was a Lua function instead of native C code, so it had the constraints of running through the script engine instead of directly in code.

It took a comparison function like table.sort() does without restriction, if a check returned true, the values remained, otherwise, it swapped them.

The switch back to table.sort() was ultimately to save CPU time. To handle the glitch with equal values, I would usually add a check against a unique number in each value. For items, this would be their location.

Originally Posted by Saiket View Post
lua Code:
  1. local QuickSort;
  2. do
  3.     local func = function(v1, v2)
  4.         if v1[1] == 0 or v2[1] == 0 then -- Empty bag slot
  5.             return v1[1] > v2[1]; -- Put empty slots last
  6.         elseif v1[2] ~= v2[2] then
  7.             return v1[2] > v2[2]; -- Higher quality first
  8.         elseif v1[1] ~= v2[1] then
  9.             return v1[1] < v2[1];
  10.         else -- Compare stack counts
  11.             local _, c1 = GetContainerItemInfo(v1[3].bagID, v1[3].slotID);
  12.             local _, c2 = GetContainerItemInfo(v2[3].bagID, v2[3].slotID);
  13.             return c1 < c2;
  14.         end
  15.     end;
  16.     QuickSort = function(tbl) table.sort(tbl, func); end
  17. end
It really makes me cringe seeing data function calls in a sort comparison function. I would rather have the info directly in the data structure. Then again, to have it make any sense would require an update in the whole codebase to accept the new structure. It barely makes sense now.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-26-10, 05:23 AM   #25
Luzzifus
A Warpwood Thunder Caller
 
Luzzifus's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 94
Originally Posted by SDPhantom View Post
It really makes me cringe seeing data function calls in a sort comparison function. I would rather have the info directly in the data structure. Then again, to have it make any sense would require an update in the whole codebase to accept the new structure. It barely makes sense now.
Adapting the data structure is pretty easy:
lua Code:
  1. local buttonIDs = {}
  2. for i, button in self:IterateButtons() do
  3.     local item = cargBags:GetItemInfo(button.bagID, button.slotID)
  4.     if item.link then
  5.         local _,_,tQ = GetItemInfo(item.link)
  6.         local _,cnt = GetContainerItemInfo(button.bagID, button.slotID)
  7.         buttonIDs[i] = { item.id, tQ, button, cnt }
  8.     else
  9.         buttonIDs[i] = { 0, -2, button, -1 }
  10.     end
  11. end
  12. QuickSort(buttonIDs) -- sorting call
And the corresponding sort function would be this:
lua Code:
  1. local QuickSort;
  2. do
  3.     local func = function(v1, v2)
  4.         if not v1 or not v2 then return (v1 and true or false) end
  5.         if v1[1] == 0 or v2[1] == 0 then
  6.             return v1[1] > v2[1] -- empty slots last
  7.         elseif v1[2] ~= v2[2] then
  8.             return v1[2] > v2[2] -- higher quality first
  9.         elseif v1[1] ~= v2[1] then
  10.             return v1[1] > v2[1] -- group identical item ids
  11.         else
  12.             return v1[4] > v2[4] -- full/larger stacks first
  13.         end
  14.     end;
  15.     QuickSort = function(tbl) table.sort(tbl, func) end
  16. end

Last edited by Luzzifus : 08-26-10 at 05:33 AM.
  Reply With Quote
08-26-10, 06:49 AM   #26
sacrife
An Onyxian Warder
 
sacrife's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 384
Thanks a lot to all of you, really appreciate this, solved so many problems for me.
__________________

  Reply With Quote
08-26-10, 10:54 PM   #27
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Luzzifus View Post
Adapting the data structure is pretty easy:
Again, having it make sense, then again, my style is more for readability than being nit-picky on resources.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
08-27-10, 02:10 AM   #28
Luzzifus
A Warpwood Thunder Caller
 
Luzzifus's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2007
Posts: 94
Originally Posted by SDPhantom View Post
Again, having it make sense, then again, my style is more for readability than being nit-picky on resources.
When it comes to compare functions which are being called about n times for each data entity in a sorting call (worst case scenario) it definititly is the better way to be nit-picky on ressources, especially cpu time. Readability is overrated.
  Reply With Quote
08-28-10, 05:33 PM   #29
sacrife
An Onyxian Warder
 
sacrife's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 384
I had to update it a bit to not do anything if it returned nil as opening clams, etc returned errors.

Code:
      local QuickSort;
      do
          local func = function(v1, v2)
		  if (v1 or v2 ~= nil) then
              if v1[1] == 0 or v2[1] == 0 then -- Empty bag slot
                  return v1[1] > v2[1]; -- Put empty slots last
              elseif v1[2] ~= v2[2] then
                  return v1[2] > v2[2]; -- Higher quality first
              elseif v1[1] ~= v2[1] then
                  return v1[1] < v2[1];
              else -- Compare stack counts
                  local _, c1 = GetContainerItemInfo(v1[3].bagID, v1[3].slotID);
                  local _, c2 = GetContainerItemInfo(v2[3].bagID, v2[3].slotID);
                  return c1 < c2;
              end
		  end
          end;
          QuickSort = function(tbl) table.sort(tbl, func); end
      end
__________________

  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Itemsort stack overflow.

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off