Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- queueman = {}
- --
- function queueman.ctor()
- --
- local self = {}
- local queue = { first = 0, last = -1 }
- function self.pushleft( value )
- local first = queue.first - 1
- queue.first = first
- queue[first] = value
- end
- function self.pushright( value )
- local last = queue.last + 1
- queue.last = last
- queue[last] = value
- end
- function self.popleft()
- local first = queue.first
- if first > queue.last then return nil end
- local value = queue[first]
- queue[first] = nil -- to allow garbage collection
- queue.first = first + 1
- return value
- end
- function self.popright()
- local last = queue.last
- if queue.first > last then return nil end
- local value = queue[last]
- queue[last] = nil -- to allow garbage collection
- queue.last = last - 1
- return value
- end
- function self.peekleft()
- local first = queue.first
- if first <= queue.last then return queue[first] end
- return nil
- end
- function self.peekright()
- local last = queue.last
- if queue.first <= last then return queue[last] end
- return nil
- end
- function self.skipleft()
- local first = queue.first
- if first <= queue.last then
- queue[first] = nil -- to allow garbage collection
- queue.first = first + 1
- end
- end
- function self.skipright()
- local last = queue.last
- if queue.first <= last then
- queue[last] = nil -- to allow garbage collection
- queue.last = last - 1
- end
- end
- function self.clear()
- queue = { first = 0, last = -1 }
- end
- function self.getfirst()
- return queue.first
- end
- function self.getlast()
- return queue.last
- end
- function self.list()
- local wrap, yield = coroutine.wrap, coroutine.yield
- local i, k = 1, queue.first
- local function iterator( i, k )
- while k <= queue.last
- do
- yield( i, queue[k] )
- i = i + 1
- k = k + 1
- end
- end
- return wrap( function() iterator( i, k ) end )
- end
- return self
- end
- --
- setmetatable( queueman, { __call = function( _, ... ) return queueman.ctor( ... ) end } )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement