This commit is contained in:
alexander.nutz
2024-03-25 09:19:27 +01:00
parent b3150fd948
commit 6e9300fac6
18 changed files with 418 additions and 364 deletions

35
gradlew vendored
View File

@@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop. # Darwin, MinGW, and NonStop.
# #
# (3) This script is generated from the Groovy template # (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project. # within the Gradle project.
# #
# You can find Gradle at https://github.com/gradle/gradle/. # You can find Gradle at https://github.com/gradle/gradle/.
@@ -80,13 +80,11 @@ do
esac esac
done done
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit # This is normally unused
# shellcheck disable=SC2034
APP_NAME="Gradle"
APP_BASE_NAME=${0##*/} APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Use the maximum available, or set MAX_FD != -1 to use that value. # Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum MAX_FD=maximum
@@ -133,22 +131,29 @@ location of your Java installation."
fi fi
else else
JAVACMD=java JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the Please set the JAVA_HOME variable in your environment to match the
location of your Java installation." location of your Java installation."
fi
fi fi
# Increase the maximum file descriptors if we can. # Increase the maximum file descriptors if we can.
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #( case $MAX_FD in #(
max*) max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) || MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit" warn "Could not query maximum file descriptor limit"
esac esac
case $MAX_FD in #( case $MAX_FD in #(
'' | soft) :;; #( '' | soft) :;; #(
*) *)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" || ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD" warn "Could not set maximum file descriptor limit to $MAX_FD"
esac esac
@@ -193,11 +198,15 @@ if "$cygwin" || "$msys" ; then
done done
fi fi
# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
# shell script including quotes and variable substitutions, so put them in DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded. # Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.
set -- \ set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \ "-Dorg.gradle.appname=$APP_BASE_NAME" \

1
gradlew.bat vendored
View File

@@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal
set DIRNAME=%~dp0 set DIRNAME=%~dp0
if "%DIRNAME%"=="" set DIRNAME=. if "%DIRNAME%"=="" set DIRNAME=.
@rem This is normally unused
set APP_BASE_NAME=%~n0 set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME% set APP_HOME=%DIRNAME%

View File

@@ -43,12 +43,12 @@ class Either<A, B> private constructor(
companion object { companion object {
fun <A, B> ofA(a: A): Either<A, B> = fun <A, B> ofA(a: A): Either<A, B> =
Either(Obj(a), null) Either(Obj.of(a), null)
fun <A, B> ofB(b: B): Either<A, B> = fun <A, B> ofB(b: B): Either<A, B> =
Either(null, Obj(b)) Either(null, Obj.of(b))
} }
} }
fun <A, B, R> Either<A, B>.commonize(): R where A: R, B: R = fun <A, B, R> Either<A, B>.flatten(): R where A: R, B: R =
getAOrNull() ?: getB() getAOrNull() ?: getB()

View File

@@ -1,5 +1,7 @@
package blitz package blitz
import blitz.collections.contents
import blitz.collections.easyMappingSequence
import blitz.func.* import blitz.func.*
import blitz.func.io.* import blitz.func.io.*

View File

@@ -1,11 +1,39 @@
package blitz package blitz
data class Obj<T>(val v: T) import blitz.async.Lock
interface Obj<T> {
val v: T
companion object {
fun <T> of(v: T): Obj<T> =
object : Obj<T> {
override val v: T = v
}
}
}
fun <I, O> Obj<I>?.mapNotNull(transform: (I) -> O): Obj<O>? = fun <I, O> Obj<I>?.mapNotNull(transform: (I) -> O): Obj<O>? =
this?.v?.let { Obj(transform(it)) } this?.v?.let { Obj.of(transform(it)) }
fun <I, O> Obj<I>.map(transform: (I) -> O): Obj<O> = fun <I, O> Obj<I>.map(transform: (I) -> O): Obj<O> =
Obj(transform(v)) Obj.of(transform(v))
data class MutObj<T>(var v: T) interface MutObj<T> {
var v: T
companion object {
fun <T> of(v: T): MutObj<T> =
object : MutObj<T> {
override var v: T = v
}
fun <T> mutex(v: T): MutObj<T> =
object : MutObj<T> {
val lock = Lock()
override var v: T = v
get() = lock.use { field }
set(inp) = lock.use { field = inp }
}
}
}

View File

