Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. local MultiQueue = {}
  2. do
  3.     local multi_queue = {}
  4.     multi_queue.__index = multi_queue
  5.  
  6.     function multi_queue:__tostring()
  7.         local str = "MultiQueue"
  8.         for i=1,#self.Tags do
  9.             str = str.."\n"..self.Tags[i].." = ["..self.Queues[ self.Tags[i] ].Count.."]{"..table.concat(self.Queues[ self.Tags[i] ].Items, ", ").."}"
  10.         end
  11.         return str
  12.     end
  13.  
  14.     function multi_queue:Insert(tag, obj)
  15.         local q = self.Queues[tag]
  16.         if not q then error("Invalid tag") end
  17.        
  18.         q.Count = q.Count + 1
  19.         q.Items[q.Count] = obj
  20.     end
  21.    
  22.     function multi_queue:Remove(tag, obj)
  23.         local q = self.Queues[tag]
  24.         if not q then error("Invalid tag") end
  25.         if not obj then return end
  26.        
  27.         local shift = 0
  28.         local i = 1
  29.         local c = q.Count
  30.         while i <= c do
  31.             q.Items[i] = q.Items[i + shift]
  32.             if q.Items[i] == obj then
  33.                 shift = shift + 1
  34.                 q.Count = q.Count - 1
  35.             else
  36.                 i = i + 1
  37.             end
  38.         end
  39.     end
  40.    
  41.     -- Terminates when f returns a truthy value
  42.     function multi_queue:Iterate(tag, f)
  43.         local q = self.Queues[tag]
  44.         if not q then error("Invalid tag") end
  45.        
  46.         local items = q.Items
  47.         for i=1,q.Count do
  48.             local res = f(items[i])
  49.             if res then return res end
  50.         end
  51.     end
  52.    
  53.     -- Terminates when f returns a truthy value
  54.     function multi_queue:IterateAll(f)
  55.         for i=1,#self.Tags do
  56.             local q = self.Queues[ self.Tags[i] ]      
  57.             local items = q.Items
  58.             for i=1,q.Count do
  59.                 local res = f(items[i])
  60.                 if res then return res end
  61.             end
  62.         end
  63.     end
  64.    
  65.     local new = function(tags)
  66.         local queues = {}
  67.        
  68.         for i=1,#tags do
  69.             queues[ tags[i] ] = {
  70.                 Count = 0,
  71.                 Items = {}
  72.             }
  73.         end
  74.        
  75.         local t = setmetatable({
  76.             Queues = queues,
  77.             Tags = tags
  78.         }, multi_queue)
  79.        
  80.         return t
  81.     end
  82.    
  83.     MultiQueue = setmetatable({
  84.         new = new
  85.     }, {
  86.         __call = function(_, ...) return new(...) end
  87.     })
  88. end
  89.  
  90. return MultiQueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement