Better read/write of imprecise fraction
This commit is contained in:
parent
17af248426
commit
1415c34206
@ -12,57 +12,60 @@ import kotlin.math.absoluteValue
|
|||||||
|
|
||||||
private fun isZero(value: BigInteger) = value == BigInteger.ZERO
|
private fun isZero(value: BigInteger) = value == BigInteger.ZERO
|
||||||
|
|
||||||
private val LONG_BUFF = ByteBuffer.allocate(8)
|
private fun unsignedInt(value: Byte): Int {
|
||||||
|
return value.toInt() and 0xFF
|
||||||
private fun byteToInt(value: Byte): Int {
|
|
||||||
if (value < 0) {
|
|
||||||
return 256 + value
|
|
||||||
}
|
|
||||||
|
|
||||||
return value.toInt()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun longToBytes(value: Long): ByteArray {
|
private fun usignedLong(value: Byte): Long {
|
||||||
LONG_BUFF.position(0)
|
return value.toLong() and 0xFFL
|
||||||
LONG_BUFF.putLong(value)
|
|
||||||
return LONG_BUFF.array().copyOf()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bytesToLong(value: ByteArray, from: Int = 0): Long {
|
private fun longToBytesBE(value: Long): ByteArray {
|
||||||
val arr = LONG_BUFF.array()
|
return byteArrayOf(
|
||||||
arr[0] = value[from]
|
(value ushr 56).toByte(),
|
||||||
arr[1] = value[from + 1]
|
(value ushr 48).toByte(),
|
||||||
arr[2] = value[from + 2]
|
(value ushr 40).toByte(),
|
||||||
arr[3] = value[from + 3]
|
(value ushr 32).toByte(),
|
||||||
arr[4] = value[from + 4]
|
(value ushr 24).toByte(),
|
||||||
arr[5] = value[from + 5]
|
(value ushr 16).toByte(),
|
||||||
arr[6] = value[from + 6]
|
(value ushr 8).toByte(),
|
||||||
arr[7] = value[from + 7]
|
(value).toByte(),
|
||||||
LONG_BUFF.position(0)
|
)
|
||||||
return LONG_BUFF.long
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun bytesToLong(
|
private fun bytesToLongBE(value: ByteArray, from: Int = 0): Long {
|
||||||
value0: Byte,
|
return (
|
||||||
value1: Byte,
|
(usignedLong(value[from + 7])) or
|
||||||
value2: Byte,
|
(usignedLong(value[from + 6]) shl 8) or
|
||||||
value3: Byte,
|
(usignedLong(value[from + 5]) shl 16) or
|
||||||
value4: Byte,
|
(usignedLong(value[from + 4]) shl 24) or
|
||||||
value5: Byte,
|
(usignedLong(value[from + 3]) shl 32) or
|
||||||
value6: Byte,
|
(usignedLong(value[from + 2]) shl 40) or
|
||||||
|
(usignedLong(value[from + 1]) shl 48) or
|
||||||
|
(usignedLong(value[from]) shl 56)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun bytesToLongBE(
|
||||||
value7: Byte,
|
value7: Byte,
|
||||||
|
value6: Byte,
|
||||||
|
value5: Byte,
|
||||||
|
value4: Byte,
|
||||||
|
value3: Byte,
|
||||||
|
value2: Byte,
|
||||||
|
value1: Byte,
|
||||||
|
value0: Byte,
|
||||||
): Long {
|
): Long {
|
||||||
val arr = LONG_BUFF.array()
|
return (
|
||||||
arr[0] = value0
|
(usignedLong(value0)) or
|
||||||
arr[1] = value1
|
(usignedLong(value1) shl 8) or
|
||||||
arr[2] = value2
|
(usignedLong(value2) shl 16) or
|
||||||
arr[3] = value3
|
(usignedLong(value3) shl 24) or
|
||||||
arr[4] = value4
|
(usignedLong(value4) shl 32) or
|
||||||
arr[5] = value5
|
(usignedLong(value5) shl 40) or
|
||||||
arr[6] = value6
|
(usignedLong(value6) shl 48) or
|
||||||
arr[7] = value7
|
(usignedLong(value7) shl 56)
|
||||||
LONG_BUFF.position(0)
|
)
|
||||||
return LONG_BUFF.long
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const val EPSILON = 0.0000001
|
const val EPSILON = 0.0000001
|
||||||
@ -330,7 +333,7 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
fun toByteArray(): ByteArray {
|
fun toByteArray(): ByteArray {
|
||||||
val whole = whole.toByteArray()
|
val whole = whole.toByteArray()
|
||||||
return byteArrayOf(whole.size.toByte(), *whole, *longToBytes(decimal.toBits()))
|
return byteArrayOf(whole.size.toByte(), *whole, *longToBytesBE(decimal.toBits()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun serializeNBT(): Tag {
|
fun serializeNBT(): Tag {
|
||||||
@ -388,7 +391,10 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun write(buff: FriendlyByteBuf) {
|
fun write(buff: FriendlyByteBuf) {
|
||||||
buff.writeBytes(toByteArray())
|
val whole = whole.toByteArray()
|
||||||
|
buff.writeByte(whole.size)
|
||||||
|
buff.writeBytes(whole)
|
||||||
|
buff.writeBytes(longToBytesBE(decimal.toBits()))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toFloat(): Float {
|
fun toFloat(): Float {
|
||||||
@ -446,9 +452,9 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun fromByteArray(input: ByteArray): ImpreciseFraction {
|
fun fromByteArray(input: ByteArray): ImpreciseFraction {
|
||||||
val size = byteToInt(input[0])
|
val size = unsignedInt(input[0])
|
||||||
val slice = input.copyOfRange(1, 1 + size)
|
val slice = input.copyOfRange(1, 1 + size)
|
||||||
val bits = bytesToLong(input, 1 + size)
|
val bits = bytesToLongBE(input, 1 + size)
|
||||||
return ImpreciseFraction(BigInteger(slice), Double.fromBits(bits))
|
return ImpreciseFraction(BigInteger(slice), Double.fromBits(bits))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -465,13 +471,13 @@ class ImpreciseFraction @JvmOverloads constructor(whole: BigInteger, decimal: Do
|
|||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun read(buff: FriendlyByteBuf): ImpreciseFraction {
|
fun read(buff: FriendlyByteBuf): ImpreciseFraction {
|
||||||
val len = byteToInt(buff.readByte())
|
val len = unsignedInt(buff.readByte())
|
||||||
val slice = ByteArray(len)
|
val slice = ByteArray(len)
|
||||||
|
|
||||||
for (i in 0 until len)
|
for (i in 0 until len)
|
||||||
slice[i] = buff.readByte()
|
slice[i] = buff.readByte()
|
||||||
|
|
||||||
val bits = bytesToLong(buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte())
|
val bits = bytesToLongBE(buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte(), buff.readByte())
|
||||||
return ImpreciseFraction(BigInteger(slice), Double.fromBits(bits))
|
return ImpreciseFraction(BigInteger(slice), Double.fromBits(bits))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,12 +133,12 @@ class ItemBattery : Item {
|
|||||||
p_41423_.add(INFINITE_STORAGE)
|
p_41423_.add(INFINITE_STORAGE)
|
||||||
p_41423_.add(throughputText)
|
p_41423_.add(throughputText)
|
||||||
} else {
|
} else {
|
||||||
stack.getCapability(MatteryCapability.ENERGY).ifPresent { cap: IMatteryEnergyStorage ->
|
stack.getCapability(MatteryCapability.ENERGY).ifPresent {
|
||||||
p_41423_.add(
|
p_41423_.add(
|
||||||
TranslatableComponent(
|
TranslatableComponent(
|
||||||
"otm.item.power.normal.storage",
|
"otm.item.power.normal.storage",
|
||||||
FormattingHelper.formatPower(cap.batteryLevel),
|
FormattingHelper.formatPower(it.batteryLevel),
|
||||||
FormattingHelper.formatPower(cap.maxBatteryLevel)
|
FormattingHelper.formatPower(it.maxBatteryLevel)
|
||||||
).withStyle(ChatFormatting.GRAY)
|
).withStyle(ChatFormatting.GRAY)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -36,8 +36,16 @@ object ImpreciseFractionTests {
|
|||||||
@Test
|
@Test
|
||||||
@DisplayName("ImpreciseFraction store/load")
|
@DisplayName("ImpreciseFraction store/load")
|
||||||
fun storeLoad() {
|
fun storeLoad() {
|
||||||
val f = ImpreciseFraction(4, 0.28)
|
run {
|
||||||
val loaded = ImpreciseFraction.fromByteArray(f.toByteArray())
|
val f = ImpreciseFraction(4, 0.28)
|
||||||
check(f == loaded) { "$f != $loaded" }
|
val loaded = ImpreciseFraction.fromByteArray(f.toByteArray())
|
||||||
|
check(f == loaded) { "$f != $loaded" }
|
||||||
|
}
|
||||||
|
|
||||||
|
run {
|
||||||
|
val f = ImpreciseFraction(32748293658335L, 0.3472302174)
|
||||||
|
val loaded = ImpreciseFraction.fromByteArray(f.toByteArray())
|
||||||
|
check(f == loaded) { "$f != $loaded" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user