-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_optimizer.pony
61 lines (53 loc) · 1.72 KB
/
path_optimizer.pony
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
use "collections"
interface val PathCommandOptimizer
fun apply(path_commands: Array[PathCommand] box): Array[PathCommand]
primitive OptimizeConsecutiveMoves
fun apply(path_commands: Array[PathCommand] box): Array[PathCommand] =>
let pcs = Array[PathCommand]
for i in Range(0, path_commands.size() - 1) do
try
match (path_commands(i)?, path_commands(i + 1)?)
| (let pm1: PathMove, let pm2: PathMove) =>
None
else
try
pcs.push(path_commands(i)?)
end
end
end
end
try
pcs.push(path_commands(path_commands.size() - 1)?)
end
pcs
primitive OptimizeConnectCommands
fun apply(path_commands: Array[PathCommand] box): Array[PathCommand] =>
let seen = Array[Bool].init(false, path_commands.size())
let pcs = Array[PathCommand]
try
for i in Range(0, path_commands.size()) do
if seen(i)? == false then
let cur_command = path_commands(i)?
var at = cur_command.at()
pcs.push(cur_command)
for j in Range(i + 1, path_commands.size()) do
if seen(j)? == false then
let candidate_command = path_commands(j)?
match candidate_command
| (let pm: PathMove) if pm.test_at(at) =>
pcs.push(path_commands(j + 1)?)
at = path_commands(j + 1)?.at()
seen(j + 1)? = true
seen(j)? = true
| (let pc: PathCommand) if pc.test_at(at) =>
pcs.push(path_commands(j)?)
at = path_commands(j)?.at()
seen(j)? = true
end
end
end
seen(i)? = true
end
end
end
pcs