Cleaned up counter classes.

This commit is contained in:
Joachim 2018-08-26 16:19:51 +02:00
parent 5a2131cb64
commit e0bd2fb01b
7 changed files with 53 additions and 24 deletions

View File

@ -1,5 +1,7 @@
package be.nielandt package be.nielandt
import be.nielandt.counter.CounterBasic
class CrossSolverBase : CrossSolver() { class CrossSolverBase : CrossSolver() {
/** /**
* Solve the minimal cross for all colors. * Solve the minimal cross for all colors.
@ -10,7 +12,7 @@ class CrossSolverBase : CrossSolver() {
for (moveCount in 1..8) { for (moveCount in 1..8) {
// build a counter of moveCount big // build a counter of moveCount big
println("allCrossMoveCount basic doing $moveCount") 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 // count up, each state of the counter corresponds to a combination of moves
do { do {

View File

@ -1,5 +1,7 @@
package be.nielandt package be.nielandt
import be.nielandt.counter.CounterBasic
/** /**
* This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops. * 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) { for (moveCount in 1..8) {
println("all cross move count upgrade doing $moveCount") println("all cross move count upgrade doing $moveCount")
// build a counter of moveCount big // build a counter of moveCount big
val counter = Counter(moveCount, 18) val counter = CounterBasic(moveCount)
val edgeModelFactory = EdgeModelFactory(edgeModel, counter) val edgeModelFactory = EdgeModelFactory(edgeModel, counter)
while (edgeModelFactory.hasNext()) { while (edgeModelFactory.hasNext()) {

View File

@ -1,5 +1,7 @@
package be.nielandt package be.nielandt
import be.nielandt.counter.CounterSkip
/** /**
* This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops. * This solver avoids redoing edgemodel manipulations. Should be equivalent to X nested for loops.
*/ */

View File

@ -1,5 +1,7 @@
package be.nielandt 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 * 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), * completely from scratch: previous states are kept, so if, e.g., the third digit changes in the counter (of length 5),

View File

@ -1,9 +1,9 @@
package be.nielandt package be.nielandt.counter
/** /**
* Counter for X digits of a given base. * 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. * 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 var lastModifiedIndex: Int = 0
/** /**
* Increase the counter. * Increase the counter one step. True if it succeeded and a new value is there, false if we've reached the end.
*
* @return true if the increase happened, false if we hit the ceiling.
*/ */
open fun increase(): Boolean { abstract 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
}
/** /**
* How many digits does this counter contain? * How many digits does this counter contain?

View File

@ -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<String>) {
val counterSkip = CounterSkip(4)
while (counterSkip.increase()) {
println("counterSkip.counter = ${Arrays.toString(counterSkip.counter)}")
}
}

View File

@ -1,4 +1,4 @@
package be.nielandt package be.nielandt.counter
import java.util.* import java.util.*