WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Itemsort stack overflow. (https://www.wowinterface.com/forums/showthread.php?t=34594)

sacrife 08-25-10 08:39 AM

Both c1 and c2 returns 'nil'.

Luzzifus 08-25-10 09:47 AM

Quote:

Originally Posted by sacrife (Post 203774)
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.

Quote:

Originally Posted by sacrife (Post 203774)
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. ^^

Saiket 08-25-10 09:55 AM

Quote:

Originally Posted by sacrife (Post 203788)
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

SDPhantom 08-25-10 12:56 PM

Quote:

Originally Posted by Luzzifus (Post 203771)
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.

Quote:

Originally Posted by Saiket (Post 203795)
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.

Luzzifus 08-26-10 05:23 AM

Quote:

Originally Posted by SDPhantom (Post 203806)
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

sacrife 08-26-10 06:49 AM

Thanks a lot to all of you, really appreciate this, solved so many problems for me.

SDPhantom 08-26-10 10:54 PM

Quote:

Originally Posted by Luzzifus (Post 203885)
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.

Luzzifus 08-27-10 02:10 AM

Quote:

Originally Posted by SDPhantom (Post 203984)
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. :p

sacrife 08-28-10 05:33 PM

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



All times are GMT -6. The time now is 04:04 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI