-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPriorityQueue.lua
208 lines (113 loc) · 2.96 KB
/
PriorityQueue.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
module("shadows.PriorityQueue", package.seeall)
Object = require("shadows.Object")
PriorityQueue = setmetatable( {}, Object )
PriorityQueue.__index = PriorityQueue
PriorityQueue.__type = "PriorityQueue"
function PriorityQueue:new()
local self = setmetatable( {}, PriorityQueue )
self.Array = {}
self.Size = 0
return self
end
function PriorityQueue:__tostring()
local Concat = ""
for i = 1, self.Size do
Concat = Concat .. tostring(self.Array[i]) .. ","
end
return "{" .. Concat:sub(1, -2) .. "}"
end
function PriorityQueue:Insert(Value)
-- Binary search the insertion position
local Left = 1
local Right = self.Size
local Middle = 1
while Left <= Right do
Middle = math.floor( ( Left + Right ) / 2 )
local ArrayValue = self.Array[Middle]
if ArrayValue == Value then
break
elseif ArrayValue < Value then
Left = Middle + 1
if Left > Right then
Middle = Middle + 1
break
end
else
Right = Middle - 1
if Left > Right then
break
end
end
end
for i = #self.Array, Middle, -1 do
self.Array[i + 1] = self.Array[i]
end
self.Array[Middle] = Value
self.Size = self.Size + 1
end
function PriorityQueue:Contains(Value)
local Left = 1
local Right = self.Size
local Middle = 1
while Left <= Right do
Middle = math.floor( ( Left + Right ) / 2 )
local ArrayValue = self.Array[Middle]
if ArrayValue == Value then
return Middle
elseif Value < ArrayValue then
Right = Middle - 1
elseif Value > ArrayValue then
Left = Middle + 1
else
-- Linear search
local LeftPointer = Middle - 1
local PointerValue = self.Array[LeftPointer]
while PointerValue and not ( PointerValue < Value ) do
if PointerValue == Value then
return LeftPointer
end
LeftPointer = LeftPointer - 1
PointerValue = self.Array[LeftPointer]
end
local RightPointer = Middle + 1
local PointerValue = self.Array[RightPointer]
while PointerValue and not ( PointerValue > Value ) do
if PointerValue == Value then
return RightPointer
end
RightPointer = RightPointer + 1
PointerValue = self.Array[RightPointer]
end
return false
end
end
if self.Array[Middle] == Value then
return Middle
end
return false
end
function PriorityQueue:Get(Index)
return self.Array[Index]
end
function PriorityQueue:GetLength()
return self.Size
end
function PriorityQueue:Remove(Element)
local Index = self:Contains(Element)
if Index then
self:RemoveAt(Index)
end
end
function PriorityQueue:RemoveAt(Index)
if self.Array[Index] then
for i = Index, self.Size do
self.Array[i] = self.Array[i + 1]
end
self.Array[self.Size] = nil
self.Size = self.Size - 1
end
end
function PriorityQueue:GetArray()
return self.Array
end
return PriorityQueue