more stuff; v0.14

This commit is contained in:
alex-s168
2024-04-12 21:27:47 +02:00
parent 469d5fcb8c
commit b212ca8902
7 changed files with 61 additions and 5 deletions

View File

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

View File

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

View File

@@ -0,0 +1,12 @@
package blitz.collections
import kotlin.math.min
fun Sequence<Int>.movingAverage(factor: Double): Sequence<Double> {
var avg = 0.0
return mapIndexed { index, v->
val count = index + 1
avg += (v - avg) / min(count.toDouble(), factor)
avg
}
}

View File

@@ -20,6 +20,13 @@ class Matrix<T>(
rows[y][x] = value
}
operator fun get(pos: Pair<Int, Int>): T =
rows[pos.second][pos.first]
operator fun set(pos: Pair<Int, Int>, value: T) {
rows[pos.second][pos.first] = value
}
fun column(col: Int): IndexableSequence<T> =
generateSequenceWithIndex(height) { row -> this[col, row] }
@@ -83,6 +90,9 @@ class Matrix<T>(
Matrix(w, h) { sx, sy -> this[x * w + sx, y * w + sy] }
}
fun randomPos(): Pair<Int, Int> =
(0..<width).random() to (0..<height).random()
fun toString(alignRight: Boolean): String {
val str = stringMat()
val widths = str.columnWidths()
@@ -117,4 +127,8 @@ fun Matrix<String>.columnWidths(): IntArray =
IntArray(width) { col -> column(col).maxOf { it.length } }
fun Matrix<String>.lengths(): Matrix<Int> =
Matrix(width, height) { x, y -> this[x, y].length }
Matrix(width, height) { x, y -> this[x, y].length }
operator fun Matrix<Float>.plusAssign(other: Matrix<Float>) {
fill { x, y -> this[x, y] + other[x, y] }
}

View File

@@ -21,5 +21,24 @@ fun <A, B> Sequence<A>.limitBy(other: Sequence<B>): Sequence<A> =
}
}
fun <T> Sequence<T>.limit(len: Int): Sequence<T> =
object : Sequence<T> {
override fun iterator() =
object : Iterator<T> {
val iter = this@limit.iterator()
var i = 0
override fun hasNext(): Boolean =
i < len && iter.hasNext()
override fun next(): T {
if (!hasNext())
error("No next")
i ++
return iter.next()
}
}
}
fun <A, B> IndexableSequence<A>.limitBy(other: Sequence<B>): IndexableSequence<A> =
modifier { it.limitBy(other) }

View File

@@ -30,8 +30,9 @@ value class Path(
fun exists(): Boolean =
SystemFileSystem.exists(toKotlinxPath())
internal fun toKotlinxPath() =
kotlinx.io.files.Path("", *parts)
fun toKotlinxPath() =
if (separator == '/') kotlinx.io.files.Path("/", *parts)
else kotlinx.io.files.Path("", *parts)
private fun kotlinxMeta() =
SystemFileSystem.metadataOrNull(toKotlinxPath())

View File

@@ -0,0 +1,10 @@
package blitz.math
infix fun Pair<Int, Int>.lerpi(t: Double): Double =
(1.0f - t) * first + second * t
infix fun Pair<Double, Double>.lerpd(t: Double): Double =
(1.0f - t) * first + second * t
infix fun Pair<Float, Float>.lerpf(t: Double): Double =
(1.0f - t) * first + second * t