Skip to content

Commit 012e07e

Browse files
s5bugSirius902
andcommitted
Change ListIota to use Vector
Co-authored-by: Sirius902 <10891979+Sirius902@users.noreply.github.com>
1 parent 378a2fe commit 012e07e

File tree

17 files changed

+55
-52
lines changed

17 files changed

+55
-52
lines changed

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

Lines changed: 6 additions & 5 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
@@ -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 List<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
@@ -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 List<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(
@@ -312,8 +313,8 @@ inline val Boolean.asActionResult get() = listOf(BooleanIota(this))
312313
inline val Double.asActionResult get() = listOf(DoubleIota(this))
313314
inline val Number.asActionResult get() = 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 SpellList.asActionResult get() = listOf(ListIota(Vector.from(this)))
317+
inline val List<Iota>.asActionResult get() = listOf(ListIota(Vector.from(this)))
317318

318319
inline val BlockPos.asActionResult get() = listOf(Vec3Iota(Vec3.atCenterOf(this)))
319320
inline val Vector3f.asActionResult get() = listOf(Vec3Iota(Vec3(this)))

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/CastingImage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ data class CastingImage private constructor(
4545
tag.put(TAG_IOTAS, ListTag())
4646
tag.put(TAG_ESCAPED, ListTag())
4747
} else {
48-
tag.put(TAG_IOTAS, ListIota(this.map { it.iota }).serialize())
48+
tag.put(TAG_IOTAS, ListIota(Vector.from(this).map { it.iota }).serialize())
4949
tag.put(TAG_ESCAPED, this.map { it.escaped }.serializeToNBT())
5050
}
5151

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/CastingVM.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
219219
displayDepth--
220220
if (newParenCount == 0) {
221221
val newStack = this.image.stack.toMutableList()
222-
newStack.add(ListIota(this.image.parenthesized.toList().map { it.iota }))
222+
newStack.add(ListIota(Vector.from(this.image.parenthesized.toList().map { it.iota })))
223223
this.image.copy(
224224
stack = newStack,
225225
parenCount = newParenCount,

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/FrameEvaluate.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType
66
import at.petrak.hexcasting.api.casting.iota.Iota
77
import at.petrak.hexcasting.api.casting.iota.ListIota
88
import at.petrak.hexcasting.api.utils.NBTBuilder
9+
import at.petrak.hexcasting.api.utils.Vector
910
import at.petrak.hexcasting.api.utils.getList
1011
import at.petrak.hexcasting.api.utils.serializeToNBT
1112
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
@@ -44,7 +45,7 @@ data class FrameEvaluate(val list: SpellList, val isMetacasting: Boolean) : Cont
4445
}
4546
} else {
4647
// If there are no patterns (e.g. empty Hermes), just return OK.
47-
CastResult(ListIota(list), continuation, null, listOf(), ResolvedPatternType.EVALUATED, HexEvalSounds.HERMES)
48+
CastResult(ListIota(Vector.from(list)), continuation, null, listOf(), ResolvedPatternType.EVALUATED, HexEvalSounds.HERMES)
4849
}
4950
}
5051

