11package org.operatorfoundation.codex
22
33import org.operatorfoundation.codex.symbols.Symbol
4- import kotlin .math.min
4+ import java .math.BigInteger
55
66/* *
77 * Encoder class that converts an integer to a list of encoded values.
@@ -28,7 +28,7 @@ class Encoder(private val symbols: List<Symbol>)
2828 * @return List of ByteArrays, one for each symbol
2929 * @throws Exception if the value is too large to encode
3030 */
31- fun encode (integerToEncode : Int ): List <ByteArray >
31+ fun encode (integerToEncode : BigInteger ): List <ByteArray >
3232 {
3333 val results = mutableListOf<ByteArray >()
3434 var remainingValue = integerToEncode
@@ -40,7 +40,7 @@ class Encoder(private val symbols: List<Symbol>)
4040 remainingValue = leftoverValue
4141 }
4242
43- if (remainingValue != 0 ) {
43+ if (remainingValue != 0 .toBigInteger() ) {
4444 throw Exception (" Encoder error, results: ${results.map { it.decodeToString() }} , leftover: $remainingValue " )
4545 }
4646
@@ -55,7 +55,7 @@ class Encoder(private val symbols: List<Symbol>)
5555 * @param index The position of this symbol in the list
5656 * @return Pair of (encoded ByteArray for this symbol, remaining value)
5757 */
58- private fun encodeStep (currentValue : Int , symbol : Symbol , index : Int ): Pair <ByteArray , Int >
58+ private fun encodeStep (currentValue : BigInteger , symbol : Symbol , index : Int ): Pair <ByteArray , BigInteger >
5959 {
6060 if (symbol.size() == 1 )
6161 {
@@ -92,7 +92,7 @@ class Encoder(private val symbols: List<Symbol>)
9292 {
9393 // Last symbol: encode all remaining value
9494 val encodedBytes = symbol.encode(currentValue)
95- val result = Pair (encodedBytes, 0 )
95+ val result = Pair (encodedBytes, 0 .toBigInteger() )
9696 println (" result: (${encodedBytes.decodeToString()} , 0)" )
9797
9898 return result
@@ -108,11 +108,11 @@ class Encoder(private val symbols: List<Symbol>)
108108
109109 // Determine value for this symbol position
110110 // Division gives us how many "chunks" of remaining capacity we have
111- val symbolValue = min (currentValue / remainingCapacity, symbol.size() - 1 )
111+ val symbolValue = (currentValue / remainingCapacity.toBigInteger()).min( symbol.size().toBigInteger() - 1 .toBigInteger() )
112112 println (" n: $symbolValue " )
113113
114114 // Modulo gives us what's left for the remaining symbols
115- val leftoverValue = currentValue % remainingCapacity
115+ val leftoverValue = currentValue % remainingCapacity.toBigInteger()
116116 println (" m: $leftoverValue " )
117117
118118 // Encode this symbol's portion
0 commit comments