@@ -1,5 +1,7 @@
package blitz package blitz
import blitz.collections.selfInitializingSequence
class OperationChain<I, O> private constructor( class OperationChain<I, O> private constructor(
private val impl: Impl = Impl() private val impl: Impl = Impl()
) { ) {

View File

@@ -0,0 +1,11 @@
package blitz.async
@JvmInline
value class Lock private constructor(
private val impl: Any
) {
constructor(): this(Any())
fun <R> use(block: () -> R): R =
synchronized(impl, block)
}

View File

@@ -1,128 +1,128 @@
package blitz package blitz.collections
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
interface BatchIterator<T>: Iterator<T> { interface BatchIterator<T>: Iterator<T> {
fun next(limit: Int): List<T> fun next(limit: Int): List<T>
fun next(dest: Array<T>): Int fun next(dest: Array<T>): Int
fun next(dest: MutableList<T>, limit: Int) fun next(dest: MutableList<T>, limit: Int)
} }
interface ByteBatchIterator: BatchIterator<Byte> { interface ByteBatchIterator: BatchIterator<Byte> {
fun nextBytes(limit: Int): ByteArray fun nextBytes(limit: Int): ByteArray
fun nextBytes(dest: ByteArray): Int fun nextBytes(dest: ByteArray): Int
} }
interface BatchSequence<T>: Sequence<T> { interface BatchSequence<T>: Sequence<T> {
override fun iterator(): BatchIterator<T> override fun iterator(): BatchIterator<T>
} }
interface ByteBatchSequence: BatchSequence<Byte> { interface ByteBatchSequence: BatchSequence<Byte> {
override fun iterator(): ByteBatchIterator override fun iterator(): ByteBatchIterator
} }
/** /**
* Batches all get operations on the sequence. * Batches all get operations on the sequence.
*/ */
fun <T> BatchSequence<T>.batched(count: Int): BatchSequence<T> = fun <T> BatchSequence<T>.batched(count: Int): BatchSequence<T> =
object : BatchSequence<T> { object : BatchSequence<T> {
inner class Iter: BatchIterator<T> { inner class Iter: BatchIterator<T> {
val parent = this@batched.iterator() val parent = this@batched.iterator()
var batch = mutableListOf<T>() var batch = mutableListOf<T>()
override fun next(limit: Int): List<T> { override fun next(limit: Int): List<T> {
if (!hasNext()) if (!hasNext())
throw Exception("no next") throw Exception("no next")
val c = min(limit, batch.size) val c = min(limit, batch.size)
val ret = batch.take(c) val ret = batch.take(c)
batch.removeFirst(c) batch.removeFirst(c)
return ret return ret
} }
override fun next(dest: MutableList<T>, limit: Int) { override fun next(dest: MutableList<T>, limit: Int) {
if (!hasNext()) if (!hasNext())
throw Exception("no next") throw Exception("no next")
val c = min(limit, batch.size) val c = min(limit, batch.size)
dest.addAll(batch.subList(0, max(0, c-1))) dest.addAll(batch.subList(0, max(0, c-1)))
batch.removeFirst(c) batch.removeFirst(c)
return return
} }
override fun next(dest: Array<T>): Int { override fun next(dest: Array<T>): Int {
if (!hasNext()) if (!hasNext())
throw Exception("no next") throw Exception("no next")
val c = min(dest.size, batch.size) val c = min(dest.size, batch.size)
batch.subList(0, max(0, c-1)).forEachIndexed { i, t -> batch.subList(0, max(0, c-1)).forEachIndexed { i, t ->
dest[i] = t dest[i] = t
} }
batch.removeFirst(c) batch.removeFirst(c)
return c return c
} }
override fun next(): T { override fun next(): T {
if (!hasNext()) if (!hasNext())
throw Exception("no next") throw Exception("no next")
val v = batch.first() val v = batch.first()
batch.removeFirst() batch.removeFirst()
return v return v
} }
override fun hasNext(): Boolean { override fun hasNext(): Boolean {
while (batch.isEmpty()) { while (batch.isEmpty()) {
if (!parent.hasNext()) if (!parent.hasNext())
return false return false
parent.next(batch, count) parent.next(batch, count)
} }
return true return true
} }
} }
override fun iterator(): BatchIterator<T> = override fun iterator(): BatchIterator<T> =
Iter() Iter()
} }
fun <T> Sequence<T>.asBatch(): BatchSequence<T> = fun <T> Sequence<T>.asBatch(): BatchSequence<T> =
object : BatchSequence<T> { object : BatchSequence<T> {
inner class Iter: BatchIterator<T> { inner class Iter: BatchIterator<T> {
var iter = this@asBatch.iterator() var iter = this@asBatch.iterator()
override fun next(limit: Int): List<T> = override fun next(limit: Int): List<T> =
mutableListOf<T>() mutableListOf<T>()
.also { next(it, limit) } .also { next(it, limit) }
override fun next(dest: MutableList<T>, limit: Int) { override fun next(dest: MutableList<T>, limit: Int) {
for (i in 0..<limit) { for (i in 0..<limit) {
if (!iter.hasNext()) if (!iter.hasNext())
break break
dest.add(iter.next()) dest.add(iter.next())
} }
} }
override fun next(dest: Array<T>): Int { override fun next(dest: Array<T>): Int {
var i = 0 var i = 0
while (i < dest.size) { while (i < dest.size) {
if (!iter.hasNext()) if (!iter.hasNext())
break break
dest[i ++] = iter.next() dest[i ++] = iter.next()
} }
return i return i
} }
override fun next(): T { override fun next(): T {
return iter.next() return iter.next()
} }
override fun hasNext(): Boolean { override fun hasNext(): Boolean {
return iter.hasNext() return iter.hasNext()
} }
} }
override fun iterator(): BatchIterator<T> = override fun iterator(): BatchIterator<T> =
Iter() Iter()
} }

View File

@@ -1,10 +1,10 @@
package blitz package blitz.collections
fun ByteBatchSequence.stringify(batch: Int = 64): Sequence<String> { fun ByteBatchSequence.stringify(batch: Int = 64): Sequence<String> {
val iter = iterator() val iter = iterator()
return generateSequence { return generateSequence {
if (iter.hasNext()) if (iter.hasNext())
iter.nextBytes(batch).decodeToString() iter.nextBytes(batch).decodeToString()
else null else null
} }
} }

View File

@@ -1,52 +1,51 @@
package blitz package blitz.collections
class Contents<T> internal constructor( class Contents<T> internal constructor(
private val iterable: Iterable<T> private val iterable: Iterable<T>
): Iterable<T> { ): Iterable<T> {
override fun iterator(): Iterator<T> = override fun iterator(): Iterator<T> =
iterable.iterator() iterable.iterator()
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other !is Contents<*>) if (other !is Contents<*>)
return false return false
val it1 = this.iterable.iterator() val it1 = this.iterable.iterator()
val it2 = other.iterable.iterator() val it2 = other.iterable.iterator()
while (true) { while (true) {
val hasNext1 = it1.hasNext() val hasNext1 = it1.hasNext()
val hasNext2 = it2.hasNext() val hasNext2 = it2.hasNext()
if ((hasNext1 && !hasNext2) || (hasNext2 && !hasNext1)) if ((hasNext1 && !hasNext2) || (hasNext2 && !hasNext1))
return false return false
if (!hasNext1) if (!hasNext1)
return true return true
if (it1.next() != it2.next()) if (it1.next() != it2.next())
return false return false
} }
} }
override fun hashCode(): Int = override fun hashCode(): Int =
iterable.hashCode() iterable.hashCode()
override fun toString(): String = override fun toString(): String =
joinToString( joinToString(
separator = ", ", separator = ", ",
prefix = "[", prefix = "[",
postfix = "]" postfix = "]"
) { ) {
it.toString() it.toString()
} }
} }
val <T> Iterable<T>.contents get() =
val <T> Iterable<T>.contents get() = Contents(this)
Contents(this)
val <T> Sequence<T>.contents get() =
val <T> Sequence<T>.contents get() = Contents(this.asIterable())
Contents(this.asIterable())
val <T> Array<T>.contents get() =
val <T> Array<T>.contents get() =
Contents(this.asIterable()) Contents(this.asIterable())

