refactor
This commit is contained in:
35
gradlew
vendored
35
gradlew
vendored
@@ -55,7 +55,7 @@
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (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.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
@@ -80,13 +80,11 @@ do
|
||||
esac
|
||||
done
|
||||
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit
|
||||
|
||||
APP_NAME="Gradle"
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
@@ -133,22 +131,29 @@ location of your Java installation."
|
||||
fi
|
||||
else
|
||||
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
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
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 ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | 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" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
@@ -193,11 +198,15 @@ if "$cygwin" || "$msys" ; then
|
||||
done
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command;
|
||||
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
|
||||
# shell script including quotes and variable substitutions, so put them in
|
||||
# double quotes to make sure that they get re-expanded; and
|
||||
# * put everything else in single quotes, so that it's not re-expanded.
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||
|
||||
# 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 -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
|
1
gradlew.bat
vendored
1
gradlew.bat
vendored
@@ -26,6 +26,7 @@ if "%OS%"=="Windows_NT" setlocal
|
||||
|
||||
set DIRNAME=%~dp0
|
||||
if "%DIRNAME%"=="" set DIRNAME=.
|
||||
@rem This is normally unused
|
||||
set APP_BASE_NAME=%~n0
|
||||
set APP_HOME=%DIRNAME%
|
||||
|
||||
|
@@ -43,12 +43,12 @@ class Either<A, B> private constructor(
|
||||
|
||||
companion object {
|
||||
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> =
|
||||
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()
|
@@ -1,5 +1,7 @@
|
||||
package blitz
|
||||
|
||||
import blitz.collections.contents
|
||||
import blitz.collections.easyMappingSequence
|
||||
import blitz.func.*
|
||||
import blitz.func.io.*
|
||||
|
||||
|
@@ -1,11 +1,39 @@
|
||||
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>? =
|
||||
this?.v?.let { Obj(transform(it)) }
|
||||
this?.v?.let { Obj.of(transform(it)) }
|
||||
|
||||
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 }
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
package blitz
|
||||
|
||||
import blitz.collections.selfInitializingSequence
|
||||
|
||||
class OperationChain<I, O> private constructor(
|
||||
private val impl: Impl = Impl()
|
||||
) {
|
||||
|
11
src/main/kotlin/blitz/async/Lock.kt
Normal file
11
src/main/kotlin/blitz/async/Lock.kt
Normal 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)
|
||||
}
|
@@ -1,128 +1,128 @@
|
||||
package blitz
|
||||
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
interface BatchIterator<T>: Iterator<T> {
|
||||
fun next(limit: Int): List<T>
|
||||
fun next(dest: Array<T>): Int
|
||||
fun next(dest: MutableList<T>, limit: Int)
|
||||
}
|
||||
|
||||
interface ByteBatchIterator: BatchIterator<Byte> {
|
||||
fun nextBytes(limit: Int): ByteArray
|
||||
|
||||
fun nextBytes(dest: ByteArray): Int
|
||||
}
|
||||
|
||||
interface BatchSequence<T>: Sequence<T> {
|
||||
override fun iterator(): BatchIterator<T>
|
||||
}
|
||||
|
||||
interface ByteBatchSequence: BatchSequence<Byte> {
|
||||
override fun iterator(): ByteBatchIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Batches all get operations on the sequence.
|
||||
*/
|
||||
fun <T> BatchSequence<T>.batched(count: Int): BatchSequence<T> =
|
||||
object : BatchSequence<T> {
|
||||
inner class Iter: BatchIterator<T> {
|
||||
val parent = this@batched.iterator()
|
||||
|
||||
var batch = mutableListOf<T>()
|
||||
|
||||
override fun next(limit: Int): List<T> {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(limit, batch.size)
|
||||
val ret = batch.take(c)
|
||||
batch.removeFirst(c)
|
||||
return ret
|
||||
}
|
||||
|
||||
override fun next(dest: MutableList<T>, limit: Int) {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(limit, batch.size)
|
||||
dest.addAll(batch.subList(0, max(0, c-1)))
|
||||
batch.removeFirst(c)
|
||||
return
|
||||
}
|
||||
|
||||
override fun next(dest: Array<T>): Int {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(dest.size, batch.size)
|
||||
batch.subList(0, max(0, c-1)).forEachIndexed { i, t ->
|
||||
dest[i] = t
|
||||
}
|
||||
batch.removeFirst(c)
|
||||
return c
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
val v = batch.first()
|
||||
batch.removeFirst()
|
||||
return v
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
while (batch.isEmpty()) {
|
||||
if (!parent.hasNext())
|
||||
return false
|
||||
parent.next(batch, count)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): BatchIterator<T> =
|
||||
Iter()
|
||||
}
|
||||
|
||||
fun <T> Sequence<T>.asBatch(): BatchSequence<T> =
|
||||
object : BatchSequence<T> {
|
||||
inner class Iter: BatchIterator<T> {
|
||||
var iter = this@asBatch.iterator()
|
||||
|
||||
override fun next(limit: Int): List<T> =
|
||||
mutableListOf<T>()
|
||||
.also { next(it, limit) }
|
||||
|
||||
override fun next(dest: MutableList<T>, limit: Int) {
|
||||
for (i in 0..<limit) {
|
||||
if (!iter.hasNext())
|
||||
break
|
||||
dest.add(iter.next())
|
||||
}
|
||||
}
|
||||
|
||||
override fun next(dest: Array<T>): Int {
|
||||
var i = 0
|
||||
while (i < dest.size) {
|
||||
if (!iter.hasNext())
|
||||
break
|
||||
dest[i ++] = iter.next()
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
return iter.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iter.hasNext()
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): BatchIterator<T> =
|
||||
Iter()
|
||||
package blitz.collections
|
||||
|
||||
import kotlin.math.max
|
||||
import kotlin.math.min
|
||||
|
||||
interface BatchIterator<T>: Iterator<T> {
|
||||
fun next(limit: Int): List<T>
|
||||
fun next(dest: Array<T>): Int
|
||||
fun next(dest: MutableList<T>, limit: Int)
|
||||
}
|
||||
|
||||
interface ByteBatchIterator: BatchIterator<Byte> {
|
||||
fun nextBytes(limit: Int): ByteArray
|
||||
|
||||
fun nextBytes(dest: ByteArray): Int
|
||||
}
|
||||
|
||||
interface BatchSequence<T>: Sequence<T> {
|
||||
override fun iterator(): BatchIterator<T>
|
||||
}
|
||||
|
||||
interface ByteBatchSequence: BatchSequence<Byte> {
|
||||
override fun iterator(): ByteBatchIterator
|
||||
}
|
||||
|
||||
/**
|
||||
* Batches all get operations on the sequence.
|
||||
*/
|
||||
fun <T> BatchSequence<T>.batched(count: Int): BatchSequence<T> =
|
||||
object : BatchSequence<T> {
|
||||
inner class Iter: BatchIterator<T> {
|
||||
val parent = this@batched.iterator()
|
||||
|
||||
var batch = mutableListOf<T>()
|
||||
|
||||
override fun next(limit: Int): List<T> {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(limit, batch.size)
|
||||
val ret = batch.take(c)
|
||||
batch.removeFirst(c)
|
||||
return ret
|
||||
}
|
||||
|
||||
override fun next(dest: MutableList<T>, limit: Int) {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(limit, batch.size)
|
||||
dest.addAll(batch.subList(0, max(0, c-1)))
|
||||
batch.removeFirst(c)
|
||||
return
|
||||
}
|
||||
|
||||
override fun next(dest: Array<T>): Int {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
|
||||
val c = min(dest.size, batch.size)
|
||||
batch.subList(0, max(0, c-1)).forEachIndexed { i, t ->
|
||||
dest[i] = t
|
||||
}
|
||||
batch.removeFirst(c)
|
||||
return c
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
if (!hasNext())
|
||||
throw Exception("no next")
|
||||
val v = batch.first()
|
||||
batch.removeFirst()
|
||||
return v
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
while (batch.isEmpty()) {
|
||||
if (!parent.hasNext())
|
||||
return false
|
||||
parent.next(batch, count)
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): BatchIterator<T> =
|
||||
Iter()
|
||||
}
|
||||
|
||||
fun <T> Sequence<T>.asBatch(): BatchSequence<T> =
|
||||
object : BatchSequence<T> {
|
||||
inner class Iter: BatchIterator<T> {
|
||||
var iter = this@asBatch.iterator()
|
||||
|
||||
override fun next(limit: Int): List<T> =
|
||||
mutableListOf<T>()
|
||||
.also { next(it, limit) }
|
||||
|
||||
override fun next(dest: MutableList<T>, limit: Int) {
|
||||
for (i in 0..<limit) {
|
||||
if (!iter.hasNext())
|
||||
break
|
||||
dest.add(iter.next())
|
||||
}
|
||||
}
|
||||
|
||||
override fun next(dest: Array<T>): Int {
|
||||
var i = 0
|
||||
while (i < dest.size) {
|
||||
if (!iter.hasNext())
|
||||
break
|
||||
dest[i ++] = iter.next()
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
override fun next(): T {
|
||||
return iter.next()
|
||||
}
|
||||
|
||||
override fun hasNext(): Boolean {
|
||||
return iter.hasNext()
|
||||
}
|
||||
}
|
||||
|
||||
override fun iterator(): BatchIterator<T> =
|
||||
Iter()
|
||||
}
|
@@ -1,10 +1,10 @@
|
||||
package blitz
|
||||
|
||||
fun ByteBatchSequence.stringify(batch: Int = 64): Sequence<String> {
|
||||
val iter = iterator()
|
||||
return generateSequence {
|
||||
if (iter.hasNext())
|
||||
iter.nextBytes(batch).decodeToString()
|
||||
else null
|
||||
}
|
||||
package blitz.collections
|
||||
|
||||
fun ByteBatchSequence.stringify(batch: Int = 64): Sequence<String> {
|
||||
val iter = iterator()
|
||||
return generateSequence {
|
||||
if (iter.hasNext())
|
||||
iter.nextBytes(batch).decodeToString()
|
||||
else null
|
||||
}
|
||||
}
|
@@ -1,52 +1,51 @@
|
||||
package blitz
|
||||
|
||||
class Contents<T> internal constructor(
|
||||
private val iterable: Iterable<T>
|
||||
): Iterable<T> {
|
||||
override fun iterator(): Iterator<T> =
|
||||
iterable.iterator()
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Contents<*>)
|
||||
return false
|
||||
|
||||
val it1 = this.iterable.iterator()
|
||||
val it2 = other.iterable.iterator()
|
||||
|
||||
while (true) {
|
||||
val hasNext1 = it1.hasNext()
|
||||
val hasNext2 = it2.hasNext()
|
||||
|
||||
if ((hasNext1 && !hasNext2) || (hasNext2 && !hasNext1))
|
||||
return false
|
||||
|
||||
if (!hasNext1)
|
||||
return true
|
||||
|
||||
if (it1.next() != it2.next())
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override fun hashCode(): Int =
|
||||
iterable.hashCode()
|
||||
|
||||
override fun toString(): String =
|
||||
joinToString(
|
||||
separator = ", ",
|
||||
prefix = "[",
|
||||
postfix = "]"
|
||||
) {
|
||||
it.toString()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
val <T> Iterable<T>.contents get() =
|
||||
Contents(this)
|
||||
|
||||
val <T> Sequence<T>.contents get() =
|
||||
Contents(this.asIterable())
|
||||
|
||||
val <T> Array<T>.contents get() =
|
||||
package blitz.collections
|
||||
|
||||
class Contents<T> internal constructor(
|
||||
private val iterable: Iterable<T>
|
||||
): Iterable<T> {
|
||||
override fun iterator(): Iterator<T> =
|
||||
iterable.iterator()
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other !is Contents<*>)
|
||||
return false
|
||||
|
||||
val it1 = this.iterable.iterator()
|
||||
val it2 = other.iterable.iterator()
|
||||
|
||||
while (true) {
|
||||
val hasNext1 = it1.hasNext()
|
||||
val hasNext2 = it2.hasNext()
|
||||
|
||||
if ((hasNext1 && !hasNext2) || (hasNext2 && !hasNext1))
|
||||
return false
|
||||
|
||||
if (!hasNext1)
|
||||
return true
|
||||
|
||||
if (it1.next() != it2.next())
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
override fun hashCode(): Int =
|
||||
iterable.hashCode()
|
||||
|
||||
override fun toString(): String =
|
||||
joinToString(
|
||||
separator = ", ",
|
||||
prefix = "[",
|
||||
postfix = "]"
|
||||
) {
|
||||
it.toString()
|
||||
}
|
||||
}
|
||||
|
||||
val <T> Iterable<T>.contents get() =
|
||||
Contents(this)
|
||||
|
||||
val <T> Sequence<T>.contents get() =
|
||||
Contents(this.asIterable())
|
||||
|
||||
val <T> Array<T>.contents get() =
|
||||
Contents(this.asIterable())
|
@@ -1,13 +1,13 @@
|
||||
package blitz
|
||||
|
||||
fun <T> MutableList<T>.removeFirst(count: Int) {
|
||||
repeat(count) {
|
||||
removeFirst()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> MutableList<T>.removeLast(count: Int) {
|
||||
repeat(count) {
|
||||
removeLast()
|
||||
}
|
||||
package blitz.collections
|
||||
|
||||
fun <T> MutableList<T>.removeFirst(count: Int) {
|
||||
repeat(count) {
|
||||
removeFirst()
|
||||
}
|
||||
}
|
||||
|
||||
fun <T> MutableList<T>.removeLast(count: Int) {
|
||||
repeat(count) {
|
||||
removeLast()
|
||||
}
|
||||
}
|
@@ -1,42 +1,42 @@
|
||||
package blitz
|
||||
|
||||
interface IndexableSequence<T>: Sequence<T> {
|
||||
operator fun get(index: Int): T
|
||||
}
|
||||
|
||||
fun <T> IndexableSequence<T>.modifier(mod: (Sequence<T>) -> Sequence<T>) =
|
||||
object : IndexableSequence<T> {
|
||||
val other = mod(this@modifier)
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
other.iterator()
|
||||
|
||||
override fun get(index: Int): T =
|
||||
this@modifier[index]
|
||||
}
|
||||
|
||||
fun <T> Sequence<T>.asIndexable(): IndexableSequence<T> =
|
||||
object : IndexableSequence<T> {
|
||||
val iter = this@asIndexable.iterator()
|
||||
val values = mutableListOf<T>()
|
||||
|
||||
override fun get(index: Int): T {
|
||||
if (index >= values.size) {
|
||||
repeat(index + 1 - values.size) {
|
||||
values.add(iter.next())
|
||||
}
|
||||
}
|
||||
return values[index]
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
object : Iterator<T> {
|
||||
var i = 0
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
i < values.size || iter.hasNext()
|
||||
|
||||
override fun next(): T =
|
||||
get(i ++)
|
||||
}
|
||||
package blitz.collections
|
||||
|
||||
interface IndexableSequence<T>: Sequence<T> {
|
||||
operator fun get(index: Int): T
|
||||
}
|
||||
|
||||
fun <T> IndexableSequence<T>.modifier(mod: (Sequence<T>) -> Sequence<T>) =
|
||||
object : IndexableSequence<T> {
|
||||
val other = mod(this@modifier)
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
other.iterator()
|
||||
|
||||
override fun get(index: Int): T =
|
||||
this@modifier[index]
|
||||
}
|
||||
|
||||
fun <T> Sequence<T>.asIndexable(): IndexableSequence<T> =
|
||||
object : IndexableSequence<T> {
|
||||
val iter = this@asIndexable.iterator()
|
||||
val values = mutableListOf<T>()
|
||||
|
||||
override fun get(index: Int): T {
|
||||
if (index >= values.size) {
|
||||
repeat(index + 1 - values.size) {
|
||||
values.add(iter.next())
|
||||
}
|
||||
}
|
||||
return values[index]
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
object : Iterator<T> {
|
||||
var i = 0
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
i < values.size || iter.hasNext()
|
||||
|
||||
override fun next(): T =
|
||||
get(i ++)
|
||||
}
|
||||
}
|
@@ -1,75 +1,77 @@
|
||||
package blitz
|
||||
|
||||
import kotlin.math.max
|
||||
|
||||
fun <T> lazySequence(vararg init: Pair<Int, T>, default: Obj<T>?, f: (Int, (Int) -> T) -> T): IndexableSequence<T> =
|
||||
object : IndexableSequence<T> {
|
||||
val map = mutableMapOf(*init)
|
||||
|
||||
var current: Int? = null
|
||||
|
||||
fun comp(iIn: Int): T {
|
||||
val i = max(0, iIn)
|
||||
if (current == i)
|
||||
return (default ?: throw Exception("recursion detected")).v
|
||||
return map[i] ?: let {
|
||||
current = i
|
||||
val res = f(i, ::comp)
|
||||
map[i] = res
|
||||
current = null
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
override fun get(index: Int) = comp(index)
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
object : Iterator<T> {
|
||||
override fun hasNext() = true
|
||||
|
||||
private var i = 0
|
||||
|
||||
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 ->
|
||||
var indexC = index
|
||||
var v: T? = null
|
||||
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?
|
||||
): Sequence<T?> {
|
||||
val indexable = this.asIndexable()
|
||||
return easySequence(*init) { i, ff ->
|
||||
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)
|
||||
|
||||
inner class Iter : Iterator<T> {
|
||||
val iter = seq.iterator()
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
iter.hasNext()
|
||||
|
||||
override fun next(): T =
|
||||
iter.next()
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
Iter()
|
||||
package blitz.collections
|
||||
|
||||
import blitz.Obj
|
||||
import blitz.Provider
|
||||
import kotlin.math.max
|
||||
|
||||
fun <T> lazySequence(vararg init: Pair<Int, T>, default: Obj<T>?, f: (Int, (Int) -> T) -> T): IndexableSequence<T> =
|
||||
object : IndexableSequence<T> {
|
||||
val map = mutableMapOf(*init)
|
||||
|
||||
var current: Int? = null
|
||||
|
||||
fun comp(iIn: Int): T {
|
||||
val i = max(0, iIn)
|
||||
if (current == i)
|
||||
return (default ?: throw Exception("recursion detected")).v
|
||||
return map[i] ?: let {
|
||||
current = i
|
||||
val res = f(i, ::comp)
|
||||
map[i] = res
|
||||
current = null
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
override fun get(index: Int) = comp(index)
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
object : Iterator<T> {
|
||||
override fun hasNext() = true
|
||||
|
||||
private var i = 0
|
||||
|
||||
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.of(null)) { i, ff ->
|
||||
f(i) { index ->
|
||||
var indexC = index
|
||||
var v: T? = null
|
||||
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?
|
||||
): Sequence<T?> {
|
||||
val indexable = this.asIndexable()
|
||||
return easySequence(*init) { i, ff ->
|
||||
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)
|
||||
|
||||
inner class Iter : Iterator<T> {
|
||||
val iter = seq.iterator()
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
iter.hasNext()
|
||||
|
||||
override fun next(): T =
|
||||
iter.next()
|
||||
}
|
||||
|
||||
override fun iterator(): Iterator<T> =
|
||||
Iter()
|
||||
}
|
@@ -1,25 +1,25 @@
|
||||
package blitz
|
||||
|
||||
fun <T> Sequence<T>.removeNull(): Sequence<T> =
|
||||
mapNotNull { it }
|
||||
|
||||
fun <T> IndexableSequence<T>.removeNull(): IndexableSequence<T> =
|
||||
modifier { it.removeNull() }
|
||||
|
||||
fun <A, B> Sequence<A>.limitBy(other: Sequence<B>): Sequence<A> =
|
||||
object : Sequence<A> {
|
||||
override fun iterator(): Iterator<A> =
|
||||
object : Iterator<A> {
|
||||
val s = this@limitBy.iterator()
|
||||
val o = other.iterator()
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
o.hasNext() && s.hasNext()
|
||||
|
||||
override fun next(): A =
|
||||
s.next().also { o.next() }
|
||||
}
|
||||
}
|
||||
|
||||
fun <A, B> IndexableSequence<A>.limitBy(other: Sequence<B>): IndexableSequence<A> =
|
||||
package blitz.collections
|
||||
|
||||
fun <T> Sequence<T>.removeNull(): Sequence<T> =
|
||||
mapNotNull { it }
|
||||
|
||||
fun <T> IndexableSequence<T>.removeNull(): IndexableSequence<T> =
|
||||
modifier { it.removeNull() }
|
||||
|
||||
fun <A, B> Sequence<A>.limitBy(other: Sequence<B>): Sequence<A> =
|
||||
object : Sequence<A> {
|
||||
override fun iterator(): Iterator<A> =
|
||||
object : Iterator<A> {
|
||||
val s = this@limitBy.iterator()
|
||||
val o = other.iterator()
|
||||
|
||||
override fun hasNext(): Boolean =
|
||||
o.hasNext() && s.hasNext()
|
||||
|
||||
override fun next(): A =
|
||||
s.next().also { o.next() }
|
||||
}
|
||||
}
|
||||
|
||||
fun <A, B> IndexableSequence<A>.limitBy(other: Sequence<B>): IndexableSequence<A> =
|
||||
modifier { it.limitBy(other) }
|
@@ -1,7 +1,7 @@
|
||||
package blitz.func
|
||||
|
||||
import blitz.ByteBatchSequence
|
||||
import blitz.stringify
|
||||
import blitz.collections.ByteBatchSequence
|
||||
import blitz.collections.stringify
|
||||
|
||||
fun <T> Monad<Sequence<Sequence<T>>>.flatten(): Monad<Sequence<T>> =
|
||||
bind { it.flatten() }
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package blitz.func.io
|
||||
|
||||
import blitz.*
|
||||
import blitz.collections.ByteBatchSequence
|
||||
import blitz.func.*
|
||||
import blitz.io.*
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
package blitz.io
|
||||
|
||||
import blitz.ByteBatchSequence
|
||||
import blitz.collections.ByteBatchSequence
|
||||
import kotlinx.io.Buffer
|
||||
import kotlinx.io.files.SystemFileSystem
|
||||
|
||||
|
@@ -2,8 +2,8 @@ package blitz.io
|
||||
|
||||
import kotlinx.io.RawSource
|
||||
import kotlinx.io.buffered
|
||||
import blitz.ByteBatchIterator
|
||||
import blitz.ByteBatchSequence
|
||||
import blitz.collections.ByteBatchIterator
|
||||
import blitz.collections.ByteBatchSequence
|
||||
import blitz.Provider
|
||||
|
||||
fun Provider<RawSource>.readerSequence(): ByteBatchSequence =
|
||||
|
Reference in New Issue
Block a user