Advertisement
Guest User

Lua Min-Heap, w/Actions

a guest
Jan 7th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.27 KB | None | 0 0
  1. heap = {
  2.     new = function()
  3.         return {
  4.             size = 0,
  5.             elements = {}
  6.     }
  7.     end,
  8.  
  9.     _exch = function(h, x, y)
  10.         assert(type(x) == "number")
  11.         assert(type(y) == "number")
  12.  
  13.         local temp = h.elements[x]
  14.         h.elements[x] = h.elements[y]
  15.         h.elements[y] = temp
  16.     end,
  17.  
  18.     _sink = function(h, parentIndex)
  19.         local N = h.size
  20.  
  21.         while (parentIndex*2) <= N do
  22.             local childIndex = parentIndex * 2
  23.            
  24.             if childIndex < N and lowerPriority(h.elements[childIndex],
  25.                                                                                                         h.elements[childIndex+1]) then
  26.                 childIndex = childIndex + 1
  27.             end
  28.  
  29.             if not lowerPriority(h.elements[parentIndex], h.elements[childIndex]) then
  30.                 break
  31.             else
  32.                 heap._exch(h, parentIndex, childIndex)
  33.                 parentIndex = childIndex
  34.             end
  35.         end
  36.     end,
  37.  
  38.     _swim = function(h, childIndex)
  39.         while childIndex > 1 and lowerPriority(h.elements[math.floor(childIndex/2)],
  40.                                                                                                             h.elements[childIndex]) do
  41.             local parentIndex = math.floor(childIndex / 2)
  42.             heap._exch(h, childIndex, parentIndex)
  43.             childIndex = parentIndex
  44.         end
  45.     end,
  46.  
  47.     insert = function(h, element)
  48.         table.insert(h.elements, element)
  49.         h.size = h.size + 1
  50.  
  51.         heap._swim(h, h.size)
  52.     end,
  53.  
  54.     extract = function(h)      
  55.         heap._exch(h, 1, h.size)
  56.         h.size = h.size - 1
  57.         local ret = table.remove(h.elements)
  58.        
  59.         heap._sink(h, 1)
  60.  
  61.         return ret
  62.     end,
  63.  
  64.     head = function(h)
  65.         local element = h.elements[1]
  66.         return element
  67.     end
  68. }
  69.  
  70. function action(name, balance, priority, command)
  71.   assert(type(name) == "string")
  72.   assert(type(balance) == "string")
  73.   assert(type(priority) == "number")
  74.   assert(type(command) == "function" or type(command) == "string")
  75.  
  76.     local actionCommand = function() end
  77.  
  78.   if type(command) == "string" then
  79.     actionCommand = function()
  80.       send(command)
  81.     end
  82.   else
  83.     actionCommand = command
  84.   end
  85.  
  86.   local action = {
  87.     name = name,
  88.     balance = balance,
  89.     priority = priority,
  90.     command = actionCommand
  91.     }
  92.  
  93.     setmetatable(action, {
  94.         __lt = function(a, b)
  95.             return a.priority < b.priority
  96.         end
  97.     })
  98.  
  99.     return action
  100. end
  101.  
  102. function lowerPriority(x, y)
  103.     assert(type(x) == "table")
  104.     assert(type(y) == "table")
  105.  
  106.     assert(type(x.priority) == "number")
  107.     assert(type(y.priority) == "number")
  108.  
  109.     return x.priority >= y.priority
  110. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement