1- local M = {}
2-
3- local Deque = {}
4- Deque .__index = Deque
1+ local Dequeue = {}
2+ Dequeue .__index = Dequeue
53
64--- @class Deque
75--- A double ended queue
86---
97--- @return Deque
10- function Deque .new ()
8+ function Dequeue .new ()
119 -- the indexes are created with an offset so that the indices are consequtive
1210 -- otherwise, when both pushleft and pushright are used, the indices will have a 1 length hole in the middle
13- return setmetatable ({ first = 0 , last = - 1 }, Deque )
11+ return setmetatable ({ first = 0 , last = - 1 }, Dequeue )
1412end
1513
1614--- push to the left of the deque
1715--- @param value any
18- function Deque :pushleft (value )
16+ function Dequeue :pushleft (value )
1917 local first = self .first - 1
2018 self .first = first
2119 self [first ] = value
2220end
2321
2422--- push to the right of the deque
2523--- @param value any
26- function Deque :pushright (value )
24+ function Dequeue :pushright (value )
2725 local last = self .last + 1
2826 self .last = last
2927 self [last ] = value
3028end
3129
3230--- pop from the left of the deque
3331--- @return any
34- function Deque :popleft ()
32+ function Dequeue :popleft ()
3533 local first = self .first
3634 if first > self .last then
3735 return nil
4442
4543--- pops from the right of the deque
4644--- @return any
47- function Deque :popright ()
45+ function Dequeue :popright ()
4846 local last = self .last
4947 if self .first > last then
5048 return nil
5755
5856--- checks if the deque is empty
5957--- @return boolean
60- function Deque :is_empty ()
58+ function Dequeue :is_empty ()
6159 return self :len () == 0
6260end
6361
6462--- returns the number of elements of the deque
6563--- @return number
66- function Deque :len ()
64+ function Dequeue :len ()
6765 return self .last - self .first + 1
6866end
6967
7068--- returns and iterator of the indices and values starting from the left
7169--- @return function
72- function Deque :ipairs_left ()
70+ function Dequeue :ipairs_left ()
7371 local i = self .first
7472
7573 return function ()
8684
8785--- returns and iterator of the indices and values starting from the right
8886--- @return function
89- function Deque :ipairs_right ()
87+ function Dequeue :ipairs_right ()
9088 local i = self .last
9189
9290 return function ()
@@ -103,14 +101,12 @@ end
103101
104102--- removes all values from the deque
105103--- @return nil
106- function Deque :clear ()
104+ function Dequeue :clear ()
107105 for i , _ in self :ipairs_left () do
108106 self [i ] = nil
109107 end
110108 self .first = 0
111109 self .last = - 1
112110end
113111
114- M .Deque = Deque
115-
116- return M
112+ return Dequeue
0 commit comments