diff --git a/README.md b/README.md index eb5617e..0fc6396 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ repositories { } dependencies { - implementation("me.alex_s168:blitz:0.13") + implementation("me.alex_s168:blitz:0.14") } ``` diff --git a/build.gradle.kts b/build.gradle.kts index 6c25c2c..cd3e3f7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "me.alex_s168" -version = "0.13" +version = "0.14" repositories { mavenCentral() diff --git a/src/main/kotlin/blitz/collections/Analytics.kt b/src/main/kotlin/blitz/collections/Analytics.kt new file mode 100644 index 0000000..7ed5919 --- /dev/null +++ b/src/main/kotlin/blitz/collections/Analytics.kt @@ -0,0 +1,12 @@ +package blitz.collections + +import kotlin.math.min + +fun Sequence.movingAverage(factor: Double): Sequence { + var avg = 0.0 + return mapIndexed { index, v-> + val count = index + 1 + avg += (v - avg) / min(count.toDouble(), factor) + avg + } +} \ No newline at end of file diff --git a/src/main/kotlin/blitz/collections/Matrix.kt b/src/main/kotlin/blitz/collections/Matrix.kt index 6afb32d..d0cc4e1 100644 --- a/src/main/kotlin/blitz/collections/Matrix.kt +++ b/src/main/kotlin/blitz/collections/Matrix.kt @@ -20,6 +20,13 @@ class Matrix( rows[y][x] = value } + operator fun get(pos: Pair): T = + rows[pos.second][pos.first] + + operator fun set(pos: Pair, value: T) { + rows[pos.second][pos.first] = value + } + fun column(col: Int): IndexableSequence = generateSequenceWithIndex(height) { row -> this[col, row] } @@ -83,6 +90,9 @@ class Matrix( Matrix(w, h) { sx, sy -> this[x * w + sx, y * w + sy] } } + fun randomPos(): Pair = + (0...columnWidths(): IntArray = IntArray(width) { col -> column(col).maxOf { it.length } } fun Matrix.lengths(): Matrix = - Matrix(width, height) { x, y -> this[x, y].length } \ No newline at end of file + Matrix(width, height) { x, y -> this[x, y].length } + +operator fun Matrix.plusAssign(other: Matrix) { + fill { x, y -> this[x, y] + other[x, y] } +} \ No newline at end of file diff --git a/src/main/kotlin/blitz/collections/SequenceOps.kt b/src/main/kotlin/blitz/collections/SequenceOps.kt index 2282b13..7cabe6c 100644 --- a/src/main/kotlin/blitz/collections/SequenceOps.kt +++ b/src/main/kotlin/blitz/collections/SequenceOps.kt @@ -21,5 +21,24 @@ fun Sequence.limitBy(other: Sequence): Sequence = } } +fun Sequence.limit(len: Int): Sequence = + object : Sequence { + override fun iterator() = + object : Iterator { + 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 IndexableSequence.limitBy(other: Sequence): IndexableSequence = modifier { it.limitBy(other) } \ No newline at end of file diff --git a/src/main/kotlin/blitz/io/Path.kt b/src/main/kotlin/blitz/io/Path.kt index aea26ce..4aeb324 100644 --- a/src/main/kotlin/blitz/io/Path.kt +++ b/src/main/kotlin/blitz/io/Path.kt @@ -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()) diff --git a/src/main/kotlin/blitz/math/Lerp.kt b/src/main/kotlin/blitz/math/Lerp.kt new file mode 100644 index 0000000..2aa353c --- /dev/null +++ b/src/main/kotlin/blitz/math/Lerp.kt @@ -0,0 +1,10 @@ +package blitz.math + +infix fun Pair.lerpi(t: Double): Double = + (1.0f - t) * first + second * t + +infix fun Pair.lerpd(t: Double): Double = + (1.0f - t) * first + second * t + +infix fun Pair.lerpf(t: Double): Double = + (1.0f - t) * first + second * t