serializable and hashable BitArray. Comparable BitList. Small improvements
This commit is contained in:
1 parent
2696f1546d
commit
3481a718b1
11 files changed
+80
-14
No files matched your search
@@ -17,8 +17,10 @@
|
||||
|
||||
package net.sergeych.lynon
|
||||
|
||||
import kotlin.math.min
|
||||
|
||||
@Suppress("unused")
|
||||
interface BitList {
|
||||
interface BitList: Comparable<BitList> {
|
||||
operator fun get(bitIndex: Long): Int
|
||||
operator fun set(bitIndex: Long,value: Int)
|
||||
val size: Long
|
||||
@@ -31,6 +33,22 @@ interface BitList {
|
||||
if( index < size) this@BitList[index++]
|
||||
else null
|
||||
}
|
||||
|
||||
override fun compareTo(other: BitList): Int {
|
||||
val m = min(size, other.size)
|
||||
for( i in 0 ..< m) {
|
||||
val a = this[i]
|
||||
val b = other[i]
|
||||
when {
|
||||
a < b -> return -1
|
||||
a > b -> return 1
|
||||
}
|
||||
}
|
||||
if( size > other.size) return 1
|
||||
if( size < other.size) return -1
|
||||
return 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun bitListOf(vararg bits: Int): BitList {
|
||||
|
||||
@@ -17,13 +17,18 @@
|
||||
|
||||
package net.sergeych.lynon
|
||||
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.math.min
|
||||
|
||||
/**
|
||||
* BitList implementation as fixed suze array of bits; indexing works exactly same as if
|
||||
* [MemoryBitInput] is used with [MemoryBitInput.getBit]. See [MemoryBitOutput] for
|
||||
* bits order and more information.
|
||||
*
|
||||
* It is [BitList] - comparable, and provides valid [hashCode] and [equals], so it could
|
||||
* also be used as a key in maps.
|
||||
*/
|
||||
@Serializable
|
||||
class BitArray(val bytes: UByteArray, val lastByteBits: Int) : BitList {
|
||||
|
||||
val bytesSize: Int get() = bytes.size
|
||||
@@ -73,7 +78,15 @@ class BitArray(val bytes: UByteArray, val lastByteBits: Int) : BitList {
|
||||
fun asByteArray(): ByteArray = bytes.asByteArray()
|
||||
|
||||
@Suppress("unused")
|
||||
fun asUbyteArray(): UByteArray = bytes
|
||||
fun asUByteArray(): UByteArray = bytes
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return other is BitList && this.compareTo(other) == 0
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
return bytes.contentHashCode()
|
||||
}
|
||||
|
||||
companion object {
|
||||
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
package net.sergeych.lynon
|
||||
|
||||
import net.sergeych.collections.SortedList
|
||||
import net.sergeych.lynon.Huffman.Alphabet
|
||||
|
||||
|
||||
/**
|
||||
@@ -285,7 +284,7 @@ object Huffman {
|
||||
|
||||
fun <T>decompress(bin: BitInput,alphabet: Alphabet<T>): UByteArray {
|
||||
val codes = deserializeCanonicCodes(bin, alphabet)
|
||||
return decompressUsingCodes(bin, codes, alphabet).asUbyteArray()
|
||||
return decompressUsingCodes(bin, codes, alphabet).asUByteArray()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -56,7 +56,7 @@ object ObjLynonClass : ObjClass("Lynon") {
|
||||
@Suppress("unused")
|
||||
suspend fun lynonEncodeAny(scope: Scope, value: Obj): UByteArray =
|
||||
(ObjLynonClass.encodeAny(scope, value))
|
||||
.bitArray.asUbyteArray()
|
||||
.bitArray.asUByteArray()
|
||||
|
||||
@Suppress("unused")
|
||||
suspend fun lynonDecodeAny(scope: Scope, encoded: UByteArray): Obj =
|
||||
|
||||
Reference in new issue
Block a user