Skip to content

Commit 6e6316b

Browse files
author
Dr. Brandon Wiley
committed
Convereted from Int to BigInteger
1 parent db3cfb5 commit 6e6316b

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

Codex/src/main/java/org/operatorfoundation/codex/Decoder.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.operatorfoundation.codex
22

33
import org.operatorfoundation.codex.symbols.Symbol
4+
import java.math.BigInteger
45

56
/**
67
* Decoder class that converts a list of encoded values back to an integer.
@@ -27,9 +28,9 @@ class Decoder(private val symbols: List<Symbol>)
2728
* @param encodedValues List of ByteArrays, one for each symbol
2829
* @return The decoded integer value
2930
*/
30-
fun decode(encodedValues: List<ByteArray>): Int
31+
fun decode(encodedValues: List<ByteArray>): BigInteger
3132
{
32-
val results = mutableListOf<Int>()
33+
val results = mutableListOf<BigInteger>()
3334

3435
// Process each symbol with its corresponding encoded value
3536
symbols.forEachIndexed { index, symbol ->
@@ -38,7 +39,7 @@ class Decoder(private val symbols: List<Symbol>)
3839
results.add(result)
3940
}
4041

41-
return results.sum()
42+
return results.reduce(BigInteger::add)
4243
}
4344

4445
/**
@@ -49,14 +50,14 @@ class Decoder(private val symbols: List<Symbol>)
4950
* @param index The position of this symbol in the list
5051
* @return The numeric contribution of this symbol to the total
5152
*/
52-
private fun decodeStep(encodedValue: ByteArray, symbol: Symbol, index: Int): Int
53+
private fun decodeStep(encodedValue: ByteArray, symbol: Symbol, index: Int): BigInteger
5354
{
5455
if (symbol.size() == 1)
5556
{
5657
// Symbols with size 1 don't contribute to the numeric value
5758
println("decode_step(${encodedValue.decodeToString()}, $symbol, $index)")
5859

59-
return 0
60+
return 0.toBigInteger()
6061
}
6162
else
6263
{
@@ -77,7 +78,7 @@ class Decoder(private val symbols: List<Symbol>)
7778
println("history: $remainingSymbolSizes, p: $positionMultiplier")
7879

7980
// Multiply decoded value by position weight
80-
val result = symbol.decode(encodedValue) * positionMultiplier
81+
val result = symbol.decode(encodedValue) * positionMultiplier.toBigInteger()
8182
println("result: $result")
8283

8384
return result

Codex/src/main/java/org/operatorfoundation/codex/Encoder.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.operatorfoundation.codex
22

33
import 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

Comments
 (0)