View File

@@ -1,13 +1,13 @@
package blitz package blitz.collections
fun <T> MutableList<T>.removeFirst(count: Int) { fun <T> MutableList<T>.removeFirst(count: Int) {
repeat(count) { repeat(count) {
removeFirst() removeFirst()
} }
} }
fun <T> MutableList<T>.removeLast(count: Int) { fun <T> MutableList<T>.removeLast(count: Int) {
repeat(count) { repeat(count) {
removeLast() removeLast()
} }
} }

View File

@@ -1,42 +1,42 @@
package blitz package blitz.collections
interface IndexableSequence<T>: Sequence<T> { interface IndexableSequence<T>: Sequence<T> {
operator fun get(index: Int): T operator fun get(index: Int): T
} }
fun <T> IndexableSequence<T>.modifier(mod: (Sequence<T>) -> Sequence<T>) = fun <T> IndexableSequence<T>.modifier(mod: (Sequence<T>) -> Sequence<T>) =
object : IndexableSequence<T> { object : IndexableSequence<T> {
val other = mod(this@modifier) val other = mod(this@modifier)
override fun iterator(): Iterator<T> = override fun iterator(): Iterator<T> =
other.iterator() other.iterator()
override fun get(index: Int): T = override fun get(index: Int): T =
this@modifier[index] this@modifier[index]
} }
fun <T> Sequence<T>.asIndexable(): IndexableSequence<T> = fun <T> Sequence<T>.asIndexable(): IndexableSequence<T> =
object : IndexableSequence<T> { object : IndexableSequence<T> {
val iter = this@asIndexable.iterator() val iter = this@asIndexable.iterator()
val values = mutableListOf<T>() val values = mutableListOf<T>()
override fun get(index: Int): T { override fun get(index: Int): T {
if (index >= values.size) { if (index >= values.size) {
repeat(index + 1 - values.size) { repeat(index + 1 - values.size) {
values.add(iter.next()) values.add(iter.next())
} }
} }
return values[index] return values[index]
} }
override fun iterator(): Iterator<T> = override fun iterator(): Iterator<T> =
object : Iterator<T> { object : Iterator<T> {
var i = 0 var i = 0
override fun hasNext(): Boolean = override fun hasNext(): Boolean =
i < values.size || iter.hasNext() i < values.size || iter.hasNext()
override fun next(): T = override fun next(): T =
get(i ++) get(i ++)
} }
} }

