Skip to content

Commit a668dce

Browse files
committed
Change CommandCountAnalysis to support all kinds of commands, fixes #18
1 parent 25d52d8 commit a668dce

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/main/groovy/net/zomis/brainf/analyze/analyzers/CommandCountAnalysis.groovy

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@ import net.zomis.brainf.model.BrainfuckCommand
77
import net.zomis.brainf.model.BrainfuckRunner
88
import net.zomis.brainf.model.classic.BrainFCommand
99

10+
import java.util.function.BiFunction
11+
1012
class CommandCountAnalysis implements BrainfuckAnalyzer {
1113

1214
private int[] times
13-
private int[] actionsPerCommand
14-
private int[] codeCommands
15+
private final Map<BrainfuckCommand, Integer> actionsPerCommand = [:]
16+
private final Map<BrainfuckCommand, Integer> codeCommands = [:]
17+
18+
private static final BiFunction<Integer, Integer, Integer> MERGE_FUNCTION = {a, b -> a + b}
1519

1620
@Override
1721
void beforeStart(BrainfuckRunner runner) {
1822
this.times = new int[runner.code.commandCount];
19-
this.actionsPerCommand = new int[BrainFCommand.values().length];
20-
this.codeCommands = new int[BrainFCommand.values().length];
2123
}
2224

2325
@Override
@@ -31,52 +33,50 @@ class CommandCountAnalysis implements BrainfuckAnalyzer {
3133
}
3234

3335
@Override
34-
void beforePerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand cmd) {
36+
void beforePerform(MemoryCell cell, BrainfuckRunner runner, BrainfuckCommand command) {
3537
int codeIndex = runner.code.commandIndex
3638
this.times[codeIndex]++
37-
if (!(cmd instanceof BrainFCommand)) {
38-
return
39-
}
40-
def command = cmd as BrainFCommand
41-
actionsPerCommand[command.ordinal()]++
39+
actionsPerCommand.merge(command, 1, MERGE_FUNCTION)
4240
}
4341

4442
@Override
4543
void after(Brainalyze analyze, BrainfuckRunner runner) {
4644
int commandCount = runner.code.commandCount
4745
for (int i = 0; i < commandCount; i++) {
4846
BrainfuckCommand command = runner.code.getCommandAt(i)
49-
if (command instanceof BrainFCommand) {
50-
BrainFCommand cmd = command as BrainFCommand
51-
codeCommands[cmd.ordinal()]++
52-
}
47+
codeCommands.merge(command, 1, MERGE_FUNCTION)
5348
}
5449
}
5550

56-
static void printCommands(int[] ints) {
51+
static void printCommands(Map<BrainfuckCommand, Integer> ints) {
5752
int sum = 0
58-
BrainFCommand.values().each {
59-
int count = ints[it.ordinal()]
53+
ints.entrySet().stream().forEach({
54+
BrainfuckCommand command = it.key
55+
Integer count = it.value
6056
if (count > 0) {
61-
println "$it: $count"
57+
println "$command: $count"
6258
}
6359
if (it != BrainFCommand.NONE) {
6460
sum += count
6561
}
66-
}
62+
})
6763
println "Total: $sum"
6864
}
6965

7066
int getActionsForCommand(BrainFCommand command) {
71-
this.@actionsPerCommand[command.ordinal()]
67+
this.@actionsPerCommand[command]
7268
}
7369

7470
int[] getTimes() {
7571
return Arrays.copyOf(this.@times, this.@times.length)
7672
}
7773

78-
int[] getActionsPerCommand() {
79-
return Arrays.copyOf(this.@actionsPerCommand, this.@actionsPerCommand.length)
74+
Map<BrainfuckCommand, Integer> getActionsPerCommand() {
75+
return new HashMap<>(this.@actionsPerCommand)
76+
}
77+
78+
Map<BrainfuckCommand, Integer> getCodeCommands() {
79+
return new HashMap<>(this.@codeCommands)
8080
}
8181

8282
}

0 commit comments

Comments
 (0)