Compile against kommons immutable* factory functions and mapped comparator

This commit is contained in:
DBotThePony 2025-03-30 18:16:43 +07:00
parent 88d47ef4d5
commit e0aa5ebbae
Signed by: DBot
GPG Key ID: DCC23B5715498507
32 changed files with 30 additions and 218 deletions

View File

@ -9,11 +9,11 @@ import net.minecraft.nbt.ListTag
import net.minecraft.world.level.block.Block
import net.minecraft.world.level.block.entity.BlockEntityType
import net.minecraft.world.level.block.state.BlockState
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.capability.IMatteryUpgrade
import ru.dbotthepony.mc.otm.capability.UpgradeType
import ru.dbotthepony.mc.otm.capability.energy.IMatteryEnergyStorage
import ru.dbotthepony.mc.otm.container.UpgradeContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.mc.otm.util.getCompoundList
import ru.dbotthepony.mc.otm.util.set
import ru.dbotthepony.mc.otm.util.countingLazy

View File

@ -27,7 +27,7 @@ import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.multiblock.ShapedMultiblock
import ru.dbotthepony.mc.otm.util.getBlockStateNow
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.math.plus
import ru.dbotthepony.mc.otm.util.math.times

View File

@ -20,7 +20,7 @@ import net.neoforged.neoforge.fluids.capability.IFluidHandler
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.immutableMap
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.mapPresent

View File

@ -18,7 +18,7 @@ import ru.dbotthepony.mc.otm.capability.matter.IMatterStorage
import ru.dbotthepony.mc.otm.capability.matter.ProfiledMatterStorage
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode
import ru.dbotthepony.mc.otm.menu.matter.MatterCapacitorBankMenu

View File

@ -12,13 +12,12 @@ import net.minecraft.world.inventory.AbstractContainerMenu
import ru.dbotthepony.mc.otm.menu.matter.PatternStorageMenu
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.Block
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.mc.otm.block.entity.MatteryDeviceBlockEntity
import ru.dbotthepony.mc.otm.capability.matter.*
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.collect.filterNotNull
import ru.dbotthepony.mc.otm.util.collect.map
import ru.dbotthepony.mc.otm.util.filterNotNull
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.graph.matter.SimpleMatterNode
import ru.dbotthepony.mc.otm.registry.game.MBlockEntities

View File

@ -24,7 +24,6 @@ import ru.dbotthepony.mc.otm.capability.energy.ProfiledEnergyStorage
import ru.dbotthepony.mc.otm.capability.energy.WorkerEnergyStorage
import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.container.ItemFilterSet
import ru.dbotthepony.mc.otm.core.*
import ru.dbotthepony.mc.otm.util.math.RelativeSide
import ru.dbotthepony.mc.otm.util.math.isPositive
import ru.dbotthepony.mc.otm.util.math.toIntSafe

View File

@ -35,7 +35,7 @@ import ru.dbotthepony.mc.otm.container.slotted.AutomationFilters
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.SimpleCache
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.otmRandom
import ru.dbotthepony.mc.otm.container.ItemStackKey
import ru.dbotthepony.mc.otm.container.asKey

View File

@ -22,7 +22,7 @@ import ru.dbotthepony.mc.otm.capability.maxEnergyStoredMattery
import ru.dbotthepony.mc.otm.capability.transcieveEnergy
import ru.dbotthepony.mc.otm.container.slotted.FilteredContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.otmRandom

View File

@ -22,7 +22,7 @@ import ru.dbotthepony.mc.otm.client.render.WidgetLocation
import ru.dbotthepony.mc.otm.client.render.sprites.AbstractMatterySprite
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.get
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.math.BlockRotationFreedom
import ru.dbotthepony.mc.otm.util.math.rotate
import ru.dbotthepony.mc.otm.util.math.rotateY

View File

