Skip to content

Commit c6f1f63

Browse files
author
Dr. Brandon Wiley
committed
Merge branch 'main' of github.com:OperatorFoundation/CodexKotlin
2 parents 83a34f9 + c71756f commit c6f1f63

File tree

8 files changed

+1197
-261
lines changed

8 files changed

+1197
-261
lines changed

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,10 @@ class Decoder(private val symbols: List<Symbol>)
5555
if (symbol.size() == 1)
5656
{
5757
// Symbols with size 1 don't contribute to the numeric value
58-
println("decode_step(encoded value: ${encodedValue.decodeToString()}, symbol: $symbol, index: $index)")
59-
6058
return 0.toBigInteger()
6159
}
6260
else
6361
{
64-
println("decode_step(encoded value: ${encodedValue.decodeToString()}, symbol: $symbol, index: $index)")
65-
6662
if (index == symbols.size - 1)
6763
{
6864
// Last symbol: just return its decoded value
@@ -73,13 +69,11 @@ class Decoder(private val symbols: List<Symbol>)
7369
// Calculate product of remaining symbol sizes
7470
val remainingSymbols = symbols.subList(index + 1, symbols.size)
7571
val remainingSymbolSizes = remainingSymbols.map { it.size() }
76-
val positionMultiplier = remainingSymbolSizes.fold(1) { acc, size -> acc * size }
77-
78-
println("history:/n remaining symbol sizes - $remainingSymbolSizes, position multiplier: $positionMultiplier")
72+
// Use BigInteger to avoid integer overflow when multiplying symbol sizes
73+
val positionMultiplier = remainingSymbolSizes.fold(BigInteger.ONE) { acc, size -> acc * size.toBigInteger() }
7974

8075
// Multiply decoded value by position weight
81-
val result = symbol.decode(encodedValue) * positionMultiplier.toBigInteger()
82-
println("result: $result")
76+
val result = symbol.decode(encodedValue) * positionMultiplier
8377

8478
return result
8579
}

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

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)