implement optimizations

This commit is contained in:
2026-01-14 15:36:50 +01:00
parent 9391009403
commit cc9961084c
31 changed files with 1025 additions and 1037 deletions

View File

@@ -0,0 +1,59 @@
package com.infendro.bytes.uint
import com.infendro.bytes.ByteOrder
import com.infendro.bytes.ByteOrder.BIG_ENDIAN
import com.infendro.bytes.ByteOrder.LITTLE_ENDIAN
fun UInt.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) {
require(destinationOffset in 0..(destination.size - 4))
when (order) {
BIG_ENDIAN -> {
destination[destinationOffset] = (this shr 24).toUByte()
destination[destinationOffset + 1] = (this shr 16).toUByte()
destination[destinationOffset + 2] = (this shr 8).toUByte()
destination[destinationOffset + 3] = this.toUByte()
}
LITTLE_ENDIAN -> {
destination[destinationOffset] = this.toUByte()
destination[destinationOffset + 1] = (this shr 8).toUByte()
destination[destinationOffset + 2] = (this shr 16).toUByte()
destination[destinationOffset + 3] = (this shr 24).toUByte()
}
}
}
fun UInt.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, order)
fun UIntArray.copyInto(
destination: UByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) {
val length = endIndex - startIndex
require(length >= 0)
require(startIndex in 0..(size - length))
require(destinationOffset in 0..(destination.size - (length * 4)))
repeat(length) { i ->
this[startIndex + i].copyInto(destination, destinationOffset + (i * 4), order)
}
}
fun UIntArray.copyInto(
destination: ByteArray,
destinationOffset: Int = 0,
startIndex: Int = 0,
endIndex: Int = size,
order: ByteOrder = BIG_ENDIAN,
) = copyInto(destination.asUByteArray(), destinationOffset, startIndex, endIndex, order)