forked from Sidoine/Ovale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.lua
129 lines (109 loc) · 2.87 KB
/
Queue.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
--[[--------------------------------------------------------------------
Copyright (C) 2013, 2014 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
-- Double-ended queue.
local OVALE, Ovale = ...
local OvaleQueue = {}
Ovale.OvaleQueue = OvaleQueue
--<private-static-properties>
local setmetatable = setmetatable
--</private-static-properties>
--<public-static-properties>
OvaleQueue.name = "OvaleQueue"
OvaleQueue.first = 1
OvaleQueue.last = 0
OvaleQueue.__index = OvaleQueue
--</public-static-properties>
--<private-static-methods>
local function BackToFrontIterator(invariant, control)
control = control - 1
local element = invariant[control]
if element then
return control, element
end
end
local function FrontToBackIterator(invariant, control)
control = control + 1
local element = invariant[control]
if element then
return control, element
end
end
--</private-static-methods>
--<public-static-methods>
function OvaleQueue:NewDeque(name)
return setmetatable({ name = name, first = 0, last = -1 }, OvaleQueue)
end
function OvaleQueue:InsertFront(element)
local first = self.first - 1
self.first = first
self[first] = element
end
function OvaleQueue:InsertBack(element)
local last = self.last + 1
self.last = last
self[last] = element
end
function OvaleQueue:RemoveFront()
local first = self.first
local element = self[first]
if element then
self[first] = nil
self.first = first + 1
end
return element
end
function OvaleQueue:RemoveBack()
local last = self.last
local element = self[last]
if element then
self[last] = nil
self.last = last - 1
end
return element
end
function OvaleQueue:At(index)
if index > self:Size() then
return
end
return self[self.first + index - 1]
end
function OvaleQueue:Front()
return self[self.first]
end
function OvaleQueue:Back()
return self[self.last]
end
function OvaleQueue:BackToFrontIterator()
return BackToFrontIterator, self, self.last + 1
end
function OvaleQueue:FrontToBackIterator()
return FrontToBackIterator, self, self.first - 1
end
function OvaleQueue:Reset()
for i in self:BackToFrontIterator() do
self[i] = nil
end
self.first = 0
self.last = -1
end
function OvaleQueue:Size()
return self.last - self.first + 1
end
function OvaleQueue:DebuggingInfo()
Ovale:Print("Queue %s has %d item(s), first=%d, last=%d.", self.name, self:Size(), self.first, self.last)
end
--</public-static-methods>
--<public-static-properties>
-- Queue (FIFO) methods
OvaleQueue.NewQueue = OvaleQueue.NewDeque
OvaleQueue.Insert = OvaleQueue.InsertBack
OvaleQueue.Remove = OvaleQueue.RemoveFront
OvaleQueue.Iterator = OvaleQueue.FrontToBackIterator
-- Stack (LIFO) methods
OvaleQueue.NewStack = OvaleQueue.NewDeque
OvaleQueue.Push = OvaleQueue.InsertBack
OvaleQueue.Pop = OvaleQueue.RemoveBack
OvaleQueue.Top = OvaleQueue.Back
--</public-static-properties>