2
local push_right = function(self, x)
4
self.tail = self.tail + 1
8
local push_left = function(self, x)
11
self.head = self.head - 1
14
local peek_right = function(self)
15
return self[self.tail]
18
local peek_left = function(self)
19
return self[self.head+1]
22
local pop_right = function(self)
23
if self:is_empty() then return nil end
24
local r = self[self.tail]
26
self.tail = self.tail - 1
30
local pop_left = function(self)
31
if self:is_empty() then return nil end
32
local r = self[self.head+1]
33
self.head = self.head + 1
34
local r = self[self.head]
39
local rotate_right = function(self, n)
41
if self:is_empty() then return nil end
42
for i=1,n do self:push_left(self:pop_right()) end
45
local rotate_left = function(self, n)
47
if self:is_empty() then return nil end
48
for i=1,n do self:push_right(self:pop_left()) end
51
local _remove_at_internal = function(self, idx)
52
for i=idx, self.tail do self[i] = self[i+1] end
53
self.tail = self.tail - 1
56
local remove_right = function(self, x)
57
for i=self.tail,self.head+1,-1 do
59
_remove_at_internal(self, i)
66
local remove_left = function(self, x)
67
for i=self.head+1,self.tail do
69
_remove_at_internal(self, i)
76
local length = function(self)
77
return self.tail - self.head
80
local is_empty = function(self)
81
return self:length() == 0
84
local contents = function(self)
86
for i=self.head+1,self.tail do
87
r[i-self.head] = self[i]
92
local iter_right = function(self)
95
if i > self.head+1 then
102
local iter_left = function(self)
105
if i < self.tail then
113
push_right = push_right,
114
push_left = push_left,
115
peek_right = peek_right,
116
peek_left = peek_left,
117
pop_right = pop_right,
119
rotate_right = rotate_right,
120
rotate_left = rotate_left,
121
remove_right = remove_right,
122
remove_left = remove_left,
123
iter_right = iter_right,
124
iter_left = iter_left,
130
local new = function()
131
local r = {head=0,tail=0}
132
return setmetatable(r, {__index=methods})