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.*;
import java.text.NumberFormat;
import java.util.TimeZone;
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;
final public G22RegionThinner_v2 setLookupTable(byte[] lookupTable){ return lookupTable(lookupTable); }
public G22RegionThinner_v2 lookupTable(byte[] lookupTable) { this.lookupTable = lookupTable; return this; } final public byte[] getLookupTable(){ return lookupTable(); }
public byte[] lookupTable() { return lookupTable; }
byte[] lookupTable;
// 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(long p) {
return getPixel(firstIntFromLong(p), secondIntFromLong(p));
}
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++) {
boolean del = deletableBorderPoint(points, i);
/*bool del2 = deletableBorderPoint_old(points, i);
if (del != del2)
failWithVars("deletableBorderPoint", +i, +nPoints, +del);*/
if (del)
deletable.set(i);
}
// handle special cases
// (preventing single isolated pixels from being left standing)
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) {
//L range = cyclicSubList_incl(points, i-3, i+3);
LongBuffer pointsBuf = points.buf;
long p_long = pointsBuf.get(i);
// special case (sharp corner)
if (p_long == cyclicGet(pointsBuf, i-2)
|| p_long == cyclicGet(pointsBuf, i+2)) {
return false;
}
// Basic logic:
// a pixel is deletable if at least one inner pixel is next to it
// and no "foreign" border pixels are next to it.
// This leaves diagonal lines two pixels wide though
// which can lead to them eventually shrinking down to nothing
// (the "k" problem).
// Idea to fix: Ignore foreign border pixels in certain directions
int surroundingBorderPixels = 0, surroundingInnerPixels = 0;
for (int dir = 1; dir <= 8; dir++) {
long p2_long = onePathDirection_long(p_long, dir);
byte value = getPixel(p2_long);
int foreignBorderPixels = 0;
if (value == 2 && !rangeContains(pointsBuf, i, p2_long)) {
// found a "foreign" border pixel
//surroundingBorderPixels++;
// FIX (dev.)
//if (dir < 4) false;
foreignBorderPixels |= 1 << dir;
} else if (value == 1) {
surroundingInnerPixels++;
}
}
// FIX v2
if (foreignBorderPixels == ((1 << 4) | (1 << 5) | (1 << 6)))
return true;
if (foreignBorderPixels > 0) return false;
return surroundingInnerPixels > 0;
//bool deletable = surroundingInnerPixels > 0 && surroundingBorderPixels == 0;
//printVars ifdef G22RegionThinner_debug(+p, +surroundingInnerPixels, +surroundingBorderPixels, +deletable, +range);
//ret deletable;
}
boolean rangeContains(LongBuffer pointsBuf, int i, long p2_long) {
for (int j = i-3; j <= i+3; j++)
if (cyclicGet(pointsBuf, j) == p2_long)
return true;
return false;
}
boolean deletableBorderPoint_old(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;
if (lookupTable == null)
lookupTable = new G22RegionThinner_LookupTable()
.lastPhaseThreshold(lastPhaseThreshold)
.lastPhaseThreshold1(lastPhaseThreshold1).get();
boolean change = false;
int x1 = bounds.x, x2 = bounds.x2();
int y1 = bounds.y, y2 = bounds.y2();
var pingSource = pingSource();
for (int y = y1; y < y2; y++) {
ping(pingSource);
for (int x = x1; x < x2; x++) {
// 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 imgPattern = ubyteFromBits(
contains(x-1, y-1),
contains(x, y-1),
contains(x+1, y-1),
contains(x-1, y),
contains(x+1, y),
contains(x-1, y+1),
contains(x, y+1),
contains(x+1, y+1));
boolean delete = getBit(lookupTable, imgPattern);
if (delete)
change |= clearPixel(x, y);
}
}
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 int firstIntFromLong(long l) {
return (int) (l >> 32);
}
static int secondIntFromLong(long l) {
return (int) l;
}
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));
}
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 long cyclicGet(LongBuffer l, int i) {
return l.get(mod(i, l.size()));
}
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(Chain c) { return c == null; }
static boolean empty(AppendableChain c) { return c == null; }
static long onePathDirection_long(int index) {
return ptToLong(onePathDirections()[index]);
}
static long onePathDirection_long(long p, int index) {
Pt p2 = onePathDirection(index);
return twoIntsToLong(firstIntFromLong(p)+p2.x, secondIntFromLong(p)+p2.y);
}
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;
}
// assumptions:
// -pingSource() stays constant within a stack frame (so you can cache it)
// -all changes happen with tempSetPingSource
// -multiple threads can share a PingSource
static PingSource pingSource() {
return pingSource_tl().get();
}
static PingSource pingSource(Thread thread) {
return pingSource_tl().get(thread);
}
// legacy mode
//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();
// ignore pingSource if not PingV3
static boolean ping(PingSource pingSource) { return ping(); }
// 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 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); }
static int ubyteFromBits(boolean b7, boolean b6, boolean b5, boolean b4, boolean b3, boolean b2, boolean b1, boolean b0) {
return (b7 ? 1 : 0) << 7
| (b6 ? 1 : 0) << 6
| (b5 ? 1 : 0) << 5
| (b4 ? 1 : 0) << 4
| (b3 ? 1 : 0) << 3
| (b2 ? 1 : 0) << 2
| (b1 ? 1 : 0) << 1
| (b0 ? 1 : 0);
}
static boolean getBit(byte[] b, int bit) {
int idx = bit >> 3;
if (idx < 0 || idx > l(b)) return false;
byte mask = (byte) (1 << (bit & 7));
return (b[idx] & mask) != 0;
}
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 A last(ReverseChain l) {
return l == null ? null : l.element;
}
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); } }
// 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 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 long ptToLong(Pt p) {
return p == null ? -1: intPairToLong(p.x, p.y);
}
static long ptToLong(int x, int y) {
return intPairToLong(x, y);
}
static long twoIntsToLong(int a, int b) {
return (((long) a) << 32) | (((long) b) & 0xFFFFFFFF);
}
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 BetterThreadLocal pingSource_tl_var = new BetterThreadLocal() {
@Override
public PingSource initialValue() {
return ping_v3_pingSourceMaker().get();
}
};
static BetterThreadLocal pingSource_tl() {
return pingSource_tl_var;
}
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 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 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 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 Chain dropFirst(Chain c) {
return c == null ? null : c.next;
}
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 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 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 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 RuntimeException asRuntimeException(Throwable t) {
if (t instanceof Error)
_handleError((Error) t);
return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t);
}
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