This commit is contained in:
alex-s168
2024-10-30 18:00:49 +01:00
parent f0b2736af5
commit 4ed0225b4f
11 changed files with 115 additions and 3 deletions

View File

@@ -12,7 +12,7 @@ repositories {
} }
dependencies { dependencies {
implementation("me.alex_s168:blitz:0.22h2") implementation("me.alex_s168:blitz:0.23")
} }
``` ```

View File

@@ -5,7 +5,7 @@ plugins {
} }
group = "me.alex_s168" group = "me.alex_s168"
version = "0.22h2" version = "0.23"
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -135,6 +135,9 @@ class ByteVec(private val initCap: Int = 0): Vec<Byte>, ByteBatchSequence {
array[index] = value array[index] = value
} }
override fun idx(value: Byte): Int =
array.indexOf(value)
companion object { companion object {
fun from(bytes: ByteArray) = fun from(bytes: ByteArray) =
ByteVec(bytes.size).also { ByteVec(bytes.size).also {

View File

@@ -135,6 +135,9 @@ class CharVec(private val initCap: Int = 0): Vec<Char>, BatchSequence<Char> {
array[index] = value array[index] = value
} }
override fun idx(value: Char): Int =
array.indexOf(value)
companion object { companion object {
fun from(data: String) = fun from(data: String) =
CharVec(data.length).also { CharVec(data.length).also {

View File

@@ -132,6 +132,9 @@ class IntVec(private val initCap: Int = 0): Vec<Int>, BatchSequence<Int> {
array[index] = value array[index] = value
} }
override fun idx(value: Int): Int =
array.indexOf(value)
companion object { companion object {
fun from(bytes: IntArray) = fun from(bytes: IntArray) =
IntVec(bytes.size).also { IntVec(bytes.size).also {

View File

@@ -0,0 +1,51 @@
package blitz.collections
class LightCache<K: Any, V>(
private val keys: Vec<K>,
private val vals: Vec<V>,
) {
private var lastKey: K? = null
private var lastVal: V? = null
@Suppress("UNCHECKED_CAST")
fun getOrPut(key: K, compute: (K) -> V): V {
if (key == lastKey)
return (lastVal as V)
val idx = keys.indexOf(key)
val v = if (idx >= 0) {
vals[idx]
} else {
val x = compute(key)
keys.pushBack(key)
vals.pushBack(x)
x
}
lastKey = key
lastVal = v
return v
}
internal fun getOrNullInternal(key: K): V? {
if (key == lastKey)
return lastVal
val idx = keys.indexOf(key)
return if (idx >= 0) {
lastKey = key
lastVal = vals[idx]
lastVal
} else {
null
}
}
companion object {
inline fun <reified K: Any, reified V> new(initCap: Int = 0): LightCache<K, V> =
LightCache(
SmartVec<K>(initCap),
SmartVec<V>(initCap),
)
}
}
fun <K: Any, V: Any> LightCache<K, V>.getOrNull(key: K): V? =
getOrNullInternal(key)

View File

@@ -132,6 +132,9 @@ class LongVec(private val initCap: Int = 0): Vec<Long>, BatchSequence<Long> {
array[index] = value array[index] = value
} }
override fun idx(value: Long): Int =
array.indexOf(value)
companion object { companion object {
fun from(bytes: LongArray) = fun from(bytes: LongArray) =
LongVec(bytes.size).also { LongVec(bytes.size).also {

View File

@@ -101,6 +101,9 @@ class RefVec<T>(private val initCap: Int = 0): Vec<T> {
inline fun <R> map(fn: (T) -> R): MutableList<R> = inline fun <R> map(fn: (T) -> R): MutableList<R> =
MutableList(size) { fn(this[it]) } MutableList(size) { fn(this[it]) }
override fun idx(value: T): Int =
_array?.indexOf(value) ?: -1
companion object { companion object {
fun <T> from(data: Array<T>) = fun <T> from(data: Array<T>) =
RefVec<T>(data.size).also { RefVec<T>(data.size).also {

View File

@@ -41,4 +41,39 @@ fun <T> Sequence<T>.limit(len: Int): Sequence<T> =
} }
fun <A, B> IndexableSequence<A>.limitBy(other: Sequence<B>): IndexableSequence<A> = fun <A, B> IndexableSequence<A>.limitBy(other: Sequence<B>): IndexableSequence<A> =
modifier { it.limitBy(other) } modifier { it.limitBy(other) }
fun <T> Sequence<T>.hasLeast(n: Int): Boolean {
val i = iterator()
repeat(n) {
if (!i.hasNext())
return false
i.next()
}
return true
}
/** cache already computed values across iterations */
fun <T> Sequence<T>.caching(): Sequence<T> =
object : Sequence<T> {
val cache = RefVec<T>()
val iter = this@caching.iterator()
override fun iterator() = object : Iterator<T> {
var idx = 0
override fun hasNext(): Boolean =
idx < cache.size || iter.hasNext()
override fun next(): T {
val v = if (idx < cache.size) {
cache[idx]
} else {
iter.next()
.also(cache::pushBack)
}
idx ++
return v
}
}
}

View File

@@ -132,6 +132,9 @@ class ShortVec(private val initCap: Int = 0): Vec<Short>, BatchSequence<Short> {
array[index] = value array[index] = value
} }
override fun idx(value: Short): Int =
array.indexOf(value)
companion object { companion object {
fun from(bytes: ShortArray) = fun from(bytes: ShortArray) =
ShortVec(bytes.size).also { ShortVec(bytes.size).also {

View File

@@ -37,4 +37,12 @@ interface Vec<T>: IndexableSequence<T> {
operator fun set(index: Int, value: T) operator fun set(index: Int, value: T)
fun clear() fun clear()
fun idx(value: T): Int {
for (i in 0 until size) {
if (this[i] == value)
return i
}
return -1
}
} }