View File

@@ -1,75 +1,77 @@
package blitz package blitz.collections
import kotlin.math.max import blitz.Obj
import blitz.Provider
fun <T> lazySequence(vararg init: Pair<Int, T>, default: Obj<T>?, f: (Int, (Int) -> T) -> T): IndexableSequence<T> = import kotlin.math.max
object : IndexableSequence<T> {
val map = mutableMapOf(*init) fun <T> lazySequence(vararg init: Pair<Int, T>, default: Obj<T>?, f: (Int, (Int) -> T) -> T): IndexableSequence<T> =
object : IndexableSequence<T> {
var current: Int? = null val map = mutableMapOf(*init)
fun comp(iIn: Int): T { var current: Int? = null
val i = max(0, iIn)
if (current == i) fun comp(iIn: Int): T {
return (default ?: throw Exception("recursion detected")).v val i = max(0, iIn)
return map[i] ?: let { if (current == i)
current = i return (default ?: throw Exception("recursion detected")).v
val res = f(i, ::comp) return map[i] ?: let {
map[i] = res current = i
current = null val res = f(i, ::comp)
res map[i] = res
} current = null
} res
}
override fun get(index: Int) = comp(index) }
override fun iterator(): Iterator<T> = override fun get(index: Int) = comp(index)
object : Iterator<T> {
override fun hasNext() = true override fun iterator(): Iterator<T> =
object : Iterator<T> {
private var i = 0 override fun hasNext() = true
override fun next(): T = private var i = 0
comp(i ++)
} override fun next(): T =
} comp(i ++)
}
fun <T> easySequence(vararg init: Pair<Int, T?>, f: (Int, (Int) -> T?) -> T?): Sequence<T?> = }
lazySequence(*init, default = Obj(null)) { i, ff ->
f(i) { index -> fun <T> easySequence(vararg init: Pair<Int, T?>, f: (Int, (Int) -> T?) -> T?): Sequence<T?> =
var indexC = index lazySequence(*init, default = Obj.of(null)) { i, ff ->
var v: T? = null f(i) { index ->
while (indexC > 0 && v == null) var indexC = index
v = ff(indexC --) var v: T? = null
v while (indexC > 0 && v == null)
} v = ff(indexC --)
} v
}
fun <I, T> Sequence<I>.easyMappingSequence( }
vararg init: Pair<Int, T?>,
f: (Int, (Int) -> T?, (Int) -> I) -> T? fun <I, T> Sequence<I>.easyMappingSequence(
): Sequence<T?> { vararg init: Pair<Int, T?>,
val indexable = this.asIndexable() f: (Int, (Int) -> T?, (Int) -> I) -> T?
return easySequence(*init) { i, ff -> ): Sequence<T?> {
f(i, ff, indexable::get) val indexable = this.asIndexable()
}.limitBy(indexable) return easySequence(*init) { i, ff ->
.removeNull() f(i, ff, indexable::get)
} }.limitBy(indexable)
.removeNull()
fun <T> selfInitializingSequence(block: Provider<Sequence<T>>): Sequence<T> = }
object : Sequence<T> {
val seq by lazy(block) fun <T> selfInitializingSequence(block: Provider<Sequence<T>>): Sequence<T> =
object : Sequence<T> {
inner class Iter : Iterator<T> { val seq by lazy(block)
val iter = seq.iterator()
inner class Iter : Iterator<T> {
override fun hasNext(): Boolean = val iter = seq.iterator()
iter.hasNext()
override fun hasNext(): Boolean =
override fun next(): T = iter.hasNext()
iter.next()
} override fun next(): T =
iter.next()
override fun iterator(): Iterator<T> = }
Iter()
override fun iterator(): Iterator<T> =
Iter()
} }