@ -19,7 +19,6 @@ import ru.dbotthepony.mc.otm.block.entity.WorkerState
import ru.dbotthepony.mc.otm.client.ShiftPressedCond
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.*
import ru.dbotthepony.mc.otm.core.*
import ru.dbotthepony.mc.otm.util.math.BlockRotationFreedom
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.mc.otm.block.entity.tech.GravitationStabilizerBlockEntity.Companion.RANGE

View File

@ -9,6 +9,7 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Items
import net.neoforged.neoforge.common.Tags
import ru.dbotthepony.kommons.math.RGBAColor
import ru.dbotthepony.kommons.util.map
import ru.dbotthepony.mc.otm.block.entity.decorative.PainterBlockEntity
import ru.dbotthepony.mc.otm.client.render.FlatRectangleIcon
import ru.dbotthepony.mc.otm.client.render.IGUIRenderable

View File

@ -31,7 +31,7 @@ import ru.dbotthepony.mc.otm.client.screen.tech.EssenceStorageScreen
import ru.dbotthepony.mc.otm.config.ClientConfig
import ru.dbotthepony.mc.otm.util.TextComponent
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.math.RelativeSide
import ru.dbotthepony.mc.otm.util.ItemStackSorter
import ru.dbotthepony.mc.otm.util.getLevelFromXp

View File

@ -13,7 +13,6 @@ import ru.dbotthepony.mc.otm.client.playGuiClickSound
import ru.dbotthepony.mc.otm.client.render.IGUIRenderable
import ru.dbotthepony.mc.otm.client.render.UVWindingOrder
import ru.dbotthepony.mc.otm.client.screen.panels.EditablePanel
import ru.dbotthepony.mc.otm.core.*
import ru.dbotthepony.mc.otm.menu.input.IPlayerInputWithFeedback
import ru.dbotthepony.mc.otm.network.MatteryStreamCodec
import ru.dbotthepony.mc.otm.util.TextComponent

View File

@ -16,6 +16,7 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.enchantment.EnchantmentEffectComponents
import net.minecraft.world.item.enchantment.EnchantmentHelper
import net.neoforged.neoforge.fluids.capability.IFluidHandler
import ru.dbotthepony.kommons.util.map
import ru.dbotthepony.mc.otm.container.util.ItemStackHashStrategy
import ru.dbotthepony.mc.otm.container.util.containerSlot
import ru.dbotthepony.mc.otm.container.util.slotIterator

View File

@ -4,7 +4,7 @@ import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import net.minecraft.world.item.Item
import ru.dbotthepony.mc.otm.capability.FlowDirection
import ru.dbotthepony.mc.otm.capability.matter.*
import ru.dbotthepony.mc.otm.util.filterNotNull
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.graph.GraphNodeList
import java.util.*

View File

@ -35,7 +35,7 @@ import ru.dbotthepony.mc.otm.capability.moveFluid
import ru.dbotthepony.mc.otm.container.get
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.any
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.immutableMap
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.registry.CapabilitiesRegisterListener

View File

@ -84,7 +84,7 @@ import ru.dbotthepony.mc.otm.util.TextComponent
import ru.dbotthepony.mc.otm.util.TranslatableComponent
import ru.dbotthepony.mc.otm.util.collect.any
import ru.dbotthepony.mc.otm.util.collect.filter
import ru.dbotthepony.mc.otm.util.filterNotNull
import ru.dbotthepony.kommons.collect.filterNotNull
import ru.dbotthepony.mc.otm.util.getReverseTag
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal

View File

@ -22,7 +22,7 @@ import ru.dbotthepony.mc.otm.container.ItemFilterSet
import ru.dbotthepony.mc.otm.container.UpgradeContainer
import ru.dbotthepony.mc.otm.container.util.containerSlotOrNull
import ru.dbotthepony.mc.otm.util.collect.ConditionalEnumSet
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.menu.input.InstantBooleanInput

View File

