-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.lua
64 lines (54 loc) · 1.11 KB
/
list.lua
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
List = {}
function List:new ()
local l = {first = 0, last = -1 }
setmetatable(l, self)
self.__index = self
return l
end
function List:pushleft (value)
local first = self.first - 1
self.first = first
self[first] = value
return self:len()
end
function List:push (value)
return self:pushright(value)
end
function List:pushright (value)
local last = self.last + 1
self.last = last
self[last] = value
return self:len()
end
function List:pop (value)
return self:popleft(value)
end
function List:popleft ()
local first = self.first
if first > self.last then return nil end
local value = self[first]
self[first] = nil -- to allow garbage collection
self.first = first + 1
return value
end
function List:popright ()
local last = self.last
if self.first > last then return nil end
local value = self[last]
self[last] = nil -- to allow garbage collection
self.last = last - 1
return value
end
function List:all ()
local res = {}
local k = 1
for i = self.first, self.last do
res[k] = self[i]
k = k + 1
end
return res
end
function List:len ()
return self.last - self.first + 1
end
return List