@@ -62,10 +63,10 @@ data class FrameEvaluate(val list: SpellList, val isMetacasting: Boolean) : Cont
6263
val TYPE: ContinuationFrame.Type<FrameEvaluate> = object : ContinuationFrame.Type<FrameEvaluate> {
6364
override fun deserializeFromNBT(tag: CompoundTag, world: ServerLevel): FrameEvaluate {
6465
return FrameEvaluate(
65-
HexIotaTypes.LIST.deserialize(
66+
SpellList.LList(0, HexIotaTypes.LIST.deserialize(
6667
tag.getList("patterns", Tag.TAG_COMPOUND),
6768
world
68-
)!!.list,
69+
)!!.list),
6970
tag.getBoolean("isMetacasting"))
7071
}
7172

Common/src/main/java/at/petrak/hexcasting/api/casting/eval/vm/FrameForEach.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ data class FrameForEach(
6969
val tStack = stack.toMutableList()
7070
tStack.add(stackTop)
7171
return CastResult(
72-
ListIota(code),
72+
ListIota(Vector.from(code)),
7373
newCont,
7474
// reset escapes so they don't carry over to other iterations or out of thoth
7575
newImage.withResetEscape().copy(stack = tStack),
@@ -96,8 +96,8 @@ data class FrameForEach(
9696
val TYPE: ContinuationFrame.Type<FrameForEach> = object : ContinuationFrame.Type<FrameForEach> {
9797
override fun deserializeFromNBT(tag: CompoundTag, world: ServerLevel): FrameForEach {
9898
return FrameForEach(
99-
HexIotaTypes.LIST.deserialize(tag.getList("data", Tag.TAG_COMPOUND), world)!!.list,
100-
HexIotaTypes.LIST.deserialize(tag.getList("code", Tag.TAG_COMPOUND), world)!!.list,
99+
SpellList.LList(0, HexIotaTypes.LIST.deserialize(tag.getList("data", Tag.TAG_COMPOUND), world)!!.list),
100+
SpellList.LList(0, HexIotaTypes.LIST.deserialize(tag.getList("code", Tag.TAG_COMPOUND), world)!!.list),
101101
if (tag.hasList("base", Tag.TAG_COMPOUND))
102102
HexIotaTypes.LIST.deserialize(tag.getList("base", Tag.TAG_COMPOUND), world)!!.list.toList()
103103
else

Common/src/main/java/at/petrak/hexcasting/api/casting/iota/ListIota.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package at.petrak.hexcasting.api.casting.iota;
22

3-
import at.petrak.hexcasting.api.casting.SpellList;
43
import at.petrak.hexcasting.api.utils.HexUtils;
4+
import at.petrak.hexcasting.api.utils.Vector;
55
import at.petrak.hexcasting.common.lib.hex.HexIotaTypes;
66
import net.minecraft.ChatFormatting;
77
import net.minecraft.nbt.CompoundTag;
@@ -12,19 +12,18 @@
1212
import org.jetbrains.annotations.NotNull;
1313
import org.jetbrains.annotations.Nullable;
1414

15-
import java.util.ArrayList;
16-
import java.util.List;
15+
import java.util.Iterator;
1716

1817
import static java.lang.Math.max;
1918

2019
/**
21-
* This is a <i>wrapper</i> for {@link SpellList}.
20+
* This is a <i>wrapper</i> for {@link Vector<Iota>}.
2221
*/
2322
public class ListIota extends Iota {
2423
private final int depth;
2524
private final int size;
2625

27-
public ListIota(@NotNull SpellList list) {
26+
public ListIota(@NotNull Vector<Iota> list) {
2827
super(HexIotaTypes.LIST, list);
2928
int maxChildDepth = 0;
3029
int totalSize = 1;
@@ -36,17 +35,13 @@ public ListIota(@NotNull SpellList list) {
3635
size = totalSize;
3736
}
3837

39-
public ListIota(@NotNull List<Iota> list) {
40-
this(new SpellList.LList(list));
41-
}
42-
43-
public SpellList getList() {
44-
return (SpellList) this.payload;
38+
public Vector<Iota> getList() {
39+
return (Vector<Iota>) this.payload;
4540
}
4641

4742
@Override
4843
public boolean isTruthy() {
49-
return this.getList().getNonEmpty();
44+
return !this.getList().isEmpty();
5045
}
5146

5247
@Override
@@ -60,7 +55,7 @@ public boolean toleratesOther(Iota that) {
6055
}
6156
var b = list.getList();
6257

63-
SpellList.SpellListIterator aIter = a.iterator(), bIter = b.iterator();
58+
Iterator<Iota> aIter = a.iterator(), bIter = b.iterator();
6459
for (; ; ) {
6560
if (!aIter.hasNext() && !bIter.hasNext()) {
6661
// we ran out together!
@@ -106,18 +101,18 @@ public int depth() {
106101
@Override
107102
public ListIota deserialize(Tag tag, ServerLevel world) throws IllegalArgumentException {
108103
var listTag = HexUtils.downcast(tag, ListTag.TYPE);
109-
var out = new ArrayList<Iota>(listTag.size());
104+
var out = new Vector.VectorBuilder<Iota>();
110105

111106
for (var sub : listTag) {
112107
var csub = HexUtils.downcast(sub, CompoundTag.TYPE);
113108
var subiota = IotaType.deserialize(csub, world);
114109
if (subiota == null) {
115110
return null;
116111
}
117-
out.add(subiota);
112+
out.addOne(subiota);
118113
}
119114

120-
return new ListIota(out);
115+
return new ListIota(out.result());
121116
}
122117

123118
@Override

Common/src/main/java/at/petrak/hexcasting/api/utils/HexUtils.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ fun Iterable<Iota>.serializeToNBT() =
265265
if (IotaType.isTooLargeToSerialize(this))
266266
ListTag()
267267
else
268-
ListIota(this.toList()).serialize()
268+
ListIota(Vector.from(this)).serialize()
269269

270270
fun Iterable<Boolean>.serializeToNBT(): ByteArrayTag {
271271
val out = ByteArray(if (this is Collection<*>) this.size else 10)

Common/src/main/java/at/petrak/hexcasting/common/casting/actions/eval/OpEval.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object OpEval : Action {
3030
continuation.pushFrame(FrameFinishEval) // install a break-boundary after eval
3131
}
3232

33-
val instrsList = instrs.map({ SpellList.LList(0, listOf(it)) }, { it })
33+
val instrsList = instrs.map({ SpellList.LList(0, listOf(it)) }, { SpellList.LList(0, it) })
3434
val frame = FrameEvaluate(instrsList, true)
3535

3636
val image2 = image.withUsedOp().copy(stack = newStack)

Common/src/main/java/at/petrak/hexcasting/common/casting/actions/eval/OpForEach.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package at.petrak.hexcasting.common.casting.actions.eval
22

3+
import at.petrak.hexcasting.api.casting.SpellList
34
import at.petrak.hexcasting.api.casting.castables.Action
45
import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
56
import at.petrak.hexcasting.api.casting.eval.OperationResult
67
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage
78
import at.petrak.hexcasting.api.casting.eval.vm.FrameForEach
89
import at.petrak.hexcasting.api.casting.eval.vm.SpellContinuation
910
import at.petrak.hexcasting.api.casting.getList
10-
import at.petrak.hexcasting.api.casting.iota.Iota
1111
import at.petrak.hexcasting.api.casting.mishaps.MishapNotEnoughArgs
1212
import at.petrak.hexcasting.api.utils.Vector
1313
import at.petrak.hexcasting.common.lib.hex.HexEvalSounds
@@ -24,7 +24,7 @@ object OpForEach : Action {
2424
stack.removeLastOrNull()
2525
stack.removeLastOrNull()
2626

27-
val frame = FrameForEach(datums, instrs, null, Vector.empty<Iota>())
27+
val frame = FrameForEach(SpellList.LList(0, datums), SpellList.LList(0, instrs), null, Vector.empty())
2828
val image2 = image.withUsedOp().copy(stack = stack)
2929

3030
return OperationResult(image2, listOf(), continuation.pushFrame(frame), HexEvalSounds.THOTH)

Common/src/main/java/at/petrak/hexcasting/common/casting/actions/lists/OpCons.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ object OpCons : ConstMediaAction {
1212
override fun execute(args: List<Iota>, env: CastingEnvironment): List<Iota> {
1313
val bottom = args.getList(0, argc)
1414
val top = args[1]
15-
return SpellList.LPair(top, bottom).asActionResult
15+
return SpellList.LPair(top, SpellList.LList(0, bottom)).asActionResult
1616
}
1717
}

0 commit comments

Comments
 (0)