From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Lucy-t <64509677+Lucy-t@users.noreply.github.com> Date: Thu, 7 Jan 2021 00:24:16 -0500 Subject: [PATCH] Port Cadmium Code from LucilleTea's port of cadmium to 1.16: https://github.com/LucilleTea/DataFixerUpper diff --git a/src/main/java/com/mojang/datafixers/DataFixUtils.java b/src/main/java/com/mojang/datafixers/DataFixUtils.java index cf8959aea3e48c1276ae9536d4de0f0127e1801e..b730a4c0b8b8c9036641d033568b3554062999cb 100644 --- a/src/main/java/com/mojang/datafixers/DataFixUtils.java +++ b/src/main/java/com/mojang/datafixers/DataFixUtils.java @@ -54,6 +54,15 @@ public class DataFixUtils { return other; } + // Yatopia start - Cadmium port + public static U orElse(final U nullable, final U other) { + if (nullable!=null) { + return nullable; + } + return other; + } + // Yatopia end + public static U orElseGet(final Optional optional, final Supplier other) { if (optional.isPresent()) { return optional.get(); diff --git a/src/main/java/com/mojang/datafixers/DataFixerBuilder.java b/src/main/java/com/mojang/datafixers/DataFixerBuilder.java index abc265b00044b14abb55c2628d454ee01fef467b..c8c19535fa64edc392fa18093ba47b16a168ebaf 100644 --- a/src/main/java/com/mojang/datafixers/DataFixerBuilder.java +++ b/src/main/java/com/mojang/datafixers/DataFixerBuilder.java @@ -64,13 +64,17 @@ public class DataFixerBuilder { public DataFixer build(final Executor executor) { final DataFixerUpper fixerUpper = new DataFixerUpper(new Int2ObjectAVLTreeMap<>(schemas), new ArrayList<>(globalList), new IntAVLTreeSet(fixerVersions)); + // Yatopia start - Cadmium port + long startTime = System.nanoTime(); + final List> futures = Lists.newArrayList(); + // Yatopia end final IntBidirectionalIterator iterator = fixerUpper.fixerVersions().iterator(); while (iterator.hasNext()) { final int versionKey = iterator.nextInt(); if (versionKey < minDataFixPrecacheVersion) continue; // Paper final Schema schema = schemas.get(versionKey); for (final String typeName : schema.types()) { - CompletableFuture.runAsync(() -> { + futures.add(CompletableFuture.runAsync(() -> { // Yatopia - Cadmium port final Type dataType = schema.getType(() -> typeName); final TypeRewriteRule rule = fixerUpper.getRule(DataFixUtils.getVersion(versionKey), dataVersion); dataType.rewrite(rule, DataFixerUpper.OPTIMIZATION_RULE); @@ -78,9 +82,16 @@ public class DataFixerBuilder { LOGGER.error("Unable to build datafixers", e); Runtime.getRuntime().exit(1); return null; - }); + })); // Yatopia - Cadmium port } } + // Yatopia start - Cadmium port + CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) + .thenAccept((res) -> { + long endTime = System.nanoTime(); + LOGGER.info("Finished building data fixers after {}ms", (endTime - startTime) / 1_000_000); + }); + // Yatopia end return fixerUpper; } diff --git a/src/main/java/com/mojang/datafixers/FieldFinder.java b/src/main/java/com/mojang/datafixers/FieldFinder.java index a20a9dedcff2f3d633c5fd09d238a98fe18968c0..7ae4fab4c448ec3444624b6e671364a89243f6dc 100644 --- a/src/main/java/com/mojang/datafixers/FieldFinder.java +++ b/src/main/java/com/mojang/datafixers/FieldFinder.java @@ -118,7 +118,7 @@ public final class FieldFinder implements OpticFinder { (Type>) choiceType, type, type, - new Proj1<>() + Proj1.instance() // Yatopia - Cadmium port ); } diff --git a/src/main/java/com/mojang/datafixers/TypeRewriteRule.java b/src/main/java/com/mojang/datafixers/TypeRewriteRule.java index 6f836c3382ed83e07000c0a5e4b6e9b69f8c45bf..37690009f2f3782e741f71272df525e9093794bc 100644 --- a/src/main/java/com/mojang/datafixers/TypeRewriteRule.java +++ b/src/main/java/com/mojang/datafixers/TypeRewriteRule.java @@ -105,20 +105,24 @@ public interface TypeRewriteRule { } } - static TypeRewriteRule orElse(final TypeRewriteRule first, final TypeRewriteRule second) { + // Yatopia start - Cadmium port + static TypeRewriteRule orElse(final TypeRewriteRule first, final TypeRewriteRule second) { + /* return orElse(first, () -> second); } static TypeRewriteRule orElse(final TypeRewriteRule first, final Supplier second) { - return new OrElse(first, second); - } + */ + return new OrElse(first, second); + } + // Yatopia end final class OrElse implements TypeRewriteRule { protected final TypeRewriteRule first; - protected final Supplier second; + protected final TypeRewriteRule second; // Yatopia - Cadmium port private final int hashCode; - public OrElse(final TypeRewriteRule first, final Supplier second) { + public OrElse(final TypeRewriteRule first, final TypeRewriteRule second) { // Yatopia - Cadmium port this.first = first; this.second = second; hashCode = Objects.hash(first, second); @@ -130,7 +134,7 @@ public interface TypeRewriteRule { if (view.isPresent()) { return view; } - return second.get().rewrite(type); + return second.rewrite(type); // Yatopia - Cadmium port } @Override @@ -159,9 +163,13 @@ public interface TypeRewriteRule { return new One(rule); } + // Yatopia start - Cadmium port + /* static TypeRewriteRule once(final TypeRewriteRule rule) { return orElse(rule, () -> one(once(rule))); } + */ + // Yatopia end static TypeRewriteRule checkOnce(final TypeRewriteRule rule, final Consumer> onFail) { // TODO: toggle somehow diff --git a/src/main/java/com/mojang/datafixers/Typed.java b/src/main/java/com/mojang/datafixers/Typed.java index 55c55beba58fb0505ccf8270a27d6c6ffc01c629..4b84d12d51eedf99d0b5d2168f31c8a2f819b432 100644 --- a/src/main/java/com/mojang/datafixers/Typed.java +++ b/src/main/java/com/mojang/datafixers/Typed.java @@ -184,11 +184,11 @@ public final class Typed { } public Typed> inj1(final Type type) { - return new Typed<>(DSL.or(this.type, type), ops, new Inj1().build(value)); + return new Typed<>(DSL.or(this.type, type), ops, Inj1.instance().build(value)); // Yatopia - Cadmium port } public Typed> inj2(final Type type) { - return new Typed<>(DSL.or(type, this.type), ops, new Inj2().build(value)); + return new Typed<>(DSL.or(type, this.type), ops, Inj2.instance().build(value)); // Yatopia - Cadmium port } public static Typed> pair(final Typed first, final Typed second) { diff --git a/src/main/java/com/mojang/datafixers/TypedOptic.java b/src/main/java/com/mojang/datafixers/TypedOptic.java index 10c5565709510609f8b6c4c39bb25b163dd878bc..9e2b6e46ad846ebd964b969932da84c671ebc651 100644 --- a/src/main/java/com/mojang/datafixers/TypedOptic.java +++ b/src/main/java/com/mojang/datafixers/TypedOptic.java @@ -131,7 +131,7 @@ public final class TypedOptic { DSL.and(newType, gType), fType, newType, - new Proj1<>() + Proj1.instance() // Yatopia - Cadmium port ); } @@ -142,7 +142,7 @@ public final class TypedOptic { DSL.and(fType, newType), gType, newType, - new Proj2<>() + Proj2.instance() // Yatopia - Cadmium port ); } @@ -153,7 +153,7 @@ public final class TypedOptic { DSL.or(newType, gType), fType, newType, - new Inj1<>() + Inj1.instance() // Yatopia - Cadmium port ); } @@ -164,7 +164,7 @@ public final class TypedOptic { DSL.or(fType, newType), gType, newType, - new Inj2<>() + Inj2.instance() // Yatopia - Cadmium port ); } diff --git a/src/main/java/com/mojang/datafixers/View.java b/src/main/java/com/mojang/datafixers/View.java index 4139dc999917d84c7dea5b02117a50af51206f04..58f856d95405f02c107fad12e830e98d1e1a8337 100644 --- a/src/main/java/com/mojang/datafixers/View.java +++ b/src/main/java/com/mojang/datafixers/View.java @@ -11,7 +11,6 @@ import com.mojang.datafixers.types.Type; import com.mojang.serialization.DynamicOps; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; public final class View implements App2 { @@ -73,9 +72,15 @@ public final class View implements App2 { return Objects.hash(type, newType, function); } - public Optional> rewrite(final PointFreeRule rule) { - return rule.rewrite(DSL.func(type, newType), function()).map(f -> create(type, newType, f)); + // Yatopia start - Cadmium port + public View rewrite(final PointFreeRule rule) { + PointFree> result = rule.rewrite(DSL.func(type, newType), function()); + if (result!=null) { + return create(type, newType, result); + } + return null; } + // Yatopia end public View rewriteOrNop(final PointFreeRule rule) { return DataFixUtils.orElse(rewrite(rule), this); diff --git a/src/main/java/com/mojang/datafixers/functions/Apply.java b/src/main/java/com/mojang/datafixers/functions/Apply.java index 32c55622c7a6de4d4eb4c70c62f4fbf11385667b..89793b452e95c9ae8bec42d22bebd762533a8046 100644 --- a/src/main/java/com/mojang/datafixers/functions/Apply.java +++ b/src/main/java/com/mojang/datafixers/functions/Apply.java @@ -3,11 +3,11 @@ package com.mojang.datafixers.functions; import com.mojang.datafixers.DSL; +import com.mojang.datafixers.DataFixUtils; // Yatopia - Cadmium port import com.mojang.datafixers.types.Type; import com.mojang.serialization.DynamicOps; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; final class Apply extends PointFree { @@ -31,20 +31,33 @@ final class Apply extends PointFree { return "(ap " + func.toString(level + 1) + "\n" + indent(level + 1) + arg.toString(level + 1) + "\n" + indent(level) + ")"; } + // Yatopia start - Cadmium port @Override - public Optional> all(final PointFreeRule rule, final Type type) { - return Optional.of(Functions.app( - rule.rewrite(DSL.func(argType, type), func).map(f1 -> (PointFree>) f1).orElse(func), - rule.rewrite(argType, arg).map(f -> (PointFree) f).orElse(arg), - argType - )); + public PointFree all(final PointFreeRule rule, final Type type) { + return Functions.app( + DataFixUtils.orElse(rule.rewrite(DSL.func(argType, type), func), func), + DataFixUtils.orElse(rule.rewrite(argType, arg), arg), + argType + ); } @Override - public Optional> one(final PointFreeRule rule, final Type type) { - return rule.rewrite(DSL.func(argType, type), func).map(f -> Optional.of(Functions.app(f, arg, argType))) - .orElseGet(() -> rule.rewrite(argType, arg).map(a -> Functions.app(func, a, argType))); + public PointFree one(final PointFreeRule rule, final Type type) { + final PointFree> result1 = rule.rewrite(DSL.func(argType, type), func); + + if (result1!=null) { + return Functions.app(result1, arg, argType); + } + + final PointFree result2 = rule.rewrite(argType, arg); + + if (result2!=null) { + return Functions.app(func, result2, argType); + } + + return null; } + // Yatopia end @Override public boolean equals(final Object o) { diff --git a/src/main/java/com/mojang/datafixers/functions/Comp.java b/src/main/java/com/mojang/datafixers/functions/Comp.java index a7a0ba66c715709801c2839018a6bb19df5cfdc6..6ea5ecad576f880783b9503ec370a5171d4f4da4 100644 --- a/src/main/java/com/mojang/datafixers/functions/Comp.java +++ b/src/main/java/com/mojang/datafixers/functions/Comp.java @@ -3,12 +3,12 @@ package com.mojang.datafixers.functions; import com.mojang.datafixers.DSL; +import com.mojang.datafixers.DataFixUtils; // Yatopia - Cadmium port import com.mojang.datafixers.types.Func; import com.mojang.datafixers.types.Type; import com.mojang.serialization.DynamicOps; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; final class Comp extends PointFree> { @@ -28,21 +28,34 @@ final class Comp extends PointFree> { } @Override - public Optional>> all(final PointFreeRule rule, final Type> type) { + // Yatopia start - Cadmium port + public PointFree> all(final PointFreeRule rule, final Type> type) { final Func funcType = (Func) type; - return Optional.of(Functions.comp( - middleType, - rule.rewrite(DSL.func(middleType, funcType.second()), first).map(f -> (PointFree>) f).orElse(first), - rule.rewrite(DSL.func(funcType.first(), middleType), second).map(f1 -> (PointFree>) f1).orElse(second) - )); + return Functions.comp( + middleType, + DataFixUtils.orElse(rule.rewrite(DSL.func(middleType, funcType.second()), first), first), + DataFixUtils.orElse(rule.rewrite(DSL.func(funcType.first(), middleType), second), second) + ); } @Override - public Optional>> one(final PointFreeRule rule, final Type> type) { + public PointFree> one(final PointFreeRule rule, final Type> type) { final Func funcType = (Func) type; - return rule.rewrite(DSL.func(middleType, funcType.second()), first).map(f -> Optional.of(Functions.comp(middleType, f, second))) - .orElseGet(() -> rule.rewrite(DSL.func(funcType.first(), middleType), second).map(s -> Functions.comp(middleType, first, s))); + final PointFree> result1 = rule.rewrite(DSL.func(middleType, funcType.second()), first); + + if (result1!=null) { + return Functions.comp(middleType, result1, second); + } + + final PointFree> result2 = rule.rewrite(DSL.func(funcType.first(), middleType), second); + + if (result2!=null) { + return Functions.comp(middleType, first, result2); + } + + return null; } + // Yatopia end @Override public boolean equals(final Object o) { diff --git a/src/main/java/com/mojang/datafixers/functions/PointFree.java b/src/main/java/com/mojang/datafixers/functions/PointFree.java index f7593e3baf71a8992aa094719d9a369399996d46..b743d6d7188c53ac04ec087d9f89b6f87374e811 100644 --- a/src/main/java/com/mojang/datafixers/functions/PointFree.java +++ b/src/main/java/com/mojang/datafixers/functions/PointFree.java @@ -7,7 +7,6 @@ import com.mojang.serialization.DynamicOps; import org.apache.commons.lang3.StringUtils; import javax.annotation.Nullable; -import java.util.Optional; import java.util.function.Function; public abstract class PointFree { @@ -30,13 +29,15 @@ public abstract class PointFree { public abstract Function, T> eval(); - Optional> all(final PointFreeRule rule, final Type type) { - return Optional.of(this); + // Yatopia start - Cadmium port + PointFree all(final PointFreeRule rule, final Type type) { + return this; } - Optional> one(final PointFreeRule rule, final Type type) { - return Optional.empty(); + PointFree one(final PointFreeRule rule, final Type type) { + return null; } + // Yatopia end @Override public final String toString() { diff --git a/src/main/java/com/mojang/datafixers/functions/PointFreeRule.java b/src/main/java/com/mojang/datafixers/functions/PointFreeRule.java index eaee52d2eb5c9051b1114ae307022d83984e5840..0e31fe8412cef25b209c72a628756d5b67cce79f 100644 --- a/src/main/java/com/mojang/datafixers/functions/PointFreeRule.java +++ b/src/main/java/com/mojang/datafixers/functions/PointFreeRule.java @@ -25,16 +25,23 @@ import org.apache.commons.lang3.ObjectUtils; import java.util.BitSet; import java.util.List; import java.util.Objects; -import java.util.Optional; import java.util.function.Function; import java.util.function.Supplier; public interface PointFreeRule { - Optional> rewrite(final Type type, final PointFree expr); + // Yatopia start - Cadmium port + PointFree rewrite(final Type type, final PointFree expr); - default Optional> rewrite(final View view) { - return rewrite(view.getFuncType(), view.function()).map(pf -> View.create(view.type(), view.newType(), pf)); + default View rewrite(final View view) { + PointFree> result = rewrite(view.getFuncType(), view.function()); + + if (result!=null) { + return View.create(view.type(), view.newType(), result); + } + + return null; } + // Yatopia end default PointFree rewriteOrNop(final Type type, final PointFree expr) { return DataFixUtils.orElse(rewrite(type, expr), expr); @@ -52,9 +59,11 @@ public interface PointFreeRule { INSTANCE; @Override - public Optional> rewrite(final Type type, final PointFree expr) { - return Optional.of(expr); + // Yatopia start - Cadmium port + public PointFree rewrite(final Type type, final PointFree expr) { + return expr; } + // Yatopia end @Override public PointFreeRule get() { @@ -67,25 +76,28 @@ public interface PointFreeRule { @SuppressWarnings("unchecked") @Override - public Optional> rewrite(final Type type, final PointFree expr) { + // Yatopia start - Cadmium port + public PointFree rewrite(final Type type, final PointFree expr) { if (expr instanceof Bang) { - return Optional.empty(); + return null; } if (type instanceof Func) { final Func func = (Func) type; if (func.second() instanceof EmptyPart) { - return Optional.of((PointFree) Functions.bang()); + return (PointFree) Functions.bang(); } } - return Optional.empty(); + return null; } + // Yatopia end } enum CompAssocLeft implements PointFreeRule { INSTANCE; @Override - public Optional> rewrite(final Type type, final PointFree expr) { + // Yatopia start - Cadmium port + public PointFree rewrite(final Type type, final PointFree expr) { if (expr instanceof Comp) { final Comp comp2 = (Comp) expr; final PointFree> second = comp2.second; @@ -94,13 +106,14 @@ public interface PointFreeRule { return swap(comp1, comp2); } } - return Optional.empty(); + return null; } + // Yatopia end @SuppressWarnings("unchecked") - private static Optional> swap(final Comp comp1, final Comp comp2raw) { + private static PointFree swap(final Comp comp1, final Comp comp2raw) { // Yatopia - Cadmium port final Comp comp2 = (Comp) comp2raw; - return Optional.of((PointFree) new Comp<>(comp1.middleType, new Comp<>(comp2.middleType, comp2.first, comp1.first), comp1.second)); + return (PointFree) new Comp<>(comp1.middleType, new Comp<>(comp2.middleType, comp2.first, comp1.first), comp1.second); // Yatopia - Cadmium port } } @@ -108,7 +121,7 @@ public interface PointFreeRule { INSTANCE; @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port if (expr instanceof Comp) { final Comp comp1 = (Comp) expr; final PointFree> first = comp1.first; @@ -117,13 +130,13 @@ public interface PointFreeRule { return swap(comp1, comp2); } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") - private static Optional> swap(final Comp comp1, final Comp comp2raw) { + private static PointFree swap(final Comp comp1, final Comp comp2raw) { // Yatopia - Cadmium port final Comp comp2 = (Comp) comp2raw; - return Optional.of((PointFree) new Comp<>(comp2.middleType, comp2.first, new Comp<>(comp1.middleType, comp2.second, comp1.second))); + return (PointFree) new Comp<>(comp2.middleType, comp2.first, new Comp<>(comp1.middleType, comp2.second, comp1.second)); // Yatopia - Cadmium port } } @@ -133,15 +146,15 @@ public interface PointFreeRule { // (ap lens id) -> id @SuppressWarnings("unchecked") @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port if (expr instanceof Apply) { final Apply apply = (Apply) expr; final PointFree> func = apply.func; if (func instanceof ProfunctorTransformer && Objects.equals(apply.arg, Functions.id())) { - return Optional.of((PointFree) Functions.id()); + return (PointFree) Functions.id(); // Yatopia - Cadmium port } } - return Optional.empty(); + return null; } } @@ -150,7 +163,7 @@ public interface PointFreeRule { // (ap f1 (ap f2 arg)) -> (ap (f1 ◦ f2) arg) @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port if (expr instanceof Apply) { final Apply applyFirst = (Apply) expr; if (applyFirst.arg instanceof Apply) { @@ -158,46 +171,54 @@ public interface PointFreeRule { return cap(applyFirst, applySecond); } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") - private Optional> cap(final Apply applyFirst, final Apply applySecond) { + private PointFree cap(final Apply applyFirst, final Apply applySecond) { // Yatopia - Cadmium port final PointFree func = applySecond.func; - return Optional.of((PointFree) Functions.app(Functions.comp(applyFirst.argType, applyFirst.func, (PointFree>) func), applySecond.arg, applySecond.argType)); + return (PointFree) Functions.app(Functions.comp(applyFirst.argType, applyFirst.func, (PointFree>) func), applySecond.arg, applySecond.argType); // Yatopia - Cadmium port } } interface CompRewrite extends PointFreeRule { @Override - default Optional> rewrite(final Type type, final PointFree expr) { + default PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port if (expr instanceof Comp) { final Comp comp = (Comp) expr; final PointFree> first = comp.first; final PointFree> second = comp.second; if (first instanceof Comp) { final Comp firstComp = (Comp) first; - return doRewrite(type, comp.middleType, firstComp.second, comp.second).map(result -> { + final PointFree result = doRewrite(type, comp.middleType, firstComp.second, comp.second); // Yatopia - Cadmium port + + if (result!=null) { // Yatopia - Cadmium port if (result instanceof Comp) { final Comp resultComp = (Comp) result; return buildLeftNested(resultComp, firstComp); } return buildRight(firstComp, result); - }); + } // Yatopia - Cadmium port + + return null; } if (second instanceof Comp) { final Comp secondComp = (Comp) second; - return doRewrite(type, comp.middleType, comp.first, secondComp.first).map(result -> { + final PointFree result = doRewrite(type, comp.middleType, comp.first, secondComp.first); // Yatopia - Cadmium port + + if (result!=null) { // Yatopia - Cadmium port if (result instanceof Comp) { final Comp resultComp = (Comp) result; return buildRightNested(secondComp, resultComp); } return buildLeft(result, secondComp); - }); + } + + return null; // Yatopia - Cadmium port } - return (Optional>) doRewrite(type, comp.middleType, comp.first, comp.second); + return (PointFree) doRewrite(type, comp.middleType, comp.first, comp.second); // Yatopia - Cadmium port } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") @@ -222,7 +243,7 @@ public interface PointFreeRule { return (PointFree) new Comp<>(comp2.middleType, comp2.first, new Comp<>(comp1.middleType, comp2.second, comp1.second)); } - Optional> doRewrite(Type type, Type middleType, PointFree> first, PointFree> second); + PointFree doRewrite(Type type, Type middleType, PointFree> first, PointFree> second); // Yatopia - Cadmium port } enum SortProj implements CompRewrite { @@ -230,7 +251,7 @@ public interface PointFreeRule { // (ap π1 f)◦(ap π2 g) -> (ap π2 g)◦(ap π1 f) @Override - public Optional> doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { + public PointFree doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { // Yatopia - Cadmium port if (first instanceof Apply && second instanceof Apply) { final Apply applyFirst = (Apply) first; final Apply applySecond = (Apply) second; @@ -253,11 +274,11 @@ public interface PointFreeRule { if (Objects.equals(fo, Optics.proj2()) && Objects.equals(so, Optics.proj1())) { final Func firstArg = (Func) applyFirst.argType; final Func secondArg = (Func) applySecond.argType; - return Optional.of(cap(firstArg, secondArg, applyFirst, applySecond)); + return cap(firstArg, secondArg, applyFirst, applySecond); // Yatopia - Cadmium port } } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") @@ -275,7 +296,7 @@ public interface PointFreeRule { // (ap i1 f)◦(ap i2 g) -> (ap i2 g)◦(ap i1 f) @Override - public Optional> doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { + public PointFree doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { // Yatopia - Cadmium port if (first instanceof Apply && second instanceof Apply) { final Apply applyFirst = (Apply) first; final Apply applySecond = (Apply) second; @@ -298,11 +319,11 @@ public interface PointFreeRule { if (Objects.equals(fo, Optics.inj2()) && Objects.equals(so, Optics.inj1())) { final Func firstArg = (Func) applyFirst.argType; final Func secondArg = (Func) applySecond.argType; - return Optional.of(cap(firstArg, secondArg, applyFirst, applySecond)); + return cap(firstArg, secondArg, applyFirst, applySecond); // Yatopia - Cadmium port } } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") @@ -320,7 +341,7 @@ public interface PointFreeRule { // Optic[o1] ◦ Optic[o2] -> Optic[o1 ◦ o2] @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port if (expr instanceof Comp) { final Comp comp = (Comp) expr; final PointFree> first = comp.first; @@ -328,10 +349,10 @@ public interface PointFreeRule { if (first instanceof ProfunctorTransformer && second instanceof ProfunctorTransformer) { final ProfunctorTransformer firstOptic = (ProfunctorTransformer) first; final ProfunctorTransformer secondOptic = (ProfunctorTransformer) second; - return Optional.of(cap(firstOptic, secondOptic)); + return cap(firstOptic, secondOptic); // Yatopia - Cadmium port } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") @@ -345,9 +366,8 @@ public interface PointFreeRule { INSTANCE; // (ap lens f)◦(ap lens g) -> (ap lens (f ◦ g)) - @SuppressWarnings("unchecked") @Override - public Optional> doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { + public PointFree doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { // Yatopia - Cadmium port if (first instanceof Apply && second instanceof Apply) { final Apply applyFirst = (Apply) first; final Apply applySecond = (Apply) second; @@ -364,17 +384,17 @@ public interface PointFreeRule { } } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } - private Optional> cap(final ProfunctorTransformer l1, final ProfunctorTransformer l2, final PointFree f1, final PointFree f2, final Func firstType, final Func secondType) { + private PointFree cap(final ProfunctorTransformer l1, final ProfunctorTransformer l2, final PointFree f1, final PointFree f2, final Func firstType, final Func secondType) { // Yatopia - Cadmium port return cap2(l1, (ProfunctorTransformer) l2, (PointFree>) f1, (PointFree>) f2, (Func) firstType, (Func) secondType); } - private Optional> cap2(final ProfunctorTransformer l1, final ProfunctorTransformer l2, final PointFree> f1, final PointFree> f2, final Func firstType, final Func secondType) { + private PointFree cap2(final ProfunctorTransformer l1, final ProfunctorTransformer l2, final PointFree> f1, final PointFree> f2, final Func firstType, final Func secondType) { // Yatopia - Cadmium port final PointFree, Function>> lens = (PointFree, Function>>) (PointFree) l1; final PointFree> arg = Functions.comp(firstType.first(), f1, f2); - return Optional.of((PointFree) Functions.app(lens, arg, DSL.func(secondType.first(), firstType.second()))); + return (PointFree) Functions.app(lens, arg, DSL.func(secondType.first(), firstType.second())); // Yatopia - Cadmium port } } @@ -382,15 +402,14 @@ public interface PointFreeRule { INSTANCE; // (fold g ◦ in) ◦ fold (f ◦ in) -> fold ( g ◦ f ◦ in), <== g ◦ in ◦ fold (f ◦ in) ◦ out == in ◦ fold (f ◦ in) ◦ out ◦ g <== g doesn't touch fold's index - @SuppressWarnings("unchecked") @Override - public Optional> doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { + public PointFree doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { // Yatopia - Cadmium port if (first instanceof Fold && second instanceof Fold) { // fold (_) ◦ fold (_) final Fold firstFold = (Fold) first; final Fold secondFold = (Fold) second; final RecursiveTypeFamily family = firstFold.aType.family(); - if (Objects.equals(family, secondFold.aType.family()) && firstFold.index == secondFold.index) { + if (firstFold.index==secondFold.index && Objects.equals(family, secondFold.aType.family())) { // Yatopia - Cadmium port // same fold final List> newAlgebra = Lists.newArrayList(); @@ -399,8 +418,8 @@ public interface PointFreeRule { boolean foundOne = false; for (int i = 0; i < family.size(); i++) { final RewriteResult firstAlgFunc = firstFold.algebra.apply(i); - final RewriteResult secondAlgFunc = secondFold.algebra.apply(i); final boolean firstId = Objects.equals(CompAssocRight.INSTANCE.rewriteOrNop(firstAlgFunc.view()).function(), Functions.id()); + /* final boolean secondId = Objects.equals(secondAlgFunc.view().function(), Functions.id()); if (firstId && secondId) { @@ -408,15 +427,37 @@ public interface PointFreeRule { } else if (!foundOne && !firstId && !secondId) { newAlgebra.add(getCompose(firstAlgFunc, secondAlgFunc)); foundOne = true; + */ + // Yatopia start - Cadmium port + if (firstId) { + final RewriteResult secondAlgFunc = secondFold.algebra.apply(i); + final boolean secondId = Objects.equals(secondAlgFunc.view().function(), Functions.id()); + + if (secondId) { + newAlgebra.add(firstFold.algebra.apply(i)); + } else { + return null; + } + } else if (!foundOne) { + final RewriteResult secondAlgFunc = secondFold.algebra.apply(i); + final boolean secondId = Objects.equals(secondAlgFunc.view().function(), Functions.id()); + + if (!secondId) { + newAlgebra.add(getCompose(firstAlgFunc, secondAlgFunc)); + foundOne = true; + } else { + return null; + } + // Yatopia end } else { - return Optional.empty(); + return null; // Yatopia - Cadmium port } } final Algebra algebra = new ListAlgebra("FusedSame", newAlgebra); - return Optional.of((PointFree) family.fold(algebra).apply(firstFold.index).view().function()); + return family.fold(algebra).apply(firstFold.index).view().function(); // Yatopia - Cadmium port } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } @SuppressWarnings("unchecked") @@ -431,13 +472,14 @@ public interface PointFreeRule { // (fold g ◦ in) ◦ fold (f ◦ in) -> fold ( g ◦ f ◦ in), <== g ◦ in ◦ fold (f ◦ in) ◦ out == in ◦ fold (f ◦ in) ◦ out ◦ g <== g doesn't touch fold's index @SuppressWarnings("unchecked") @Override - public Optional> doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { + public PointFree doRewrite(final Type type, final Type middleType, final PointFree> first, final PointFree> second) { // Yatopia - Cadmium port if (first instanceof Fold && second instanceof Fold) { // fold (_) ◦ fold (_) final Fold firstFold = (Fold) first; final Fold secondFold = (Fold) second; final RecursiveTypeFamily family = firstFold.aType.family(); - if (Objects.equals(family, secondFold.aType.family()) && firstFold.index == secondFold.index) { + // if (Objects.equals(family, secondFold.aType.family()) && firstFold.index == secondFold.index) { + if (firstFold.index==secondFold.index && Objects.equals(family, secondFold.aType.family())) { // Yatopia - Cadmium port // same fold final List> newAlgebra = Lists.newArrayList(); @@ -460,46 +502,57 @@ public interface PointFreeRule { // TODO: verify that this is enough for (int i = 0; i < family.size(); i++) { final RewriteResult firstAlgFunc = firstFold.algebra.apply(i); + // Yatopia start - Cadmium port + if (firstAlgFunc.recData().intersects(secondModifies)) { + // outer function depends on the result of the inner one + return null; + } + // Yatopia end final RewriteResult secondAlgFunc = secondFold.algebra.apply(i); + /* final PointFree firstF = CompAssocRight.INSTANCE.rewriteOrNop(firstAlgFunc.view()).function(); final PointFree secondF = CompAssocRight.INSTANCE.rewriteOrNop(secondAlgFunc.view()).function(); final boolean firstId = Objects.equals(firstF, Functions.id()); final boolean secondId = Objects.equals(secondF, Functions.id()); if (firstAlgFunc.recData().intersects(secondModifies) || secondAlgFunc.recData().intersects(firstModifies)) { + */ + // Yatopia start - Cadmium port + if (secondAlgFunc.recData().intersects(firstModifies)) { // outer function depends on the result of the inner one - return Optional.empty(); + return null; } - if (firstId) { + // Yatopia end + if (Objects.equals(CompAssocRight.INSTANCE.rewriteOrNop(firstAlgFunc.view()).function(), Functions.id())) { // Yatopia - Cadmium port newAlgebra.add(secondAlgFunc); - } else if (secondId) { + } else if (Objects.equals(CompAssocRight.INSTANCE.rewriteOrNop(secondAlgFunc.view()).function(), Functions.id())) { // Yatopia - Cadmium port newAlgebra.add(firstAlgFunc); } else { - return Optional.empty(); + return null; // Yatopia - Cadmium port } } // have new algebra - make a new fold final Algebra algebra = new ListAlgebra("FusedDifferent", newAlgebra); - return Optional.of((PointFree) family.fold(algebra).apply(firstFold.index).view().function()); + return family.fold(algebra).apply(firstFold.index).view().function(); // Yatopia - Cadmium port } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } } /*final class ReflexCata implements Rule { @SuppressWarnings("unchecked") @Override - public Optional> rewrite(final Type type, final PF expr) { + public PF rewrite(final Type type, final PF expr) { // Yatopia - Cadmium port if (type instanceof Type.Func && expr instanceof PF.Cata) { final Type.Func funcType = (Type.Func) type; final PF.Cata cata = (PF.Cata) expr; // TODO better equality if (Objects.equals(cata.alg, PF.genBF(cata.family.to)) && Objects.equals(funcType.first, funcType.second)) { - return Optional.of((PF) PF.id()); + return (PF) PF.id(); // Yatopia - Cadmium port } } - return Optional.empty(); + return null; // Yatopia - Cadmium port } }*/ @@ -518,14 +571,20 @@ public interface PointFreeRule { this.rules = ImmutableList.copyOf(rules); } + // Yatopia start - Cadmium port @Override - public Optional> rewrite(final Type type, final PointFree expr) { - Optional> result = Optional.of(expr); + public PointFree rewrite(final Type type, final PointFree expr) { + PointFree result = expr; for (final Supplier rule : rules) { - result = result.flatMap(pf -> rule.get().rewrite(type, pf)); + if (result==null) { + return null; + } + + result = rule.get().rewrite(type, result); } return result; } + // Yatopia end @Override public boolean equals(final Object obj) { @@ -546,30 +605,28 @@ public interface PointFreeRule { } static PointFreeRule orElse(final PointFreeRule first, final PointFreeRule second) { - return new OrElse(first, () -> second); - } - - static PointFreeRule orElseStrict(final PointFreeRule first, final Supplier second) { return new OrElse(first, second); } final class OrElse implements PointFreeRule { protected final PointFreeRule first; - protected final Supplier second; + protected final PointFreeRule second; // Yatopia - Cadmium port - public OrElse(final PointFreeRule first, final Supplier second) { + public OrElse(final PointFreeRule first, final PointFreeRule second) { // Yatopia - Cadmium port this.first = first; this.second = second; } + // Yatopia start - Cadmium port @Override - public Optional> rewrite(final Type type, final PointFree expr) { - final Optional> view = first.rewrite(type, expr); - if (view.isPresent()) { + public PointFree rewrite(final Type type, final PointFree expr) { + final PointFree view = first.rewrite(type, expr); + if (view!=null) { return view; } - return second.get().rewrite(type, expr); + return second.rewrite(type, expr); } + // Yatopia end @Override public boolean equals(final Object obj) { @@ -589,6 +646,42 @@ public interface PointFreeRule { } } + // Yatopia start - Cadmium port + final class OrElseStrict implements PointFreeRule { + protected final PointFreeRule rule; + + public OrElseStrict(final PointFreeRule rule) { + this.rule = rule; + } + + @Override + public PointFree rewrite(final Type type, final PointFree expr) { + final PointFree view = rule.rewrite(type, expr); + if (view!=null) { + return view; + } + return expr.one(this, type); + } + + @Override + public boolean equals(final Object obj) { + if (obj == this) { + return true; + } + if (!(obj instanceof OrElse)) { + return false; + } + final OrElse that = (OrElse) obj; + return Objects.equals(rule, that.first); + } + + @Override + public int hashCode() { + return Objects.hash(rule); + } + } + // Yatopia end + static PointFreeRule all(final PointFreeRule rule) { return new All(rule); } @@ -598,7 +691,7 @@ public interface PointFreeRule { } static PointFreeRule once(final PointFreeRule rule) { - return orElseStrict(rule, () -> one(once(rule))); + return new OrElseStrict(rule); // Yatopia - Cadmium port } static PointFreeRule many(final PointFreeRule rule) { @@ -617,7 +710,7 @@ public interface PointFreeRule { } @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port return expr.all(rule, type); } @@ -647,7 +740,7 @@ public interface PointFreeRule { } @Override - public Optional> rewrite(final Type type, final PointFree expr) { + public PointFree rewrite(final Type type, final PointFree expr) { // Yatopia - Cadmium port return expr.one(rule, type); } @@ -676,17 +769,22 @@ public interface PointFreeRule { this.rule = rule; } + // Yatopia start - Cadmium port @Override - public Optional> rewrite(final Type type, final PointFree expr) { - Optional> result = Optional.of(expr); + public PointFree rewrite(final Type type, final PointFree expr) { + PointFree result = expr; while (true) { - final Optional> newResult = result.flatMap(e -> rule.rewrite(type, e).map(r -> r)); - if (!newResult.isPresent()) { + if (result==null) { + return null; + } + final PointFree newResult = rule.rewrite(type, result); + if (newResult==null) { return result; } result = newResult; } } + // Yatopia end @Override public boolean equals(final Object o) { diff --git a/src/main/java/com/mojang/datafixers/optics/IdAdapter.java b/src/main/java/com/mojang/datafixers/optics/IdAdapter.java index 3f1793aca685a627d0e925d037f663a257522d8d..0e4bf4b58710be135e6573ccff3f8c0a2a41191a 100644 --- a/src/main/java/com/mojang/datafixers/optics/IdAdapter.java +++ b/src/main/java/com/mojang/datafixers/optics/IdAdapter.java @@ -3,6 +3,14 @@ package com.mojang.datafixers.optics; class IdAdapter implements Adapter { + // Yatopia start - Cadmium port + private static final Adapter INSTANCE = new IdAdapter<>(); + + private IdAdapter() { + + } + // Yatopia end + @Override public S from(final S s) { return s; @@ -22,4 +30,11 @@ class IdAdapter implements Adapter { public String toString() { return "id"; } + + // Yatopia start - Cadmium port + @SuppressWarnings("unchecked") + public static Adapter instance() { + return (Adapter) INSTANCE; + } + // Yatopia end } diff --git a/src/main/java/com/mojang/datafixers/optics/Inj1.java b/src/main/java/com/mojang/datafixers/optics/Inj1.java index 8c56955c9d6c3d6021d5c9fda452e39774ac587c..4813f335cd536d1b32b9095dbd76e18c22a6dea1 100644 --- a/src/main/java/com/mojang/datafixers/optics/Inj1.java +++ b/src/main/java/com/mojang/datafixers/optics/Inj1.java @@ -5,6 +5,14 @@ package com.mojang.datafixers.optics; import com.mojang.datafixers.util.Either; public final class Inj1 implements Prism, Either, F, F2> { + // Yatopia start - Cadmium port + private static final Inj1 INSTANCE = new Inj1<>(); + + private Inj1() { + + } + // Yatopia end + @Override public Either, F> match(final Either either) { return either.map(Either::right, g -> Either.left(Either.right(g))); @@ -24,4 +32,11 @@ public final class Inj1 implements Prism, Either, public boolean equals(final Object obj) { return obj instanceof Inj1; } + + // Yatopia start - Cadmium port + @SuppressWarnings("unchecked") + public static Inj1 instance() { + return (Inj1) INSTANCE; + } + // Yatopia end } diff --git a/src/main/java/com/mojang/datafixers/optics/Inj2.java b/src/main/java/com/mojang/datafixers/optics/Inj2.java index 2f95715f310faec6e43c715cbf204a092bfc107e..ad2400ccdb159cbf8b882d0b5afc53d3e220f770 100644 --- a/src/main/java/com/mojang/datafixers/optics/Inj2.java +++ b/src/main/java/com/mojang/datafixers/optics/Inj2.java @@ -2,9 +2,18 @@ // Licensed under the MIT license. package com.mojang.datafixers.optics; +import com.mojang.datafixers.DataFixerBuilder; // Yatopia - Cadmium port import com.mojang.datafixers.util.Either; public final class Inj2 implements Prism, Either, G, G2> { + // Yatopia start - Cadmium port + private static final Inj2 INSTANCE = new Inj2<>(); + + private Inj2() { + + } + // Yatopia end + @Override public Either, G> match(final Either either) { return either.map(f -> Either.left(Either.left(f)), Either::right); @@ -24,4 +33,11 @@ public final class Inj2 implements Prism, Either, public boolean equals(final Object obj) { return obj instanceof Inj2; } + + // Yatopia start - Cadmium port + @SuppressWarnings("unchecked") + public static Inj2 instance() { + return (Inj2) INSTANCE; + } + // Yatopia end } diff --git a/src/main/java/com/mojang/datafixers/optics/Optics.java b/src/main/java/com/mojang/datafixers/optics/Optics.java index 4bf469df6a9092a554df4dafdbfe37f51da28f32..cec492b8e633adedbf6c4d83cf257f640412de06 100644 --- a/src/main/java/com/mojang/datafixers/optics/Optics.java +++ b/src/main/java/com/mojang/datafixers/optics/Optics.java @@ -65,7 +65,7 @@ public abstract class Optics { } public static Adapter id() { - return new IdAdapter<>(); + return IdAdapter.instance(); // Yatopia - Cadmium port } public static Adapter adapter(final Function from, final Function to) { @@ -223,19 +223,19 @@ public abstract class Optics { } public static Proj1 proj1() { - return new Proj1<>(); + return Proj1.instance(); // Yatopia - Cadmium port } public static Proj2 proj2() { - return new Proj2<>(); + return Proj2.instance(); // Yatopia - Cadmium port } public static Inj1 inj1() { - return new Inj1<>(); + return Inj1.instance(); // Yatopia - Cadmium port } public static Inj2 inj2() { - return new Inj2<>(); + return Inj2.instance(); // Yatopia - Cadmium port } /*public static Optic, Either, A, B> choosing(final Optic first, final Optic second) { diff --git a/src/main/java/com/mojang/datafixers/optics/Proj1.java b/src/main/java/com/mojang/datafixers/optics/Proj1.java index 488e4cd9a87178a441eda0ee5bff9c5feca69e6d..ae8850e52feeff51f61c4527c792446ef5e23344 100644 --- a/src/main/java/com/mojang/datafixers/optics/Proj1.java +++ b/src/main/java/com/mojang/datafixers/optics/Proj1.java @@ -5,6 +5,14 @@ package com.mojang.datafixers.optics; import com.mojang.datafixers.util.Pair; public final class Proj1 implements Lens, Pair, F, F2> { + // Yatopia start - Cadmium port + private static final Proj1 INSTANCE = new Proj1<>(); + + private Proj1() { + + } + // Yatopia end + @Override public F view(final Pair pair) { return pair.getFirst(); @@ -24,4 +32,11 @@ public final class Proj1 implements Lens, Pair, F, F public boolean equals(final Object obj) { return obj instanceof Proj1; } + + // Yatopia start - Cadmium port + @SuppressWarnings("unchecked") + public static Proj1 instance() { + return (Proj1) INSTANCE; + } + // Yatopia end } diff --git a/src/main/java/com/mojang/datafixers/optics/Proj2.java b/src/main/java/com/mojang/datafixers/optics/Proj2.java index 3bd3689115f25d34fceaa8f8f915c21656267af5..76d05449689808e3d0435aea7480dcad134730f1 100644 --- a/src/main/java/com/mojang/datafixers/optics/Proj2.java +++ b/src/main/java/com/mojang/datafixers/optics/Proj2.java @@ -5,6 +5,14 @@ package com.mojang.datafixers.optics; import com.mojang.datafixers.util.Pair; public final class Proj2 implements Lens, Pair, G, G2> { + // Yatopia start - Cadmium port + private static final Proj2 INSTANCE = new Proj2<>(); + + private Proj2() { + + } + // Yatopia end + @Override public G view(final Pair pair) { return pair.getSecond(); @@ -24,4 +32,11 @@ public final class Proj2 implements Lens, Pair, G, G public boolean equals(final Object obj) { return obj instanceof Proj2; } + + // Yatopia start - Cadmium port + @SuppressWarnings("unchecked") + public static Proj2 instance() { + return (Proj2) INSTANCE; + } + // Yatopia end } diff --git a/src/main/java/com/mojang/datafixers/types/Type.java b/src/main/java/com/mojang/datafixers/types/Type.java index 4592da3c75e639164d10cc05518c27d8f488a103..53ab2332aaff5970e5f3825734906fc7f1f8c53c 100644 --- a/src/main/java/com/mojang/datafixers/types/Type.java +++ b/src/main/java/com/mojang/datafixers/types/Type.java @@ -87,7 +87,7 @@ public abstract class Type implements App { } public Optional> everywhere(final TypeRewriteRule rule, final PointFreeRule optimizationRule, final boolean recurse, final boolean checkIndex) { - final TypeRewriteRule rule2 = TypeRewriteRule.seq(TypeRewriteRule.orElse(rule, TypeRewriteRule::nop), TypeRewriteRule.all(TypeRewriteRule.everywhere(rule, optimizationRule, recurse, checkIndex), recurse, checkIndex)); + final TypeRewriteRule rule2 = TypeRewriteRule.seq(TypeRewriteRule.orElse(rule, TypeRewriteRule.Nop.INSTANCE), TypeRewriteRule.all(TypeRewriteRule.everywhere(rule, optimizationRule, recurse, checkIndex), recurse, checkIndex)); // Yatopia - Cadmium port return rewrite(rule2, optimizationRule); } @@ -187,7 +187,17 @@ public abstract class Type implements App { }); if (ref.getValue() != null) { - Optional> result = rule.rewrite(this).flatMap(r -> r.view().rewrite(fRule).map(view -> RewriteResult.create(view, r.recData()))); + // Yatopia start - Cadmium port + Optional> result = rule.rewrite(this).flatMap(r -> { + View view = r.view().rewrite(fRule); + + if (view!=null) { + return Optional.of(RewriteResult.create(view, r.recData())); + } + + return Optional.empty(); + }); + // Yatopia end REWRITE_CACHE.put(key, result); pending.complete(result); PENDING_REWRITE_CACHE.remove(key);