Improve shorthand bytearray operator functions
This commit is contained in:
@@ -5,14 +5,13 @@ import kotlin.experimental.and
|
|||||||
import kotlin.experimental.or
|
import kotlin.experimental.or
|
||||||
import kotlin.experimental.xor
|
import kotlin.experimental.xor
|
||||||
|
|
||||||
private inline fun ByteArray.combineInto(
|
fun ByteArray.andInto(
|
||||||
that: ByteArray,
|
that: ByteArray,
|
||||||
destination: ByteArray,
|
destination: ByteArray,
|
||||||
destinationOffset: Int = 0,
|
destinationOffset: Int = 0,
|
||||||
startIndex: Int = 0,
|
startIndex: Int = 0,
|
||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
combine: (Byte, Byte) -> Byte,
|
|
||||||
) {
|
) {
|
||||||
val length = endIndex - startIndex
|
val length = endIndex - startIndex
|
||||||
require(length >= 0)
|
require(length >= 0)
|
||||||
@@ -21,17 +20,16 @@ private inline fun ByteArray.combineInto(
|
|||||||
require(destinationOffset in 0..(destination.size - length))
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
for (i in 0..<length) {
|
for (i in 0..<length) {
|
||||||
destination[destinationOffset + i] = combine(this[startIndex + i], that[thatStartIndex + i])
|
destination[destinationOffset + i] = this[startIndex + i] and that[thatStartIndex + i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun ByteArray.combineInto(
|
fun ByteArray.andInto(
|
||||||
that: Byte,
|
that: Byte,
|
||||||
destination: ByteArray,
|
destination: ByteArray,
|
||||||
destinationOffset: Int = 0,
|
destinationOffset: Int = 0,
|
||||||
startIndex: Int = 0,
|
startIndex: Int = 0,
|
||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
combine: (Byte, Byte) -> Byte,
|
|
||||||
) {
|
) {
|
||||||
val length = endIndex - startIndex
|
val length = endIndex - startIndex
|
||||||
require(length >= 0)
|
require(length >= 0)
|
||||||
@@ -39,55 +37,26 @@ private inline fun ByteArray.combineInto(
|
|||||||
require(destinationOffset in 0..(destination.size - length))
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
for (i in 0..<length) {
|
for (i in 0..<length) {
|
||||||
destination[destinationOffset + i] = combine(this[startIndex + i], that)
|
destination[destinationOffset + i] = this[startIndex + i] and that
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun ByteArray.combine(that: ByteArray, combine: (Byte, Byte) -> Byte): ByteArray {
|
fun ByteArray.andWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
||||||
require(this.size == that.size)
|
andInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
val result = ByteArray(size)
|
fun ByteArray.andWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
combineInto(that, result, combine = combine)
|
andInto(that, this, startIndex, startIndex, endIndex)
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun ByteArray.combine(that: Byte, combine: (Byte, Byte) -> Byte): ByteArray {
|
|
||||||
val result = ByteArray(size)
|
|
||||||
combineInto(that, result, combine = combine)
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.andInto(
|
|
||||||
that: ByteArray,
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
thatStartIndex: Int = 0,
|
|
||||||
) {
|
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a and b }
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.andInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
|
||||||
andInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun ByteArray.andInto(
|
|
||||||
that: Byte,
|
|
||||||
destination: ByteArray,
|
|
||||||
destinationOffset: Int = 0,
|
|
||||||
startIndex: Int = 0,
|
|
||||||
endIndex: Int = size,
|
|
||||||
) {
|
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a and b }
|
|
||||||
}
|
|
||||||
|
|
||||||
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
infix fun ByteArray.and(that: ByteArray): ByteArray {
|
||||||
return combine(that) { a, b -> a and b }
|
val result = ByteArray(size)
|
||||||
|
andInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun ByteArray.and(that: Byte): ByteArray {
|
infix fun ByteArray.and(that: Byte): ByteArray {
|
||||||
return combine(that) { a, b -> a and b }
|
val result = ByteArray(size)
|
||||||
|
andInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.orInto(
|
fun ByteArray.orInto(
|
||||||
@@ -98,11 +67,15 @@ fun ByteArray.orInto(
|
|||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
) {
|
) {
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a or b }
|
val length = endIndex - startIndex
|
||||||
}
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(thatStartIndex in 0..(that.size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
fun ByteArray.orInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
for (i in 0..<length) {
|
||||||
orInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
destination[destinationOffset + i] = this[startIndex + i] or that[thatStartIndex + i]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.orInto(
|
fun ByteArray.orInto(
|
||||||
@@ -112,15 +85,32 @@ fun ByteArray.orInto(
|
|||||||
startIndex: Int = 0,
|
startIndex: Int = 0,
|
||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
) {
|
) {
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a or b }
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] or that
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ByteArray.orWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
||||||
|
orInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
|
fun ByteArray.orWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
|
orInto(that, this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
infix fun ByteArray.or(that: ByteArray): ByteArray {
|
||||||
return combine(that) { a, b -> a or b }
|
val result = ByteArray(size)
|
||||||
|
orInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun ByteArray.or(that: Byte): ByteArray {
|
infix fun ByteArray.or(that: Byte): ByteArray {
|
||||||
return combine(that) { a, b -> a or b }
|
val result = ByteArray(size)
|
||||||
|
orInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.xorInto(
|
fun ByteArray.xorInto(
|
||||||
@@ -131,11 +121,15 @@ fun ByteArray.xorInto(
|
|||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
thatStartIndex: Int = 0,
|
thatStartIndex: Int = 0,
|
||||||
) {
|
) {
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex, thatStartIndex) { a, b -> a xor b }
|
val length = endIndex - startIndex
|
||||||
}
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(thatStartIndex in 0..(that.size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
fun ByteArray.xorInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
for (i in 0..<length) {
|
||||||
xorInto(destination, destination, destinationOffset, startIndex, endIndex, destinationOffset)
|
destination[destinationOffset + i] = this[startIndex + i] xor that[thatStartIndex + i]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.xorInto(
|
fun ByteArray.xorInto(
|
||||||
@@ -145,15 +139,32 @@ fun ByteArray.xorInto(
|
|||||||
startIndex: Int = 0,
|
startIndex: Int = 0,
|
||||||
endIndex: Int = size,
|
endIndex: Int = size,
|
||||||
) {
|
) {
|
||||||
combineInto(that, destination, destinationOffset, startIndex, endIndex) { a, b -> a xor b }
|
val length = endIndex - startIndex
|
||||||
|
require(length >= 0)
|
||||||
|
require(startIndex in 0..(size - length))
|
||||||
|
require(destinationOffset in 0..(destination.size - length))
|
||||||
|
|
||||||
|
for (i in 0..<length) {
|
||||||
|
destination[destinationOffset + i] = this[startIndex + i] xor that
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun ByteArray.xorWith(that: ByteArray, startIndex: Int = 0, endIndex: Int = size, thatStartIndex: Int) =
|
||||||
|
xorInto(that, this, startIndex, startIndex, endIndex, thatStartIndex)
|
||||||
|
|
||||||
|
fun ByteArray.xorWith(that: Byte, startIndex: Int = 0, endIndex: Int = size) =
|
||||||
|
xorInto(that, this, startIndex, startIndex, endIndex)
|
||||||
|
|
||||||
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
infix fun ByteArray.xor(that: ByteArray): ByteArray {
|
||||||
return combine(that) { a, b -> a xor b }
|
val result = ByteArray(size)
|
||||||
|
xorInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
infix fun ByteArray.xor(that: Byte): ByteArray {
|
infix fun ByteArray.xor(that: Byte): ByteArray {
|
||||||
return combine(that) { a, b -> a xor b }
|
val result = ByteArray(size)
|
||||||
|
xorInto(that, result)
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startIndex: Int = 0, endIndex: Int = size) {
|
||||||
@@ -167,9 +178,8 @@ fun ByteArray.invInto(destination: ByteArray, destinationOffset: Int = 0, startI
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun ByteArray.inv(startIndex: Int = 0, endIndex: Int = size) {
|
fun ByteArray.inv(startIndex: Int, endIndex: Int) =
|
||||||
invInto(this, startIndex, startIndex, endIndex)
|
invInto(this, startIndex, startIndex, endIndex)
|
||||||
}
|
|
||||||
|
|
||||||
operator fun ByteArray.not(): ByteArray {
|
operator fun ByteArray.not(): ByteArray {
|
||||||
val result = ByteArray(size)
|
val result = ByteArray(size)
|
||||||
|
|||||||
Reference in New Issue
Block a user