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 java.awt.geom.*;
import javax.imageio.*;
import java.math.*;
import java.awt.geom.*;
import static x30_pkg.x30_util.DynamicObject;
import java.text.NumberFormat;
class main {
static class G22RegionThinner_v2 implements Steppable , IFieldsToList{
IImageRegion originalRegion;
G22RegionThinner_v2() {}
G22RegionThinner_v2(IImageRegion originalRegion) {
this.originalRegion = originalRegion;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + originalRegion + ")"; }public Object[] _fieldsToList() { return new Object[] {originalRegion}; }
Rect bounds;
IImageRegion region;
boolean phase1done = false;
final public G22RegionThinner_v2 setDebug(boolean debug){ return debug(debug); }
public G22RegionThinner_v2 debug(boolean debug) { this.debug = debug; return this; } final public boolean getDebug(){ return debug(); }
public boolean debug() { return debug; }
boolean debug = false;
final public G22RegionThinner_v2 setDoFinalStep(boolean doFinalStep){ return doFinalStep(doFinalStep); }
public G22RegionThinner_v2 doFinalStep(boolean doFinalStep) { this.doFinalStep = doFinalStep; return this; } final public boolean getDoFinalStep(){ return doFinalStep(); }
public boolean doFinalStep() { return doFinalStep; }
boolean doFinalStep = true;
final public G22RegionThinner_v2 setLastPhaseThreshold1(int lastPhaseThreshold1){ return lastPhaseThreshold1(lastPhaseThreshold1); }
public G22RegionThinner_v2 lastPhaseThreshold1(int lastPhaseThreshold1) { this.lastPhaseThreshold1 = lastPhaseThreshold1; return this; } final public int getLastPhaseThreshold1(){ return lastPhaseThreshold1(); }
public int lastPhaseThreshold1() { return lastPhaseThreshold1; }
int lastPhaseThreshold1 = 2;
final public G22RegionThinner_v2 setLastPhaseThreshold(int lastPhaseThreshold){ return lastPhaseThreshold(lastPhaseThreshold); }
public G22RegionThinner_v2 lastPhaseThreshold(int lastPhaseThreshold) { this.lastPhaseThreshold = lastPhaseThreshold; return this; } final public int getLastPhaseThreshold(){ return lastPhaseThreshold(); }
public int lastPhaseThreshold() { return lastPhaseThreshold; }
int lastPhaseThreshold = 5;
// 0 = outside of region
// 1 = inner pixel
// 2 = border pixel
// index is in bounds coordinates
byte[] pixels;
int idx(int x, int y) {
return (y-bounds.y)*bounds.w+x-bounds.x;
}
int idx(Pt p) { return idx(p.x, p.y); }
Pt idxToPt(int idx) {
return pt(bounds.x+(idx % bounds.w), bounds.y+idx/bounds.w);
}
byte getPixel(Pt p) {
return !containsPt(bounds, p) ? 0 : pixels[idx(p)];
}
byte getPixel(int x, int y) {
return !containsPt(bounds, x, y) ? 0 : pixels[idx(x, y)];
}
boolean contains(int x, int y) {
return getPixel(x, y) != 0;
}
boolean clearPixel(int x, int y) {
if (!region.contains(x, y)) return false;
pixels[idx(x, y)] = 0;
return true;
}
void init() {
if (bounds != null) return;
bounds = originalRegion.bounds();
pixels = new byte[area(bounds)];
for (Pt p : originalRegion.pixelIterator())
pixels[idx(p.x, p.y)] = 1;
region = new ThinnedRegion();
}
class ThinnedRegion implements IImageRegion {
public Img image() { return originalRegion.image(); }
public Rect bounds() { return bounds; }
public boolean contains(int x, int y) {
return containsPt(bounds, x, y) && pixels[idx(x, y)] > 0;
}
public IterableIterator pixelIterator() {
return iff_null(new IF0() {
int idx = 0;
public Pt get() {
for (; idx < pixels.length; idx++)
if (pixels[idx] > 0)
return idxToPt(idx++);
return null;
}
});
}
}
public boolean step() {
init();
if (phase1done)
return finalStep();
List traces = g22_allBorderTraces_withDiagonals(region);
for (var points : traces)
for (var p : points)
pixels[idx(p)] = 2;
PtBuffer toDelete = new PtBuffer();
for (var points : traces) {
int nPoints = l(points);
BitSet deletable = emptyBitSet(nPoints);
for (int i = 0; i < nPoints; i++) {
ping();
if (deletableBorderPoint(points, i))
deletable.set(i);
}
// handle special cases
for (int i = 0; i < nPoints; i++)
if (cyclicGet(deletable, nPoints, i-1)
&& !deletable.get(i)
&& cyclicGet(deletable, nPoints, i+1)) {
Pt p = points.get(i);
Pt prev = ptMinus(cyclicGet(points, i-1), p);
Pt next = ptMinus(cyclicGet(points, i+1), p);
int dir1 = onePathLookupDirection(prev);
int dir2 = onePathLookupDirection(next);
int diff = mod(dir2-dir1, 8);
if (debug) printVars("special case", "p", p, "prev", prev, "next", next, "dir1", dir1, "dir2", dir2, "diff", diff);
if (diff == 1 || diff == 7)
deletable.set(i);
}
for (int i = 0; i < nPoints; i++)
if (deletable.get(i))
toDelete.add(points.get(i));
}
for (var p : toDelete)
pixels[idx(p)] = 0;
if (empty(toDelete))
phase1done = true;
return true;
}
boolean deletableBorderPoint(PtBuffer points, int i) {
List range = cyclicSubList_incl(points, i-3, i+3);
Pt p = points.get(i);
// special case
if (eq(range.get(1), p) || eq(range.get(5), p)) return false;
int surroundingBorderPixels = 0, surroundingInnerPixels = 0;
for (int dir = 1; dir <= 8; dir++) {
Pt p2 = ptPlus(p, onePathDirection(dir));
byte value = getPixel(p2);
if (value == 2 && !range.contains(p2))
surroundingBorderPixels++;
else if (value == 1)
surroundingInnerPixels++;
}
boolean deletable = surroundingInnerPixels > 0 && surroundingBorderPixels == 0;
return deletable;
}
final IImageRegion get(){ return region(); }
IImageRegion region() { return or(region, originalRegion); }
// go from 2 pixels wide to 1 pixel wide (TODO)
boolean finalStep() {
if (!doFinalStep) return false;
boolean change = false;
int x1 = bounds.x, x2 = bounds.x2();
int y1 = bounds.y, y2 = bounds.y2();
for (int x = x1; x < x2; x++)
for (int y = y1; y < y2; y++) {
// need a set pixel
if (!contains(x, y)) continue;
// check if this pixel is essential to hold the structure
// together by doing a floodfill in the 3x3 neighborhood
// (simulating the pixel being cleared already)
var _x_1 = x;
var _y_2 = y;
var bwImage = bwImageFromFunction(3, (xx, yy) ->
(xx != 1 || yy != 1) && contains(_x_1+xx-1, _y_2+yy-1) ? 0 : 1);
FastRegions_BWImage regionMaker = new FastRegions_BWImage(bwImage);
regionMaker.withDiagonals(true);
regionMaker.run();
// get all the black regions out of the 3x3 image
var regions = regionMaker.regions();
regions = filter(regions, r -> r.brightness() == 0);
boolean delete = false;
int pixels = 0;
// no regions? it's a lonely pixel - keep it
if (empty(regions)) {}
else if (l(regions) == 1) {
// one region
pixels = first(regions).numberOfPixels();
// if it's only one or two pixels, we are at the end of a line - keep
if (pixels <= lastPhaseThreshold1)
delete = false;
else {
// delete pixel if the region is small, but not too small
// (lastPhaseThreshold is 5 by default)
if (pixels < lastPhaseThreshold)
delete = true;
else if (pixels == lastPhaseThreshold) {
// if it's exactly 5 pixels, check out how far
// the region "surrounds" the pixel (does it touch 2 or 3 sides?)
// delete pixel if it is not surrounded
int sides = 4-(int)
(bwImage.getFloatPixel(1, 0)
+ bwImage.getFloatPixel(0, 1)
+ bwImage.getFloatPixel(2, 1)
+ bwImage.getFloatPixel(1, 2));
delete = sides < 3;
}
}
} else
// we have more than one region - pixel was
// structurally important, keep it
{}
if (delete)
change |= clearPixel(_x_1, _y_2);
}
return change;
}
}
static String shortClassName_dropNumberPrefix(Object o) {
return dropNumberPrefix(shortClassName(o));
}
static Pt pt(int x, int y) {
return new Pt(x, y);
}
static Pt pt(int x) {
return new Pt(x, x);
}
static int getPixel(BufferedImage img, int x, int y) {
return img.getRGB(x, y);
}
static int getPixel(BufferedImage img, Pt p) {
return img.getRGB(p.x, p.y);
}
static boolean containsPt(int x1, int y1, int w, int h, Pt p) {
return p.x >= x1 && p.y >= y1 && p.x < x1+w && p.y < y1+h;
}
static boolean containsPt(Rect a, int x, int y) {
return a != null && a.contains(x, y);
}
static boolean containsPt(Rect a, Pt p) {
return a != null && p != null && a.contains(p.x, p.y);
}
static int area(Rect r) {
return rectArea(r);
}
static double area(DoubleRect r) {
return r == null ? 0 : r.w*r.h;
}
static int area(WidthAndHeight img) {
return img == null ? 0 : img.getWidth()*img.getHeight();
}
// f: func -> A (stream ends when f returns null)
static IterableIterator iff_null(final Object f) {
return iteratorFromFunction(f);
}
static IterableIterator iff_null(F0 f) {
return iteratorFromFunction(f);
}
static IterableIterator iff_null(IF0 f) {
return iteratorFromFunction(f);
}
static List g22_allBorderTraces_withDiagonals(IImageRegion region) {
RegionBorder_innerPoints_withDiagonals walker = new RegionBorder_innerPoints_withDiagonals(region);
List out = new ArrayList();
walker.onNewTrace(hole -> out.add(new PtBuffer()));
walker.onFoundPoint(p -> last(out).add(p));
walker.run();
return out;
}
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(IMultiMap mm) { return mm == null ? 0 : mm.size(); }
static int l(IntBuffer b) { return b == null ? 0 : b.size(); }
static int l(LongBuffer b) { return b == null ? 0 : b.size(); }
static int l(AppendableChain a) { return a == null ? 0 : a.size; }
static BitSet emptyBitSet(int capacity) {
return new BitSet(capacity);
}
static BitSet emptyBitSet(Collection capacity) {
return emptyBitSet(l(capacity));
}
//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 A cyclicGet(List l, int i) {
return empty(l) ? null : get(l, mod(i, l(l)));
}
static boolean cyclicGet(BitSet bs, int n, int i) {
return bs != null && bs.get(mod(i, n));
}
static Pt ptMinus(Pt a, Pt b) {
if (b == null) return a;
return new Pt(a.x-b.x, a.y-b.y);
}
static int onePathLookupDirection(Pt p) {
return indexOf(onePathDirections(), p);
}
// better modulo that gives positive numbers always
static int mod(int n, int m) {
return (n % m + m) % m;
}
static long mod(long n, long m) {
return (n % m + m) % m;
}
static BigInteger mod(BigInteger n, int m) {
return n.mod(bigint(m));
}
static double mod(double n, double m) {
return (n % m + m) % m;
}
// Use like this: printVars(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(, +x);
static void printVars(Object... params) {
printVars_str(params);
}
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(BitSet bs) { return bs == null || bs.isEmpty(); }
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(IMultiMap mm) { return mm == null || mm.size() == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static boolean empty(IntBuffer b) { return b == null || b.isEmpty(); }
static boolean empty(LongBuffer b) { return b == null || b.isEmpty(); }
static boolean empty(Rect r) { return !(r != null && r.w != 0 && r.h != 0); }
static boolean empty(AppendableChain c) { return c == null; }
static List cyclicSubList_incl(List l, int startIndex, int endIndex) {
return cyclicSubList(l, startIndex, endIndex+1);
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
// a little kludge for stuff like eq(symbol, "$X")
static boolean eq(Symbol a, String b) {
return eq(str(a), b);
}
static Pt ptPlus(Pt a, Pt b) {
return addPts(a, b);
}
static Pt onePathDirection(int index) {
return onePathDirections()[index];
}
static Pt onePathDirection(Pt p, int index) {
return ptPlus(p, onePathDirections()[index]);
}
static A or(A a, A b) {
return a != null ? a : b;
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
static boolean contains(Iterable it, Object a) {
if (it != null)
for (Object o : it)
if (eq(a, o))
return true;
return false;
}
static boolean contains(Object[] x, Object o) {
if (x != null)
for (Object a : x)
if (eq(a, o))
return true;
return false;
}
static boolean contains(String s, char c) {
return s != null && s.indexOf(c) >= 0;
}
static boolean contains(String s, String b) {
return s != null && s.indexOf(b) >= 0;
}
static boolean contains(BitSet bs, int i) {
return bs != null && bs.get(i);
}
static boolean contains(Rect r, Pt p) { return rectContains(r, p); }
// f should return a value between 0 and 1 (will be clamped for you)
static BWImage bwImageFromFunction(int w, IF2_IntInt_Double f) { return bwImageFromFunction(w, w, f); }
static BWImage bwImageFromFunction(int w, int h, IF2_IntInt_Double f) {
BWImage img = new BWImage(w, h);
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++)
img.setPixel(x, y, (float) f.get(x, y));
return img;
}
static BWImage bwImageFromFunction(int w, IF0 f) { return bwImageFromFunction(w, w, f); }
static BWImage bwImageFromFunction(int w, int h, IF0 f) {
return bwImageFromFunction(w, (x, y) -> f.get());
}
static List filter(Iterable c, Object pred) {
if (pred instanceof F1) return filter(c, (F1) pred);
List x = new ArrayList();
if (c != null) for (Object o : c)
if (isTrue(callF(pred, o)))
x.add(o);
return x;
}
static List filter(Object pred, Iterable c) {
return filter(c, pred);
}
static List filter(Iterable c, F1 pred) {
List x = new ArrayList();
if (c != null) for (B o : c)
if (pred.get(o))
x.add(o);
return x;
}
static List filter(F1 pred, Iterable c) {
return filter(c, pred);
}
//ifclass IF1
static List filter(Iterable c, IF1 pred) {
List x = new ArrayList();
if (c != null) for (B o : c)
if (pred.get(o))
x.add(o);
return x;
}
static List filter(B[] c, IF1 pred) {
List x = new ArrayList();
if (c != null) for (B o : c)
if (pred.get(o))
x.add(o);
return x;
}
static List filter(IF1 pred, Iterable c) {
return filter(c, pred);
}
//endif
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 Pair first(Map map) {
return mapEntryToPair(first(entrySet(map)));
}
static Pair first(MultiMap mm) {
if (mm == null) return null;
var e = first(mm.data.entrySet());
if (e == null) return null;
return pair(e.getKey(), first(e.getValue()));
}
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 int first(IntBuffer buf) {
return buf.get(0);
}
static byte first(ByteBuffer buf) {
return buf.get(0);
}
static A first(A[] l, IF1 pred) {
return firstThat(l, pred);
}
static A first(Iterable l, IF1 pred) {
return firstThat(l, pred);
}
static A first(IF1 pred, Iterable l) {
return firstThat(pred, l);
}
static A first(AppendableChain a) {
return a == null ? null : a.element;
}
static String dropNumberPrefix(String s) {
return dropFirst(s, indexOfNonDigit(s));
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static int rectArea(Rect r) {
return r == null ? 0 : r.w*r.h;
}
// f: func -> A (stream ends when f returns null)
static IterableIterator iteratorFromFunction(final Object f) {
class IFF extends IterableIterator {
A a;
boolean done = false;
public boolean hasNext() {
getNext();
return !done;
}
public A next() {
getNext();
if (done) throw fail();
A _a = a;
a = null;
return _a;
}
void getNext() {
if (done || a != null) return;
a = (A) callF(f);
done = a == null;
}
};
return new IFF();
}
// optimized version for F0 argument
static IterableIterator iteratorFromFunction(F0 f) {
return iteratorFromFunction_f0(f);
}
static IterableIterator iteratorFromFunction(IF0 f) {
return iteratorFromFunction_if0(f);
}
static A last(List l) {
return empty(l) ? null : l.get(l.size()-1);
}
static char last(String s) {
return empty(s) ? '#' : s.charAt(l(s)-1);
}
static byte last(byte[] a) {
return l(a) != 0 ? a[l(a)-1] : 0;
}
static int last(int[] a) {
return l(a) != 0 ? a[l(a)-1] : 0;
}
static double last(double[] a) {
return l(a) != 0 ? a[l(a)-1] : 0;
}
static A last(A[] a) {
return l(a) != 0 ? a[l(a)-1] : null;
}
static A last(Iterator it) {
A a = null;
while (it.hasNext()) { ping(); a = it.next(); }
return a;
}
static A last(Collection l) {
if (l == null) return null;
if (l instanceof List) return (A) last((List) l);
if (l instanceof SortedSet) return (A) last((SortedSet) l);
Iterator it = iterator(l);
A a = null;
while (it.hasNext()) { ping(); a = it.next(); }
return a;
}
static A last(SortedSet l) {
return l == null ? null : l.last();
}
static int last(IntBuffer buf) {
return buf.get(buf.size()-1);
}
static byte last(ByteBuffer buf) {
return buf.get(buf.size()-1);
}
static A last(CompactLinkedHashSet set) {
return set == null ? null : set.last();
}
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 Map newWeakHashMap() {
return _registerWeakMap(synchroMap(new WeakHashMap()));
}
static void newPing() {
var tl = newPing_actionTL();
Runnable action = tl == null ? null : tl.get();
{ if (action != null) action.run(); }
}
// TODO: test if android complains about this
static boolean isAWTThread() {
if (isAndroid()) return false;
if (isHeadless()) return false;
return isAWTThread_awt();
}
static boolean isAWTThread_awt() {
return SwingUtilities.isEventDispatchThread();
}
static boolean isTrue(Object o) {
if (o instanceof Boolean)
return ((Boolean) o).booleanValue();
if (o == null) return false;
if (o instanceof ThreadLocal) // TODO: remove this
return isTrue(((ThreadLocal) o).get());
throw fail(getClassName(o));
}
static boolean isTrue(Boolean b) {
return b != null && b.booleanValue();
}
static void failIfUnlicensed() {
assertTrue("license off", licensed());
}
static Thread currentThread() {
return Thread.currentThread();
}
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 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);
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
makeAccessible(f);
return f.get(o);
}
if (o instanceof DynamicObject)
return getOptDynOnly(((DynamicObject) o), field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(String field, Object o) {
return get_raw(o, field);
}
static Object get_raw(Object o, String field) { try {
if (o == null) return null;
Field f = get_findField(o.getClass(), field);
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
makeAccessible(f);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static Object get(String field, Object o) {
return get(o, field);
}
static boolean get(BitSet bs, int idx) {
return bs != null && bs.get(idx);
}
static int indexOf(List l, A a, int startIndex) {
if (l == null) return -1;
int n = l(l);
for (int i = startIndex; i < n; i++)
if (eq(l.get(i), a))
return i;
return -1;
}
static int indexOf(List l, int startIndex, A a) {
return indexOf(l, a, startIndex);
}
static int indexOf(List l, A a) {
if (l == null) return -1;
return l.indexOf(a);
}
static int indexOf(String a, String b) {
return a == null || b == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, String b, int i) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, char b) {
return a == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, int i, char b) {
return indexOf(a, b, i);
}
static int indexOf(String a, char b, int i) {
return a == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, int i, String b) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(A[] x, A a) {
int n = l(x);
for (int i = 0; i < n; i++)
if (eq(x[i], a))
return i;
return -1;
}
static int indexOf(Iterable l, A a) {
if (l == null) return -1;
int i = 0;
for (A x : l) {
if (eq(x, a))
return i;
i++;
}
return -1;
}
static final Pt[] onePathDirections_directions = {
pt(0, 0),
pt(-1, -1),
pt(0, -1),
pt(1, -1),
pt(1, 0),
pt(1, 1),
pt(0, 1),
pt(-1, 1),
pt(-1, 0)
};
static Pt[] onePathDirections() { return onePathDirections_directions; }
static BigInteger bigint(String s) {
return new BigInteger(s);
}
static BigInteger bigint(long l) {
return BigInteger.valueOf(l);
}
// Use like this: printVars_str(+x, +y);
// Or: printVars("bla", +x);
// Or: printVars bla(+x);
static void printVars_str(Object... params) {
print(renderVars_str(params));
}
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 List cyclicSubList(List l, int startIndex, int endIndex) {
if (l == null) return null;
List subList = new ArrayList();
for (int i = startIndex; i < endIndex; i++)
subList.add(cyclicGet(l, i));
return subList;
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static String str(char[] c, int offset, int count) {
return new String(c, offset, count);
}
static Pt addPts(Pt a, Pt b) {
return a == null ? b : b == null ? a : new Pt(a.x+b.x, a.y+b.y);
}
static boolean rectContains(int x1, int y1, int w, int h, Pt p) {
return p.x >= x1 && p.y >= y1 && p.x < x1+w && p.y < y1+h;
}
static boolean rectContains(Rect a, Rect b) {
return b.x >= a.x && b.y >= a.y && b.x2() <= a.x2() && b.y2() <= a.y2();
}
static boolean rectContains(Rect a, Rectangle b) {
return rectContains(a, toRect(b));
}
static boolean rectContains(Rect a, int x, int y) {
return a != null && a.contains(x, y);
}
static boolean rectContains(Rect a, Pt p) {
return a != null && p != null && a.contains(p);
}
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 B callF(A a, IF1 f) {
return f == null ? null : f.get(a);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static void callF(A a, IVF1 f) {
if (f != null) f.get(a);
}
static void callF(IVF1 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) {
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) {
if (f instanceof String)
throw fail("Legacy call: " + f);
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 Pair mapEntryToPair(Map.Entry e) {
return e == null ? null : pair(e.getKey(), e.getValue());
}
static Set> entrySet(Map map) {
return _entrySet(map);
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
static A firstThat(Iterable l, IF1 pred) {
for (A a : unnullForIteration(l))
if (pred.get(a))
return a;
return null;
}
static A firstThat(A[] l, IF1 pred) {
for (A a : unnullForIteration(l))
if (pred.get(a))
return a;
return null;
}
static A firstThat(IF1 pred, Iterable l) {
return firstThat(l, pred);
}
static A firstThat(IF1 pred, A[] l) {
return firstThat(l, pred);
}
static String[] dropFirst(int n, String[] a) {
return drop(n, a);
}
static String[] dropFirst(String[] a) {
return drop(1, a);
}
static Object[] dropFirst(Object[] a) {
return drop(1, a);
}
static List dropFirst(List l) {
return dropFirst(1, l);
}
static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); }
static List dropFirst(Iterable i) { return dropFirst(toList(i)); }
static List dropFirst(int n, List l) {
return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}
static List dropFirst(List l, int n) {
return dropFirst(n, l);
}
static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s, int n) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static int indexOfNonDigit(String s) {
int n = l(s);
for (int i = 0; i < n; i++)
if (!isDigit(s.charAt(i)))
return i;
return -1;
}
static String shortenClassName(String name) {
if (name == null) return null;
int i = lastIndexOf(name, "$");
if (i < 0) i = lastIndexOf(name, ".");
return i < 0 ? name : substring(name, i+1);
}
static IterableIterator iteratorFromFunction_f0(final F0 f) {
class IFF2 extends IterableIterator {
A a;
boolean done = false;
public boolean hasNext() {
getNext();
return !done;
}
public A next() {
getNext();
if (done) throw fail();
A _a = a;
a = null;
return _a;
}
void getNext() {
if (done || a != null) return;
a = f.get();
done = a == null;
}
};
return new IFF2();
}
static IterableIterator iteratorFromFunction_if0(final IF0 f) {
class IFF2 extends IterableIterator {
A a;
boolean done = false;
public boolean hasNext() {
getNext();
return !done;
}
public A next() {
getNext();
if (done) throw fail();
A _a = a;
a = null;
return _a;
}
void getNext() {
if (done || a != null) return;
a = f.get();
done = a == null;
}
};
return new IFF2();
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
static List _registerWeakMap_preList;
static A _registerWeakMap(A map) {
if (javax() == null) {
// We're in class init
if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList();
_registerWeakMap_preList.add(map);
return map;
}
try {
call(javax(), "_registerWeakMap", map);
} catch (Throwable e) {
printException(e);
print("Upgrade JavaX!!");
}
return map;
}
static void _onLoad_registerWeakMap() {
assertNotNull(javax());
if (_registerWeakMap_preList == null) return;
for (Object o : _registerWeakMap_preList)
_registerWeakMap(o);
_registerWeakMap_preList = null;
}
static Map synchroMap() {
return synchroHashMap();
}
static Map synchroMap(Map map) {
return Collections.synchronizedMap(map);
}
static x30_pkg.x30_util.BetterThreadLocal newPing_actionTL;
static x30_pkg.x30_util.BetterThreadLocal newPing_actionTL() {
if (newPing_actionTL == null)
newPing_actionTL = vm_generalMap_getOrCreate("newPing_actionTL",
() -> {
Runnable value = (Runnable) (callF_gen(vm_generalMap_get("newPing_valueForNewThread")));
var tl = new x30_pkg.x30_util.BetterThreadLocal();
tl.set(value);
return tl;
});
return newPing_actionTL;
}
static int isAndroid_flag;
static boolean isAndroid() {
if (isAndroid_flag == 0)
isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1;
return isAndroid_flag > 0;
}
static Boolean isHeadless_cache;
static boolean isHeadless() {
if (isHeadless_cache != null) return isHeadless_cache;
if (isAndroid()) return isHeadless_cache = true;
if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true;
// Also check if AWT actually works.
// If DISPLAY variable is set but no X server up, this will notice.
try {
SwingUtilities.isEventDispatchThread();
return isHeadless_cache = false;
} catch (Throwable e) { return isHeadless_cache = true; }
}
static String getClassName(Object o) {
return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName();
}
static void assertTrue(Object o) {
if (!(eq(o, true) /*|| isTrue(pcallF(o))*/))
throw fail(str(o));
}
static boolean assertTrue(String msg, boolean b) {
if (!b)
throw fail(msg);
return b;
}
static boolean assertTrue(boolean b) {
if (!b)
throw fail("oops");
return b;
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
if (!licensed_yes) return false;
ping_okInCleanUp();
return true;
}
static void licensed_off() {
licensed_yes = false;
}
static RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
static void _handleError(Error e) {
//call(javax(), '_handleError, e);
}
static Field getOpt_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
return null;
}
static Field makeAccessible(Field f) {
try {
f.setAccessible(true);
} catch (Throwable e) {
// Note: The error reporting only works with Java VM option --illegal-access=deny
vmBus_send("makeAccessible_error", e, f);
}
return f;
}
static Method makeAccessible(Method m) {
try {
m.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, m);
}
return m;
}
static Constructor makeAccessible(Constructor c) {
try {
c.setAccessible(true);
} catch (Throwable e) {
vmBus_send("makeAccessible_error", e, c);
}
return c;
}
static Object getOptDynOnly(DynamicObject o, String field) {
if (o == null || o.fieldValues == null) return null;
return o.fieldValues.get(field);
}
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