Skip to content

Commit 2cb0957

Browse files
s5bugSirius902
andcommitted
Use immutable asymptotically-efficient vector in most places of the VM
Co-authored-by: Sirius902 <10891979+Sirius902@users.noreply.github.com>
1 parent 3c25474 commit 2cb0957

File tree

183 files changed

+3983
-879
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

183 files changed

+3983
-879
lines changed

Common/src/main/java/at/petrak/hexcasting/api/casting/ActionUtils.kt

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.casting.iota.*
66
import at.petrak.hexcasting.api.casting.math.HexPattern
77
import at.petrak.hexcasting.api.casting.mishaps.MishapInvalidIota
88
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
9+
import at.petrak.hexcasting.api.utils.Vector
910
import at.petrak.hexcasting.api.utils.asTranslatedComponent
1011
import com.mojang.datafixers.util.Either
1112
import net.minecraft.core.BlockPos
@@ -22,7 +23,7 @@ import kotlin.math.abs
2223
import kotlin.math.roundToInt
2324
import kotlin.math.roundToLong
2425

25-
fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
26+
fun Vector<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
2627
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
2728
if (x is DoubleIota) {
2829
return x.double
@@ -32,7 +33,7 @@ fun List<Iota>.getDouble(idx: Int, argc: Int = 0): Double {
3233
}
3334
}
3435

35-
fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
36+
fun Vector<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
3637
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
3738
if (x is EntityIota) {
3839
return x.entity
@@ -41,7 +42,7 @@ fun List<Iota>.getEntity(idx: Int, argc: Int = 0): Entity {
4142
}
4243
}
4344

44-
fun List<Iota>.getList(idx: Int, argc: Int = 0): SpellList {
45+
fun Vector<Iota>.getList(idx: Int, argc: Int = 0): Vector<Iota> {
4546
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
4647
if (x is ListIota) {
4748
return x.list
@@ -50,7 +51,7 @@ fun List<Iota>.getList(idx: Int, argc: Int = 0): SpellList {
5051
}
5152
}
5253

53-
fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
54+
fun Vector<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
5455
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
5556
if (x is PatternIota) {
5657
return x.pattern
@@ -59,7 +60,7 @@ fun List<Iota>.getPattern(idx: Int, argc: Int = 0): HexPattern {
5960
}
6061
}
6162

62-
fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
63+
fun Vector<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
6364
val x = this.getOrElse(idx) { throw MishapNotEnoughArgs(idx + 1, this.size) }
6465
if (x is Vec3Iota) {
6566
return x.vec3
@@ -68,7 +69,7 @@ fun List<Iota>.getVec3(idx: Int, argc: Int = 0): Vec3 {
6869
}
6970
}
7071

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

8081
// Helpers
8182

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

311-
inline val Boolean.asActionResult get() = listOf(BooleanIota(this))
312-
inline val Double.asActionResult get() = listOf(DoubleIota(this))
313-
inline val Number.asActionResult get() = listOf(DoubleIota(this.toDouble()))
312+
inline val Boolean.asActionResult: Vector<Iota> get() = Vector.from(listOf(BooleanIota(this)))
313+
inline val Double.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this)))
314+
inline val Number.asActionResult: Vector<Iota> get() = Vector.from(listOf(DoubleIota(this.toDouble())))
314315

315-
inline val SpellList.asActionResult get() = listOf(ListIota(this))
316-
inline val List<Iota>.asActionResult get() = listOf(ListIota(this))
316+
inline val Vector<Iota>.asActionResult: Vector<Iota> get() = Vector.from(listOf(ListIota(this)))
317317

318-
inline val BlockPos.asActionResult get() = listOf(Vec3Iota(Vec3.atCenterOf(this)))
319-
inline val Vector3f.asActionResult get() = listOf(Vec3Iota(Vec3(this)))
320-
inline val Vec3.asActionResult get() = listOf(Vec3Iota(this))
318+
inline val BlockPos.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3.atCenterOf(this))))
319+
inline val Vector3f.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(Vec3(this))))
320+
inline val Vec3.asActionResult: Vector<Iota> get() = Vector.from(listOf(Vec3Iota(this)))
321321

322-
inline val Entity?.asActionResult get() = listOf(if (this == null) NullIota() else EntityIota(this))
323-
inline val HexPattern.asActionResult get() = listOf(PatternIota(this))
322+
inline val Entity?.asActionResult: Vector<Iota> get() = Vector.from(listOf(if (this == null) NullIota() else EntityIota(this)))
323+
inline val HexPattern.asActionResult: Vector<Iota> get() = Vector.from(listOf(PatternIota(this)))

Common/src/main/java/at/petrak/hexcasting/api/casting/SpellList.kt

Lines changed: 0 additions & 93 deletions
This file was deleted.

Common/src/main/java/at/petrak/hexcasting/api/casting/arithmetic/operator/OperatorBasic.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
77
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
88
import at.petrak.hexcasting.api.casting.iota.Iota
99
import at.petrak.hexcasting.api.casting.mishaps.Mishap
10+
import at.petrak.hexcasting.api.utils.Vector
1011
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
1112
import java.util.function.Consumer
1213

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

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

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

24-
val image2 = image.copy(stack = stack, opsConsumed = image.opsConsumed + 1)
25+
val image2 = image.copy(stack = stack.result(), opsConsumed = image.opsConsumed + 1)
2526
return OperationResult(image2, listOf(), continuation, HexEvalSounds.NORMAL_EXECUTE)
2627
}
2728

Common/src/main/java/at/petrak/hexcasting/api/casting/castables/Action.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import at.petrak.hexcasting.api.casting.eval.OperationResult
55
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
66
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
77
import at.petrak.hexcasting.api.casting.iota.Iota
8+
import at.petrak.hexcasting.api.utils.Vector
89
import net.minecraft.world.phys.Vec3
910
import java.text.DecimalFormat
1011

@@ -54,8 +55,8 @@ interface Action {
5455
override val argc: Int
5556
get() = 0
5657

57-
override fun execute(args: List<Iota>, env: CastingEnvironment): List<Iota> =
58-
listOf(x)
58+
override fun execute(args: Vector<Iota>, env: CastingEnvironment): Vector<Iota> =
59+
Vector.from(listOf(x))
5960
}
6061

6162
public val DOUBLE_FORMATTER = DecimalFormat("####.####")

0 commit comments

Comments
 (0)