@@ -60,8 +60,6 @@ class Encoder(private val symbols: List<Symbol>)
6060 if (symbol.size() == 1 )
6161 {
6262 // (size = 1) don't consume any of the value
63- println (" encode_step( current value: $currentValue , symbol: $symbol , index: $index )" )
64-
6563 // For debugging: show symbol capacity up to this point
6664 val symbolsUpToHere = if (index == 0 )
6765 {
@@ -71,56 +69,40 @@ class Encoder(private val symbols: List<Symbol>)
7169 {
7270 symbols.subList(0 , symbols.size - index)
7371 }
74-
75- val symbolSizes = symbolsUpToHere.map { it.size() }
76- val totalCapacity = symbolSizes.fold(1 ) { acc, size -> acc * size }
77-
78- println (" history (symbol sizes): $symbolSizes , total capacity: $totalCapacity " )
7972
8073 // Symbol with size 1 encodes its fixed value and passes through the current value
8174 val encodedBytes = symbol.encode(currentValue)
8275 val result = Pair (encodedBytes, currentValue)
8376
84- println (" result: (${encodedBytes.decodeToString()} , $currentValue )" )
85-
8677 return result
8778 }
8879 else
8980 {
90- println (" encode_step(currrent value: $currentValue , symbol: $symbol , index: $index )" )
91-
9281 if (index == symbols.size - 1 )
9382 {
9483 // Last symbol: encode all remaining value
9584 val encodedBytes = symbol.encode(currentValue)
9685 val result = Pair (encodedBytes, 0 .toBigInteger())
9786
98- println (" result: (${encodedBytes.decodeToString()} , 0)" )
99-
10087 return result
10188 }
10289 else
10390 {
10491 // Calculate capacity of remaining symbols
10592 val remainingSymbols = symbols.subList(index + 1 , symbols.size)
10693 val remainingSymbolSizes = remainingSymbols.map { it.size() }
107- val remainingCapacity = remainingSymbolSizes.fold(1 ) { acc, size -> acc * size }
108-
109- println (" history:\n remaining symbol sizes $remainingSymbolSizes , remaining capacity: $remainingCapacity " )
94+ val remainingCapacity = remainingSymbolSizes.fold(BigInteger .ONE ) { acc, size -> acc * size.toBigInteger() }
11095
11196 // Determine value for this symbol position
11297 // Division gives us how many "chunks" of remaining capacity we have
113- val symbolValue = (currentValue / remainingCapacity.toBigInteger()).min(symbol.size().toBigInteger() - 1 .toBigInteger())
114- println (" Symbol value: $symbolValue " )
98+ val symbolValue = (currentValue / remainingCapacity).min(symbol.size().toBigInteger() - BigInteger .ONE )
11599
116100 // Modulo gives us what's left for the remaining symbols
117- val leftoverValue = currentValue % remainingCapacity.toBigInteger()
118- println (" Leftover value: $leftoverValue " )
101+ val leftoverValue = currentValue % remainingCapacity
119102
120103 // Encode this symbol's portion
121104 val encodedBytes = symbol.encode(symbolValue)
122105 val result = Pair (encodedBytes, leftoverValue)
123- println (" result: (${encodedBytes.decodeToString()} , $leftoverValue )" )
124106
125107 return result
126108 }
0 commit comments