[DSC] Implement Demonic Covenant

This commit is contained in:
theelk801 2025-04-30 10:16:07 -04:00
parent 51b24a7b8a
commit 563e7fb712
3 changed files with 198 additions and 0 deletions

View file

@ -58,6 +58,9 @@ import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ -2230,6 +2233,71 @@ public final class CardUtil {
return stream.filter(clazz::isInstance).map(clazz::cast).filter(Objects::nonNull);
}
public static <T> boolean checkAnyPairs(Collection<T> collection, BiPredicate<T, T> predicate) {
return streamPairsWithMap(collection, (t1, t2) -> predicate.test(t1, t2)).anyMatch(x -> x);
}
public static <T> Stream<T> streamAllPairwiseMatches(Collection<T> collection, BiPredicate<T, T> predicate) {
return streamPairsWithMap(
collection,
(t1, t2) -> predicate.test(t1, t2)
? Stream.of(t1, t2)
: Stream.<T>empty()
).flatMap(Function.identity()).distinct();
}
private static class IntPairIterator implements Iterator<AbstractMap.SimpleImmutableEntry<Integer, Integer>> {
private final int amount;
private int firstCounter = 0;
private int secondCounter = 1;
IntPairIterator(int amount) {
this.amount = amount;
}
@Override
public boolean hasNext() {
return firstCounter + 1 < amount;
}
@Override
public AbstractMap.SimpleImmutableEntry<Integer, Integer> next() {
AbstractMap.SimpleImmutableEntry<Integer, Integer> value
= new AbstractMap.SimpleImmutableEntry(firstCounter, secondCounter);
secondCounter++;
if (secondCounter == amount) {
firstCounter++;
secondCounter = firstCounter + 1;
}
return value;
}
public int getMax() {
// amount choose 2
return (amount * amount - amount) / 2;
}
}
public static <T, U> Stream<U> streamPairsWithMap(Collection<T> collection, BiFunction<T, T, U> function) {
if (collection.size() < 2) {
return Stream.empty();
}
List<T> list;
if (collection instanceof List) {
list = (List<T>) collection;
} else {
list = new ArrayList<>(collection);
}
IntPairIterator it = new IntPairIterator(list.size());
return Stream
.generate(it::next)
.limit(it.getMax())
.map(pair -> function.apply(
list.get(pair.getKey()),
list.get(pair.getValue())
));
}
public static void AssertNoControllerOwnerPredicates(Target target) {
List<Predicate> list = new ArrayList<>();
Predicates.collectAllComponents(target.getFilter().getPredicates(), target.getFilter().getExtraPredicates(), list);