Added irregular counter

This commit is contained in:
Joachim 2018-08-27 21:03:26 +02:00
parent 23b5bcbce1
commit 0c779c7a5b
4 changed files with 132 additions and 66 deletions

View File

@ -7,9 +7,9 @@ import be.nielandt.decodeMove
* 1) tier1 : axes / move classes (FB/UD/RL) -> three classes, but multiple sizes (1 or 2) possible
* 2) tier2: each block of class (22, 11, 2, 11, 0, ...) needs to be expanded into the full range of related moves
*/
class CounterTiered : Counter {
class CounterTiered : Counter(8) {
private val moveIterator: Iterator<Array<Int>>
private val moveIterator: Iterator<Array<Int>> = listOf<Array<Int>>().iterator()
override fun increase(): Boolean {
if (!this.moveIterator.hasNext())
@ -18,12 +18,6 @@ class CounterTiered : Counter {
return true
}
constructor(size: Int) : super(size, 18) {
this.moveIterator = moveIterator().iterator()
this.increase()
}
override fun atMax(): Boolean {
return !this.moveIterator.hasNext()
}
@ -65,73 +59,17 @@ class CounterTiered : Counter {
}
fun moveIterator(): Iterator<Array<Int>> {
return MoveIterator(classIterator()).iterator()
}
/**
* These are the classes (FB/UD/RL), correctly configured (no illegal combinations here)
*/
fun classIterator(): List<List<Int>> {
val classes = appendRandomClass(this.counter.size, listOf())
return classes
}
/**
* Helper function for classIterator()
*/
private fun appendRandomClass(size: Int, base: List<Int>): List<List<Int>> {
val result = mutableListOf<List<Int>>()
when {
base.size < size - 1 -> // add all classes, but don't repeat a group already there
(0..2).filter { if (base.isNotEmpty()) it != base.last() else true }
.forEach { theClass ->
// and both amounts, 1+2
(1..2).forEach { amount ->
val l = base.toMutableList()
for (i in 1..amount) {
l.add(theClass)
}
result.addAll(appendRandomClass(size, l))
}
}
base.size == size -> {
// we're done here
result.add(base)
}
base.size == size - 1 -> // the base is not empty, only add class that is not at the end of the base list
(0..2).filter { if (base.isEmpty()) true else it != base.last() }.forEach { theClass ->
val l = base.toMutableList()
l.add(theClass)
result.add(l)
}
}
return result
}
}
class CounterTieredFactory {
companion object {
fun create(size: Int): CounterTiered {
return CounterTiered(size)
return CounterTiered()
}
}
}
fun main(args: Array<String>) {
var count = 0
CounterTieredFactory.create(4).classIterator().forEach {
println("it = ${it}")
count++
}
println("count = ${count}")
// CounterTieredFactory.create(7).moveIterator().forEach {
// println("Arrays.tostring(it) = ${Arrays.toString(it)} ${it.map { decodeMove(it) }.toList()}")
// count++
// }
// println("count = ${count}")
}
/**
* Expand the class ints into the full range of moves.
*

View File

@ -0,0 +1,55 @@
package be.nielandt.counter
import java.util.*
/**
* The variable counter has irregular base size for each digit.
*
* basesize: [1,2,1]
* iterates:
* - [0,0,0]
* - [0,0,1]
* - [0,1,0]
* - [0,1,1]
* - [0,2,0]
* - ...
*/
class VariableCounter(private val baseSizes: IntArray) : Iterator<Array<Int>> {
// init the counter with 0's, we only have a maximum as our basesize
val counter = Array(baseSizes.size) {
when (it) {
baseSizes.size - 1 -> -1
else -> 0
}
}
override fun hasNext(): Boolean {
// check if all elements in the counter has reached their maximum (basesize - 1)
counter.forEachIndexed { index, sh ->
if (sh < baseSizes[index] - 1)
return true
}
return false
}
override fun next(): Array<Int> {
for (i in this.counter.size - 1 downTo 0) {
this.counter[i]++
if (this.counter[i] == baseSizes[i]) {
this.counter[i] = 0
} else {
return counter.copyOf()
}
}
return counter.copyOf()
}
}
fun main(args: Array<String>) {
val counter = VariableCounter(intArrayOf(2, 3, 2))
while (counter.hasNext()) {
val next = counter.next()
println("counter.next() = ${Arrays.toString(next)} ${counter.hasNext()}")
}
}

View File

@ -0,0 +1,70 @@
package be.nielandt.iterator
import java.time.Duration
import java.time.Instant
class ValidClassesIterator(val size: Int) : Iterator<List<Int>> {
private var classes: Iterator<List<Int>> = classIterator().iterator()
override fun hasNext(): Boolean {
return this.classes.hasNext()
}
override fun next(): List<Int> {
return this.classes.next()
}
/**
* These are the classes (FB/UD/RL), correctly configured (no illegal combinations here)
*/
private fun classIterator(): List<List<Int>> {
val classes = appendRandomClass(size, listOf())
return classes
}
/**
* Helper function for classIterator()
*/
private fun appendRandomClass(size: Int, base: List<Int>): List<List<Int>> {
val result = mutableListOf<List<Int>>()
when {
base.size < size - 1 -> // add all classes, but don't repeat a group already there
(0..2).filter { if (base.isNotEmpty()) it != base.last() else true }
.forEach { theClass ->
// and both amounts, 1+2
(1..2).forEach { amount ->
val l = base.toMutableList()
for (i in 1..amount) {
l.add(theClass)
}
result.addAll(appendRandomClass(size, l))
}
}
base.size == size -> {
// we're done here
result.add(base)
}
base.size == size - 1 -> // the base is not empty, only add class that is not at the end of the base list
(0..2).filter { if (base.isEmpty()) true else it != base.last() }.forEach { theClass ->
val l = base.toMutableList()
l.add(theClass)
result.add(l)
}
}
return result
}
}
fun main(args: Array<String>) {
val now = Instant.now()
val iter = ValidClassesIterator(8)
var count: Int = 0
while(iter.hasNext()) {
println("iter.next() = ${iter.next()}")
count++
}
println("count = ${count}")
println("Duration.between(Instant.now(), now) = ${Duration.between(now, Instant.now()).toMillis()}")
}

View File

@ -1,6 +1,9 @@
package be.nielandt.iterator
class ValidMoveIterator: Iterator<Array<Int>> {
/**
* Iterates over the different moves of the given specific classes.
*/
class ValidMoveIterator(val classes: List<Int>): Iterator<Array<Int>> {
override fun hasNext(): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.