blob: 564216a8c375360ca3adae16a0b7dc25aa62f85e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
local List = {}
List.__index = List
function List.new()
local t = {first = 0, last = -1}
setmetatable(t, List)
return t
end
function List:pushleft(value)
local first = self.first - 1
self.first = first
self[first] = value
end
function List:pushright(value)
local last = self.last + 1
self.last = last
self[last] = value
end
function List:popleft()
local first = self.first
if first > self.last then error("List is empty") end
local value = self[first]
self[first] = nil
self.first = first + 1
return value
end
function List:popright()
local last = self.last
if self.first > last then error("List is empty") end
local value = self[last]
self[last] = nil
self.last = last - 1
return value
end
function List:size()
return self.last - self.first + 1
end
function List:empty()
return self.last < self.first
end
return List
|