Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MultiQueue = {}
- do
- local multi_queue = {}
- multi_queue.__index = multi_queue
- function multi_queue:__tostring()
- local str = "MultiQueue"
- for i=1,#self.Tags do
- str = str.."\n"..self.Tags[i].." = ["..self.Queues[ self.Tags[i] ].Count.."]{"..table.concat(self.Queues[ self.Tags[i] ].Items, ", ").."}"
- end
- return str
- end
- function multi_queue:Insert(tag, obj)
- local q = self.Queues[tag]
- if not q then error("Invalid tag") end
- q.Count = q.Count + 1
- q.Items[q.Count] = obj
- end
- function multi_queue:Remove(tag, obj)
- local q = self.Queues[tag]
- if not q then error("Invalid tag") end
- if not obj then return end
- local shift = 0
- local i = 1
- local c = q.Count
- while i <= c do
- q.Items[i] = q.Items[i + shift]
- if q.Items[i] == obj then
- shift = shift + 1
- q.Count = q.Count - 1
- else
- i = i + 1
- end
- end
- end
- -- Terminates when f returns a truthy value
- function multi_queue:Iterate(tag, f)
- local q = self.Queues[tag]
- if not q then error("Invalid tag") end
- local items = q.Items
- for i=1,q.Count do
- local res = f(items[i])
- if res then return res end
- end
- end
- -- Terminates when f returns a truthy value
- function multi_queue:IterateAll(f)
- for i=1,#self.Tags do
- local q = self.Queues[ self.Tags[i] ]
- local items = q.Items
- for i=1,q.Count do
- local res = f(items[i])
- if res then return res end
- end
- end
- end
- local new = function(tags)
- local queues = {}
- for i=1,#tags do
- queues[ tags[i] ] = {
- Count = 0,
- Items = {}
- }
- end
- local t = setmetatable({
- Queues = queues,
- Tags = tags
- }, multi_queue)
- return t
- end
- MultiQueue = setmetatable({
- new = new
- }, {
- __call = function(_, ...) return new(...) end
- })
- end
- return MultiQueue
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement