Skip to content

Use asymptotically efficient immutable Vector in many places in the VM #897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.casting.iota.*
import at.petrak.hexcasting.api.casting.math.HexPattern
import at.petrak.hexcasting.api.casting.mishaps.MishapInvalidIota
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
import at.petrak.hexcasting.api.utils.Vector
import at.petrak.hexcasting.api.utils.asTranslatedComponent
import com.mojang.datafixers.util.Either
import net.minecraft.core.BlockPos
Expand All @@ -22,7 +23,7 @@ import kotlin.math.abs
import kotlin.math.roundToInt
import kotlin.math.roundToLong

fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
fun Vector<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
return x.double
Expand All @@ -32,7 +33,7 @@ fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
}
}

fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
fun Vector<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is EntityIota) {
return x.entity
Expand All @@ -41,7 +42,7 @@ fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
}
}

fun List<Iota>.getList(idx: Int, argc: Int = 0): SpellList {
fun Vector<Iota>.getList(idx: Int, argc: Int = 0): Vector<Iota> {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is ListIota) {
return x.list
Expand All @@ -50,7 +51,7 @@ fun List<Iota>.getList(idx: Int, argc: Int = 0): SpellList {
}
}

fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
fun Vector<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is PatternIota) {
return x.pattern
Expand All @@ -59,7 +60,7 @@ fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
}
}

fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
fun Vector<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is Vec3Iota) {
return x.vec3
Expand All @@ -68,7 +69,7 @@ fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
}
}

fun List<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {
fun Vector<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is BooleanIota) {
return x.bool
Expand All @@ -79,7 +80,7 @@ fun List<Iota>.getBool(idx: Int, argc: Int = 0): Boolean {

// Helpers

fun List<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
fun Vector<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is EntityIota) {
val e = x.entity
Expand All @@ -89,7 +90,7 @@ fun List<Iota>.getItemEntity(idx: Int, argc: Int = 0): ItemEntity {
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.item")
}

fun List<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
fun Vector<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is EntityIota) {
val e = x.entity
Expand All @@ -99,7 +100,7 @@ fun List<Iota>.getPlayer(idx: Int, argc: Int = 0): ServerPlayer {
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.player")
}

fun List<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
fun Vector<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is EntityIota) {
val e = x.entity
Expand All @@ -109,7 +110,7 @@ fun List<Iota>.getMob(idx: Int, argc: Int = 0): Mob {
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.mob")
}

fun List<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingEntity {
fun Vector<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingEntity {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is EntityIota) {
val e = x.entity
Expand All @@ -119,7 +120,7 @@ fun List<Iota>.getLivingEntityButNotArmorStand(idx: Int, argc: Int = 0): LivingE
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "entity.living")
}

fun List<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
fun Vector<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -130,7 +131,7 @@ fun List<Iota>.getPositiveDouble(idx: Int, argc: Int = 0): Double {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive")
}

fun List<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Double {
fun Vector<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Double {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -141,7 +142,7 @@ fun List<Iota>.getPositiveDoubleUnder(idx: Int, max: Double, argc: Int = 0): Dou
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive.less", max)
}

fun List<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int = 0): Double {
fun Vector<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int = 0): Double {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -152,7 +153,7 @@ fun List<Iota>.getPositiveDoubleUnderInclusive(idx: Int, max: Double, argc: Int
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.positive.less.equal", max)
}

fun List<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int = 0): Double {
fun Vector<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int = 0): Double {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -163,7 +164,7 @@ fun List<Iota>.getDoubleBetween(idx: Int, min: Double, max: Double, argc: Int =
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "double.between", min, max)
}

fun List<Iota>.getInt(idx: Int, argc: Int = 0): Int {
fun Vector<Iota>.getInt(idx: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -175,7 +176,7 @@ fun List<Iota>.getInt(idx: Int, argc: Int = 0): Int {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int")
}

fun List<Iota>.getLong(idx: Int, argc: Int = 0): Long {
fun Vector<Iota>.getLong(idx: Int, argc: Int = 0): Long {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -187,7 +188,7 @@ fun List<Iota>.getLong(idx: Int, argc: Int = 0): Long {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int") // shh we're lying
}

fun List<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
fun Vector<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -199,7 +200,7 @@ fun List<Iota>.getPositiveInt(idx: Int, argc: Int = 0): Int {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
}

fun List<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
fun Vector<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -211,7 +212,7 @@ fun List<Iota>.getPositiveLong(idx: Int, argc: Int = 0): Long {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive")
}

fun List<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
fun Vector<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -223,7 +224,7 @@ fun List<Iota>.getPositiveIntUnder(idx: Int, max: Int, argc: Int = 0): Int {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less", max)
}

fun List<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0): Int {
fun Vector<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -235,7 +236,7 @@ fun List<Iota>.getPositiveIntUnderInclusive(idx: Int, max: Int, argc: Int = 0):
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.positive.less.equal", max)
}

fun List<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
fun Vector<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is DoubleIota) {
val double = x.double
Expand All @@ -247,7 +248,7 @@ fun List<Iota>.getIntBetween(idx: Int, min: Int, max: Int, argc: Int = 0): Int {
throw MishapInvalidIota.of(x, if (argc == 0) idx else argc - (idx + 1), "int.between", min, max)
}

fun List<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
fun Vector<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (x is Vec3Iota) {
return BlockPos.containing(x.vec3)
Expand All @@ -256,7 +257,7 @@ fun List<Iota>.getBlockPos(idx: Int, argc: Int = 0): BlockPos {
throw MishapInvalidIota.ofType(x, if (argc == 0) idx else argc - (idx + 1), "vector")
}

fun List<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
fun Vector<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
val datum = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
return when (datum) {
is DoubleIota -> Either.left(datum.double)
Expand All @@ -269,7 +270,7 @@ fun List<Iota>.getNumOrVec(idx: Int, argc: Int = 0): Either<Double, Vec3> {
}
}

fun List<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, SpellList> {
fun Vector<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, Vector<Iota>> {
val datum = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
if (datum is DoubleIota) {
val double = datum.double
Expand All @@ -287,7 +288,7 @@ fun List<Iota>.getLongOrList(idx: Int, argc: Int = 0): Either<Long, SpellList> {
)
}

fun evaluatable(datum: Iota, reverseIdx: Int): Either<Iota, SpellList> =
fun evaluatable(datum: Iota, reverseIdx: Int): Either<Iota, Vector<Iota>> =
when (datum) {
is ListIota -> Either.right(datum.list)
else -> if (datum.executable()) Either.left(datum) else throw MishapInvalidIota(
Expand All @@ -308,16 +309,15 @@ fun aplKinnie(operatee: Either<Double, Vec3>, fn: DoubleUnaryOperator): Iota =
{ vec -> Vec3Iota(Vec3(fn.applyAsDouble(vec.x), fn.applyAsDouble(vec.y), fn.applyAsDouble(vec.z))) }
)

inline val Boolean.asActionResult get() = listOf(BooleanIota(this))
inline val Double.asActionResult get() = listOf(DoubleIota(this))
inline val Number.asActionResult get() = listOf(DoubleIota(this.toDouble()))
inline val Boolean.asActionResult: Vector<Iota> get() = Vector.from(listOf(BooleanIota(this)))
inline val Double.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this)))
inline val Number.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this.toDouble())))

inline val SpellList.asActionResult get() = listOf(ListIota(this))
inline val List<Iota>.asActionResult get() = listOf(ListIota(this))
inline val Vector<Iota>.asActionResult: Vector<Iota> get() = Vector.from(listOf(ListIota(this)))

inline val BlockPos.asActionResult get() = listOf(Vec3Iota(Vec3.atCenterOf(this)))
inline val Vector3f.asActionResult get() = listOf(Vec3Iota(Vec3(this)))
inline val Vec3.asActionResult get() = listOf(Vec3Iota(this))
inline val BlockPos.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3.atCenterOf(this))))
inline val Vector3f.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3(this))))
inline val Vec3.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(this)))

inline val Entity?.asActionResult get() = listOf(if (this == null) NullIota() else EntityIota(this))
inline val HexPattern.asActionResult get() = listOf(PatternIota(this))
inline val Entity?.asActionResult: Vector<Iota> get() = Vector.from(listOf(if (this == null) NullIota() else EntityIota(this)))
inline val HexPattern.asActionResult: Vector<Iota> get() = Vector.from(listOf(PatternIota(this)))
93 changes: 0 additions & 93 deletions Common/src/main/java/at/petrak/hexcasting/api/casting/SpellList.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.casting.mishaps.Mishap
import at.petrak.hexcasting.api.utils.Vector
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
import java.util.function.Consumer

abstract class OperatorBasic(arity: Int, accepts: IotaMultiPredicate) : Operator(arity, accepts) {

@Throws(Mishap::class)
override fun operate(env: CastingEnvironment, image: CastingImage, continuation: SpellContinuation): OperationResult {
val stack = image.stack.toMutableList()
val args = stack.takeLast(arity)
repeat(arity) { stack.removeLast() }
val stack = Vector.VectorBuilder<Iota>()
stack.addAll(image.stack.dropRight(arity))
val args = image.stack.takeRight(arity)

val ret = apply(args, env)
ret.forEach(Consumer { e: Iota -> stack.add(e) })
ret.forEach(Consumer { e: Iota -> stack.addOne(e) })

val image2 = image.copy(stack = stack, opsConsumed = image.opsConsumed + 1)
val image2 = image.copy(stack = stack.result(), opsConsumed = image.opsConsumed + 1)
return OperationResult(image2, listOf(), continuation, HexEvalSounds.NORMAL_EXECUTE)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.casting.eval.OperationResult
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.utils.Vector
import net.minecraft.world.phys.Vec3
import java.text.DecimalFormat

Expand Down Expand Up @@ -54,8 +55,8 @@ interface Action {
override val argc: Int
get() = 0

override fun execute(args: List<Iota>, env: CastingEnvironment): List<Iota> =
listOf(x)
override fun execute(args: Vector<Iota>, env: CastingEnvironment): Vector<Iota> =
Vector.from(listOf(x))
}

public val DOUBLE_FORMATTER = DecimalFormat("####.####")
Expand Down
Loading