View File

@@ -1,25 +1,25 @@
package blitz package blitz.collections
fun <T> Sequence<T>.removeNull(): Sequence<T> = fun <T> Sequence<T>.removeNull(): Sequence<T> =
mapNotNull { it } mapNotNull { it }
fun <T> IndexableSequence<T>.removeNull(): IndexableSequence<T> = fun <T> IndexableSequence<T>.removeNull(): IndexableSequence<T> =
modifier { it.removeNull() } modifier { it.removeNull() }
fun <A, B> Sequence<A>.limitBy(other: Sequence<B>): Sequence<A> = fun <A, B> Sequence<A>.limitBy(other: Sequence<B>): Sequence<A> =
object : Sequence<A> { object : Sequence<A> {
override fun iterator(): Iterator<A> = override fun iterator(): Iterator<A> =
object : Iterator<A> { object : Iterator<A> {
val s = this@limitBy.iterator() val s = this@limitBy.iterator()
val o = other.iterator() val o = other.iterator()
override fun hasNext(): Boolean = override fun hasNext(): Boolean =
o.hasNext() && s.hasNext() o.hasNext() && s.hasNext()
override fun next(): A = override fun next(): A =
s.next().also { o.next() } s.next().also { o.next() }
} }
} }
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) }

View File

@@ -1,7 +1,7 @@
package blitz.func package blitz.func
import blitz.ByteBatchSequence import blitz.collections.ByteBatchSequence
import blitz.stringify import blitz.collections.stringify
fun <T> Monad<Sequence<Sequence<T>>>.flatten(): Monad<Sequence<T>> = fun <T> Monad<Sequence<Sequence<T>>>.flatten(): Monad<Sequence<T>> =
bind { it.flatten() } bind { it.flatten() }

View File

@@ -1,6 +1,6 @@
package blitz.func.io package blitz.func.io
import blitz.* import blitz.collections.ByteBatchSequence
import blitz.func.* import blitz.func.*
import blitz.io.* import blitz.io.*

View File

@@ -1,6 +1,6 @@
package blitz.io package blitz.io
import blitz.ByteBatchSequence import blitz.collections.ByteBatchSequence
import kotlinx.io.Buffer import kotlinx.io.Buffer
import kotlinx.io.files.SystemFileSystem import kotlinx.io.files.SystemFileSystem

View File

@@ -2,8 +2,8 @@ package blitz.io
import kotlinx.io.RawSource import kotlinx.io.RawSource
import kotlinx.io.buffered import kotlinx.io.buffered
import blitz.ByteBatchIterator import blitz.collections.ByteBatchIterator
import blitz.ByteBatchSequence import blitz.collections.ByteBatchSequence
import blitz.Provider import blitz.Provider
fun Provider<RawSource>.readerSequence(): ByteBatchSequence = fun Provider<RawSource>.readerSequence(): ByteBatchSequence =