Add mutating operations to streamy iterators

This commit is contained in:
DBotThePony 2023-08-03 19:41:58 +07:00
parent 5e7c8c08f5
commit 9ae099ec23
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 49 additions and 14 deletions

View File

@ -20,6 +20,8 @@ import ru.dbotthepony.mc.otm.container.MatteryContainer
import ru.dbotthepony.mc.otm.container.HandlerFilter
import ru.dbotthepony.mc.otm.container.UpgradeContainer
import ru.dbotthepony.mc.otm.container.balance
import ru.dbotthepony.mc.otm.core.collect.filter
import ru.dbotthepony.mc.otm.core.collect.findAny
import ru.dbotthepony.mc.otm.menu.tech.PlatePressMenu
import ru.dbotthepony.mc.otm.menu.tech.TwinPlatePressMenu
import ru.dbotthepony.mc.otm.registry.MBlockEntities
@ -88,10 +90,10 @@ class PlatePressBlockEntity(
val recipe = level.recipeManager
.byType(MRecipes.PLATE_PRESS)
.values
.stream()
.iterator()
.filter { it.matches(inputContainer, id) }
.findAny().
orElse(null) ?: return JobContainer.noItem()
.findAny()
.orElse(null) ?: return JobContainer.noItem()
val toProcess = inputContainer[id].count.coerceAtMost(1 + upgrades.processingItems)

View File

@ -14,7 +14,7 @@ import java.util.stream.Collector
// Aside parallel work, unimplemented Stream API elements can be easily implemented when required
class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>) : Iterator<T> {
class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate: Predicate<in T>) : MutableIterator<T> {
private var foundValue: Any? = Companion
override fun hasNext(): Boolean {
@ -54,10 +54,19 @@ class FilteredIterator<T>(private val parent: Iterator<T>, private val predicate
return foundValue as T
}
override fun remove() {
if (foundValue === Companion) {
throw NoSuchElementException()
}
(parent as MutableIterator<T>).remove()
foundValue = Companion
}
private companion object
}
class MappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> R) : Iterator<R> {
class MappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> R) : MutableIterator<R> {
override fun hasNext(): Boolean {
return parent.hasNext()
}
@ -65,10 +74,15 @@ class MappingIterator<T, R>(private val parent: Iterator<T>, private val transfo
override fun next(): R {
return transform.invoke(parent.next())
}
override fun remove() {
(parent as MutableIterator<T>).remove()
}
}
class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> Iterator<R>) : Iterator<R> {
class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val transform: (T) -> Iterator<R>) : MutableIterator<R> {
private var current: Iterator<R>? = null
private var last: Iterator<R>? = null
override fun hasNext(): Boolean {
while (current?.hasNext() != true && parent.hasNext()) {
@ -82,7 +96,14 @@ class FlatMappingIterator<T, R>(private val parent: Iterator<T>, private val tra
if (!hasNext())
throw NoSuchElementException()
return current!!.next()
val v = current!!.next()
last = current
return v
}
override fun remove() {
(last as MutableIterator<R>? ?: throw NoSuchElementException()).remove()
last = null
}
}
@ -105,12 +126,13 @@ class LimitingIterator<T>(val parent: Iterator<T>, val limit: Long) : Iterator<T
}
}
class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : Iterator<T> {
class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : MutableIterator<T> {
init {
require(skip >= 0) { "Invalid skip amount $skip" }
}
private var found = skip
private var returned = false
override fun hasNext(): Boolean {
while (parent.hasNext() && found > 0L) {
@ -125,15 +147,26 @@ class SkippingIterator<T>(val parent: Iterator<T>, skip: Long) : Iterator<T> {
if (!hasNext())
throw NoSuchElementException()
return parent.next()
val v = parent.next()
returned = true
return v
}
override fun remove() {
if (!returned) {
throw NoSuchElementException()
}
returned = false
(parent as MutableIterator<T>).remove()
}
}
fun <T> Iterator<T>.filter(condition: Predicate<in T>): Iterator<T> = FilteredIterator(this, condition)
fun <T, R> Iterator<T>.map(mapper: (T) -> R): Iterator<R> = MappingIterator(this, mapper)
fun <T, R> Iterator<T>.map(mapper: java.util.function.Function<T, R>): Iterator<R> = MappingIterator(this, mapper::apply)
fun <T, R> Iterator<T>.flatMap(mapper: (T) -> Iterator<R>): Iterator<R> = FlatMappingIterator(this, mapper)
fun <T, R> Iterator<T>.flatMap(mapper: java.util.function.Function<T, Iterator<R>>): Iterator<R> = FlatMappingIterator(this, mapper::apply)
fun <T> Iterator<T>.filter(condition: Predicate<in T>) = FilteredIterator(this, condition)
fun <T, R> Iterator<T>.map(mapper: (T) -> R) = MappingIterator(this, mapper)
fun <T, R> Iterator<T>.map(mapper: java.util.function.Function<T, R>) = MappingIterator(this, mapper::apply)
fun <T, R> Iterator<T>.flatMap(mapper: (T) -> Iterator<R>) = FlatMappingIterator(this, mapper)
fun <T, R> Iterator<T>.flatMap(mapper: java.util.function.Function<T, Iterator<R>>) = FlatMappingIterator(this, mapper::apply)
fun interface O2DFunction<T> {
fun apply(value: T): Double