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.time.Duration;
import java.lang.invoke.VarHandle;
import java.lang.invoke.MethodHandles;
// Not as useful as expected because a singleton simple mask set
// is already optimized but doesn't implement this interface.
// Should we make a dedicated singleton mask set class?
import java.awt.geom.*;
import java.text.NumberFormat;
import static x30_pkg.x30_util.DynamicObject;
import java.nio.file.Path;
import java.text.*;
import java.nio.charset.Charset;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
class main {
interface IG22OptimizedMasksHolder extends IG22MasksHolder {}
interface IG22MasksHolder {
WidthAndHeight maskSize();
List maskImages();
//LPair maskImagesWithLabels();
List extends IG22Mask> masks();
G22GhostImage ghost();
double certainty();
default int nMasks() { return l(masks()); }
default String renderCountAndCertainty() {
return main.nMasks(nMasks()) + " with c=" + formatDouble3(certainty());
}
default boolean canSplit() { return nMasks() > 1; }
default List> subHolders() { return null; }
default void transformSubHolders(IF1, IG22MasksHolder> f) {}
default PStackComputable findSimilarMask(
Image2B maskImage, ProbabilisticList> outList) {
return new FindSimilarMaskSimple(this, maskImage, outList);
}
static class FindSimilarMaskSimple extends PStackComputableWithStep implements IFieldsToList{
IG22MasksHolder masksHolder;
Image2B maskImage;
ProbabilisticList> outList;
FindSimilarMaskSimple() {}
FindSimilarMaskSimple(IG22MasksHolder masksHolder, Image2B maskImage, ProbabilisticList> outList) {
this.outList = outList;
this.maskImage = maskImage;
this.masksHolder = masksHolder;}
public String toString() { return shortClassName_dropNumberPrefix(this) + "(" + masksHolder + ", " + maskImage + ", " + outList + ")"; }public Object[] _fieldsToList() { return new Object[] {masksHolder, maskImage, outList}; }
IG22Mask currentMask;
void step(IPStack stack) {
if (step == 0) {
++step;
stack.options(this, map(masksHolder.masks(), mask ->
self -> self.currentMask = mask));
} else {
long diff = binaryImagePixelDifference_sameSize(
maskImage,
currentMask.image());
double p = 1-doubleRatio(diff, area(currentMask.image()));
outList.at(p, currentMask);
stack._return();
}
}
}
}
interface IPStack extends IStackWithOptions {
}
static interface IFieldsToList {
Object[] _fieldsToList();
}
abstract static class PStackComputableWithStep extends PStackComputable {
int step;
// phasing out the signature with subComputationResult
public final void step(VStack stack, Object subComputationResult) {
step((IPStack) stack);
}
void step(IPStack stack) {}
public String toString() { return shortClassName(this) + " step " + step; }
}
static class Image2B implements IBinaryImage {
Image2B() {}
int w, h;
byte[] pixels;
Image2B(int w, int h, byte[] pixels) {
this.pixels = pixels;
this.h = h;
this.w = w; cleanPixelArray(); }
Image2B(Image2B img) {
w = img.getWidth();
h = img.getHeight();
pixels = cloneByteArray(img.pixels);
}
Image2B(RGBImage img) {
w = img.getWidth();
h = img.getHeight();
pixels = new byte[(w*h+7)/8];
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++)
if (img.getPixel(x, y).getBrightness() >= 0.5f) {
int i = y*w+x;
pixels[i/8] |= 1 << (i & 7);
}
}
Image2B(BWImage img) {
this(img, 128);
}
// >= threshold
Image2B(BWImage img, int threshold) {
w = img.w(); h = img.h();
int n = w*h;
int nOut = (n+7)/8;
byte[] pixels = this.pixels = new byte[nOut];
byte[] bwPixels = img.pixels;
int iIn = 0;
// do the bulk
for (int iOut = 0; iOut < nOut-1; iOut++) {
int value = 0;
for (int bit = 0; bit < 8; bit++) {
value >>= 1;
if (ubyteToInt(bwPixels[iIn++]) >= threshold)
value |= 0x80;
}
pixels[iOut] = (byte) value;
}
// do last (up to 7) bits
for (; iIn < n; iIn++)
if (ubyteToInt(bwPixels[iIn]) >= threshold)
pixels[nOut-1] |= 1 << (iIn & 7);
}
Image2B(BufferedImage img) {
this(img, 128);
}
Image2B(BufferedImage img, int threshold) {
this(new BWImage(img), threshold);
}
// initializes with black
Image2B(int w, int h) {
this.h = h;
this.w = w;
pixels = new byte[(w*h+7)/8];
}
RGBImage toRGB() {
RGBImage img = new RGBImage(w, h, Color.black);
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) {
int i = y*w+x;
if ((pixels[i/8] & (1 << (i & 7))) != 0)
img.setPixel(x, y, Color.white);
}
return img;
}
BWImage toBW() {
BWImage img = new BWImage(w, h, 0f);
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) {
int i = y*w+x;
if ((pixels[i/8] & (1 << (i & 7))) != 0)
img.setPixel(x, y, 1f);
}
return img;
}
public BufferedImage getBufferedImage() { return toBW().getBufferedImage(); }
// x and y must be inside the image
final public boolean getBoolPixel(int x, int y){ return getPixel(x, y); }
public boolean getPixel(int x, int y) {
int i = y*w+x;
return (pixels[i/8] & (1 << (i & 7))) != 0;
}
// defaultColor is color outside of image
final public boolean getBoolPixel(int x, int y, boolean defaultColor){ return getPixel(x, y, defaultColor); }
public boolean getPixel(int x, int y, boolean defaultColor) {
if (x < 0 || y < 0 || x >= w || y >= h) return defaultColor;
return getPixel(x, y);
}
public void setPixel(int x, int y, boolean b) {
int i = y*w+x;
byte val = pixels[i/8], shifted = (byte) (1 << (i & 7));
val = (byte) (b ? val | shifted : val & ~shifted);
pixels[i/8] = val;
}
void setPixel(int x, int y) {
int i = y*w+x;
pixels[i/8] |= 1 << (i & 7);
}
public int getWidth() { return w; }
public int getHeight() { return h; }
public String toString() {
return "Image2B " + str_px(w, h);
}
// clear unused bits in pixel array after we received
// a possibly dirty array
void cleanPixelArray() {
int n = w*h;
if ((n & 7) != 0)
pixels[n/8] &= (1 << (n & 7))-1;
}
}
static interface IF1 {
B get(A a);
}
// As long as you don't access the by-numeral-index functions,
// you can add and remove elements quickly.
// Upon accessing the list, it is updated once to fit the new data.
// Note: When adding the same element twice, it will be stored only
// once with the higher probabilities of the two.
//
// class is synchronized
static class ProbabilisticList extends RandomAccessAbstractList> {
// these are the 2 main data structures we update
Map probabilities = hashMap();
MultiSetMap byProbability
= multiSetMap_outerDescTreeMap_innerCompactLinkedHashSet();
// = multiSetMap_outerDescTreeMap_innerBetterLinkedHashSet();
List> list; // this is only updated when requested
// CUSTOMIZATION
// must be >= 0. probability 0 is never added
double cutoffProbabilityOnAdd = 0;
ProbabilisticList() {}
ProbabilisticList(Iterable> l) {
main.addAll(this, l);
}
public synchronized WithProbability get(int idx) {
if (idx < 0 || idx >= size()) return null;
makeListUpToIndex(idx);
return list.get(idx);
}
public synchronized int size() { return l(probabilities); }
// assuming the index is within range
void makeListUpToIndex(int idx) {
if (idx < l(list)) return; // index already there
if (list == null) list = new ArrayList();
WithProbability lastEntry = main.last(list);
A a = lastEntry == null ? null : lastEntry.get();
double probability = lastEntry == null ? 0 : lastEntry.probability();
//print("Making list from " + l(list) + " to " + idx);
while (idx >= l(list)) {
//printVars_str(+idx, +a, +probability);
if (a == null) {
probability = byProbability.firstKey();
a = first(byProbability.get(probability));
} else {
CompactLinkedHashSet setForProbability = (CompactLinkedHashSet) (byProbability.get(probability));
a = setForProbability.nextElement(a);
//printVars_str(+probability, +setForProbability, +a);
if (a == null) {
probability = byProbability.higherKey(probability);
a = first(byProbability.get(probability));
}
}
list.add(new WithProbability(probability, a));
}
}
synchronized boolean containsElement(A a) {
return probabilities.containsKey(a);
}
final public boolean put(A a, double probability){ return add(a, probability); }
public boolean add(A a, double probability) {
return add(new WithProbability(probability, a));
}
public synchronized boolean add(WithProbability wp) {
if (wp == null || wp.probability() <= cutoffProbabilityOnAdd) return false;
A a = wp.get();
double newP = wp.probability();
Double oldP = probabilities.get(a);
if (oldP != null) {
if (oldP.doubleValue() >= newP)
return false;
else
remove(new WithProbability(oldP, a));
}
clearListFrom(newP);
probabilities.put(a, newP);
byProbability.add(newP, a);
return true;
}
// will also delete if given probability doesn't match
@Override
public boolean remove(Object wp) {
if (wp == null) return false;
return removeElement(((WithProbability) wp).get());
}
public synchronized boolean removeElement(A a) {
if (a == null) return false;
Double oldP = probabilities.get(a);
if (oldP == null) return false;
clearListFrom(oldP);
probabilities.remove(a);
byProbability.remove(oldP, a);
return true;
}
@Override
public synchronized WithProbability remove(int idx) {
var a = get(idx);
remove(a);
return a;
}
// internal
void clearListFrom(double p) {
int idx = generalizedBinarySearch2(list, a -> a.probability() <= p ? 1 : -1);
idx = -idx-1;
//printVars clearListFrom(p, idx, before := takeFirst(idx, list), after := main subList(idx, list));
truncateList(list, idx);
}
void at(double p, A a) {
add(new WithProbability(p, a));
}
// faster version of truncateList
void truncate(int size) {
if (size < 0) throw fail();
while (size() > size)
removeLast();
}
double getProbability(A a) {
return orZero(probabilities.get(a));
}
void truncateBelow(double thresholdProbability) {
A a;
while ((a = last()) != null && getProbability(a) < thresholdProbability)
removeElement(a);
}
A last() {
Double last = byProbability.lastKey();
if (last == null) return null;
CompactLinkedHashSet set = (CompactLinkedHashSet) (byProbability.getOpt(last));
return set.last();
}
void removeLast() {
removeElement(last());
}
int indexOfElement(A a) {
// quickly bail if element is not contained
double p = getProbability(a);
if (p == 0) return -1;
// TODO: binary search and stuff
int n = size();
for (int i = 0; i < n; i++)
if (eq(a, get(i).get()))
return i;
throw fail("Internal error");
}
// elements without probabilities
List rawList() {
return lazyMap_noSave(this, a -> a.get());
}
// just the probabilities in the list
List probabilities() {
return lazyMap_noSave(this, a -> a.probability());
}
}
static class G22GhostImage implements IBWImage {
G22GhostImage() {}
int w, h;
public int getWidth() { return w; }
public int getHeight() { return h; }
// number of images averaged
int imageCount;
// array contains raw sum over images
// (1 for white pixel, 0 for black pixel)
int[] pixels;
G22GhostImage(List maskImages) {
imageCount = l(maskImages);
if (imageCount == 0) return;
var img = first(maskImages);
w = img.w(); h = img.h();
pixels = sumOfBinaryImages(maskImages);
}
public float getFloatPixel(int x, int y) {
return imageCount == 0 ? 0.5f : floatRatio(pixels[y*w+x], imageCount);
}
FloatBWImage toFloatBWImage() {
float[] floatPixels = new float[pixels.length];
if (imageCount == 0)
fillArray(floatPixels, 0.5f);
else
for (int i = 0; i < l(pixels); i++)
floatPixels[i] = floatRatio(pixels[i], imageCount);
return new FloatBWImage(w, h, floatPixels);
}
public BufferedImage getBufferedImage() {
return toFloatBWImage().getBufferedImage();
}
FloatBWImage certaintyImage() {
return preciseCertaintyImage(this);
}
Double certainty_cache;
double certainty() { if (certainty_cache == null) certainty_cache = certainty_load(); return certainty_cache;}
double certainty_load() {
if (imageCount == 0) return 0;
double sum = 0;
int n = pixels.length;
for (int i = 0; i < n; i++)
sum += g22pixelCertainty(doubleRatio(pixels[i], imageCount));
return sum/(w*h);
}
G22GhostImage minus(G22GhostImage img) {
G22GhostImage ghost = new G22GhostImage();
ghost.w = w; ghost.h = h;
ghost.imageCount = imageCount-img.imageCount;
ghost.pixels = intArray_minus(pixels, img.pixels);
return ghost;
}
}
// We generally use the VStack.Computable interface even in p-stacks
// because those can also handle normal vstack-computable functions.
abstract static class PStackComputable implements VStack.Computable {
double probability = 1;
}
interface IG22Mask {
Image2B image();
A label();
}
static interface WidthAndHeight {
default int w(){ return getWidth(); }
default int width(){ return getWidth(); }
int getWidth();
default int h(){ return getHeight(); }
default int height(){ return getHeight(); }
int getHeight();
public default Rect bounds() { return rect(0, 0, getWidth(), getHeight()); }
default int area() { return toInt(areaAsLong()); }
default long areaAsLong() { return longMul(w(), h()); }
}
static class Rect implements WidthAndHeight , IFieldsToList{
static final String _fieldOrder = "x y w h";
int x;
int y;
int w;
int h;
Rect() {}
Rect(int x, int y, int w, int h) {
this.h = h;
this.w = w;
this.y = y;
this.x = x;}
public boolean equals(Object o) {
if (!(o instanceof Rect)) return false;
Rect __1 = (Rect) o;
return x == __1.x && y == __1.y && w == __1.w && h == __1.h;
}
public int hashCode() {
int h = 2543108;
h = boostHashCombine(h, _hashCode(x));
h = boostHashCombine(h, _hashCode(y));
h = boostHashCombine(h, _hashCode(w));
h = boostHashCombine(h, _hashCode(h));
return h;
}
public Object[] _fieldsToList() { return new Object[] {x, y, w, h}; }
Rect(Rectangle r) {
x = r.x;
y = r.y;
w = r.width;
h = r.height;
}
Rect(Pt p, int w, int h) {
this.h = h;
this.w = w; x = p.x; y = p.y; }
Rect(Rect r) { x = r.x; y = r.y; w = r.w; h = r.h; }
final Rectangle getRectangle() {
return new Rectangle(x, y, w, h);
}
public String toString() {
return x + "," + y + " / " + w + "," + h;
}
final int x1() { return x; }
final int y1() { return y; }
final int x2() { return x + w; }
final int y2() { return y + h; }
final boolean contains(Pt p) {
return contains(p.x, p.y);
}
final boolean contains(int _x, int _y) {
return _x >= x && _y >= y && _x < x+w && _y < y+h;
}
final boolean contains(Rectangle r) {
return rectContains(this, r);
}
final boolean empty() { return w <= 0 || h <= 0; }
final public int getWidth() { return w; }
final public int getHeight() { return h; }
final public int area() { return w*h; }
final public int x() { return x; }
final public int y() { return y; }
WidthAndHeight widthAndHeight() { return main.widthAndHeight(w, h); }
}
static interface IBWImage extends MakesBufferedImage, IRGBImage {
float getFloatPixel(int x, int y); // usually between 0 and 1
default int getInt(int x, int y) { return iround(getFloatPixel(x, y)*255f); }
// implementing IRGBImage
default int getIntPixel(int x, int y) { return rgbIntFromGrayscale(getInt(x, y)); }
default float getFloatPixel(Pt p) { return getFloatPixel(p.x, p.y); }
default float getFloatPixel(int index) {
int w = w();
return getFloatPixel(index % w, index / w);
}
default float[] toFloatArray() {
float[] data = new float[w()*h()];
for (int i = 0; i < l(data); i++)
data[i] = getFloatPixel(i);
return data;
}
public default BufferedImage getBufferedImage() {
return grayImageFromIBWImage(this);
}
default BWImage toBWImage() {
return this instanceof BWImage ? (BWImage) this : iBWImageToBWImage(this);
}
}
// uses hash sets as inner sets unless subclassed
// uses a hash map as the outer map by default
static class MultiSetMapimplements IMultiMap {
Map> data = new HashMap>();
int size; // number of values
MultiSetMap() {}
MultiSetMap(boolean useTreeMap) { if (useTreeMap) data = new TreeMap(); }
MultiSetMap(MultiSetMap map) { putAll(map); }
MultiSetMap(Map> data) {
this.data = data;}
boolean put(A key, B value) { synchronized(data) {
Set set = data.get(key);
if (set == null)
data.put(key, set = _makeEmptySet());
if (!set.add(value)) return false;
{ ++size; return true; }
}}
boolean add(A key, B value) { return put(key, value); }
void addAll(A key, Collection values) { synchronized(data) {
putAll(key, values);
}}
void addAllIfNotThere(A key, Collection values) { synchronized(data) {
for (B value : values)
setPut(key, value);
}}
void setPut(A key, B value) { synchronized(data) {
if (!containsPair(key, value))
put(key, value);
}}
final boolean contains(A key, B value){ return containsPair(key, value); }
boolean containsPair(A key, B value) { synchronized(data) {
return get(key).contains(value);
}}
void putAll(A key, Collection values) { synchronized(data) {
for (B value : values)
put(key, value);
}}
void removeAll(A key, Collection values) { synchronized(data) {
for (B value : values)
remove(key, value);
}}
public Set get(A key) { synchronized(data) {
Set set = data.get(key);
return set == null ? Collections. emptySet() : set;
}}
List getAndClear(A key) { synchronized(data) {
List l = cloneList(data.get(key));
remove(key);
return l;
}}
// return null if empty
Set getOpt(A key) { synchronized(data) {
return data.get(key);
}}
// returns actual mutable live set
// creates the set if not there
Set getActual(A key) { synchronized(data) {
Set set = data.get(key);
if (set == null)
data.put(key, set = _makeEmptySet());
return set;
}}
// TODO: this looks unnecessary
void clean(A key) { synchronized(data) {
Set list = data.get(key);
if (list != null && list.isEmpty())
data.remove(key);
}}
final public Set keys(){ return keySet(); }
public Set keySet() { synchronized(data) {
return data.keySet();
}}
void remove(A key) { synchronized(data) {
size -= l(data.get(key));
data.remove(key);
}}
void remove(A key, B value) { synchronized(data) {
Set set = data.get(key);
if (set != null) {
if (set.remove(value)) {
--size;
if (set.isEmpty())
data.remove(key);
}
}
}}
void clear() { synchronized(data) {
data.clear();
size = 0;
}}
boolean containsKey(A key) { synchronized(data) {
return data.containsKey(key);
}}
B getFirst(A key) { synchronized(data) {
return first(get(key));
}}
void addAll(MultiSetMap map) { putAll(map); }
void putAll(MultiSetMap map) { synchronized(data) {
for (A key : map.keySet())
putAll(key, map.get(key));
}}
void putAll(Map map) { synchronized(data) {
if (map != null) for (Map.Entry e : map.entrySet())
put(e.getKey(), e.getValue());
}}
final public int keyCount(){ return keysSize(); }
public int keysSize() { synchronized(data) { return l(data); }}
// full size
public int size() { synchronized(data) {
return size;
}}
// count values for key
int getSize(A key) { return l(data.get(key)); }
int count(A key) { return getSize(key); }
// expensive operation
Set reverseGet(B b) { synchronized(data) {
Set l = new HashSet();
for (A key : data.keySet())
if (data.get(key).contains(b))
l.add(key);
return l;
}}
// expensive operation
A keyForValue(B b) { synchronized(data) {
for (A key : data.keySet())
if (data.get(key).contains(b))
return key;
return null;
}}
Map> asMap() { synchronized(data) {
return cloneMap(data);
}}
boolean isEmpty() { synchronized(data) { return data.isEmpty(); }}
// override in subclasses
Set _makeEmptySet() {
return new HashSet();
}
Collection> allLists() {
synchronized(data) {
return new HashSet(data.values());
}
}
List allValues() {
return concatLists(values(data));
}
List> allEntries() { synchronized(data) {
List> l = emptyList(size);
for (Map.Entry extends A, ? extends Set> __0 : _entrySet( data))
{ A a = __0.getKey(); Set set = __0.getValue(); for (B b : set)
l.add(pair(a, b)); }
return l;
}}
Object mutex() { return data; }
public String toString() { return "mm" + str(data); }
Pair firstEntry() { synchronized(data) {
if (empty(data)) return null;
Map.Entry> entry = data.entrySet().iterator().next();
return pair(entry.getKey(), first(entry.getValue()));
}}
A firstKey() { synchronized(data) { return main.firstKey(data); }}
A lastKey() { synchronized(data) { return (A) ((NavigableMap) data).lastKey(); }}
A higherKey(Object a) { synchronized(data) { return (A) ((NavigableMap) data).higherKey(a); }}
}
interface IStackWithOptions extends IVStack {
// declare a choice point for backtracking
// (or a probabilistic choice point if it's a PStack)
default void options(B currentFrame, IVF1... options) {
options(currentFrame, arrayIterator(options));
}
void options(B currentFrame, Iterable> options);
}
static class VStack implements Steppable, IVStack {
List stack = new ArrayList();
Object latestResult;
transient Object newResult;
// the null sentinel replaces a null function result
// when stored in latestResult
static class NullSentinel {}
static NullSentinel nullSentinel = new NullSentinel();
VStack() {}
VStack(Computable computation) { push(computation); }
VStack(Iterable l) { pushAll(l); }
// A is what the function returns
interface Computable {
public void step(VStack stack, Object subComputationResult);
}
private Object deSentinel(Object o) {
return o instanceof NullSentinel ? null : o;
}
private Object sentinel(Object o) {
return o == null ? nullSentinel : o;
}
// called by computations or users to start a subroutine
public void push(Computable computation) {
stack.add(assertNotNull(computation));
}
// perform a computation step. returns false iff done
public boolean step() {
if (empty(stack)) return false;
newResult = null;
last(stack).step(this, result());
latestResult = newResult;
newResult = null;
return true;
}
// called from a computation to return a value
public void _return(Object value) {
newResult = sentinel(value);
pop();
}
// called from a computation to tail-call another routine
final void replace(Computable computation){ tailCall(computation); }
void tailCall(Computable computation) {
pop();
stack.add(computation);
}
// all-in-one evaluation function - call on an empty stack
A compute(Computable computation) {
if (computation == null) return null;
push(computation);
stepAll(this);
return (A) latestResult;
}
// return result of just completed computation or sub-computation
final public Object subResult(){ return result(); }
public Object result() {
return deSentinel(latestResult);
}
boolean hasSubResult() { return latestResult != null; }
void pushAll(Iterable l) {
for (Computable c : unnullForIteration(l))
push(c);
}
void add(Runnable r) {
if (r == null) return;
push((stack, subComputationResult) -> r.run());
}
// caller of current function
Computable caller() {
return nextToLast(stack);
}
protected void pop() {
removeLast(stack);
}
boolean isEmpty() { return empty(stack); }
}
// -has fast nextElement() and prevElement()
// -design allows for more functions like reordering the list
// -Saves up to 34% in space over LinkedHashSet
// (e.g. 22% for a set of 1,000 Ints)
static class CompactLinkedHashSet extends AbstractSet {
UnsynchronizedCompactHashSet> entries = new UnsynchronizedCompactHashSet();
Entry head, tail;
static class Entry {
A value;
Entry prev, next;
public int hashCode() {
return _hashCode(value);
}
// "magic" equals function for CompactHashSet lookup without temp object
public boolean equals(Object o) {
return o == this || eq(value, o);
}
}
public boolean add(A a) {
if (entries.contains(a)) return false;
Entry n = new Entry();
n.value = a;
n.prev = tail;
if (tail != null) tail.next = n;
tail = n;
if (head == null) head = n;
entries.add(n);
return true;
}
public boolean remove(Object a) {
return remove(entries.find(a));
}
public boolean remove(Entry node) {
if (node == null) return false;
if (node.next != null) node.next.prev = node.prev; else tail = node.prev;
if (node.prev != null) node.prev.next = node.next; else head = node.next;
entries.remove(node);
return true;
}
public int size() { return entries.size(); }
public IterableIterator iterator() {
return new IterableIterator() {
Entry entry = head, prev = null;
public boolean hasNext() { return entry != null; }
public A next() {
A a = entry.value;
prev = entry;
entry = entry.next;
return a;
}
// untested
public void remove() {
if (prev == null) throw new IllegalStateException();
CompactLinkedHashSet.this.remove(prev);
prev = null;
}
};
}
public void clear() {
entries.clear();
head = tail = null;
}
public boolean contains(Object a) {
return entries.contains(a);
}
public A find(Object o) {
Entry e = entries.find(o);
return e == null ? null : e.value;
}
public A prevElement(A a) {
Entry e = entries.find(a);
if (e == null || e.prev == null) return null;
return e.prev.value;
}
public A nextElement(A a) {
Entry e = entries.find(a);
if (e == null || e.next == null) return null;
return e.next.value;
}
public A first() { return head == null ? null : head.value; }
public A last() { return tail == null ? null : tail.value; }
boolean removeIfSame(Object o) {
A value = find(o);
if (value == o) {
remove(value);
return true;
}
return false;
}
}
static class RGBImage implements MakesBufferedImage, IRGBImage {
transient BufferedImage bufferedImage;
int width, height;
int[] pixels;
RGBImage() {}
RGBImage(BufferedImage image) {
bufferedImage = image;
width = image.getWidth();
height = image.getHeight();
pixels = new int[width*height];
var gp = grabbableIntPixels_fastOrSlow(image);
if (gp.scanlineStride == width && gp.offset == 0)
pixels = gp.data;
else {
pixels = new int[width*height];
int iIn = 0, iOut = 0;
for (int y = 0; y < height; y++) {
arrayCopy(gp.data, iIn, pixels, iOut, width);
iIn += gp.scanlineStride;
iOut += width;
}
}
cleanPixels(); // set upper byte to 0
}
RGBImage(Dimension size, Color color) {
this(size.width, size.height, color);
}
RGBImage(Dimension size, RGB color) {
this(size.width, size.height, color);
}
private void cleanPixels() {
var pixels = this.pixels;
for (int i = 0; i < pixels.length; i++)
pixels[i] &= 0xFFFFFF;
}
RGBImage(int width, int height, int[] pixels) {
this.width = width;
this.height = height;
this.pixels = pixels;
}
RGBImage(int w, int h, RGB[] pixels) {
this.width = w;
this.height = h;
this.pixels = asInts(pixels);
}
public static int[] asInts(RGB[] pixels) {
int[] ints = new int[pixels.length];
for (int i = 0; i < pixels.length; i++)
ints[i] = pixels[i] == null ? 0 : pixels[i].getColor().getRGB();
return ints;
}
public RGBImage(int w, int h) {
this(w, h, Color.black);
}
RGBImage(int w, int h, RGB rgb) {
this.width = w;
this.height = h;
this.pixels = new int[w*h];
int col = rgb.asInt();
if (col != 0)
for (int i = 0; i < pixels.length; i++)
pixels[i] = col;
}
RGBImage(RGBImage image) {
this(image.width, image.height, copyPixels(image.pixels));
}
RGBImage(int width, int height, Color color) {
this(width, height, new RGB(color));
}
RGBImage(MakesBufferedImage img) {
this(toBufferedImage(img));
}
private static int[] copyPixels(int[] pixels) {
int[] copy = new int[pixels.length];
System.arraycopy(pixels, 0, copy, 0, pixels.length);
return copy;
}
public int getIntPixel(int x, int y) {
if (inRange(x, y))
return pixels[y * width + x];
else
return 0xFFFFFF;
}
// idx = index in pixel array
public int getIntPixel_noRangeCheck(int idx) {
return pixels[idx];
}
public static RGB asRGB(int packed) {
int r = (packed >> 16) & 0xFF;
int g = (packed >> 8) & 0xFF;
int b = packed & 0xFF;
return new RGB(r / 255f, g / 255f, b / 255f);
}
public RGB getRGB(int x, int y) {
if (inRange(x, y))
return asRGB(pixels[y * width + x]);
else
return new RGB(0xFFFFFF);
}
/** alias of getRGB - I kept typing getPixel instead of getRGB all the time, so I finally created it */
RGB getPixel(int x, int y) {
return getRGB(x, y);
}
RGB getPixel(Pt p) { return getPixel(p.x, p.y); }
public int getWidth() { return width; }
public int getHeight() { return height; }
public int w() { return width; }
public int h() { return height; }
/** Attention: cached, i.e. does not change when image itself changes */
/** @NotNull */
public BufferedImage getBufferedImage() {
if (bufferedImage == null) {
bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//bufferedImage.setData(Raster.createRaster(new SampleModel()));
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
bufferedImage.setRGB(x, y, pixels[y*width+x]);
}
return bufferedImage;
}
RGBImage clip(Rect r) {
return r == null ? null : clip(r.getRectangle());
}
RGBImage clip(Rectangle r) {
r = fixClipRect(r);
if (r.x == 0 && r.y == 0 && r.width == width && r.height == height) return this;
int[] newPixels;
try {
newPixels = new int[r.width*r.height];
} catch (RuntimeException e) {
System.out.println(r);
throw e;
}
for (int y = 0; y < r.height; y++) {
System.arraycopy(pixels, (y+r.y)*width+r.x, newPixels, y*r.width, r.width);
}
return new RGBImage(r.width, r.height, newPixels);
}
private Rectangle fixClipRect(Rectangle r) {
r = r.intersection(new Rectangle(0, 0, width, height));
if (r.isEmpty())
r = new Rectangle(r.x, r.y, 0, 0);
return r;
}
public int getInt(int x, int y) {
return pixels[y * width + x];
}
public void save(File file) {
saveImage(file, getBufferedImage());
}
public static RGBImage dummyImage() {
return new RGBImage(1, 1, new int[] {0xFFFFFF});
}
public int[] getPixels() {
return pixels;
}
void setPixel(int x, int y, int r, int g, int b) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y*width+x] = (limitToUByte(r) << 16) | (limitToUByte(g) << 8) | limitToUByte(b);
}
public void setPixel(int x, int y, RGB rgb) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y*width+x] = rgb.asInt();
}
final public void set(int x, int y, Color color){ setPixel(x, y, color); }
public void setPixel(int x, int y, Color color) {
setPixel(x, y, new RGB(color));
}
void setInt(int x, int y, int rgb) {
setPixel(x, y, rgb);
}
public void setPixel(int x, int y, int rgb) {
if (x >= 0 && y >= 0 && x < width && y < height)
pixels[y*width+x] = rgb;
}
void setPixel(Pt p, RGB rgb) { setPixel(p.x, p.y, rgb); }
void setPixel(Pt p, Color color) { setPixel(p.x, p.y, color); }
public RGBImage copy() {
return new RGBImage(this);
}
public boolean inRange(int x, int y) {
return x >= 0 && y >= 0 && x < width && y < height;
}
public Dimension getSize() {
return new Dimension(width, height);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RGBImage rgbImage = (RGBImage) o;
if (height != rgbImage.height) return false;
if (width != rgbImage.width) return false;
if (!Arrays.equals(pixels, rgbImage.pixels)) return false;
return true;
}
@Override
public int hashCode() {
int result = width;
result = 31 * result + height;
result = 31 * result + Arrays.hashCode(pixels);
return result;
}
public String getHex(int x, int y) {
return getPixel(x, y).getHexString();
}
public RGBImage clip(int x, int y, int width, int height) {
return clip(new Rectangle(x, y, width, height));
}
public RGBImage clipLine(int y) {
return clip(0, y, width, 1);
}
public int numPixels() {
return width*height;
}
RGBImage uncacheBufferedImage() {
bufferedImage = null;
return this;
}
}
static final class BWImage extends Meta implements MakesBufferedImage, IBWImage {
int width, height;
byte[] pixels;
// color returned when getPixel is called with a position outside the actual image
float borderColor = 0.0f;
// for unstructure()
BWImage() {}
// BLACK!
BWImage(int width, int height) {
this.height = height;
this.width = width;
pixels = new byte[width*height];
}
BWImage(int width, int height, float brightness) {
this.height = height;
this.width = width;
pixels = new byte[width*height];
fillArrayUnlessZero(pixels, _toByte(brightness));
}
BWImage(int width, int height, float[] pixels) {
this.pixels = new byte[pixels.length];
this.height = height;
this.width = width;
for (int i = 0; i < pixels.length; i++)
this.pixels[i] = _toByte(pixels[i]);
}
public BWImage(int width, int height, byte[] pixels) {
this.height = height;
this.width = width;
this.pixels = pixels;
}
public BWImage(BWImage image) {
width = image.getWidth();
height = image.getHeight();
byte[] pixels = this.pixels = new byte[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
pixels[y*width+x] = image.getByte(x, y);
}
// TODO: optimize!
BWImage(RGBImage image) {
width = image.getWidth();
height = image.getHeight();
byte[] pixels = this.pixels = new byte[height*width];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
RGB rgb = image.getRGB(x, y);
pixels[y*width+x] = BWImage._toByte(rgb.getBrightness());
}
}
/*public BWImage(BufferedImage image) {
this(new RGBImage(image));
}*/
BWImage(BufferedImage image) { try {
width = image.getWidth();
height = image.getHeight();
int[] pixels = new int[width*height];
byte[] bytePixels = this.pixels = new byte[width*height];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
if (!pixelGrabber.grabPixels())
throw fail("Could not grab pixels");
int n = width*height;
for (int i = 0; i < n; i++) {
//bytePixels[i] = pixelToByte(pixels[i]);
int packed = pixels[i];
/*float r = ((packed >> 16) & 0xFF)/255f;
float g = ((packed >> 8) & 0xFF)/255f;
float b = (packed & 0xFF)/255f;
bytePixels[i] = (byte) iround((r+g+b)/3.0f*255f);*/
int r = ((packed >> 16) & 0xFF);
int g = ((packed >> 8) & 0xFF);
int b = (packed & 0xFF);
bytePixels[i] = (byte) ((r+g+b+1)/3);
}
} catch (Exception __e) { throw rethrow(__e); } }
// TODO: does it exactly match the other method? (asRGB+getBrightness+_toByte)
static byte pixelToByte(int packed) {
/*int r = (packed >> 16) & 0xFF;
int g = (packed >> 8) & 0xFF;
int b = packed & 0xFF;
ret (byte) ((r+g+b)/3.0f);*/
float r = ((packed >> 16) & 0xFF)/255f;
float g = ((packed >> 8) & 0xFF)/255f;
float b = (packed & 0xFF)/255f;
return (byte) ((r+g+b)/3.0f*255f);
}
public byte getByte(int x, int y) {
return inRange(x, y) ? getByte_noRangeCheck(x, y) : _toByte(borderColor);
}
// pretty bad function name
// gets brightness (0 to 255) at pixel
public int getInt(int x, int y) {
return ubyteToInt(getByte(x, y));
}
// idx = index in pixel array
public int getInt_noRangeCheck(int idx) {
return ubyteToInt(pixels[idx]);
}
// gets brightness (0 to 255) at pixel with default if out of image
public int getInt(int x, int y, int defaultValue) {
return inRange(x, y) ? getInt(x, y) : defaultValue;
}
public double averageBrightness() {
double sum = 0;
int n = width*height;
for (int i = 0; i < n; i++)
sum += getInt_noRangeCheck(i);
return sum/n;
}
public float minimumBrightness() {
float min = 1;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
min = Math.min(min, getPixel(x, y));
return min;
}
public float maximumBrightness() {
float max = 0;
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
max = Math.max(max, getPixel(x, y));
return max;
}
float getPixel(int x, int y) {
return inRange(x, y) ? _toFloat(getByte(x,y )) : borderColor;
}
public float getFloatPixel(int x, int y) { return getPixel(x, y); }
float getPixel(Pt p) { return getPixel(p.x, p.y); }
static byte _toByte(float pixel) {
return (byte) (pixel*255f);
}
static float _toFloat(byte pixel) {
return (((int) pixel) & 255)/255f;
}
private boolean inRange(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < height;
}
public int getWidth() { return width; }
public int getHeight() { return height; }
public RGBImage toRGB() {
int[] rgbs = new int[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int b = getByte(x, y) & 0xFF;
rgbs[y*width+x] = 0xFF000000 | b*0x010101;
}
return new RGBImage(width, height, rgbs);
}
public RGBImage toRGB_slow() {
RGB[] rgbs = new RGB[width*height];
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
float p = getPixel(x, y);
rgbs[y*width+x] = new RGB(p, p, p);
}
return new RGBImage(width, height, rgbs);
}
public BWImage clip(int x, int y, int w, int h) {
return clip(new Rectangle(x, y, w, h));
}
private Rectangle fixClipRect(Rectangle r) {
return r.intersection(new Rectangle(0, 0, width, height));
}
BWImage clip(Rect r) {
return clip(r.getRectangle());
}
/** this should be multithread-safe */
public BWImage clip(Rectangle r) {
r = fixClipRect(r);
byte[] newPixels = new byte[r.height*r.width];
for (int y = 0; y < r.height; y++)
for (int x = 0; x < r.width; x++)
newPixels[y*r.width+x] = getByte(r.x+x, r.y+y);
return new BWImage(r.width, r.height, newPixels);
}
public void setPixel(int x, int y, float brightness) {
setByte(x, y, _toByte(fixPixel(brightness)));
}
// i = 0 to 255
public void setInt(int x, int y, int i) {
setByte(x, y, (byte) limitToUByte(i));
}
public void setInt(Pt p, int i) {
setInt(p.x, p.y, i);
}
public void setByte(int x, int y, byte b) {
if (x >= 0 && x < width && y >= 0 && y < height)
pixels[y*width+x] = b;
}
byte getByte_noRangeCheck(int x, int y) {
return pixels[y*width+x];
}
public void setByte(int x, int y, int brightness) {
setByte(x, y, (byte) brightness);
}
private float fixPixel(float pixel) {
return Math.max(0, Math.min(1, pixel));
}
public float getBorderColor() {
return borderColor;
}
public void setBorderColor(float borderColor) {
this.borderColor = borderColor;
}
public boolean anyPixelBrighterThan(double threshold) {
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
if (getPixel(x, y) > threshold)
return true;
return false;
}
int[] getRGBPixels() {
int n = width*height;
int[] out = new int[n];
for (int i = 0; i < n; i++) {
var b = ubyteToInt(pixels[i]);
b |= (b << 8) | (b << 16);
out[i] = b | 0xFF000000;
}
return out;
}
public BufferedImage getBufferedImage() {
return bufferedImage(getRGBPixels(), width, height);
}
byte[] getBytes() {
return pixels;
}
}
static interface IBinaryImage extends MakesBufferedImage {
boolean getBoolPixel(int x, int y);
default void setPixel(int x, int y, boolean b) { throw unimplemented(); }
default boolean getBoolPixel(Pt p) {
return getBoolPixel(p.x, p.y);
}
public default BufferedImage getBufferedImage() {
return toImage2B().getBufferedImage();
}
default Image2B toImage2B() {
return this instanceof Image2B ? (Image2B) this : iBinaryImageToImage2B(this);
}
}
// pixels may contain any float number (e.g. for a distance map)
static final class FloatBWImage implements IBWImage {
int width, height;
float[] pixels; // 0 to 1 usually
// color returned when getPixel is called with a position outside the actual image
float borderColor = 0.0f;
// for unstructure()
FloatBWImage() {}
// BLACK!
FloatBWImage(int width, int height) {
this.height = height;
this.width = width;
pixels = new float[width*height];
}
FloatBWImage(int width, int height, float brightness) {
this.height = height;
this.width = width;
pixels = new float[width*height];
fillArrayUnlessZero(pixels, brightness);
}
FloatBWImage(int width, int height, float[] pixels) {
this.pixels = pixels;
this.height = height;
this.width = width;}
final public float getFloatPixel(int x, int y){ return getPixel(x, y); }
public float getPixel(int x, int y) {
return inRange(x, y) ? getPixel_noRangeCheck(x, y) : borderColor;
}
public float getFloatPixel(int index) {
return pixels[index];
}
float getPixel(Pt p) { return getPixel(p.x, p.y); }
private boolean inRange(int x, int y) {
return x >= 0 && x < width && y >= 0 && y < height;
}
public int getWidth() { return width; }
public int getHeight() { return height; }
public void setPixel(int x, int y, float brightness) {
pixels[y*width+x] = brightness;
}
float getPixel_noRangeCheck(int x, int y) {
return pixels[y*width+x];
}
public byte getByte(int x, int y) {
return inRange(x, y) ? getByte_noRangeCheck(x, y) : _toByte(borderColor);
}
byte getByte_noRangeCheck(int x, int y) {
return _toByte(pixels[y*width+x]);
}
static byte _toByte(float pixel) {
return (byte) (Math.max(0, Math.min(1, pixel))*255f);
}
// TODO: optimize
public BufferedImage getBufferedImage() {
BufferedImage img = bufferedImage(width, height);
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++) {
int b = ((int) getByte(x, y) & 0xFF);
img.setRGB(x, y, b*0x010101);
}
return img;
}
public double averageBrightness() {
return floatSumAsDouble(pixels)/(width*height);
}
}
static class WithProbability extends Var implements Comparable> {
double probability; // assumed between 0 and 1
WithProbability() {}
WithProbability(A a) { this(1, a); }
WithProbability(double probability, A a) { super(a);
this.probability = probability; }
public String toString() {
return "p=" + renderedProbability() + ": " + str(get());
}
String renderedProbability() {
return formatDouble_noLeadingZero(probability, 2);
}
double probability() { return probability; }
public int hashCode() {
return boostHashCombine(main.hashCode(probability), main.hashCode(get()));
}
public boolean equals(Object o) {
if (o instanceof WithProbability) {
return probability == ((WithProbability) o).probability && eq(get(), ((WithProbability) o).get());
}
return false;
}
// Higher probability comes first!
public int compareTo(WithProbability wp) {
return wp == null ? 1
: cmp(wp.probability, probability);
}
}
static interface IRGBImage extends MakesBufferedImage {
int getIntPixel(int x, int y);
}
// Meta - a "minimal" approach to adding meta-level to Java objects
static class Meta implements IMeta {
// Meta - a "minimal" approach to adding meta-level to Java objects
// (implementing the interface IMeta)
// We allocate one extra field for each Java object to make it
// reasoning-compatible (reasoning-compatible = extensible with
// fields of any name at runtime).
//
// We couldn't go for 0 extra fields (meta values must be linked
// directly from the object) and there are no half fields in
// Java... so there you go.
//
// Also, if you don't use any meta data, you are probably not
// reasoning about anything. The point of reasoning in JavaX is
// to attach information to objects directly used in the program.
// Possible information contained in the meta field:
// Origin, destination, security level, sender, cost center,
// purpose, list of reifications, ...
// So here it is. THE FIELD YOU HAVE BEEN WAITING FOR!
// [We also have IMeta to retrofit foreign classes (rare but
// probably useful).]
//////////////////////
// The "meta" field //
//////////////////////
// Generic meta value of any kind, but the typical case is it's a
// Map with extra field values for the object etc.
// "meta" is volatile to avoid synchronization; but you can also synchronize on
// _tempMetaMutex() which is usually the object itself. Collections
// and maps are exempt from using the collections's monitor as the meta
// mutex because their monitor tends to be held for long operations
// (e.g. cloneList). For those we use a substantially more complex
// algorithm using a weakMap. Probably overkill. I may reconsider.
volatile Object meta;
// The meta field is not transient, thus by default it will be
// persisted like anything else unless you customize your object
// to suppress or modulate this.
// ...and the interface methods
public void _setMeta(Object meta) { this.meta = meta; }
public Object _getMeta() { return meta; }
// MOST functions are implemented in IMeta (default implementations)
// Scaffolding convenience functions
final boolean scaffolding(){ return scaffoldingEnabled(); }
boolean scaffoldingEnabled() { return main.scaffoldingEnabled(this); }
boolean scaffoldingEnabled(Object o) { return main.scaffoldingEnabled(o); }
// Implementing setMetaToString
String toString_base() { return super.toString(); }
public String toString() {
Object o = metaGet("toString", this);
if (o instanceof String) return ((String) o);
if (o instanceof IF1) return str(((IF1) o).get(this));
return toString_base();
}
}
static class Pt implements Comparable, IDoublePt {
int x, y;
Pt() {}
Pt(Point p) {
x = p.x;
y = p.y;
}
Pt(int x, int y) {
this.y = y;
this.x = x;}
Point getPoint() {
return new Point(x, y);
}
public boolean equals(Object o) {
return o instanceof Pt && x == ((Pt) o).x && y == ((Pt) o).y;
}
public int hashCode() {
return boostHashCombine(x, y);
}
// compare in scan order
public int compareTo(Pt p) {
if (y != p.y) return cmp(y, p.y);
return cmp(x, p.x);
}
public String toString() {
return x + ", " + y;
}
double length() { return sqrt(x*x+y*y); }
public Pt minus(Pt p) { return ptMinus(this, p); }
public double x_double() { return x; }
public double y_double() { return y; }
}
static class Var implements IVar, ISetter {
Var() {}
Var(A v) {
this.v = v;}
A v; // you can access this directly if you use one thread
public synchronized void set(A a) {
if (v != a) {
v = a;
notifyAll();
}
}
public synchronized A get() { return v; }
public synchronized boolean has() { return v != null; }
public void clear() { set(null); }
public synchronized A getAndSet(A a) {
var value = v;
set(a);
return value;
}
public IF0 getter() { return () -> get(); }
public IVF1 setter() { return __21 -> set(__21); }
public String toString() { return str(this.get()); }
}
/*
* #!
* Ontopia Engine
* #-
* Copyright (C) 2001 - 2013 The Ontopia Project
* #-
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* !#
*/
// modified by Stefan Reich
// Implements the Set interface more compactly than
// java.util.HashSet by using a closed hashtable.
// Note: equals is always called on the _stored_ object, not the one
// passed as an argument to find(), contains() etc.
// (In case you want to put special magic in your equals() function)
static class UnsynchronizedCompactHashSet extends java.util.AbstractSet {
protected final static int INITIAL_SIZE = 3;
public final static double LOAD_FACTOR = 0.75;
protected final static Object nullObject = new Object();
protected final static Object deletedObject = new Object();
protected int elements;
protected int freecells;
protected A[] objects;
protected int modCount;
UnsynchronizedCompactHashSet() {
this(INITIAL_SIZE);
}
UnsynchronizedCompactHashSet(int size) {
// NOTE: If array size is 0, we get a
// "java.lang.ArithmeticException: / by zero" in add(Object).
objects = (A[]) new Object[(size==0 ? 1 : size)];
elements = 0;
freecells = objects.length;
modCount = 0;
}
UnsynchronizedCompactHashSet(Collection c) {
this(c.size());
addAll(c);
}
@Override
public Iterator iterator() {
return new CompactHashIterator();
}
@Override
public int size() {
return elements;
}
@Override
public boolean isEmpty() {
return elements == 0;
}
@Override
public boolean contains(Object o) {
return find(o) != null;
}
A find(Object o) {
if (o == null) o = nullObject;
int hash = o.hashCode();
int index = (hash & 0x7FFFFFFF) % objects.length;
int offset = 1;
// search for the object (continue while !null and !this object)
while(objects[index] != null &&
!(objects[index].hashCode() == hash &&
objects[index].equals(o))) {
index = ((index + offset) & 0x7FFFFFFF) % objects.length;
offset = offset*2 + 1;
if (offset == -1)
offset = 2;
}
return objects[index];
}
boolean removeIfSame(Object o) {
A value = find(o);
if (value == o) {
remove(value);
return true;
}
return false;
}
@Override
public boolean add(Object o) {
if (o == null) o = nullObject;
int hash = o.hashCode();
int index = (hash & 0x7FFFFFFF) % objects.length;
int offset = 1;
int deletedix = -1;
// search for the object (continue while !null and !this object)
while(objects[index] != null &&
!(objects[index].hashCode() == hash &&
objects[index].equals(o))) {
// if there's a deleted object here we can put this object here,
// provided it's not in here somewhere else already
if (objects[index] == deletedObject)
deletedix = index;
index = ((index + offset) & 0x7FFFFFFF) % objects.length;
offset = offset*2 + 1;
if (offset == -1)
offset = 2;
}
if (objects[index] == null) { // wasn't present already
if (deletedix != -1) // reusing a deleted cell
index = deletedix;
else
freecells--;
modCount++;
elements++;
// here we face a problem regarding generics:
// add(A o) is not possible because of the null Object. We cant do 'new A()' or '(A) new Object()'
// so adding an empty object is a problem here
// If (! o instanceof A) : This will cause a class cast exception
// If (o instanceof A) : This will work fine
objects[index] = (A) o;
// do we need to rehash?
if (1 - (freecells / (double) objects.length) > LOAD_FACTOR)
rehash();
return true;
} else // was there already
return false;
}
@Override
public boolean remove(Object o) {
if (o == null) o = nullObject;
int hash = o.hashCode();
int index = (hash & 0x7FFFFFFF) % objects.length;
int offset = 1;
// search for the object (continue while !null and !this object)
while(objects[index] != null &&
!(objects[index].hashCode() == hash &&
objects[index].equals(o))) {
index = ((index + offset) & 0x7FFFFFFF) % objects.length;
offset = offset*2 + 1;
if (offset == -1)
offset = 2;
}
// we found the right position, now do the removal
if (objects[index] != null) {
// we found the object
// same problem here as with add
objects[index] = (A) deletedObject;
modCount++;
elements--;
return true;
} else
// we did not find the object
return false;
}
@Override
public void clear() {
elements = 0;
for (int ix = 0; ix < objects.length; ix++)
objects[ix] = null;
freecells = objects.length;
modCount++;
}
@Override
public Object[] toArray() {
Object[] result = new Object[elements];
Object[] objects = this.objects;
int pos = 0;
for (int i = 0; i < objects.length; i++)
if (objects[i] != null && objects[i] != deletedObject) {
if (objects[i] == nullObject)
result[pos++] = null;
else
result[pos++] = objects[i];
}
// unchecked because it should only contain A
return result;
}
// not sure if this needs to have generics
@Override
public T[] toArray(T[] a) {
int size = elements;
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance(
a.getClass().getComponentType(), size);
A[] objects = this.objects;
int pos = 0;
for (int i = 0; i < objects.length; i++)
if (objects[i] != null && objects[i] != deletedObject) {
if (objects[i] == nullObject)
a[pos++] = null;
else
a[pos++] = (T) objects[i];
}
return a;
}
protected void rehash() {
int garbagecells = objects.length - (elements + freecells);
if (garbagecells / (double) objects.length > 0.05)
// rehash with same size
rehash(objects.length);
else
// rehash with increased capacity
rehash(objects.length*2 + 1);
}
protected void rehash(int newCapacity) {
int oldCapacity = objects.length;
@SuppressWarnings("unchecked")
A[] newObjects = (A[]) new Object[newCapacity];
for (int ix = 0; ix < oldCapacity; ix++) {
Object o = objects[ix];
if (o == null || o == deletedObject)
continue;
int hash = o.hashCode();
int index = (hash & 0x7FFFFFFF) % newCapacity;
int offset = 1;
// search for the object
while(newObjects[index] != null) { // no need to test for duplicates
index = ((index + offset) & 0x7FFFFFFF) % newCapacity;
offset = offset*2 + 1;
if (offset == -1)
offset = 2;
}
newObjects[index] = (A) o;
}
objects = newObjects;
freecells = objects.length - elements;
}
private class CompactHashIterator implements Iterator {
private int index;
private int lastReturned = -1;
private int expectedModCount;
@SuppressWarnings("empty-statement")
public CompactHashIterator() {
for (index = 0; index < objects.length &&
(objects[index] == null ||
objects[index] == deletedObject); index++)
;
expectedModCount = modCount;
}
@Override
public boolean hasNext() {
return index < objects.length;
}
@SuppressWarnings("empty-statement")
@Override
public T next() {
/*if (modCount != expectedModCount)
throw new ConcurrentModificationException();*/
int length = objects.length;
if (index >= length) {
lastReturned = -2;
throw new NoSuchElementException();
}
lastReturned = index;
for (index += 1; index < length &&
(objects[index] == null ||
objects[index] == deletedObject); index++)
;
if (objects[lastReturned] == nullObject)
return null;
else
return (T) objects[lastReturned];
}
@Override
public void remove() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
if (lastReturned == -1 || lastReturned == -2)
throw new IllegalStateException();
// delete object
if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) {
objects[lastReturned] = (A) deletedObject;
elements--;
modCount++;
expectedModCount = modCount; // this is expected; we made the change
}
}
}
int capacity() { return objects.length; }
// returns true if there was a shrink
boolean shrinkToFactor(double factor) {
if (factor > LOAD_FACTOR)
throw fail("Shrink factor must be equal to or smaller than load factor: " + factor + " / " + LOAD_FACTOR);
int newCapacity = max(INITIAL_SIZE, iround(size()/factor));
if (newCapacity >= capacity()) return false;
rehash(newCapacity);
return true;
}
}
// you still need to implement hasNext() and next()
static abstract class IterableIterator implements Iterator, Iterable {
public Iterator iterator() {
return this;
}
public void remove() {
unsupportedOperation();
}
}
abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess {
}
static class RGB {
// usually in range [0, 1]
public float r, g, b; // can't be final cause persistence
RGB() {}
public RGB(float brightness) {
r = g = b = brightness;
}
public RGB(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
public RGB(double r, double g, double b) {
this.r = (float) r;
this.g = (float) g;
this.b = (float) b;
}
public RGB(double[] rgb) {
this(rgb[0], rgb[1], rgb[2]);
}
public RGB(int rgb) {
r = rgbRed(rgb)/255f;
g = rgbGreen(rgb)/255f;
b = rgbBlue(rgb)/255f;
}
public RGB(double brightness) {
this((float) brightness);
}
public RGB(Color color) {
r = color.getRed()/255f;
g = color.getGreen()/255f;
b = color.getBlue()/255f;
}
// TODO: 3-char version
public RGB(String hex) {
int i = l(hex)-6;
r = Integer.parseInt(hex.substring(i, i+2), 16)/255f;
g = Integer.parseInt(hex.substring(i+2, i+4), 16)/255f;
b = Integer.parseInt(hex.substring(i+4, i+6), 16)/255f;
}
public float getComponent(int i) {
return i == 0 ? r : i == 1 ? g : b;
}
public int getInt(int i) {
return i == 0 ? redInt() : i == 1 ? greenInt() : blueInt();
}
public Color getColor() {
return new Color(clampZeroToOne(r), clampZeroToOne(g), clampZeroToOne(b));
}
public static RGB newSafe(float r, float g, float b) {
return new RGB(Math.max(0, Math.min(1, r)), Math.max(0, Math.min(1, g)), Math.max(0, Math.min(1, b)));
}
int asInt() { return getColor().getRGB() & 0xFFFFFF; }
int getInt() { return getColor().getRGB() & 0xFFFFFF; }
int asIntWithAlpha() { return rgbInt(redInt(), greenInt(), blueInt()) | 0xFF000000; }
final public float brightness(){ return getBrightness(); }
public float getBrightness() {
return (r+g+b)/3.0f;
}
public String getHexString() {
return Integer.toHexString(asInt() | 0xFF000000).substring(2).toUpperCase();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof RGB)) return false;
RGB rgb = (RGB) o;
if (Float.compare(rgb.b, b) != 0) return false;
if (Float.compare(rgb.g, g) != 0) return false;
if (Float.compare(rgb.r, r) != 0) return false;
return true;
}
@Override
public int hashCode() {
int result = (r != +0.0f ? Float.floatToIntBits(r) : 0);
result = 31 * result + (g != +0.0f ? Float.floatToIntBits(g) : 0);
result = 31 * result + (b != +0.0f ? Float.floatToIntBits(b) : 0);
return result;
}
public boolean isBlack() {
return r == 0f && g == 0f && b == 0f;
}
public boolean isWhite() {
return r == 1f && g == 1f && b == 1f;
}
// This is hyperprecise
boolean isGrayscale() {
return r == g && g == b;
}
public String toString() {
//return getHexString();
return isGrayscale() ? formatChannel(r)
: spaceCombine("RGB", formatChannel(r), formatChannel(g), formatChannel(b));
}
String formatChannel(float value) {
return formatDouble(value, 2);
}
int redInt() { return iround(r*255); }
int greenInt() { return iround(g*255); }
int blueInt() { return iround(b*255); }
static float brightnessToFloat(int brightness) { return brightness/255f; }
RGB cloneMe() { return new RGB(r, g, b); }
}
static interface MakesBufferedImage extends WidthAndHeight {
BufferedImage getBufferedImage();
public default void drawAt(Graphics2D g, int x, int y) {
g.drawImage(getBufferedImage(), x, y, null);
}
}
interface IVStack {
default void call(VStack.Computable f){ push(f); }
void push(VStack.Computable f);
default void _return() { _return(null); }
void _return(Object value);
Object subResult();
}
static interface IVF1 {
void get(A a);
}
interface IMultiMap {
public Set keySet();
public Collection get(A a);
public int size();
public int keyCount();
}
// Note for implementors: It is allowed to call step() again
// after it returned false (usually it should just return false again)
static interface Steppable {
public boolean step(); // return false if done
}
static class Pair implements Comparable> {
final public Pair setA(A a){ return a(a); }
public Pair a(A a) { this.a = a; return this; } final public A getA(){ return a(); }
public A a() { return a; }
A a;
final public Pair setB(B b){ return b(b); }
public Pair b(B b) { this.b = b; return this; } final public B getB(){ return b(); }
public B b() { return b; }
B b;
Pair() {}
Pair(A a, B b) {
this.b = b;
this.a = a;}
public int hashCode() {
return hashCodeFor(a) + 2*hashCodeFor(b);
}
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Pair)) return false;
Pair t = (Pair) o;
return eq(a, t.a) && eq(b, t.b);
}
public String toString() {
return "<" + a + ", " + b + ">";
}
public int compareTo(Pair p) {
if (p == null) return 1;
int i = ((Comparable) a).compareTo(p.a);
if (i != 0) return i;
return ((Comparable) b).compareTo(p.b);
}
}
static interface ISetter {
void set(A a);
}
static interface IMeta {
// see class "Meta" for the bla bla
public void _setMeta(Object meta);
public Object _getMeta();
default public IAutoCloseableF0 _tempMetaMutex() {
return new IAutoCloseableF0() {
public Object get() { return IMeta.this; }
public void close() {}
};
}
// actually query another object
default public Object getMeta(Object obj, Object key){ return metaGet(obj, key); }
default public Object metaGet(Object obj, Object key) {
// call global function
return metaMapGet(obj, key);
}
default public Object metaGet(String key, Object obj) {
// call global function
return metaMapGet(obj, key);
}
default public Object getMeta(Object key){ return metaGet(key); }
default public Object metaGet(Object key) {
if (key == null) return null;
Object meta = _getMeta();
if (meta instanceof Map) return ((Map) meta).get(key);
return null;
}
default public void metaSet(IMeta obj, Object key, Object value){ metaPut(obj, key, value); }
default public void metaPut(IMeta obj, Object key, Object value) {
// call global function
metaMapPut(obj, key, value);
}
default public void metaSet(Object key, Object value){ metaPut(key, value); }
default public void metaPut(Object key, Object value) {
if (key == null) return;
Map map = convertObjectMetaToMap(this);
syncMapPutOrRemove(map, key, value);
}
}
interface IDoublePt {
public double x_double();
public double y_double();
}
static interface IVar extends IF0 {
void set(A a);
A get();
// reified type of value (if available)
default Class getType() { return null; }
default IF0 getter() { return () -> get(); }
default IVF1 setter() { return __1 -> set(__1); }
default boolean has() { return get() != null; }
default void clear() { set(null); }
}
public static interface IF0 {
A get();
}
static interface IAutoCloseableF0 extends IF0, AutoCloseable {}
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(MultiSet ms) { return ms == null ? 0 : ms.size(); }
static int l(IMultiMap mm) { return mm == null ? 0 : mm.size(); }
static int l(IntSize o) { return o == null ? 0 : o.size(); }
static String nMasks(long n) { return n2(n, "mask"); }
static String nMasks(Collection l) { return nMasks(l(l)); }
static String nMasks(Map map) { return nMasks(l(map)); }
static String formatDouble3(double d) {
return formatDouble(d, 3);
}
static String shortClassName_dropNumberPrefix(Object o) {
return dropNumberPrefix(shortClassName(o));
}
static List map(Iterable l, Object f) { return map(f, l); }
static List map(Object f, Iterable l) {
List x = emptyList(l);
if (l != null) for (Object o : l)
{ ping(); x.add(callF(f, o)); }
return x;
}
// map: func(key, value) -> list element
static List map(Map map, Object f) {
List x = new ArrayList();
if (map != null) for (Object _e : map.entrySet()) { ping();
Map.Entry e = (Map.Entry) _e;
x.add(callF(f, e.getKey(), e.getValue()));
}
return x;
}
static List map(Object f, Object[] l) { return map(f, asList(l)); }
static List map(Object[] l, Object f) { return map(f, l); }
static List map(Object f, Map map) {
return map(map, f);
}
static List map(Iterable l, F1 f) { return map(f, l); }
static List map(F1 f, Iterable l) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(callF(f, o)); }
return x;
}
static List map(IF1 f, Iterable l) { return map(l, f); }
static List map(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) {
var it = l.iterator();
if (it.hasNext()) {
var pingSource = pingSource();
do {
ping(pingSource);
x.add(f.get(it.next()));
} while (it.hasNext());
}
}
return x;
}
static List map(IF1 f, A[] l) { return map(l, f); }
static List map(A[] l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : l)
{ ping(); x.add(f.get(o)); }
return x;
}
static List map(Map map, IF2 f) {
List x = new ArrayList();
if (map != null) for (Map.Entry e : map.entrySet()) { ping();
x.add(f.get(e.getKey(), e.getValue()));
}
return x;
}
// new magic alias for mapLL - does it conflict?
static List map(IF1 f, A data1, A... moreData) {
List x = emptyList(l(moreData)+1);
x.add(f.get(data1));
if (moreData != null) for (A o : moreData)
{ ping(); x.add(f.get(o)); }
return x;
}
static int binaryImagePixelDifference_sameSize(Image2B img1, Image2B img2) {
assertSameSize(img1, img2);
byte[] pixels1 = img1.pixels;
byte[] pixels2 = img2.pixels;
int n = pixels1.length;
int diff = 0;
for (int i = 0; i < n; i++)
diff += popCount(pixels1[i]^pixels2[i]);
return diff;
}
static double doubleRatio(double x, double y) {
return y == 0 ? 0 : x/y;
}
static double doubleRatio(Seconds x, Seconds y) {
return doubleRatio(x.get(), y.get());
}
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();
}
static int area(BufferedImage img) {
return img == null ? 0 : img.getWidth()*img.getHeight();
}
static boolean step(Steppable steppable) {
return steppable == null ? null : steppable.step();
}
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 byte[] cloneByteArray(byte[] a) {
if (a == null) return null;
byte[] b = new byte[a.length];
System.arraycopy(a, 0, b, 0, a.length);
return b;
}
static int ubyteToInt(byte b) {
return b & 0x0FF;
}
static int ubyteToInt(char c) {
return c & 0x0FF;
}
static BWImage toBW(RGBImage img) {
return img == null ? null : new BWImage(img);
}
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 String str_px(int w, int h) {
return n2(w) + "*" + n2(h) + "px";
}
static String str_px(WidthAndHeight image) {
return image == null ? null : str_px(image.getWidth(), image.getHeight());
}
// 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 HashMap hashMap() {
return new HashMap();
}
static MultiSetMap multiSetMap_outerDescTreeMap_innerCompactLinkedHashSet() {
return new MultiSetMap(descTreeMap()) {
Set _makeEmptySet() { return new CompactLinkedHashSet(); }
};
}
static void addAll(Collection c, Iterable b) {
if (c != null && b != null) for (A a : b) c.add(a);
}
static boolean addAll(Collection c, Collection b) {
return c != null && b != null && c.addAll(b);
}
static boolean addAll(Collection c, B... b) {
return c != null && b != null && c.addAll(Arrays.asList(b));
}
static Map addAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(b);
return a;
}
static A addAll(A c, Collection extends Component> components) {
return addComponents(c, components);
}
static A addAll(A c, Component... components) {
return addComponents(c, components);
}
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 A last(CompactLinkedHashSet set) {
return set == null ? null : set.last();
}
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 A first(T3 t) {
return t == null ? null : t.a;
}
static Byte first(byte[] l) {
return empty(l) ? null : l[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 void add(BitSet bs, int i) {
bs.set(i);
}
static boolean add(Collection c, A a) {
return c != null && c.add(a);
}
static void add(Container c, Component x) {
addToContainer(c, x);
}
static long add(AtomicLong l, long b) {
return l.addAndGet(b);
}
static void remove(List l, int i) {
if (l != null && i >= 0 && i < l(l))
l.remove(i);
}
static void remove(Collection l, A a) {
if (l != null) l.remove(a);
}
static B remove(Map map, Object a) {
return map == null ? null : map.remove(a);
}
static void remove(BitSet bs, int i) {
bs.clear(i);
}
static int generalizedBinarySearch2(List l, IF1 cmp) {
return generalizedBinarySearch2(l, 0, l(l), cmp);
}
static int generalizedBinarySearch2(List l, int fromIndex, int toIndex, IF1 cmp) {
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
int mid = (low + high) >>> 1;
A midVal = l.get(mid);
int c = cmp.get(midVal);
if (c < 0)
low = mid + 1;
else if (c > 0)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
static void truncateList(List l, int n) {
if (l(l) <= n) return;
removeSubList(l, n, l(l));
}
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 void removeLast(List l) {
if (!l.isEmpty())
l.remove(l(l)-1);
}
static void removeLast(List l, int n) {
removeSubList(l, l(l)-n);
}
static void removeLast(int n, List l) {
removeLast(l, n);
}
static void removeLast(StringBuilder buf) {
if (buf == null) return;
int n = buf.length();
if (n > 0) buf.setLength(n-1);
}
static int orZero(Integer i) { return i == null ? 0 : i; }
static long orZero(Long l) { return l == null ? 0L : l; }
static double orZero(Double d) { return d == null ? 0.0 : d; }
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 List lazyMap_noSave(IF1 f, List l) {
return new RandomAccessAbstractList() {
final int size = l(l);
public int size() { return size; }
public B get(int i) {
return f.get(l.get(i));
}
};
}
static List lazyMap_noSave(List l, IF1 f) {
return lazyMap_noSave(f, l);
}
// images must have same size
static int[] sumOfBinaryImages(List extends IBinaryImage> images) {
int n = l(images);
if (n == 0) return null;
var it = iterator(images);
var img = it.next();
int w = img.getWidth(), h = img.getHeight();
int[] pixels = new int[w*h];
int nBytes = (w*h+7)/8;
var pingSource = pingSource();
while (true) {
ping(pingSource);
Image2B realizedImage = img.toImage2B();
byte[] imgPixels = realizedImage.pixels;
int j = 0;
for (int i = 0; i < nBytes; i++) {
int b = imgPixels[i];
for (int bit = 0; bit < 8; bit++) {
pixels[j++] += b & 1;
b >>= 1;
}
}
if (!it.hasNext()) break;
img = it.next();
}
return pixels;
}
static float floatRatio(float x, float y) {
return y == 0 ? 0 : x/y;
}
static void fillArray(byte[] a, byte value) {
Arrays.fill(a, value);
}
static void fillArray(int[] a, int value) {
Arrays.fill(a, value);
}
static void fillArray(float[] a, float value) {
Arrays.fill(a, value);
}
static void fillArray(double[] a, double value) {
Arrays.fill(a, value);
}
// white = certain (either completely black or completely white)
// black = uncertain (exactly 50% gray)
static FloatBWImage preciseCertaintyImage(IBWImage img) {
return floatBWImageFromFunction(img.getWidth(), img.getHeight(),
(x, y) -> g22pixelCertainty(img.getFloatPixel(x, y)));
}
// white = certain (either completely black or completely white)
// black = uncertain (exactly 50% gray)
static double g22pixelCertainty(double pixel) {
return absdiff(pixel, 0.5)*2;
}
static int[] intArray_minus(int[] array, int x) {
if (x == 0 || empty(array)) return array;
int n = array.length;
int[] b = new int[n];
for (int i = 0; i < n; i++)
b[i] = array[i]-x;
return b;
}
static int[] intArray_minus(int x, int[] array) {
return intArray_minus(array, x);
}
static int[] intArray_minus(int[] array1, int[] array2) {
int n = l(array1);
if (l(array2) != n) throw fail("Need same size");
if (n == 0) return array1;
int[] b = new int[n];
for (int i = 0; i < n; i++)
b[i] = array1[i]-array2[i];
return b;
}
static int getWidth(Component c) {
return c == null ? 0 : (int) swingCall(c, "getWidth");
}
static int getHeight(Component c) {
return c == null ? 0 : (int) swingCall(c, "getHeight");
}
static Rect rect(int x, int y, int w, int h) {
return new Rect(x, y, w, h);
}
static Rect rect(Pt p, int w, int h) {
return new Rect(p.x, p.y, w, h);
}
static Rect rect(int w, int h) {
return new Rect(0, 0, w, h);
}
static int toInt(Object o) {
if (o == null) return 0;
if (o instanceof Number)
return ((Number) o).intValue();
if (o instanceof String)
return parseInt((String) o);
if (o instanceof Boolean)
return boolToInt((Boolean) o);
throw fail("woot not int: " + getClassName(o));
}
static int toInt(long l) {
if (l != (int) l) throw fail("Too large for int: " + l);
return (int) l;
}
static long longMul(long a, long b) {
return a*b;
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >>> 2));
// OLD (changed) 2022/3/10: ret a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
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(Producer p, A a) {
if (p != null && a != null) while (true) {
A x = p.next();
if (x == null) break;
if (eq(x, a)) return true;
}
return false;
}
static boolean contains(Rect r, Pt p) { return rectContains(r, p); }
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 WidthAndHeight widthAndHeight(BufferedImage image) {
return image == null ? null : widthAndHeight(image.getWidth(), image.getHeight());
}
static WidthAndHeight widthAndHeight(int w) { return widthAndHeight(w, w); }
static WidthAndHeight widthAndHeight(int w, int h) {
return new WidthAndHeightFinal(w, h);
}
static int iround(double d) {
return (int) Math.round(d);
}
static int iround(Number n) {
return iround(toDouble(n));
}
// brightness: 0 to 255
static int rgbIntFromGrayscale(int brightness) {
brightness = clampToUByte(brightness);
return brightness | (brightness << 8) | (brightness << 16);
}
// brightness: 0 to 1
static int rgbIntFromGrayscale(double brightness) {
return rgbIntFromGrayscale(zeroToOneToZeroTo255(brightness));
}
static int getInt(List c, int i) {
return toInt(get(c, i));
}
static int getInt(Map map, Object key) {
return toInt(mapGet(map, key));
}
static int getInt(Object o, String field) {
return toInt(getOpt(o, (String) field));
}
static int getInt(String field, Object o) {
return getInt(o, field);
}
static BufferedImage grayImageFromIBWImage(IBWImage img) {
int w = img.getWidth(), h = img.getHeight();
byte[] pixels = new byte[w*h];
int i = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
int pixel = iround(img.getFloatPixel(x, y)*255); // tODO: is it round or floor?
pixels[i++] = clampToUByte(pixel);
}
return newGrayBufferedImage(w, h, pixels);
}
static BWImage iBWImageToBWImage(IBWImage img) {
int w = img.getWidth(), h = img.getHeight();
BWImage out = new BWImage(w, h);
for (int y = 0; y < h; y++) for (int x = 0; x < w; x++) out.setPixel(x, y, img.getFloatPixel(x, y));
return out;
}
static Map putAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(b);
return a;
}
static MultiMap putAll(MultiMap a, Map extends A,? extends B> b) {
if (a != null) a.putAll((Map) b);
return a;
}
static Map putAll(Map a, Object... b) {
if (a != null)
litmap_impl(a, b);
return a;
}
static void put(Map map, A a, B b) {
if (map != null) map.put(a, b);
}
static void put(List l, int i, A a) {
if (l != null && i >= 0 && i < l(l)) l.set(i, a);
}
static Set emptySet() {
return new HashSet();
}
static A getAndClear(IVar v) {
A a = v.get();
v.set(null);
return a;
}
static ArrayList cloneList(Iterable l) {
return l instanceof Collection ? cloneList((Collection) l) : asList(l);
}
static ArrayList cloneList(Collection l) {
if (l == null) return new ArrayList();
synchronized(collectionMutex(l)) {
return new ArrayList(l);
}
}
static Object getOpt(Object o, String field) {
return getOpt_cached(o, field);
}
static Object getOpt(String field, Object o) {
return getOpt_cached(o, field);
}
static Object getOpt_raw(Object o, String field) { try {
Field f = getOpt_findField(o.getClass(), field);
if (f == null) return null;
makeAccessible(f);
return f.get(o);
} catch (Exception __e) { throw rethrow(__e); } }
// access of static fields is not yet optimized
static Object getOpt(Class c, String field) { try {
if (c == null) return null;
Field f = getOpt_findStaticField(c, field);
if (f == null) return null;
makeAccessible(f);
return f.get(null);
} catch (Exception __e) { throw rethrow(__e); } }
static Field getOpt_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);
return null;
}
static Set keys(Map map) {
return map == null ? new HashSet() : map.keySet();
}
// convenience shortcut for keys_gen
static Set keys(Object map) {
return keys((Map) map);
}
static Set keys(MultiSet ms) {
return ms.keySet();
}
static Set keys(IMultiMap mm) {
return mm.keySet();
}
static Set keySet(Map map) {
return map == null ? new HashSet() : map.keySet();
}
static Set keySet(Object map) {
return keys((Map) map);
}
static Set keySet(MultiSet ms) {
return ms.keySet();
}
static Set keySet(MultiMap mm) {
return mm.keySet();
}
static int keysSize(MultiMap mm) {
return lKeys(mm);
}
static A reverseGet(List l, int idx) {
if (l == null || idx < 0) return null;
int n = l(l);
return idx < n ? l.get(n-1-idx) : null;
}
static Map cloneMap(Map map) {
if (map == null) return new HashMap();
// assume mutex is equal to map
synchronized(map) {
return map instanceof TreeMap ? new TreeMap((TreeMap) map) // copies comparator
: map instanceof LinkedHashMap ? new LinkedHashMap(map)
: new HashMap(map);
}
}
static List cloneMap(Iterable l, IF1 f) {
List x = emptyList(l);
if (l != null) for (A o : cloneList(l))
x.add(f.get(o));
return x;
}
static > List allValues(Map map) {
List out = new ArrayList();
for (var l : values(map))
addAll(out, l);
return out;
}
static List concatLists(Iterable... lists) {
List l = new ArrayList();
if (lists != null) for (Iterable list : lists)
addAll(l, list);
return l;
}
static List concatLists(Collection extends Iterable> lists) {
List l = new ArrayList();
if (lists != null) for (Iterable list : lists)
addAll(l, list);
return l;
}
static Collection values(Map map) {
return map == null ? emptyList() : map.values();
}
// convenience shortcut for values_gen
static Collection values(Object map) {
return values((Map) map);
}
static Collection values(MultiMap mm) {
return mm == null ? emptyList() : concatLists(values(mm.data));
}
static ArrayList emptyList() {
return new ArrayList();
//ret Collections.emptyList();
}
static ArrayList emptyList(int capacity) {
return new ArrayList(max(0, capacity));
}
// Try to match capacity
static ArrayList emptyList(Iterable l) {
return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList();
}
static ArrayList emptyList(Object[] l) {
return emptyList(l(l));
}
// get correct type at once
static ArrayList emptyList(Class c) {
return new ArrayList();
}
static Set> _entrySet(Map map) {
return map == null ? Collections.EMPTY_SET : map.entrySet();
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
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 Map.Entry firstEntry(Map map) {
return empty(map) ? null : first(map.entrySet());
}
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(MultiSet ms) { return ms == null || ms.isEmpty(); }
static boolean empty(IMultiMap mm) { return mm == null || mm.size() == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
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 A firstKey(Map map) {
return first(keys(map));
}
static A firstKey(IMultiMap map) {
return map == null ? null : first(map.keySet());
}
static Concept options(List l) {
return l(l) == 1 ? first(l) : new Options(l);
}
static IterableIterator arrayIterator(A[] l) {
return l == null ? null : new IterableIterator() {
int i = 0;
public boolean hasNext() { return i < l.length; }
public A next() { return l[i++]; }
};
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
// returns number of steps
static long stepAll(Steppable s) {
long steps = 0;
if (s != null) {
var pingSource = pingSource();
while (true) {
ping(pingSource);
if (s.step())
++steps;
else
break;
}
}
return steps;
}
static String unnullForIteration(String s) {
return s == null ? "" : s;
}
static Collection unnullForIteration(Collection l) {
return l == null ? immutableEmptyList() : l;
}
static List unnullForIteration(List l) { return l == null ? immutableEmptyList() : l; }
static byte[] unnullForIteration(byte[] l) { return l == null ? emptyByteArray() : l; }
static int[] unnullForIteration(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnullForIteration(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnullForIteration(double[] l) { return l == null ? emptyDoubleArray() : l; }
static short[] unnullForIteration(short[] l) { return l == null ? emptyShortArray() : l; }
static Map unnullForIteration(Map l) {
return l == null ? immutableEmptyMap() : l;
}
static Iterable unnullForIteration(Iterable i) {
return i == null ? immutableEmptyList() : i;
}
static A[] unnullForIteration(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnullForIteration(BitSet b) {
return b == null ? new BitSet() : b;
}
static Pt unnullForIteration(Pt p) {
return p == null ? new Pt() : p;
}
//ifclass Symbol
static Symbol unnullForIteration(Symbol s) {
return s == null ? emptySymbol() : s;
}
//endif
static Pair unnullForIteration(Pair p) {
return p != null ? p : new Pair(null, null);
}
static long unnullForIteration(Long l) { return l == null ? 0L : l; }
static A nextToLast(List l) {
return get(l, l(l)-2);
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
static String find(String pattern, String text) {
Matcher matcher = Pattern.compile(pattern).matcher(text);
if (matcher.find())
return matcher.group(1);
return null;
}
static A find(Collection c, Object... data) {
for (A x : c)
if (checkFields(x, data))
return x;
return null;
}
static GrabbableIntPixels grabbableIntPixels_fastOrSlow(BufferedImage image) { try {
try {
GrabbableIntPixels gp = grabbableIntPixels(image);
if (gp != null) return gp;
} catch (Throwable __e) { printStackTrace(__e); }
// TODO: use GrabbableRGBBytePixels and convert manually
// - should be way faster than PixelGrabber
int w = image.getWidth(), h = image.getHeight();
int[] data = new int[w*h];
PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, w, h, data, 0, w);
if (!pixelGrabber.grabPixels())
throw fail("Could not grab pixels");
return new GrabbableIntPixels(data, w, h, 0, w);
} catch (Exception __e) { throw rethrow(__e); } }
static void arrayCopy(Object[] a, Object[] b) {
arraycopy(a, b);
}
static void arrayCopy(Object src, Object dest, int destPos, int n) { arrayCopy(src, 0, dest, destPos, n); }
static void arrayCopy(Object src, int srcPos, Object dest, int destPos, int n) {
arraycopy(src, srcPos, dest, destPos, n);
}
static BufferedImage toBufferedImage(Object o) {
return toBufferedImageOpt(o);
}
static boolean inRange(int x, int n) {
return x >= 0 && x < n;
}
static boolean inRange(int x, List l) {
return inRange(x, l(l));
}
static boolean inRange(int x, int a, int b) {
return x >= a && x < b;
}
static void saveImage(File f, BufferedImage img) {
if (hasJPEGExtension(f))
saveJPG(f, img);
else
savePNG(f, img);
}
static void saveImage(BufferedImage img, File f) {
saveImage(f, img);
}
static int limitToUByte(int i) {
return max(0, min(255, i));
}
static void setPixel(BufferedImage img, int p_x, int p_y, Color color) { setPixel(img, pt(p_x, p_y), color); }
static void setPixel(BufferedImage img, Pt p, Color color) {
{ if (img != null) img.setRGB(p.x, p.y, colorToIntOpaque(color)); }
}
static void setPixel(Graphics2D g, int p_x, int p_y, Color color) { setPixel(g, pt(p_x, p_y), color); }
static void setPixel(Graphics2D g, Pt p, Color color) {
g.setColor(color);
g.drawRect(p.x, p.y, 1, 1);
}
static Class> getClass(String name) {
return _getClass(name);
}
static Class getClass(Object o) {
return _getClass(o);
}
static Class getClass(Object realm, String name) {
return _getClass(realm, name);
}
static void fillArrayUnlessZero(byte[] a, byte value) {
if (value != 0) Arrays.fill(a, value);
}
static void fillArrayUnlessZero(int[] a, int value) {
if (value != 0) Arrays.fill(a, value);
}
static void fillArrayUnlessZero(float[] a, float value) {
if (value != 0) Arrays.fill(a, value);
}
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 BufferedImage bufferedImage(int[] pixels, int w, int h) {
return intArrayToBufferedImage(pixels, w, h);
}
static BufferedImage bufferedImage(int[] pixels, WidthAndHeight size) {
return bufferedImage(pixels, size.getWidth(), size.getHeight());
}
static BufferedImage bufferedImage(int w, int h, int[] pixels) {
return intArrayToBufferedImage(pixels, w, h);
}
// undefined color, seems to be all black in practice
static BufferedImage bufferedImage(int w, int h) {
return newBufferedImage(w, h);
}
static BufferedImage bufferedImage(int w, int h, RGB rgb) {
return newBufferedImage(w, h, rgb);
}
static BufferedImage bufferedImage(int w, Color color) { return bufferedImage(w, w, color); }
static BufferedImage bufferedImage(int w, int h, Color color) {
return newBufferedImage(w, h, color);
}
static BufferedImage bufferedImage(Pt p, Color color) {
return newBufferedImage(p, color);
}
static BufferedImage bufferedImage(WidthAndHeight size, Color color) {
return newBufferedImage(size.w(), size.h(), color);
}
static BufferedImage bufferedImage(Color color, WidthAndHeight size) {
return bufferedImage(size, color);
}
static RuntimeException unimplemented() {
throw fail("TODO");
}
static RuntimeException unimplemented(String msg) {
throw fail("TODO: " + msg);
}
static RuntimeException unimplemented(Object obj) {
throw fail("TODO: implement method in " + className(obj));
}
static Image2B toImage2B(BufferedImage img) {
return img == null ? null : new Image2B(img);
}
static Image2B toImage2B(MakesBufferedImage img) {
if (img == null) return null;
if (img instanceof Image2B) return ((Image2B) img);
if (img instanceof BWImage) return new Image2B((BWImage) img);
if (img instanceof IBinaryImage) return iBinaryImageToImage2B((IBinaryImage) img);
return new Image2B(toBufferedImage(img));
}
static Image2B iBinaryImageToImage2B(IBinaryImage img) {
if (img == null) return null;
if (img instanceof Image2B) return ((Image2B) img);
int w = img.getWidth(), h = img.getHeight();
byte[] pixels = new byte[(w*h+7)/8];
int i = 0;
for (int y = 0; y < h; y++)
for (int x = 0; x < w; x++) {
if (img.getBoolPixel(x, y))
pixels[i/8] |= 1 << (i & 7);
i++;
}
return new Image2B(w, h, pixels);
}
static double floatSumAsDouble(Iterable l) {
double sum = 0;
for (Float i : unnull(l))
if (i != null) sum += i;
return sum;
}
static double floatSumAsDouble(float... l) {
double sum = 0;
if (l != null) for (float i : l)
sum += i;
return sum;
}
static String formatDouble_noLeadingZero(double d, int digits) {
String s = formatDouble(d, digits);
if (startsWith(s, "-"))
return startsWith(s, "-0.") ? "-." + dropFirst(3, s) : s;
else
return startsWith(s, "0.") ? dropFirst(s) : s;
}
static int hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
static int hashCode(long l) {
return Long.hashCode(l);
}
static int hashCode(double d) {
return Double.hashCode(d);
}
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 double sqrt(double x) {
return Math.sqrt(x);
}
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 max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }
static > A max (Iterable l) {
A max = null;
var it = iterator(l);
if (it.hasNext()) {
max = it.next();
while (it.hasNext()) {
A a = it.next();
if (cmp(a, max) > 0)
max = a;
}
}
return max;
}
/*Nah.
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
ret x;
}*/
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x) x = d;
return x;
}
static int max(int[] c) {
int x = Integer.MIN_VALUE;
for (int d : c) if (d > x) x = d;
return x;
}
static > A max(A a, A b) {
return cmp(a, b) >= 0 ? a : b;
}
static UnsupportedOperationException unsupportedOperation() {
throw new UnsupportedOperationException();
}
static int rgbRed(int rgb) {
return (rgb >> 16) & 0xFF;
}
static int rgbGreen(int rgb) {
return (rgb >> 8) & 0xFF;
}
static int rgbBlue(int rgb) {
return rgb & 0xFF;
}
static Color getColor(BufferedImage img, int x, int y) {
return colorFromRGBA(img.getRGB(x, y));
}
static Color getColor(BufferedImage img, Pt p) {
return colorFromRGBA(img.getRGB(p.x, p.y));
}
static float clampZeroToOne(float x) {
return x < 0 ? 0 : x > 1 ? 1 : x;
}
static double clampZeroToOne(double x) {
return x < 0 ? 0 : x > 1 ? 1 : x;
}
static int rgbInt(int r, int g, int b) {
return (clamp(r, 0, 255) << 16) | (clamp(g, 0, 255) << 8) | clamp(b, 0, 255);
}
static int rgbInt(byte r, byte g, byte b) {
return (ubyteToInt(r) << 16) | (ubyteToInt(g) << 8) | ubyteToInt(b);
}
static int asInt(Object o) {
return toInt(o);
}
static String spaceCombine(Object... l) {
return joinNemptiesWithSpace(flattenCollections(ll(l)));
}
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 String a(String noun) {
if (eq(noun, "")) return "?";
return ("aeiou".indexOf(noun.charAt(0)) >= 0 ? "an " : "a ") + noun;
}
static String a(String contents, Object... params) {
return hfulltag("a", contents, params);
}
static String b(Object contents, Object... params) {
return tag("b", contents, params);
}
static int hashCodeFor(Object a) {
return a == null ? 0 : a.hashCode();
}
static Object metaGet(IMeta o, Object key) {
return metaMapGet(o, key);
}
static Object metaGet(Object o, Object key) {
return metaMapGet(o, key);
}
static Object metaGet(String key, IMeta o) {
return metaMapGet(o, key);
}
static Object metaGet(String key, Object o) {
return metaMapGet(o, key);
}
static Object metaMapGet(IMeta o, Object key) {
return o == null ? null : o.metaGet(key); // We now let the object itself do it (overridable!)
}
static Object metaMapGet(Object o, Object key) {
return metaMapGet(toIMeta(o), key);
}
static void metaPut(IMeta o, Object key, Object value) {
metaMapPut(o, key, value);
}
static void metaPut(Object o, Object key, Object value) {
metaMapPut(o, key, value);
}
static void metaMapPut(IMeta o, Object key, Object value) {
{ if (o != null) o.metaPut(key, value); }
}
static void metaMapPut(Object o, Object key, Object value) {
var meta = initIMeta(o);
{ if (meta != null) meta.metaPut(key, value); }
}
static Map convertObjectMetaToMap(IMeta o) { return convertObjectMetaToMap(o, () -> makeObjectMetaMap()); }
static Map convertObjectMetaToMap(IMeta o, IF0