@ -16,10 +16,11 @@ import net.minecraft.world.inventory.ClickType
import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.network.PacketDistributor
import net.neoforged.neoforge.network.handling.IPayloadContext
import ru.dbotthepony.kommons.collect.addSorted
import ru.dbotthepony.kommons.util.map
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.addSorted
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.util.map
import ru.dbotthepony.mc.otm.util.readBigInteger

View File

@ -12,7 +12,7 @@ import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.mc.otm.block.entity.decorative.GrillBlockEntity
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.menu.ChemicalFuelMenuSlot
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot

View File

@ -4,7 +4,7 @@ import net.minecraft.world.entity.player.Player
import ru.dbotthepony.kommons.util.Delegate
import ru.dbotthepony.mc.otm.container.ItemFilter
import ru.dbotthepony.mc.otm.container.ItemFilterSet
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.network.StreamCodecs
import java.util.function.Predicate

View File

@ -1,7 +1,7 @@
package ru.dbotthepony.mc.otm.menu.matter
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.block.entity.matter.MatterCapacitorBankBlockEntity
import ru.dbotthepony.mc.otm.block.entity.tech.BatteryBankBlockEntity
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer

View File

@ -10,13 +10,14 @@ import net.minecraft.world.item.Item
import net.neoforged.neoforge.network.PacketDistributor
import net.neoforged.neoforge.network.handling.IPayloadContext
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.collect.addSorted
import ru.dbotthepony.kommons.util.getValue
import ru.dbotthepony.kommons.util.map
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.block.entity.matter.MatterPanelBlockEntity
import ru.dbotthepony.mc.otm.capability.matter.*
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.util.ResourceLocation
import ru.dbotthepony.mc.otm.util.addSorted
import ru.dbotthepony.mc.otm.util.map
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.writeCollection

View File

@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.menu.widget.LevelGaugeWidget
import ru.dbotthepony.mc.otm.menu.widget.ProgressGaugeWidget
import ru.dbotthepony.mc.otm.container.CombinedContainer
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.util.isNotEmpty
import ru.dbotthepony.mc.otm.menu.OutputMenuSlot
import ru.dbotthepony.mc.otm.menu.MatteryPoweredMenu

View File

@ -1,7 +1,7 @@
package ru.dbotthepony.mc.otm.menu.matter
import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.block.entity.matter.PatternStorageBlockEntity
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.util.math.Decimal

View File

@ -4,7 +4,7 @@ import net.minecraft.world.entity.player.Inventory
import ru.dbotthepony.mc.otm.block.entity.tech.BatteryBankBlockEntity
import ru.dbotthepony.mc.otm.block.entity.RedstoneSetting
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.menu.BatteryMenuSlot
import ru.dbotthepony.mc.otm.menu.MatteryMenu
import ru.dbotthepony.mc.otm.menu.input.EnergyConfigPlayerInput

View File

@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.block.entity.tech.PlatePressBlockEntity
import ru.dbotthepony.mc.otm.compat.jei.PlatePressRecipeCategory
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.menu.OutputMenuSlot
import ru.dbotthepony.mc.otm.menu.MatteryMenuSlot
import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback

View File

@ -12,7 +12,7 @@ import ru.dbotthepony.mc.otm.block.entity.tech.PoweredSmokerBlockEntity
import ru.dbotthepony.mc.otm.compat.jei.MicrowaveRecipeCategory
import ru.dbotthepony.mc.otm.container.EnhancedContainer
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
import ru.dbotthepony.mc.otm.util.immutableList
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.mc.otm.menu.OutputMenuSlot
import ru.dbotthepony.mc.otm.menu.UserFilteredMenuSlot
import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback

View File

@ -8,6 +8,8 @@ import net.minecraft.world.item.CreativeModeTabs
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.neoforged.neoforge.common.CreativeModeTabRegistry
import ru.dbotthepony.kommons.util.nullsFirst
import ru.dbotthepony.kommons.util.nullsLast
import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.client.render.IGUIRenderable
import ru.dbotthepony.mc.otm.matter.MatterManager

