bit fields
This commit is contained in:
29
README.md
Normal file
29
README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Blitz
|
||||
Kotlin library which mainly focuses on functional programming.
|
||||
|
||||
Features:
|
||||
- Monads
|
||||
- Either
|
||||
- Caching delegated property (similar to lazy)
|
||||
- A lot of sequence utils
|
||||
- Streaming IO using kotlinx.io
|
||||
- ByteVec (alternative to ByteBuf)
|
||||
- BitVec (similar to bit sets in other languages)
|
||||
- "Lazy" sequences
|
||||
- A lot of generative sequence types
|
||||
- BitField
|
||||
|
||||
## How to get
|
||||
```kotlin
|
||||
repositories {
|
||||
maven {
|
||||
name = "alex's repo"
|
||||
url = uri("http://207.180.202.42:8080/libs")
|
||||
isAllowInsecureProtocol = true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("me.alex_s168:blitz:0.1")
|
||||
}
|
||||
```
|
@@ -5,7 +5,7 @@ plugins {
|
||||
}
|
||||
|
||||
group = "me.alex_s168"
|
||||
version = "0.1"
|
||||
version = "0.2"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
@@ -2,5 +2,5 @@ plugins {
|
||||
id("org.gradle.toolchains.foojay-resolver-convention") version "0.5.0"
|
||||
}
|
||||
|
||||
rootProject.name = "kotlin-bits"
|
||||
rootProject.name = "blitz"
|
||||
|
||||
|
33
src/main/kotlin/blitz/BitField.kt
Normal file
33
src/main/kotlin/blitz/BitField.kt
Normal file
@@ -0,0 +1,33 @@
|
||||
package blitz
|
||||
|
||||
import blitz.collections.BitVec
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
abstract class BitField {
|
||||
private var vec = BitVec(1)
|
||||
|
||||
protected class BitDelegate(
|
||||
private val thi: BitField,
|
||||
private val pos: Int,
|
||||
) {
|
||||
operator fun getValue(thisRef: Any?, property: KProperty<*>): Boolean =
|
||||
thi.vec[pos]
|
||||
|
||||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean) {
|
||||
thi.vec[pos] = value
|
||||
}
|
||||
}
|
||||
|
||||
protected fun bit(pos: Int): BitDelegate =
|
||||
BitDelegate(this, pos)
|
||||
|
||||
fun decode(byte: Byte) {
|
||||
vec = BitVec.from(byteArrayOf(byte))
|
||||
}
|
||||
|
||||
fun encode(): Byte =
|
||||
vec.toBytes()[0]
|
||||
|
||||
override fun toString(): String =
|
||||
"${this::class.simpleName}(0b${vec.toBytes()[0].toString(2)})"
|
||||
}
|
Reference in New Issue
Block a user