import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
// TreeSet variant allowing multiple elements which are considered
// equal by the comparator but not by eq().
// All operations O(log n) with n being the number of _different_ elements.
import static x30_pkg.x30_util.DynamicObject;
class main {
static class TreeSetWithDuplicates extends AbstractSet {
NavigableSet> sets;
Comparator comparator;
int size;
TreeSetWithDuplicates() { this((Comparator) null); }
TreeSetWithDuplicates(Comparator comparator) {
this.comparator = comparator;
sets = new TreeSet>(
comparator == null
? (setA, setB) -> cmp(main.first(setA), main.first(setB))
: (setA, setB) -> comparator.compare(main.first(setA), main.first(setB))
);
}
protected TreeSetWithDuplicates(NavigableSet> sets) {
this.sets = sets;}
/*swappable*/ CompactLinkedHashSet makeInnerSet() {
return new CompactLinkedHashSet();
}
public boolean add(A a) {
var probe = probe(a);
CompactLinkedHashSet found = navigableSet_find(sets, probe);
if (found != null) {
if (!found.add(a)) return false;
} else
sets.add(probe);
++size;
return true;
}
public boolean remove(Object a) {
CompactLinkedHashSet newSet = makeInnerSet();
newSet.add((A) a);
CompactLinkedHashSet found = navigableSet_find(sets, newSet);
if (found == null) return false;
if (l(found) == 1)
sets.remove(found);
else
found.remove(a);
--size;
return true;
}
A first() {
return main.first(main.first(sets));
}
A last() {
var last = sets.last();
return last == null ? null : last.last();
}
public IterableIterator iterator() {
return nestedIterator(sets, __1 -> __1.iterator());
}
public int size() { return size; }
// may return null
Set tiedForFirst() { return main.first(sets); }
CompactLinkedHashSet probe(Object a) {
return addAndReturnCollection(makeInnerSet(), (A) a);
}
public boolean contains(Object a) {
return navigableSet_find(sets, probe(a)) != null;
}
}
static int cmp(Number a, Number b) {
return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue());
}
static int cmp(double a, double b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(int a, int b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(long a, long b) {
return a < b ? -1 : a == b ? 0 : 1;
}
static int cmp(Object a, Object b) {
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
return ((Comparable) a).compareTo(b);
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static Character first(CharSequence s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static Byte first(byte[] l) {
return empty(l) ? null : l[0];
}
static A navigableSet_find(NavigableSet set, A a) {
if (set == null || a == null) return null;
A b = set.floor(a);
return b != null && sortedSet_compare(set, a, b) == 0 ? b : null;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
// TODO: clean up these type signatures?
static > IterableIterator nestedIterator(final Iterable c, final F1 makeInnerIterator) {
return nestedIterator(iterator(c), makeInnerIterator);
}
static > IterableIterator nestedIterator(Collection c, final IF1 makeInnerIterator) {
return nestedIterator(iterator(c), makeInnerIterator);
}
static > IterableIterator nestedIterator(Iterator it1, IF1 makeInnerIterator) {
return nestedIterator(it1, toF1(makeInnerIterator));
}
static > IterableIterator nestedIterator(final Iterator it1, final F1 makeInnerIterator) {
if (it1 == null || !it1.hasNext()) return emptyIterableIterator();
return iff(new F0() {
A a;
Iterator innerIterator;
{ nextOuter(); }
void nextOuter() {
a = it1.next();
innerIterator = makeInnerIterator.get(a);
}
public Object get() {
while (true) { ping();
if (innerIterator != null && innerIterator.hasNext())
return innerIterator.next();
if (!it1.hasNext()) return endMarker();
nextOuter();
}
}
});
}
static > IterableIterator nestedIterator(IF1 makeInnerIterator, Iterator it1) {
return nestedIterator(it1, makeInnerIterator);
}
static > IterableIterator nestedIterator(IF1 makeInnerIterator, Collection l) {
return nestedIterator(l, makeInnerIterator);
}
static > A addAndReturnCollection(A c, B a) {
if (c != null) c.add(a);
return c;
}
static boolean empty(Collection c) { return c == null || c.isEmpty(); }
static boolean empty(Iterable c) { return c == null || !c.iterator().hasNext(); }
static boolean empty(CharSequence s) { return s == null || s.length() == 0; }
static boolean empty(Map map) { return map == null || map.isEmpty(); }
static boolean empty(Object[] o) { return o == null || o.length == 0; }
static boolean empty(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(double[] a) { return a == null || a.length == 0; }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static int sortedSet_compare(SortedSet set, A a, A b) {
return compareWithComparator(set.comparator(), a, b);
}
static int iteratorCount_int_close(Iterator i) { try {
int n = 0;
if (i != null) while (i.hasNext()) { i.next(); ++n; }
if (i instanceof AutoCloseable) ((AutoCloseable) i).close();
return n;
} catch (Exception __e) { throw rethrow(__e); } }
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static Iterator emptyIterator() {
return Collections.emptyIterator();
}
static F1 toF1(final Object f) {
return functionToF1(f);
}
static IterableIterator emptyIterableIterator_instance = new IterableIterator() {
public Object next() { throw fail(); }
public boolean hasNext() { return false; }
};
static IterableIterator emptyIterableIterator() {
return emptyIterableIterator_instance;
}
// f: func -> A | endMarker
static IterableIterator iff(Object f) {
return iteratorFromFunction_withEndMarker(f);
}
// can't use type parameter because end marker
static IterableIterator iff(F0 f) {
return iteratorFromFunction_withEndMarker(f);
}
static IterableIterator iff(IF0 f) {
return iteratorFromFunction_withEndMarker(f);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static Object endMarker() {
return iteratorFromFunction_endMarker;
}
// requires ugly casting when used (O -> A)
static Object iteratorFromFunction_endMarker = new Object();
// f: func -> A | endMarker
static IterableIterator iteratorFromFunction_withEndMarker(final Object f) {
class IFF extends IterableIterator {
A a;
boolean have, done;
public boolean hasNext() {
getNext();
return !done;
}
public A next() {
getNext();
if (done) throw fail();
A _a = a;
a = null;
have = false;
return _a;
}
void getNext() {
if (done || have) return;
Object o = callF(f);
if (o == iteratorFromFunction_endMarker)
{ done = true; return; }
a = (A) o;
have = true;
}
};
return new IFF();
}
// optimized version for F0 argument; TODO: do same for IF0
static IterableIterator iteratorFromFunction_withEndMarker(final F0 f) {
return iteratorFromFunction_withEndMarker_f0(f);
}
static RuntimeException fail() { throw new RuntimeException("fail"); }
static RuntimeException fail(Throwable e) { throw asRuntimeException(e); }
static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); }
static RuntimeException fail(Object... objects) { throw new Fail(objects); }
static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static String getType(Object o) {
return getClassName(o);
}
static long getFileSize(String path) {
return path == null ? 0 : new File(path).length();
}
static long getFileSize(File f) {
return f == null ? 0 : f.length();
}
static int compareWithComparator(Comparator comparator, A a, A b) {
return comparator == null ? cmp(a, b) : comparator.compare(a, b);
}
static RuntimeException rethrow(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static RuntimeException rethrow(String msg, Throwable t) {
throw new RuntimeException(msg, t);
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Runnable r) { { if (r != null) r.run(); } return null; }
static Object callF(Object f, Object... args) {
if (f instanceof String)
return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static Object call_withVarargs(Object o, String method, Object... args) { try {
if (o == null) return null;
if (o instanceof Class) {
Class c = (Class) o;
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findStaticMethod(method, args);
if (me != null)
return invokeMethod(me, null, args);
// try varargs
List methods = cache.cache.get(method);
if (methods != null) methodSearch: for (Method m : methods) {
{ if (!(m.isVarArgs())) continue; }
{ if (!(isStaticMethod(m))) continue; }
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, null, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found");
} else {
Class c = o.getClass();
_MethodCache cache = callOpt_getCache(c);
Method me = cache.findMethod(method, args);
if (me != null)
return invokeMethod(me, o, args);
// try varargs
List methods = cache.cache.get(method);
if (methods != null) methodSearch: for (Method m : methods) {
{ if (!(m.isVarArgs())) continue; }
Object[] newArgs = massageArgsForVarArgsCall(m, args);
if (newArgs != null)
return invokeMethod(m, o, newArgs);
}
throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found");
}
} catch (Exception __e) { throw rethrow(__e); } }
static F1 functionToF1(final Object f) {
if (isString(f)) return mainFunctionToF1((String) f);
if (f instanceof F1) return (F1) f;
return new F1