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 java.util.function.*;
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.*;
// returns minimal time needed
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
class main {
static long benchFor1(String desc, Runnable r) {
return benchFor1Second(desc, r);
}
static long benchFor1(Runnable r) {
return benchFor1Second(r);
}
// returns result of function
static A benchFor1(IF0 f) { return benchFor1(str(f), f); }
static A benchFor1(String desc, IF0 f) {
return benchFor1Second(desc, f);
}
// returns minimal time needed
static long benchFor1Second(String desc, Runnable r) {
return benchForNSeconds(desc, r, 1);
}
static long benchFor1Second(Runnable r) {
return benchFor1Second(str(r), r);
}
// returns result of function
static A benchFor1Second(IF0 f) { return benchFor1Second(str(f), f); }
static A benchFor1Second(String desc, IF0 f) {
benchFor1Second(new Runnable() { public void run() { try { f.get() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f!"; }});
return f.get();
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
// returns result of function
static A benchForNSeconds(double n, IF0 f) { return benchForNSeconds(n, str(f), f); }
static A benchForNSeconds(double n, String desc, IF0 f) {
benchForNSeconds(new Runnable() { public void run() { try { f.get() ;
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "f!"; }}, n);
return f.get();
}
// returns minimal time needed
static long benchForNSeconds(Runnable r, double n) { return benchForNSeconds(str(r), r, n); }
static long benchForNSeconds(String desc, Runnable r, double n) {
long start = sysNow(), min = -1, print = sysNow();
int seconds = 1;
long count = 0, sum = 0;
while (seconds <= n) {
long time = nanos();
r.run();
++count;
time = nanos()-time;
min = min < 0 ? time : min(min, time);
sum += time;
double avg = doubleRatio(sum, count);
if (sysNow() >= start+seconds*1000) {
printAndSetConsoleTitleIfMain(
"avg=" + formatDouble(nanosToMS(avg), 3) + " ms, " +
"min=" + formatDouble(nanosToMS(min), 3) + " ms: " +
desc + " (" + seconds + "/" + n + " s, last=" + formatDouble(nanosToMS(time), 3) + " ms, " + n2(count) + "/s)");
++seconds;
count = sum = 0;
}
}
return min;
}
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 long sysNow() {
ping();
return System.nanoTime()/1000000;
}
static long nanos() {
return nanoTime();
}
static int min(int a, int b) {
return Math.min(a, b);
}
static long min(long a, long b) {
return Math.min(a, b);
}
static float min(float a, float b) { return Math.min(a, b); }
static float min(float a, float b, float c) { return min(min(a, b), c); }
static double min(double a, double b) {
return Math.min(a, b);
}
static double min(double[] c) {
double x = Double.MAX_VALUE;
for (double d : c) x = Math.min(x, d);
return x;
}
static float min(float[] c) {
float x = Float.MAX_VALUE;
for (float d : c) x = Math.min(x, d);
return x;
}
static byte min(byte[] c) {
byte x = 127;
for (byte d : c) if (d < x) x = d;
return x;
}
static short min(short[] c) {
short x = 0x7FFF;
for (short d : c) if (d < x) x = d;
return x;
}
static int min(int[] c) {
int x = Integer.MAX_VALUE;
for (int d : c) if (d < x) x = d;
return x;
}
static double doubleRatio(double x, double y) {
return y == 0 ? 0 : x/y;
}
static void printAndSetConsoleTitleIfMain(String s) {
print(s);
if (isMain()) consoleTitle(s);
}
static String formatDouble(double d, int digits) {
String format = digits <= 0 ? "0" : "0." + rep(digits, '#');
return decimalFormatEnglish(format, d);
}
static String formatDouble(double d) {
return str(d);
}
static double nanosToMS(double nanoseconds) {
return nsToMS(nanoseconds);
}
static String n2(long l) { return formatWithThousands(l); }
static String n2(AtomicLong l) { return n2(l.get()); }
static String n2(Collection l) { return n2(l(l)); }
static String n2(Map map) { return n2(l(map)); }
static String n2(double l, String singular) {
return empty(singular) ? str(l) : n2(l, singular, singular + "s");
}
static String n2(double l, String singular, String plural) {
if (fraction(l) == 0)
return n2((long) l, singular, plural);
else
return l + " " + plural;
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(long l, String singular) {
return empty(singular) ? n2(l) : n_fancy2(l, singular, singular + "s");
}
static String n2(Collection l, String singular) {
return n2(l(l), singular);
}
static String n2(Collection l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(Map m, String singular, String plural) {
return n_fancy2(m, singular, plural);
}
static String n2(Map m, String singular) {
return n2(l(m), singular);
}
static String n2(long[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); }
static void _handleError(Error e) {
call(javax(), "_handleError", e);
}
//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() {
//ifdef useNewPing
newPing();
//endifdef
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 long nanoTime() {
return System.nanoTime();
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static boolean printAlsoToSystemOut = true;
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal