Archived
1
0
Fork 0
This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
uranos/blokk-packet-codecs/src/main/kotlin/space/blokk/util/CompactedBitArray.kt
Moritz Ruth ffe2349884
Update everything to 1.16.4
this took me about 12 hours. it's 1 am
2020-12-21 01:05:28 +01:00

39 lines
1,010 B
Kotlin

@file:Suppress("DuplicatedCode")
package space.blokk.util
fun IntArray.toCompactLongArray(bitsPerEntry: Int): LongArray {
val itemsPerLong = Long.SIZE_BITS / bitsPerEntry
val array = LongArray(size / itemsPerLong)
var totalIndex = 0
for(longIndex in array.indices) {
var long: Long = 0
for (index in 0 until itemsPerLong) {
long = (get(totalIndex).toLong() shl (index * bitsPerEntry)) or long
totalIndex++
}
array[longIndex] = long
}
return array
}
fun ByteArray.toCompactLongArray(bitsPerEntry: Int): LongArray {
val itemsPerLong = Long.SIZE_BITS / bitsPerEntry
val array = LongArray(size / itemsPerLong)
var totalIndex = 0
for(longIndex in array.indices) {
var long: Long = 0
for (index in 0 until itemsPerLong) {
long = (get(totalIndex).toLong() shl (index * bitsPerEntry)) or long
totalIndex++
}
array[longIndex] = long
}
return array
}