Module:User:Theknightwho/Deque

-- Turbo.lua Double ended queue implementation -- -- Copyright 2013 John Abrahamsen -- -- Licensed under the Apache License, Version 2.0 (the "License"); -- you may not use this file except in compliance with the License. -- You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an "AS IS" BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License.

local class = require("Module:User:Theknightwho/middleclass")

--- Double ended queue class. local Deque = class("Deque")

function Deque:initialize self.head = nil self.tail = nil self.sz = 0 end

--- Append elements to tail. function Deque:append(item) if not self.tail then self.tail = {next = nil, prev = nil, value = item} self.head = self.tail else local new_tail = {next = nil, prev = self.tail, value = item} self.tail.next = new_tail self.tail = new_tail end self.sz = self.sz + 1 end

--- Append element to head. function Deque:appendleft(item) if not self.head then self.head = {next = nil, prev = nil, value = item} self.tail = self.head else local new_head = {next = self.head, prev = nil, value = item} self.head.prev = new_head self.head = new_head end self.sz = self.sz + 1 end

--- Removes element at tail and returns it. function Deque:pop if not self.tail then return nil end local value = self.tail.value local new_tail = self.tail.prev if not new_tail then self.tail = nil self.head = nil else self.tail = new_tail self.tail.next = nil end self.sz = self.sz - 1 return value end

--- Removes element at head and returns it. function Deque:popleft if not self.head then return nil end local value = self.head.value local new_head = self.head.next if not new_head then self.head = nil self.tail = nil else new_head.prev = nil self.head = new_head end self.sz = self.sz - 1 return value end

function Deque:size return self.sz end

--- Find length of all elements in Deque combined. Slow. function Deque:strlen local l = self.head if not l then return 0 else local sz = 0 while l do			sz = l.value:len + sz			l = l.next end return sz	end end

--- Concat all elements in Deque. Slow. function Deque:concat local l = self.head if not l then return "" else local ret = {} while l do			table.insert(ret, l.value) l = l.next end return mw.dumpObject(ret) end end

function Deque:getn(pos) local l = self.head if not l then return nil end while pos ~= 0 do		l = l.next if not l then return nil end pos = pos - 1 end return l.value end

function Deque:__concat(source) if type(self) == "string" then self = self .. source:concat end return self end

--- To string metamethod. function Deque:__tostring return self:concat end --- Returns element at tail. function Deque:peeklast return self.tail.value end --- Returns element at head. function Deque:peekfirst return self.head.value end

function Deque:not_empty return self.head ~= nil end

return Deque