From e0bd2fb01b5024ad2b89581e526305888bf18905 Mon Sep 17 00:00:00 2001 From: Joachim Date: Sun, 26 Aug 2018 16:19:51 +0200 Subject: [PATCH] Cleaned up counter classes. --- .../kotlin/be/nielandt/CrossSolverBase.kt | 4 +- .../kotlin/be/nielandt/CrossSolverUpgraded.kt | 4 +- .../be/nielandt/CrossSolverUpgradedSkip.kt | 2 + .../kotlin/be/nielandt/EdgeModelFactory.kt | 2 + .../be/nielandt/{ => counter}/Counter.kt | 25 ++---------- .../be/nielandt/counter/CounterBasic.kt | 38 +++++++++++++++++++ .../be/nielandt/{ => counter}/CounterSkip.kt | 2 +- 7 files changed, 53 insertions(+), 24 deletions(-) rename src/main/kotlin/be/nielandt/{ => counter}/Counter.kt (58%) create mode 100644 src/main/kotlin/be/nielandt/counter/CounterBasic.kt rename src/main/kotlin/be/nielandt/{ => counter}/CounterSkip.kt (99%) diff --git a/src/main/kotlin/be/nielandt/CrossSolverBase.kt b/src/main/kotlin/be/nielandt/CrossSolverBase.kt index c180608..50b151d 100644 --- a/src/main/kotlin/be/nielandt/CrossSolverBase.kt +++ b/src/main/kotlin/be/nielandt/CrossSolverBase.kt @@ -1,5 +1,7 @@ package be.nielandt +import be.nielandt.counter.CounterBasic + class CrossSolverBase : CrossSolver() { /** * Solve the minimal cross for all colors. @@ -10,7 +12,7 @@ class CrossSolverBase : CrossSolver() { for (moveCount in 1..8) { // build a counter of moveCount big println("allCrossMoveCount basic doing $moveCount") - val counter = Counter(moveCount, 18) + val counter = CounterBasic(moveCount) // count up, each state of the counter corresponds to a combination of moves do { diff --git a/src/main/kotlin/be/nielandt/CrossSolverUpgraded.kt b/src/main/kotlin/be/nielandt/CrossSolverUpgraded.kt index 6a5a738..ce6f69a 100644 --- a/src/main/kotlin/be/nielandt/CrossSolverUpgraded.kt +++ b/src/main/kotlin/be/nielandt/CrossSolverUpgraded.kt @@ -1,5 +1,7 @@ package be.nielandt +import be.nielandt.counter.CounterBasic + /** * This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops. */ @@ -10,7 +12,7 @@ class CrossSolverUpgraded : CrossSolver() { for (moveCount in 1..8) { println("all cross move count upgrade doing $moveCount") // build a counter of moveCount big - val counter = Counter(moveCount, 18) + val counter = CounterBasic(moveCount) val edgeModelFactory = EdgeModelFactory(edgeModel, counter) while (edgeModelFactory.hasNext()) { diff --git a/src/main/kotlin/be/nielandt/CrossSolverUpgradedSkip.kt b/src/main/kotlin/be/nielandt/CrossSolverUpgradedSkip.kt index 46faf7b..5730500 100644 --- a/src/main/kotlin/be/nielandt/CrossSolverUpgradedSkip.kt +++ b/src/main/kotlin/be/nielandt/CrossSolverUpgradedSkip.kt @@ -1,5 +1,7 @@ package be.nielandt +import be.nielandt.counter.CounterSkip + /** * This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops. */ diff --git a/src/main/kotlin/be/nielandt/EdgeModelFactory.kt b/src/main/kotlin/be/nielandt/EdgeModelFactory.kt index 425debc..fbd14f2 100644 --- a/src/main/kotlin/be/nielandt/EdgeModelFactory.kt +++ b/src/main/kotlin/be/nielandt/EdgeModelFactory.kt @@ -1,5 +1,7 @@ package be.nielandt +import be.nielandt.counter.Counter + /** * This thing helps us to create edgemodels using a counter. The advantage is that the edgemodel doesn't need to be calculated * completely from scratch: previous states are kept, so if, e.g., the third digit changes in the counter (of length 5), diff --git a/src/main/kotlin/be/nielandt/Counter.kt b/src/main/kotlin/be/nielandt/counter/Counter.kt similarity index 58% rename from src/main/kotlin/be/nielandt/Counter.kt rename to src/main/kotlin/be/nielandt/counter/Counter.kt index edc25d0..a9d4e11 100644 --- a/src/main/kotlin/be/nielandt/Counter.kt +++ b/src/main/kotlin/be/nielandt/counter/Counter.kt @@ -1,9 +1,9 @@ -package be.nielandt +package be.nielandt.counter /** * Counter for X digits of a given base. */ -open class Counter(size: Int, val base: Int = 18) { +open abstract class Counter(size: Int, val base: Int = 18) { /** * Empty counter, all 0 values for each digit. @@ -17,26 +17,9 @@ open class Counter(size: Int, val base: Int = 18) { var lastModifiedIndex: Int = 0 /** - * Increase the counter. - * - * @return true if the increase happened, false if we hit the ceiling. + * Increase the counter one step. True if it succeeded and a new value is there, false if we've reached the end. */ - open fun increase(): Boolean { - if (atMax()) { - return false - } - for (i in this.counter.size - 1 downTo 0) { - this.counter[i]++ - this.lastModifiedIndex = i - if (this.counter[i] == base) { - this.counter[i] = 0 - } else { - // keep track of the digit index we're breaking on. - break - } - } - return true - } + abstract fun increase(): Boolean /** * How many digits does this counter contain? diff --git a/src/main/kotlin/be/nielandt/counter/CounterBasic.kt b/src/main/kotlin/be/nielandt/counter/CounterBasic.kt new file mode 100644 index 0000000..34df17e --- /dev/null +++ b/src/main/kotlin/be/nielandt/counter/CounterBasic.kt @@ -0,0 +1,38 @@ +package be.nielandt.counter + +import java.util.* + +/** + * Counter for X digits of a given base. Skips situations where faces are the same. + */ +class CounterBasic(size: Int) : Counter(size, 18) { + + /** + * Increase the counter. + * + * @return true if the increase happened, false if we hit the ceiling. + */ + override fun increase(): Boolean { + if (atMax()) { + return false + } + for (i in this.counter.size - 1 downTo 0) { + this.counter[i]++ + this.lastModifiedIndex = i + if (this.counter[i] == base) { + this.counter[i] = 0 + } else { + // keep track of the digit index we're breaking on. + break + } + } + return true + } +} + +fun main(args: Array) { + val counterSkip = CounterSkip(4) + while (counterSkip.increase()) { + println("counterSkip.counter = ${Arrays.toString(counterSkip.counter)}") + } +} \ No newline at end of file diff --git a/src/main/kotlin/be/nielandt/CounterSkip.kt b/src/main/kotlin/be/nielandt/counter/CounterSkip.kt similarity index 99% rename from src/main/kotlin/be/nielandt/CounterSkip.kt rename to src/main/kotlin/be/nielandt/counter/CounterSkip.kt index 34e7496..17b8234 100644 --- a/src/main/kotlin/be/nielandt/CounterSkip.kt +++ b/src/main/kotlin/be/nielandt/counter/CounterSkip.kt @@ -1,4 +1,4 @@ -package be.nielandt +package be.nielandt.counter import java.util.*