View File

@ -18,6 +18,7 @@ import net.minecraft.nbt.Tag
import net.minecraft.resources.ResourceLocation
import net.neoforged.neoforge.common.util.INBTSerializable
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.kommons.guava.immutableList
import ru.dbotthepony.kommons.util.Delegate
import ru.dbotthepony.mc.otm.util.math.Decimal
import ru.dbotthepony.mc.otm.util.math.Vector

View File

@ -155,22 +155,6 @@ inline var Entity.position: Vec3
get() = position()
set(value) { setPos(value) }
inline fun <T : Any> immutableList(size: Int, initializer: (index: Int) -> T): ImmutableList<T> {
require(size >= 0) { "Invalid list size $size" }
return when (size) {
0 -> ImmutableList.of()
1 -> ImmutableList.of(initializer(0))
else -> ImmutableList.Builder<T>().let {
for (i in 0 until size) {
it.add(initializer(i))
}
it.build()
}
}
}
inline fun <K : Any, V : Any> immutableMap(initializer: ImmutableMap.Builder<K, V>.() -> Unit): ImmutableMap<K, V> {
val builder = ImmutableMap.Builder<K, V>()
initializer.invoke(builder)
@ -189,19 +173,6 @@ inline fun <V : Any> immutableSet(initializer: Consumer<V>.() -> Unit): Immutabl
return builder.build()
}
inline fun <V : Any> immutableList(initializer: Consumer<V>.() -> Unit): ImmutableList<V> {
val builder = ImmutableList.Builder<V>()
initializer.invoke(builder::add)
return builder.build()
}
fun <V : Any> immutableList(a: V, vararg values: V): ImmutableList<V> {
val builder = ImmutableList.Builder<V>()
builder.add(a)
builder.addAll(values.iterator())
return builder.build()
}
inline fun itemAttributes(builder: ItemAttributeModifiers.Builder.() -> Unit): ItemAttributeModifiers {
val value = ItemAttributeModifiers.builder()
builder(value)
@ -348,27 +319,8 @@ fun String.toUUID(): UUID {
return UUID.nameUUIDFromBytes(toByteArray(Charsets.UTF_8))
}
fun <T> Collection<T>.probablyParallelStream(): Stream<out T> {
if (size >= 400) {
// TODO: https://github.com/MinecraftForge/EventBus/issues/44
return stream()
}
return stream()
}
fun <T> Array<T>.stream(): Stream<T> = Arrays.stream(this)
@Suppress("unchecked_cast")
fun <T> Stream<T?>.filterNotNull(): Stream<T> {
return filter { it != null } as Stream<T>
}
@Suppress("unchecked_cast")
inline fun <reified T> Stream<*>.filterIsInstance(): Stream<T> {
return filter { it is T } as Stream<T>
}
inline fun <T> MutableList<out Reference<out T>>.forValidRefs(fn: (T) -> Unit) {
val iterator = listIterator()
@ -410,115 +362,6 @@ fun <T : Comparable<T>> BlockState.getValueNullable(prop: Property<T>): T? {
return null
}
fun <T> Stream<T>.asIterable(): Iterable<T> {
return object : Iterable<T> {
override fun iterator(): Iterator<T> {
return this@asIterable.iterator()
}
}
}
fun <T> Comparator<T>.nullsFirst(): Comparator<T> {
return Comparator.nullsFirst(this)
}
fun <T> Comparator<T>.nullsLast(): Comparator<T> {
return Comparator.nullsLast(this)
}
class MappedComparator<T, O>(private val parent: Comparator<O>, private val mapper: (T) -> O) : Comparator<T> {
override fun compare(o1: T, o2: T): Int {
return parent.compare(mapper.invoke(o1), mapper.invoke(o2))
}
override fun equals(other: Any?): Boolean {
return other is MappedComparator<*, *> && parent == other.parent
}
override fun hashCode(): Int {
return parent.hashCode()
}
override fun toString(): String {
return "MappedComparator[$parent]"
}
}
fun <A, B> Comparator<A>.map(mapper: (B) -> A): Comparator<B> {
return MappedComparator(this, mapper)
}
fun <T> Comparator<T>.suppliers(): Comparator<Supplier<T>> {
return MappedComparator(this) { it.get() }
}
/**
* Returns applicable index to put [element] into [List] determined by [comparator], optionally specifying ranges as [fromIndex] and [toIndex]
*
* If [List] is not sorted, result of this function is undefined
*/
fun <E> List<E>.searchInsertionIndex(element: E, comparator: Comparator<in E>, fromIndex: Int = 0, toIndex: Int = size): Int {
require(toIndex >= fromIndex) { "Invalid range: to $toIndex >= from $fromIndex" }
require(fromIndex >= 0) { "Invalid from index: $fromIndex" }
require(toIndex >= 0) { "Invalid to index: $toIndex" }
require(fromIndex <= size) { "Invalid from index: $fromIndex (list size $size)" }
require(toIndex <= size) { "Invalid to index: $toIndex (list size $size)" }
if (fromIndex == size || fromIndex == toIndex || comparator.compare(element, this[fromIndex]) <= 0) {
return fromIndex
}
// линейный поиск если границы маленькие
if (toIndex - fromIndex <= 10) {
for (i in fromIndex + 1 until toIndex) {
val compare = comparator.compare(element, this[i])
if (compare <= 0) {
return i
}
}
return size
} else {
// двоичный поиск
var lower = fromIndex
var upper = toIndex - 1
while (upper - lower >= 10) {
val middle = (upper + lower) / 2
val compare = comparator.compare(element, this[middle])
if (compare == 0) {
return middle
} else if (compare < 0) {
upper = middle
} else {
lower = middle
}
}
return searchInsertionIndex(element, comparator, lower, upper + 1)
}
}
/**
* Inserts [element] into [MutableList] at index determined by [comparator]
*
* If [MutableList] is not sorted, result of this function is undefined
*/
fun <E> MutableList<E>.addSorted(element: E, comparator: Comparator<in E>) {
add(searchInsertionIndex(element, comparator), element)
}
/**
* Inserts [element] into [MutableList] at index determined by comparing values themselves
*
* If [MutableList] is not sorted, result of this function is undefined
*/
fun <E : Comparable<E>> MutableList<E>.addSorted(element: E) {
add(searchInsertionIndex(element, ObjectComparators.NATURAL_COMPARATOR), element)
}
fun <A, B> lazy2(a: () -> A, b: A.() -> B): Supplier<B> {
val first = lazy(a)
return Supplier { b.invoke(first.value) }

View File

@ -1,34 +0,0 @@
package ru.dbotthepony.mc.otm.tests
import it.unimi.dsi.fastutil.ints.IntComparators
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import ru.dbotthepony.mc.otm.util.addSorted
import java.util.Random
object ComparatorTests {
@Test
@DisplayName("Comparator tests")
fun test() {
val sortedList = mutableListOf(1, 4, 6)
sortedList.addSorted(2, IntComparators.NATURAL_COMPARATOR)
sortedList.addSorted(3, IntComparators.NATURAL_COMPARATOR)
sortedList.addSorted(7, IntComparators.NATURAL_COMPARATOR)
sortedList.addSorted(-1, IntComparators.NATURAL_COMPARATOR)
assertEquals(mutableListOf(-1, 1, 2, 3, 4, 6, 7), sortedList)
val rand = Random()
val sorted2 = ArrayList<Int>()
for (i in 0 .. 100) {
sorted2.addSorted(rand.nextInt(-100, 100), IntComparators.NATURAL_COMPARATOR)
}
val sorted22 = ArrayList(sorted2)
sorted22.sort()
assertEquals(sorted22, sorted2)
}
}