Skip to content

Commit

Permalink
handle zero corner case
Browse files Browse the repository at this point in the history
  • Loading branch information
breandan committed May 25, 2024
1 parent 4fe1d5f commit 8471026
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,12 @@ class PTree(val root: String = ".ε", val branches: List<Π2A<PTree>> = listOf()
}

fun sampleStrWithoutReplacement(stride: Int = 1, offset: Int = 0): Sequence<String> =
if (totalTrees.bitLength() < 5) sequence {
if (6 < totalTrees.bitLength())
bigLFSRSequence(totalTrees).mapIndexedNotNull { index, i -> if (index % stride == offset) newDecoder(i) else null }
else sequence {
var i = BigInteger.ZERO
while (i < totalTrees) { yield(newDecoder(i)); i++}
} else bigLFSRSequence(totalTrees)
.mapIndexedNotNull { index, i -> if (index % stride == offset) newDecoder(i) else null }
}

fun sampleStrWithPCFG5(pcfgTable: Map<Int, Int>): Sequence<String> =
sequence { while (true) yield(samplePCFG5(pcfgTable)) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class BigLFSR(primitivePoly: BigInteger, val start: BigInteger = BigInteger.ONE)
val shiftedOutA1: Boolean = last.bitAt(0)
next = last.shr(1)
if (shiftedOutA1) { next = next.xor(taps) }
if (next == start) break else yield(next)
if (next == start) { yield(BigInteger.ZERO); break } else yield(next)
last = next
}
}
Expand Down

0 comments on commit 8471026

Please sign in to comment.