Warning : session_start(): open(/var/lib/php/sessions/sess_t1g2ra5e5ev5o3m3sd4586um06, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning : session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import org.eclipse.collections.impl.map.mutable.primitive.*;
import org.eclipse.collections.impl.set.mutable.primitive.*;
import org.eclipse.collections.api.tuple.primitive.*;
import org.eclipse.collections.api.iterator.*;
import java.text.NumberFormat;
class main {
static LineCompCompressor lcLineComp(Map data) { return lcLineComp(new LineCompCompressor(), data); }
static LineCompCompressor lcLineComp(LineCompCompressor compressor, Map data) {
return lcLineMode(compressor, data);
}
static LineCompCompressor lcLineMode(Map data) { return lcLineMode(new LineCompCompressor(), data); }
static LineCompCompressor lcLineMode(LineCompCompressor compressor, Map data) {
compressor.loadTexts(data);
compressor.run();
return compressor;
}
// This is using the implementation that seems to work fastest
// in reality, although it probably is O(n log n) due to the use
// of TreeMaps.
//
// The theoretically best implementation is O(n) (replacing the
// TreeMap with a linked list).
final static class LineCompCompressor {
int safety = 0;
abstract static class Chunk {
abstract String text(List chunks);
IntPair getPair() { return null; }
}
static class CPair extends Chunk implements IFieldsToList{
int i1;
int i2;
CPair() {}
CPair(int i1, int i2) {
this.i2 = i2;
this.i1 = i1;}
public String toString() { return shortClassName(this) + "(" + i1 + ", " + i2 + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CPair)) return false;
CPair __3 = (CPair) o;
return i1 == __3.i1 && i2 == __3.i2;
}
public int hashCode() {
int h = 64355773;
h = boostHashCombine(h, _hashCode(i1));
h = boostHashCombine(h, _hashCode(i2));
return h;
}
public Object[] _fieldsToList() { return new Object[] {i1, i2}; }
CPair(IntPair p) { i1 = p.a; i2 = p.b; }
String text(List chunks) {
return linesLL_rtrim(chunks.get(i1).text(chunks), chunks.get(i2).text(chunks));
}
IntPair getPair() {
return new IntPair(i1, i2);
}
}
static class CPrim extends Chunk implements IFieldsToList{
String s;
CPrim() {}
CPrim(String s) {
this.s = s;}
public String toString() { return shortClassName(this) + "(" + s + ")"; }
public boolean equals(Object o) {
if (!(o instanceof CPrim)) return false;
CPrim __4 = (CPrim) o;
return eq(s, __4.s);
}
public int hashCode() {
int h = 64372105;
h = boostHashCombine(h, _hashCode(s));
return h;
}
public Object[] _fieldsToList() { return new Object[] {s}; }
String text(List chunks) { return s; }
}
boolean verbose = false, verboseCompressionSteps = false;
boolean verboseStats = false;
boolean sortLines = true;
boolean verify = true;
boolean byteMode = false; // compress bytes instead of lines
boolean caseInsensitive = false; // for line mode
boolean toUpper = false; // for byte mode: convert all characters to upper case
boolean recordPairLengths = false; // for "balanced" compression
boolean balancing = false; // try to lower tree heights by compressing "shorter" pairs first
boolean fullCompression = false; // keep compressing until every file is a single symbol
LineComp_PairIndex pairIndex = new LineComp_PairIndex();
Map> textIDToLines;
List allUniqueLines;
List chunks = new ArrayList();
int primChunks;
Map lineIndex;
Map> simpleEncodings; // temporary
Map> finalEncodings;
LongIntHashMap existingPairsIndex = new LongIntHashMap(); // formerly Map
int round, printStatsEvery = /*10000*/0, printStatsInFirstRounds = 10;
int printStatsEveryNSeconds = /*10*/60; // 60 for slower computers
// initializing fullIndex = new Map will make things slow and use a lot of memory
// and I thought there might be a case where it improves the compression
// - but maybe there isn't?
Map fullIndex;
SimpleIntList pairLengths;
LineCompCompressor() {}
LineCompCompressor(Map texts) { loadTexts(texts); }
// key = version ID, values = text
void loadTexts(Map texts) {
textIDToLines = mapValuesToLinkedHashMap(__23 -> myToLines(__23),texts);
}
List myToLines(String s) {
List l = byteMode
? lambdaMap(__24 -> charToHex(__24),characters(toUpper ? upper(s) : s))
: toLines_nOnly_reversible(s);
return l;
}
String myFromLines(List l) {
return byteMode
? join(mapI("hexToChar",l)) // TODO: optimize even more
: fromLines_rtrim(l);
}
public void run() {
if (balancing) {
recordPairLengths = true;
pairIndex.pairComparatorForBalancing = (p1, p2) -> {
int l1 = pairLength(p1), l2 = pairLength(p2);
if (l1 != l2) return l1-l2;
// return arbitrary deterministic order, but not 0
if (p1.a != p2.a) return p1.a-p2.a;
return p1.b-p2.b;
};
}
if (recordPairLengths) pairLengths = new SimpleIntList();
List allLines = concatLists(values(textIDToLines));
if (verboseCompressionSteps) print("Uniquifying " + nLines(allLines));
allUniqueLines = uniquify(allLines);
if (verboseCompressionSteps) print("Have " + n2(allUniqueLines, "unique line"));
allLines = null; // allow me to forget
if (sortLines) sortInPlace(allUniqueLines);
if (verboseCompressionSteps) print("Sorted " + nLines(allUniqueLines));
for (String line : allUniqueLines)
chunks.add(new CPrim(line));
primChunks = l(chunks);
List _lines = collect("s",chunks);
lineIndex = caseInsensitive ? ciListIndex(_lines) : listIndex(_lines);
_lines = null;
// simple encoding (only direct line references)
simpleEncodings = mapValues(textIDToLines,
(IF1, List>) (lines -> wrapIntArrayAsImmutableList(mapToIntArray(lines, line -> lineIndex.get(line)))));
//printAndCheckEncodings(simpleEncodings);
if (verboseCompressionSteps) print("Have simple encodings");
finalEncodings = compressPairs();
if (verbose || verify) printAndCheckEncodings(finalEncodings);
}
void saveAsTextFile(File f) {
String out = exportEncoding(finalEncodings);
saveTextFile(f, out);
if (verify) checkDecompression(f, textIDToLines);
}
void checkDecompression(File file, Map> textIDToLines) {
BufferedReader reader = bufferedUtf8Reader(file); try {
LineCompReader lcr = new LineCompReader(reader);
assertEquals(keysList(textIDToLines), asList(lcr.versions()));
for (String version : keys(textIDToLines))
assertEquals(lcr.textForVersion(version), lines_rtrim(textIDToLines.get(version)));
if (verbose) print("Decompression OK for " + nVersions(textIDToLines));
} finally { _close(reader); }}
String asText() { return exportEncoding(finalEncodings); }
String exportEncoding(Map> encodings) {
List buf = new ArrayList();
buf.add((byteMode ? "BYTECOMP " : "LINECOMP ") + primChunks); // magic signature
for (Chunk c : chunks) {
if (c instanceof CPair)
buf.add(((CPair) c).i1 + " " + ((CPair) c).i2);
else
buf.add(((CPrim) c).s);
}
for (Map.Entry extends String, ? extends List> __1 : _entrySet( encodings))
{ String id = __1.getKey(); List l = __1.getValue(); buf.add(id + "=" + joinWithSpace(l)); }
return lines_rtrim(buf);
}
Map> compressPairs() {
pairIndex.verbose = verboseCompressionSteps || verbose;
pairIndex.init();
for (Map.Entry extends String, ? extends List> __5 : _entrySet( simpleEncodings))
{ String version = __5.getKey(); List l = __5.getValue(); ping(); pairIndex.newChain(version, l); }
pairIndex.haveChains();
simpleEncodings = null; // forget
return compressChains(keys(pairIndex.chains));
}
Map> compressChains(Collection files) {
NotTooOften nto = new NotTooOften(printStatsEveryNSeconds*1000);
IntPair toCompress;
// Compress only most popular pair in one step
while ((toCompress = fullCompression
? pairIndex.mostPopularPair()
: pairIndex.mostPopularDuplicate()) != null) { ping();
if (verbose) {
print("Chains: " + mapValuesToList(pairIndex.chains, c -> c.toList()));
//print("Highest bucket (" + pairIndex.highestBucketNr() + "): " + pairIndex.mostPopularDuplicates());
print("Buckets: " + pairIndex.byCount);
}
if (safety > 0 && --safety <= 0) throw fail("safety");
++round;
if (verboseStats)
if (printStatsEvery > 0 && (round % printStatsEvery) == 0
|| round <= printStatsInFirstRounds
|| nto.get())
printStats();
int count = pairIndex.getCount(toCompress);
Integer idx = makeCPair(toCompress);
int dups = pairIndex.numberOfDuplicates();
if (verboseCompressionSteps) print("Compressing pair " + toCompress + " (count=" + count + ") -> " + idx + ", " + (dups-1) + " remaining");
pairIndex.replacePair(toCompress.a, toCompress.b, idx);
}
if (verboseStats) printStats();
// reconvert to normal list
return mapToValues(f -> pairIndex.chains.get(f).toList(), files);
}
// TODO: check if pair is already there
Integer makeCPair(IntPair p) {
long l = intPairToLong(p);
String text = null;
if (fullIndex != null) {
text = itemToString(p.a) + itemToString(p.b);
Integer idx = fullIndex.get(text);
if (idx != null) return idx;
}
int idx = existingPairsIndex.getIfAbsent(l, -1);
if (idx < 0) {
idx = addAndReturnIndex(chunks, new CPair(p));
if (recordPairLengths) listPut(pairLengths, idx, itemLength(p.a) + itemLength(p.b), 1);
existingPairsIndex.put(l, idx);
if (fullIndex != null)
fullIndex.put(text, idx);
}
return idx;
}
void printAndCheckEncodings(Map> encodings) {
for (Map.Entry extends String, ? extends List> __0 : _entrySet( encodings)) { String id = __0.getKey(); List encoded = __0.getValue();
if (verbose) print(id + ": " + joinWithSpace(encoded));
String x = myFromLines(textIDToLines.get(id)), y = decode(encoded);
if (caseInsensitive)
assertEqualsIC(x, y);
else
assertEquals_quote(x, y);
}
}
String decode(List encoded) {
List l = lambdaMap(__25 -> chunkText(__25),encoded);
if (byteMode) l = lines(lines(l));
return myFromLines(l);
}
// for non-byte mode
String chunkText(int idx) {
return chunks.get(idx).text(chunks);
}
void printStats() {
print("Round: " + round);
pairIndex.printStats();
}
// byteMode only
String itemToString(int idx) {
StringBuilder buf = new StringBuilder();
itemToString_step(buf, idx);
return str(buf);
}
void itemToString_step(StringBuilder buf, int idx) {
Chunk c = chunks.get(idx);
if (c instanceof CPair) {
itemToString_step(buf, ((CPair) c).i1);
itemToString_step(buf, ((CPair) c).i2);
} else {
String s = ((CPrim) c).s;
buf.append(byteMode ? charFromHex(s) : s + "\n");
}
}
int nLiterals() { return primChunks; }
int nItems() { return l(chunks); }
IntPair getPair(int idx) {
return chunks.get(idx).getPair();
}
int itemLength(int idx) {
return idx >= l(pairLengths) ? 1 : pairLengths.get(idx);
}
int pairLength(IntPair p) {
return p == null ? 0 : itemLength(p.a) + itemLength(p.b);
}
List literals() { return lmap(__26 -> itemToString(__26),countIterator(primChunks)); }
}
static interface IFieldsToList {
Object[] _fieldsToList();
}static interface IF1 {
B get(A a);
}
final static class LineComp_PairIndex {
boolean verbose = false;
boolean inOrder = true;
//MultiSetMap nodes;
LongObjectHashMap> nodes = new LongObjectHashMap();
MultiSetMap byCount // values are pairs
= null; // init later
//= multiSetMap_innerHashSet_outerRevTreeMap(); // makes a difference in selecting a pair if there are multiple with the same count
// = multiSetMap_innerCompactHashSet_outerRevTreeMap(); // same but more compact
//= multiSetMap_innerTreeSet_outerRevTreeMap(); // initial version that kind of pointlessly sorts the same-count nodes lexicographically
LinkedHashMap chains = new LinkedHashMap();
Ch firstChain;
int totalPairCount;
int initialNodesSetCapacity = 1;
List> onAddingPair, onRemovingPair;
LongIntHashMap existingPairsIndex;
// This queue checks all newly made pairs to see if they already
// have a symbol.
LinkedList compressionQueue = new LinkedList();
Comparator pairComparatorForBalancing;
void init() {
/*nodes = inOrder
? multiSetMap_innerLinkedHashSet_outerHashMap()
: multiSetMap_innerHashSet_outerHashMap();*/
}
// indicate end of chain-making phase.
// we still more chains accept after this though, I think (for late input)
void haveChains() {
if (byCount == null) {
//time "Making byCount" {
if (pairComparatorForBalancing != null)
byCount = multiSetMap_innerCustomTreeSet_outerRevTreeMap(pairComparatorForBalancing);
else
byCount = multiSetMap_innerCompactHashSet_outerRevTreeMap();
for (Set set : nodes.values()) {
IntPair p = first(set).pair();
int n = set.size();
if (verbose) print("Adding node " + p + " (" + n + ")");
byCount.put(n, p);
}
//}
}
}
// a "chain" of nodes (one input file)
static class Ch {
Node tail;
Ch(LineComp_PairIndex pi, List l) {
int count = 0;
for (int i : unnull(l)) {
add(pi, i);
if (((++count) % oneMillion()) == 0)
print("Added " + n2(count) + " entries to chain");
}
}
List toList() {
List l = new ArrayList();
Node node = tail;
while (node != null) {
l.add(node.value);
node = node.prev;
}
return reverseInPlace(l);
}
void add(LineComp_PairIndex pi, int i) {
Node n = new Node();
n.ch = this;
n.value = i;
n.prev = tail;
if (tail != null) tail.next = n;
tail = n;
pi.addToIndex(n.prev);
pi.processQueue();
}
}
static class Node {
Ch ch;
int value;
Node prev, next;
int a() { return value; }
int b() { return next == null ? -1 : next.value; }
IntPair pair() { return next == null ? null : new IntPair(a(), b()); }
public String toString() {
return stringIf(prev != null, "<") + value +
(next == null ? "" : next.value
+ stringIf(next.next != null, ">"));
}
}
IntPair nodeToPair(Node n) {
return n == null ? null : n.pair();
}
// add node to pair index
void addToIndex(Node n) {
IntPair p = nodeToPair(n);
if (p == null) return;
callFAll(onAddingPair, n);
int count = nodes_getSize(p);
nodes_add(p, n);
if (byCount != null) { // not in init phase
if (count != 0) byCount.remove(count, p);
byCount.put(count+1, p);
}
if (existingPairsIndex != null)
compressionQueue.add(n);
}
// remove node from pair index
void removeFromIndex(Node n) {
IntPair p = nodeToPair(n);
if (p == null) return;
callFAll(onRemovingPair, n);
int count = nodes_getSize(p);
if (count == 0) throw fail("Can't remove pair " + p);
nodes_remove(p, n);
byCount.remove(count, p);
if (--count > 0) byCount.put(count, p);
}
// return all pairs in "most popular" bucket
Collection mostPopularDuplicates() {
Integer key = firstKey(byCount);
return toInt(key) < 2 ? null : byCount.get(key);
}
IntPair mostPopularDuplicate() {
return toInt(firstKey(byCount)) < 2 ? null : firstValue(byCount);
}
IntPair mostPopularPair() {
return firstValue(byCount);
}
int highestBucketNr() {
return or0(firstKey(byCount));
}
// counts buckets actually (lower value than totalPairCount)
int numberOfDuplicates() {
return byCount.size()-l(byCount.get(1));
}
int getCount(IntPair p) {
return nodes_getSize(p);
}
void replacePair(int pa, int pb, int replaceWith) {
IntPair p = new IntPair(pa, pb);
Set set = nodes.get(intPairToLong(p));
if (set != null)
for (Node n : cloneList(set)) {
{ if (n.a() != pa || n.b() != pb) continue; } // nodes might have been deleted or changed
replacePair(n, replaceWith);
}
}
void replacePair(Node node, int replaceWith) {
removeFromIndex(node.prev);
removeFromIndex(node);
removeFromIndex(node.next);
node.value = replaceWith;
deleteNode(node.next);
addToIndex(node.prev);
addToIndex(node);
processQueue();
}
void processQueue() {
if (existingPairsIndex != null) {
Node n;
while ((n = popFirst(compressionQueue)) != null)
checkIfPairExists(n);
}
}
void checkIfPairExists(Node node) {
if (existingPairsIndex == null) return;
if (node.value < 0) return;
IntPair pair = nodeToPair(node);
if (pair == null) return;
//print("Checking pair " + pair);
int i = existingPairsIndex.getIfAbsent(intPairToLong(pair), -1);
if (i >= 0) {
//print(" Replacing with: " + i);
replacePair(node, i);
}
}
void deleteNode(Node node) {
if (node.next != null) node.next.prev = node.prev; else getChain(node).tail = node.prev;
if (node.prev != null) node.prev.next = node.next;
node.value = -1; // mark invalid
}
Ch newChain(String version, List encoding) {
Ch ch = new Ch(this, encoding);
chains.put(version, ch);
if (firstChain == null) firstChain = ch;
return ch;
}
int nodes_getSize(IntPair p) {
return l(nodes.get(intPairToLong(p)));
}
void nodes_add(IntPair p, Node n) {
++totalPairCount;
long l = intPairToLong(p);
LinkedHashSet set = nodes.get(l);
if (set == null)
nodes.put(l, set = new LinkedHashSet(initialNodesSetCapacity));
set.add(n);
}
void nodes_remove(IntPair p, Node n) {
--totalPairCount;
long l = intPairToLong(p);
LinkedHashSet set = nodes.get(l);
if (set == null || !set.remove(n)) throw fail("Consistency error");
if (set.isEmpty())
nodes.remove(l);
}
// also shrinks sets...
void printStats() {
print("Different pairs: " + l(nodes));
print("Different counts: " + byCount.keysSize() + " (highest count: " + firstKey(byCount) + ")");
long count = 0, waste = 0, newWaste = 0;
long count1 = 0, waste1 = 0;
for (HashSet set : nodes.values()) {
count1 += set.size();
int capacity = hashSetCapacity(set);
waste1 += capacity-set.size();
//if (capacity > set.size()*3) set.shrink(); // TODO
}
if (pairComparatorForBalancing == null) for (CompactHashSet set : (Collection) (Collection) values(byCount.data)) {
count += set.size();
waste += set.capacity()-set.size();
set.shrinkToFactor(0.5);
newWaste += set.capacity()-set.size();
}
print("Nodes set count: " + count1 + ". Waste: " + waste1);
//print("Set count: " + count + ". Waste: " + waste + (waste == newWaste ? "" : " => " + newWaste));
print("Duplicates: " + n2(totalPairCount-byCount.count(1)));
}
Ch getChain(Node node) {
return node.ch;
}
/*public LineComp_PairIndex clone() {
}*/
}static class NotTooOften {
long minDelay;
long lastTime;
NotTooOften() {}
NotTooOften(long minDelay) {
this.minDelay = minDelay;}
boolean canDoAgain_willDoIfTrue() {
if (lastTime == 0 || now() >= lastTime+minDelay)
{ lastTime = now(); return true; }
return false;
}
boolean yo() { return canDoAgain_willDoIfTrue(); }
boolean get() { return canDoAgain_willDoIfTrue(); }
}static class LineCompReader {
List literals = new ArrayList();
IVF1 onPair; // if not null, don't save pairs, but stream them to onPair instead
int[] literalOffsets; // where they start in file
LongBuffer pairs = new LongBuffer();
LinkedHashMap> versions = new LinkedHashMap();
boolean byteMode = false;
// internal, optional
CountingInputStream countingInputStream;
long fileSize;
LineCompReader() {}
// takes text or gzipped input file
LineCompReader(File f) { load(f); }
LineCompReader(InputStream in) { load(rawByteReader(in, 128*1024)); }
LineCompReader(BufferedReader reader) { load(reader); }
void load(File f) {
fileSize = fileSize(f);
countingInputStream = new CountingInputStream(bufferedFileInputStream(f));
BufferedReader reader = isGZipFile(f)
? rawByteReader(gzipInputStream(countingInputStream))
: rawByteReader(countingInputStream); try {
//rawByteReader_possiblyGZipped(f);
load(reader);
} finally { _close(reader); }}
void load(BufferedReader reader) { try {
StringBuilder lineBuf = new StringBuilder();
String s = readLineIgnoreCR(reader, lineBuf);
int ofs = l(s)+1;
Matches m = new Matches();
if (startsWith(s, "BYTECOMP ", m)) byteMode = true;
else if (!startsWith(s, "LINECOMP ", m))
throw fail("Not a LINECOMP file");
int nLiterals = parseInt(m.rest());
IntBuffer offsets = new IntBuffer();
for (int i = 0; i < nLiterals; i++) {
String line = readLineIgnoreCR(reader, lineBuf);
assertNotNull(line);
literals.add(byteMode ? str(charFromHex(line)) : line);
offsets.add(ofs);
ofs += l(line)+1;
}
offsets.add(ofs);
literalOffsets = offsets.toArray();
int n = 0;
while (licensed()) {
s = readLineIgnoreCR(reader, lineBuf);
if (s == null || contains(s, "=")) break;
try {
int iSpace = s.indexOf(' ');
long pair = twoIntsToLong(
Integer.parseInt(s, 0, iSpace, 10),
Integer.parseInt(s, iSpace+1, l(s), 10));
if (onPair != null) onPair.get(pair);
else pairs.add(pair);
if (((++n) % oneMillion()) == 0) {
String percentage = "";
if (fileSize != 0 && countingInputStream != null)
percentage = " (" + intPercentRatio(countingInputStream.getFilePointer(), fileSize) + "%)";
print(nPairs(n) + " read" + percentage);
}
} catch (Throwable _e) {
print("On line " + (nLiterals + l(pairs)));
throw rethrow(_e); }
}
pairs.trimToSize();
while (contains(s, "=")) {
int i = indexOf(s, '=');
versions.put(takeFirst(s, i), compactIntList(parseInts(splitAtSpace(substring(s, i+1)))));
s = readLineIgnoreCR(reader, lineBuf);
}
} catch (Exception __e) { throw rethrow(__e); } }
Set versions() { return keys(versions); }
String getText(String version) { return textForVersion(version); }
String textForVersion(String version) {
List encoded = versions.get(version);
if (encoded == null) return null;
List buf = new ArrayList();
for (int idx : encoded)
decode(idx, buf);
return myFromLines(buf);
}
// name of first (or only) file
String firstFile() { return first(versions()); }
// text for first (or only) file
String text() { return getText(firstFile()); }
List encoding() { return versions.get(firstFile()); }
String myFromLines(List l) {
return byteMode
? join(l)
: fromLines_rtrim(l);
}
void decode(int idx, List buf) {
if (idx < l(literals))
buf.add(literals.get(idx));
else {
long p = pairs.get(idx-l(literals));
decode(firstIntFromLong(p), buf);
decode(secondIntFromLong(p), buf);
}
}
// That was it! The rest of this file is just for calculating some stats.
Map lineCountsForPairs = new HashMap();
Map byteCountsForPairs = new HashMap();
int lineCountForPointer(int idx) {
return idx < l(literals) ? 1 : lineCountForPair(idx);
}
long byteCountForPointer(int idx) {
return idx < l(literals) ? l(literals.get(idx))+1 : byteCountForPair(idx);
}
int lineCountForPair(int idx) {
Integer c = lineCountsForPairs.get(idx);
if (c == null) {
long p = pairs.get(idx-l(literals));
c = lineCountForPointer(firstIntFromLong(p)) + lineCountForPointer(secondIntFromLong(p));
lineCountsForPairs.put(idx, c);
}
return c;
}
long byteCountForPair(int idx) {
Long c = byteCountsForPairs.get(idx);
if (c == null) {
long p = pairs.get(idx-l(literals));
c = byteCountForPointer(firstIntFromLong(p)) + byteCountForPointer(secondIntFromLong(p));
byteCountsForPairs.put(idx, c);
}
return c;
}
int lineCountForVersion(String version) {
List encoded = versions.get(version);
if (encoded == null) return 0;
int n = 0;
for (int i : encoded) n += lineCountForPointer(i);
return n;
}
long byteCountForVersion(String version) {
List encoded = versions.get(version);
if (encoded == null) return 0;
long n = 0;
for (int i : encoded) n += byteCountForPointer(i);
return max(0, n-1);
}
long totalByteCount() {
return longSum(lambdaMap(__27 -> byteCountForVersion(__27),versions()));
}
// now we can also save again
void save(PrintWriter out) {
out.println((byteMode ? "BYTECOMP " : "LINECOMP ") + l(literals));
for (String s : literals)
out.println(byteMode ? charToHex(first(s)) : s);
for (long p : pairs)
out.println(firstIntFromLong(p) + " " + secondIntFromLong(p));
for (Map.Entry extends String, ? extends List> __0 : _entrySet( versions))
{ String id = __0.getKey(); List l = __0.getValue(); out.println(id + "=" + joinWithSpace(l)); }
}
}static class SimpleIntList extends RandomAccessAbstractList {
int[] data = EMPTY_ELEMENTDATA;
int size;
private static final int[] EMPTY_ELEMENTDATA = {};
public void add(int i) {
ensureCapacityInternal(size+1);
data[size++] = i;
}
public boolean add(Integer i) {
add((int) i);
return true;
}
public Integer set(int i, Integer value) {
if (i >= size) throw fail("Index out of range: " + i);
int old = data[i];
data[i] = value;
return old;
}
public int size() { return size; }
public Integer get(int i) {
if (i >= 0 && i < size) return data[i];
throw new IndexOutOfBoundsException(i + "/" + size);
}
void ensureCapacityInternal(int minCapacity) {
if (data.length < minCapacity) grow(minCapacity);
}
private void grow(int minCapacity) {
int oldCapacity = data.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
data = Arrays.copyOf(data, newCapacity);
}
// always returns a fresh copy
int[] toIntArray() {
int[] a = new int[size];
System.arraycopy(data, 0, a, 0, size);
return a;
}
}static final class IntPair implements Comparable , IFieldsToList{
int a;
int b;
IntPair() {}
IntPair(int a, int b) {
this.b = b;
this.a = a;}
public String toString() { return shortClassName(this) + "(" + a + ", " + b + ")"; }public Object[] _fieldsToList() { return new Object[] {a, b}; }
public boolean equals(Object o) {
if (!(o instanceof IntPair)) return false;
IntPair x = (IntPair) o;
return a == x.a && b == x.b;
}
public int hashCode() {
int h = -672893111;
h = boostHashCombine(h, _hashCode(a));
h = boostHashCombine(h, _hashCode(b));
return h;
}
public int compareTo(IntPair p) {
if (p == null) return 1;
int pa = p.a;
if (a < pa) return -1;
if (a > pa) return 1;
return Integer.compare(b, p.b);
}
}
static class LongBuffer implements Iterable, ILongQueue {
long[] data;
int size;
LongBuffer() {}
LongBuffer(int size) { if (size != 0) data = new long[size]; }
LongBuffer(Iterable l) { this(l(l)); addAll(l); }
public void add(long i) {
if (size >= lLongArray(data)) {
data = resizeLongArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lLongArray(data)*2L))));
if (size >= data.length) throw fail("LongBuffer too large: " + size);
}
data[size++] = i;
}
void addAll(Iterable l) {
if (l != null) for (long i : l) add(i);
}
long[] toArray() {
return size == 0 ? null : resizeLongArray(data, size);
}
List toList() {
return longArrayToList(data, 0, size);
}
List asVirtualList() {
return listFromFunction(__28 -> get(__28),size);
}
void reset() { size = 0; }
void clear() { reset(); }
int size() { return size; }
public boolean isEmpty() { return size == 0; }
long get(int idx) {
if (idx >= size) throw fail("Index out of range: " + idx + "/" + size);
return data[idx];
}
void set(int idx, long value) {
if (idx >= size) throw fail("Index out of range: " + idx + "/" + size);
data[idx] = value;
}
long popLast() {
if (size == 0) throw fail("empty buffer");
return data[--size];
}
long last() { return data[size-1]; }
long nextToLast() { return data[size-2]; }
public String toString() { return squareBracket(joinWithSpace(toList())); }
public Iterator iterator() {
return new IterableIterator() {
int i = 0;
public boolean hasNext() { return i < size; }
public Long next() {
if (!hasNext()) throw fail("Index out of bounds: " + i);
return data[i++];
}
};
}
void trimToSize() {
data = resizeLongArray(data, size);
}
// don't rely on return value if buffer is empty
public long poll() {
return size == 0 ? -1 : data[--size];
}
}// uses hash sets as inner sets unless subclassed
// uses a hash map as the outer map by default
static class MultiSetMap {
Map > data = new HashMap >();
int size;
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);
}}
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);
}}
Set get(A key) { synchronized(data) {
Set set = data.get(key);
return set == null ? Collections. emptySet() : set;
}}
// 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);
}}
Set keySet() { synchronized(data) {
return data.keySet();
}}
Set keys() { 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());
}}
int keysSize() { synchronized(data) { return l(data); }}
// full size
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;
}}
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));
}
Object mutex() { return data; }
public String toString() { return "mm" + str(data); }
Pair firstEntry() {
if (empty(data)) return null;
Map.Entry > entry = data.entrySet().iterator().next();
return pair(entry.getKey(), first(entry.getValue()));
}
}static class Matches {
String[] m;
Matches() {}
Matches(String... m) {
this.m = m;}
String get(int i) { return i < m.length ? m[i] : null; }
String unq(int i) { return unquote(get(i)); }
String tlc(int i) { return unq(i).toLowerCase(); }
boolean bool(int i) { return "true".equals(unq(i)); }
String rest() { return m[m.length-1]; } // for matchStart
int psi(int i) { return Integer.parseInt(unq(i)); }
public String toString() { return "Matches(" + joinWithComma(quoteAll(asList(m))) + ")"; }
public int hashCode() { return _hashCode(toList(m)); }
public boolean equals(Object o) { return o instanceof Matches && arraysEqual(m, ((Matches) o).m); }
}
static class IntBuffer implements Iterable {
int[] data;
int size;
IntBuffer() {}
IntBuffer(int size) { if (size != 0) data = new int[size]; }
IntBuffer(Iterable l) { this(l(l)); addAll(l); }
void add(int i) {
if (size >= lIntArray(data)) {
data = resizeIntArray(data, Math.max(1, toInt(Math.min(maximumSafeArraySize(), lIntArray(data)*2L))));
if (size >= data.length) throw fail("IntBuffer too large: " + size);
}
data[size++] = i;
}
void addAll(Iterable l) {
if (l != null) for (int i : l) add(i);
}
int[] toArray() {
return size == 0 ? null : resizeIntArray(data, size);
}
List toList() {
return intArrayToList(data, 0, size);
}
List asVirtualList() {
return new RandomAccessAbstractList() {
public int size() { return size; }
public Integer get(int i) { return IntBuffer.this.get(i); }
public Integer set(int i, Integer val) {
Integer a = get(i);
data[i] = val;
return a;
}
};
}
void reset() { size = 0; }
void clear() { reset(); }
int size() { return size; }
boolean isEmpty() { return size == 0; }
int get(int idx) {
if (idx >= size) throw fail("Index out of range: " + idx + "/" + size);
return data[idx];
}
void set(int idx, int value) {
if (idx >= size) throw fail("Index out of range: " + idx + "/" + size);
data[idx] = value;
}
int popLast() {
if (size == 0) throw fail("empty buffer");
return data[--size];
}
int last() { return data[size-1]; }
int nextToLast() { return data[size-2]; }
public String toString() { return squareBracket(joinWithSpace(toList())); }
public Iterator iterator() {
return new IterableIterator() {
int i = 0;
public boolean hasNext() { return i < size; }
public Integer next() {
//if (!hasNext()) fail("Index out of bounds: " + i);
return data[i++];
}
};
}
public IntegerIterator integerIterator() {
return new IntegerIterator() {
int i = 0;
public boolean hasNext() { return i < size; }
public int next() {
//if (!hasNext()) fail("Index out of bounds: " + i);
return data[i++];
}
public String toString() { return "Iterator@" + i + " over " + IntBuffer.this; }
};
}
void trimToSize() {
data = resizeIntArray(data, size);
}
}static class CountingInputStream extends FilterInputStream {
long counter;
CountingInputStream(InputStream in) { super(in); }
@Override
public int read() throws IOException {
int c = in.read();
if (c >= 0) ++counter;
return c;
}
@Override
public int read(byte[] b) throws IOException {
int n = in.read(b);
counter += max(n, 0);
return n;
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
int n = in.read(b, off, len);
counter += max(n, 0);
return n;
}
long getFilePointer() { return counter; }
}static interface IVF1 {
void get(A a);
}abstract static class RandomAccessAbstractList extends AbstractList implements RandomAccess {
}/*
* #!
* 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.
static class CompactHashSet 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;
CompactHashSet() {
this(INITIAL_SIZE);
}
CompactHashSet(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;
}
CompactHashSet(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;
}
synchronized 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
synchronized 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--;
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
synchronized 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;
elements--;
return true;
} else
// we did not find the object
return false;
}
@Override
synchronized public void clear() {
elements = 0;
for (int ix = 0; ix < objects.length; ix++)
objects[ix] = null;
freecells = objects.length;
}
@Override
synchronized 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
synchronized 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;
@SuppressWarnings("empty-statement")
public CompactHashIterator() {
synchronized(CompactHashSet.this) {
for (index = 0; index < objects.length &&
(objects[index] == null ||
objects[index] == deletedObject); index++)
;
}
}
@Override
public boolean hasNext() {
synchronized(CompactHashSet.this) {
return index < objects.length;
}
}
@SuppressWarnings("empty-statement")
@Override
public T next() {
synchronized(CompactHashSet.this) {
/*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() {
synchronized(CompactHashSet.this) {
if (lastReturned == -1 || lastReturned == -2)
throw new IllegalStateException();
// delete object
if (objects[lastReturned] != null && objects[lastReturned] != deletedObject) {
objects[lastReturned] = (A) deletedObject;
elements--;
}
}
}
}
synchronized int capacity() { return objects.length; }
// returns true if there was a shrink
synchronized 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 IntegerIterator {
abstract boolean hasNext();
abstract int next();
}static interface ILongQueue {
public boolean isEmpty();
public void add(long element);
// return value is undefined if queue is empty
public long poll();
}static class Pair implements Comparable> {
A a;
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 char hexToChar(String s) {
return charFromHex(s);
}
static String shortClassName(Object o) {
if (o == null) return null;
Class c = o instanceof Class ? (Class) o : o.getClass();
String name = c.getName();
return shortenClassName(name);
}
static int boostHashCombine(int a, int b) {
return a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2));
}
static int _hashCode(Object a) {
return a == null ? 0 : a.hashCode();
}
static String linesLL_rtrim(Object... x) {
return lines_rtrim(ll(x));
}
static boolean eq(Object a, Object b) {
return a == b || a != null && b != null && a.equals(b);
}
static LinkedHashMap mapValuesToLinkedHashMap(Object func, Map map) {
LinkedHashMap m = new LinkedHashMap();
for (Object key : keys(map))
m.put(key, callF(func, map.get(key)));
return m;
}
static LinkedHashMap mapValuesToLinkedHashMap(Map map, IF1 f) {
return mapValuesToLinkedHashMap(f, map);
}
static LinkedHashMap mapValuesToLinkedHashMap(IF1 f, Map map) {
return mapValuesToLinkedHashMap((Object) f, map);
}
static LinkedHashMap mapValuesToLinkedHashMap(Map map, Object func) {
return mapValuesToLinkedHashMap(func, map);
}
static List lambdaMap(IF1 f, Iterable l) {
return map(l, f);
}
static List lambdaMap(IF1 f, A[] l) {
return map(l, f);
}
static String charToHex(char c) {
return bytesToHex(charToBytes(c));
}
static List characters(final String s) {
return stringAsCharacterList(s);
}
static String upper(String s) {
return s == null ? null : s.toUpperCase();
}
static char upper(char c) {
return Character.toUpperCase(c);
}
static List toLines_nOnly_reversible(String s) {
List lines = new ArrayList();
if (s == null) return lines;
int start = 0, n = s.length();
if (n != 0) while (true) {
int i = smartIndexOf(s, '\n', start);
lines.add(s.substring(start, i));
if (i == n) break;
start = i+1;
}
return lines;
}
public static String join(String glue, Iterable strings) {
if (strings == null) return "";
if (strings instanceof Collection) {
if (((Collection) strings).size() == 1) return str(first(((Collection) strings)));
}
StringBuilder buf = new StringBuilder();
Iterator i = strings.iterator();
if (i.hasNext()) {
buf.append(i.next());
while (i.hasNext())
buf.append(glue).append(i.next());
}
return buf.toString();
}
public static String join(String glue, String... strings) {
return join(glue, Arrays.asList(strings));
}
static String join(Iterable strings) {
return join("", strings);
}
static String join(Iterable strings, String glue) {
return join(glue, strings);
}
public static String join(String[] strings) {
return join("", strings);
}
static String join(String glue, Pair p) {
return p == null ? "" : str(p.a) + glue + str(p.b);
}
static class mapI_It extends IterableIterator {
Object f;
Iterator i;
mapI_It() {}
mapI_It(Object f, Iterator i) {
this.i = i;
this.f = f;}
public boolean hasNext() {
return i.hasNext();
}
public Object next() {
return callF(f, i.next());
}
public String toString() {
return formatFunctionCall("mapI", f, i);
}
}
// apply a function to an iterator
static IterableIterator mapI(final Object f, final Iterator i) {
return new mapI_It(f, i);
}
static IterableIterator mapI(IterableIterator i, Object f) {
return mapI((Iterator) i, f);
}
static IterableIterator mapI(Object f, IterableIterator i) {
return mapI((Iterator) i, f);
}
static IterableIterator mapI(Iterator i, Object f) {
return mapI(f, i);
}
static IterableIterator mapI(Iterable i, IF1 f) {
return mapI(i, (Object) f);
}
static IterableIterator mapI(Iterator i, IF1 f) {
return mapI(f, i);
}
static IterableIterator mapI(IterableIterator i, IF1 f) {
return mapI((Iterator ) i, f);
}
static IterableIterator mapI(IF1 f, Iterable i) {
return mapI(i, (Object) f);
}
static IterableIterator mapI(Iterable i, Object f) {
return mapI(f, i.iterator());
}
static IterableIterator mapI(Object f, Iterable i) {
return mapI(i, f);
}
static String fromLines_rtrim(Collection lines) {
return rtrim_fromLines(lines);
}
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 volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
static boolean print_silent = false; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1
static volatile Object print_allThreads;
static volatile Object print_preprocess;
static void print() {
print("");
}
static A print(String s, A o) {
print((endsWithLetterOrDigit(s) ? s + ": " : s) + o);
return o;
}
// slightly overblown signature to return original object...
static A print(A o) {
ping_okInCleanUp();
if (print_silent) return o;
String s = o + "\n";
print_noNewLine(s);
return o;
}
static void print_noNewLine(String s) {
Object f = getThreadLocal(print_byThread_dontCreate());
if (f == null) f = print_allThreads;
if (f != null)
// We do need the general callF machinery here as print_byThread is sometimes shared between modules
if (isFalse(
f instanceof F1 ? ((F1) f).get(s) :
callF(f, s))) return;
print_raw(s);
}
static void print_raw(String s) {
if (print_preprocess != null) s = (String) callF(print_preprocess, s);
s = fixNewLines(s);
Appendable loc = local_log;
Appendable buf = print_log;
int loc_max = print_log_max;
if (buf != loc && buf != null) {
print_append(buf, s, print_log_max);
loc_max = local_log_max;
}
if (loc != null)
print_append(loc, s, loc_max);
System.out.print(s);
vmBus_send("printed",mc(), s);
}
static void print_autoRotate() {
}
static String nLines(long n) { return n2(n, "line"); }
static String nLines(Collection l) { return nLines(l(l)); }
static String nLines(String s) { return nLines(countLines(s)); }
static List uniquify(Collection l) {
return uniquifyList(l);
}
static String n2(long l) { return formatWithThousands(l); }
static String n2(Collection l) { return n2(l(l)); }
static String n2(Map map) { return n2(l(map)); }
static String n2(double l, String singular) {
return n2(l, singular, singular + "s");
}
static String n2(double l, String singular, String plural) {
if (fraction(l) == 0)
return n2((long) l, singular, plural);
else
return l + " " + plural;
}
static String n2(long l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(long l, String singular) {
return n_fancy2(l, singular, singular + "s");
}
static String n2(Collection l, String singular) {
return n2(l(l), singular);
}
static String n2(Collection l, String singular, String plural) {
return n_fancy2(l, singular, plural);
}
static String n2(Map m, String singular, String plural) {
return n_fancy2(m, singular, plural);
}
static String n2(Map m, String singular) {
return n2(l(m), singular);
}
static String n2(Object[] a, String singular) { return n2(l(a), singular); }
static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); }
static List sortInPlace(List l, final Object comparator) {
return sortedInPlace(l, comparator);
}
static List sortInPlace(List l) {
return sortedInPlace(l);
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); }
static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: o instanceof boolean[] ? l((boolean[]) o)
: o instanceof byte[] ? l((byte[]) o)
: o instanceof char[] ? l((char[]) o)
: o instanceof short[] ? l((short[]) o)
: o instanceof int[] ? l((int[]) o)
: o instanceof float[] ? l((float[]) o)
: o instanceof double[] ? l((double[]) o)
: o instanceof long[] ? l((long[]) o)
: (Integer) call(o, "size");
}
static int l(IntBuffer b) { return b == null ? 0 : b.size(); }
static int l(LongBuffer b) { return b == null ? 0 : b.size(); }
static List collect(Iterable c, String field) {
return collectField(c, field);
}
static List collect(String field, Iterable c) {
return collectField(c, field);
}
/*ifclass Concept
static L collect(Class c, S field) {
ret collect(list(c), field);
}
endif
TODO: make translator ignore stuff in ifclass until resolved
*/
static Map ciListIndex(List l) {
return listIndexCI(l);
}
static Map listIndex(List l) {
Map map = new HashMap();
for (int i = 0; i < l(l); i++)
map.put(l.get(i), i);
return map;
}
static Map mapValues(Object func, Map map) {
Map m = similarEmptyMap(map);
for (Object key : keys(map))
m.put(key, callF(func, map.get(key)));
return m;
}
static Map mapValues(Map map, IF1 f) {
return mapValues(f, map);
}
static Map mapValues(IF1 f, Map map) {
return mapValues((Object) f, map);
}
static Map mapValues(Map map, Object func) {
return mapValues(func, map);
}
static List wrapIntArrayAsImmutableList(int[] l) {
return new RandomAccessAbstractList() {
public int size() { return l.length; }
public Integer get(int i) { return l[i]; }
};
}
static int[] mapToIntArray(Object f, Collection l) {
return toIntArray(map(f, l));
}
static int[] mapToIntArray(Object f, Object[] l) {
return toIntArray(map(f, l));
}
static int[] mapToIntArray(Collection l, IF1 f) {
return mapToIntArray((Object) f, l);
}
/** writes safely (to temp file, then rename) */
static File saveTextFile(String fileName, String contents) throws IOException {
CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)");
try {
File file = new File(fileName);
mkdirsForFile(file);
String tempFileName = fileName + "_temp";
File tempFile = new File(tempFileName);
if (contents != null) {
if (tempFile.exists()) try {
String saveName = tempFileName + ".saved." + now();
copyFile(tempFile, new File(saveName));
} catch (Throwable e) { printStackTrace(e); }
FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
PrintWriter printWriter = new PrintWriter(outputStreamWriter);
printWriter.print(contents);
printWriter.close();
}
if (file.exists() && !file.delete())
throw new IOException("Can't delete " + fileName);
if (contents != null)
if (!tempFile.renameTo(file))
throw new IOException("Can't rename " + tempFile + " to " + file);
vmBus_send("wroteFile", file);
return file;
} finally {
action.done();
}
}
static File saveTextFile(File fileName, String contents) { try {
saveTextFile(fileName.getPath(), contents);
return fileName;
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedReader bufferedUtf8Reader(InputStream in) {
return utf8BufferedReader(in);
}
static BufferedReader bufferedUtf8Reader(File f) {
return utf8BufferedReader(f);
}
static A assertEquals(Object x, A y) {
return assertEquals(null, x, y);
}
static A assertEquals(String msg, Object x, A y) {
if (assertVerbose()) return assertEqualsVerbose(msg, x, y);
if (!(x == null ? y == null : x.equals(y)))
throw fail((msg != null ? msg + ": " : "") + y + " != " + x);
return y;
}
static List keysList(Map map) {
return cloneListSynchronizingOn(keys(map), map);
}
// unclear semantics as to whether return null on null
static ArrayList asList(A[] a) {
return a == null ? new ArrayList () : new ArrayList (Arrays.asList(a));
}
static ArrayList asList(int[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (int i : a) l.add(i);
return l;
}
static ArrayList asList(long[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (long i : a) l.add(i);
return l;
}
static ArrayList asList(float[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (float i : a) l.add(i);
return l;
}
static ArrayList asList(double[] a) {
if (a == null) return null;
ArrayList l = emptyList(a.length);
for (double i : a) l.add(i);
return l;
}
static ArrayList asList(Iterable s) {
if (s instanceof ArrayList) return (ArrayList) s;
ArrayList l = new ArrayList();
if (s != null)
for (A a : s)
l.add(a);
return l;
}
static ArrayList asList(Producer p) {
ArrayList l = new ArrayList();
A a;
if (p != null) while ((a = p.next()) != null)
l.add(a);
return l;
}
static ArrayList asList(Enumeration e) {
ArrayList l = new ArrayList();
if (e != null)
while (e.hasMoreElements())
l.add(e.nextElement());
return l;
}
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(MultiSetMap mm) {
return mm.keySet();
}
static String lines_rtrim(Collection lines) {
return rtrim_fromLines(lines);
}
static String nVersions(long n) { return n2(n, "version"); }
static String nVersions(Collection l) { return nVersions(l(l)); }
static String nVersions(Map map) { return nVersions(l(map)); }
static void _close(AutoCloseable c) {
if (c != null) try {
c.close();
} catch (Throwable e) {
// Some classes stupidly throw an exception on double-closing
if (c instanceof javax.imageio.stream.ImageOutputStream)
return;
else throw rethrow(e);
}
}
static Set> _entrySet(Map map) {
return map == null ? Collections.EMPTY_SET : map.entrySet();
}
static String joinWithSpace(Iterable c) {
return join(" ", c);
}
static String joinWithSpace(String... c) {
return join(" ", c);
}
//sbool ping_actions_shareable = true;
static volatile boolean ping_pauseAll = false;
static int ping_sleep = 100; // poll pauseAll flag every 100
static volatile boolean ping_anyActions = false;
static Map ping_actions = newWeakHashMap();
static ThreadLocal ping_isCleanUpThread = new ThreadLocal();
// always returns true
static boolean ping() {
if (ping_pauseAll || ping_anyActions) ping_impl(true /* XXX */);
//ifndef LeanMode ping_impl(); endifndef
return true;
}
// returns true when it slept
static boolean ping_impl(boolean okInCleanUp) { try {
if (ping_pauseAll && !isAWTThread()) {
do
Thread.sleep(ping_sleep);
while (ping_pauseAll);
return true;
}
if (ping_anyActions) { // don't allow sharing ping_actions
if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get()))
failIfUnlicensed();
Object action = null;
synchronized(ping_actions) {
if (!ping_actions.isEmpty()) {
action = ping_actions.get(currentThread());
if (action instanceof Runnable)
ping_actions.remove(currentThread());
if (ping_actions.isEmpty()) ping_anyActions = false;
}
}
if (action instanceof Runnable)
((Runnable) action).run();
else if (eq(action, "cancelled"))
throw fail("Thread cancelled.");
}
return false;
} catch (Exception __e) { throw rethrow(__e); } }
static List mapValuesToList(Object func, Map map) {
List out = emptyList(l(map));
for (Object key : keys(map))
out.add(callF(func, map.get(key)));
return out;
}
static List mapValuesToList(IF1 f, Map map) {
return mapValuesToList((Object) f, map);
}
static List mapValuesToList(Map map, IF1 f) {
return mapValuesToList(f, map);
}
static List mapValuesToList(Map map, Object func) {
return mapValuesToList(func, map);
}
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(String msg) { throw new RuntimeException(msg == null ? "" : msg); }
static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); }
static Map mapToValues(Iterable l, Object f) {
return mapKeyAndFunction(l, f);
}
static Map mapToValues(Object f, Iterable l) {
return mapKeyAndFunction(f, l);
}
static Map mapToValues(Iterable l, IF1 f) {
return mapKeyAndFunction(f, l);
}
static Map mapToValues(IF1 f, Iterable l) {
return mapKeyAndFunction(f, l);
}
static Map mapToValues(Map map, IF2 f) {
return mapKeyAndFunction(map, f);
}
static long intPairToLong(IntPair p) {
return p == null ? 0 : (((long) p.a) << 32) | (((long) p.b) & 0xFFFFFFFF);
}
static int addAndReturnIndex(List l, A a) {
if (l == null) return -1;
int idx = l.size();
l.add(a);
return idx;
}
static void listPut(List l, int i, A a, A emptyElement) {
listSet(l, i, a, emptyElement);
}
static void listPut(List l, int i, A a) {
listSet(l, i, a);
}
static void assertEqualsIC(String x, String y) {
assertEqic(x, y);
}
static void assertEqualsIC(String msg, String x, String y) {
assertEqic(msg, x, y);
}
static String assertEquals_quote(String x, String y) {
return assertEquals_quote(null, x, y);
}
static String assertEquals_quote(String msg, String x, String y) {
if (neq(x, y))
throw fail((msg != null ? msg + ": " : "") + quote(y) + " != " + quote(x));
return y;
}
static String lines(Iterable lines) { return fromLines(lines); }
static String lines(Object[] lines) { return fromLines(asList(lines)); }
static List lines(String s) { return toLines(s); }
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static char charFromHex(String s) {
return (char) hexToInt(s);
}
static List lmap(IF1 f, Iterable l) {
return lambdaMap(f, l);
}
static List lmap(IF1 f, A[] l) {
return lambdaMap(f, l);
}
static IterableIterator countIterator(int b) { return countIterator(0, b); }
static IterableIterator countIterator(int a, int b) {
return countIterator_exclusive(a, b);
}
static IterableIterator countIterator(int b, IF1 f) { return countIterator(0, b, f); }
static IterableIterator countIterator(int a, int b, IF1 f) {
return countIterator_exclusive(a, b, f);
}
// 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 ((DynamicObject) o).fieldValues.get(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 MultiSetMap multiSetMap_innerCustomTreeSet_outerRevTreeMap(Comparator innerComparator) {
MultiSetMap m = multiSetMap_innerTreeSet(innerComparator);
m.data = revTreeMap();
return m;
}
static MultiSetMap multiSetMap_innerCompactHashSet_outerRevTreeMap() {
return new MultiSetMap (descTreeMap()) {
Set _makeEmptySet() { return new CompactHashSet(); }
};
}
static Object first(Object list) {
return first((Iterable) list);
}
static A first(List list) {
return empty(list) ? null : list.get(0);
}
static A first(A[] bla) {
return bla == null || bla.length == 0 ? null : bla[0];
}
static A first(IterableIterator i) {
return first((Iterator ) i);
}
static A first(Iterator i) {
return i == null || !i.hasNext() ? null : i.next();
}
static A first(Iterable i) {
if (i == null) return null;
Iterator it = i.iterator();
return it.hasNext() ? it.next() : null;
}
static Character first(String s) { return empty(s) ? null : s.charAt(0); }
static Character first(CharSequence s) { return empty(s) ? null : s.charAt(0); }
static A first(Pair p) {
return p == null ? null : p.a;
}
static Byte first(byte[] l) {
return empty(l) ? null : l[0];
}
static int first(IntBuffer buf) {
return buf.get(0);
}
static String unnull(String s) {
return s == null ? "" : s;
}
static Collection unnull(Collection l) {
return l == null ? emptyList() : l;
}
static List unnull(List l) { return l == null ? emptyList() : l; }
static int[] unnull(int[] l) { return l == null ? emptyIntArray() : l; }
static char[] unnull(char[] l) { return l == null ? emptyCharArray() : l; }
static double[] unnull(double[] l) { return l == null ? emptyDoubleArray() : l; }
static Map unnull(Map l) {
return l == null ? emptyMap() : l;
}
static Iterable unnull(Iterable i) {
return i == null ? emptyList() : i;
}
static A[] unnull(A[] a) {
return a == null ? (A[]) emptyObjectArray() : a;
}
static BitSet unnull(BitSet b) {
return b == null ? new BitSet() : b;
}
//ifclass Symbol
static Pair unnull(Pair p) {
return p != null ? p : new Pair(null, null);
}
static long unnull(Long l) { return l == null ? 0L : l; }
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 int oneMillion() {
return 1000000;
}
static ArrayList toList(A[] a) { return asList(a); }
static ArrayList toList(int[] a) { return asList(a); }
static ArrayList toList(Set s) { return asList(s); }
static ArrayList toList(Iterable s) { return asList(s); }
static List reverseInPlace(List l) {
return reverseList(l);
}
static Pair pair(A a, B b) {
return new Pair(a, b);
}
static Pair pair(A a) {
return new Pair(a, a);
}
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 htag("a", contents, params);
}
static String b(Object contents, Object... params) {
return tag("b", contents, params);
}
static String stringIf(boolean b, String s) {
return stringIfTrue(b, s);
}
static List callFAll(Collection l, Object... args) {
return callF_all(l, args);
}
static A firstKey(Map map) {
return first(keys(map));
}
static A firstKey(MultiSetMap map) {
return map == null ? null : firstKey(map.data);
}
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 B firstValue(Map map) {
return first(values(map));
}
static B firstValue(MultiSetMap map) {
return map == null ? null : first(firstValue(map.data));
}
static int or0(Integer i) { return i == null ? 0 : i; }
static long or0(Long l) { return l == null ? 0L : l; }
static double or0(Double d) { return d == null ? 0.0 : d; }
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 A popFirst(List l) {
if (empty(l)) return null;
A a = first(l);
l.remove(0);
return a;
}
static A popFirst(Collection l) {
if (empty(l)) return null;
A a = first(l);
l.remove(a);
return a;
}
static List popFirst(int n, List l) {
List part = cloneSubList(l, 0, n);
removeSubList(l, 0, n);
return part;
}
static int hashSetCapacity(HashSet set) {
return (int) call(get(set, "map"), "capacity");
}
static long now_virtualTime;
static long now() {
return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis();
}
static Object load(String varName) {
readLocally(varName);
return get(mc(), varName);
}
static Object load(String progID, String varName) {
readLocally(progID, varName);
return get(mc(), varName);
}
static BufferedReader rawByteReader(InputStream in) { return rawByteReader(in, 8192); }
static BufferedReader rawByteReader(InputStream in, int bufSize) { try {
return bufferedReader(_registerIOWrap(new InputStreamReader(in, "ISO-8859-1"), in), bufSize);
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedReader rawByteReader(File f) { try {
return rawByteReader(newFileInputStream(f));
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedReader rawByteReader(File f, int bufSize) { try {
return rawByteReader(newFileInputStream(f), bufSize);
} catch (Exception __e) { throw rethrow(__e); } }
static long fileSize(String path) { return getFileSize(path); }
static long fileSize(File f) { return getFileSize(f); }
static BufferedInputStream bufferedFileInputStream(File f) {
return bufferedInputStream(f);
}
static BufferedInputStream bufferedFileInputStream(File f, int bufSize) { try {
return new BufferedInputStream(new FileInputStream(f), bufSize);
} catch (Exception __e) { throw rethrow(__e); } }
static byte[] isGZipFile_magic = bytesFromHex("1f8b08");
static boolean isGZipFile(File f) {
return byteArraysEqual(isGZipFile_magic, loadBeginningOfBinaryFile(f, l(isGZipFile_magic)));
}
static GZIPInputStream gzipInputStream(File f) {
return newGZIPInputStream(f);
}
static GZIPInputStream gzipInputStream(InputStream in) {
return newGZIPInputStream(in);
}
static String readLineIgnoreCR(Reader reader) { return readLineIgnoreCR(reader, null); }
static String readLineIgnoreCR(Reader reader, StringBuilder buf) { try {
int ch;
if (buf == null) buf = new StringBuilder(); else buf.setLength(0);
while ((ch = reader.read()) >= 0) {
if (ch == '\n') break;
buf.append((char) ch);
}
return str(buf);
} catch (Exception __e) { throw rethrow(__e); } }
static boolean startsWith(String a, String b) {
return a != null && a.startsWith(unnull(b));
}
static boolean startsWith(String a, char c) {
return nemptyString(a) && a.charAt(0) == c;
}
static boolean startsWith(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
m.m = new String[] {substring(a, strL(b))};
return true;
}
static boolean startsWith(List a, List b) {
if (a == null || listL(b) > listL(a)) return false;
for (int i = 0; i < listL(b); i++)
if (neq(a.get(i), b.get(i)))
return false;
return true;
}
static int parseInt(String s) {
return emptyString(s) ? 0 : Integer.parseInt(s);
}
static int parseInt(char c) {
return Integer.parseInt(str(c));
}
static A assertNotNull(A a) {
assertTrue(a != null);
return a;
}
static A assertNotNull(String msg, A a) {
assertTrue(msg, a != null);
return a;
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
if (!licensed_yes) return false;
ping_okInCleanUp();
return true;
}
static void licensed_off() {
licensed_yes = false;
}
static boolean contains(Collection c, Object o) {
return c != null && c.contains(o);
}
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 long twoIntsToLong(int a, int b) {
return (((long) a) << 32) | (((long) b) & 0xFFFFFFFF);
}
static int intPercentRatio(double x, double y) {
return ratioToIntPercent(x, y);
}
static String nPairs(long n) { return n2(n, "pair"); }
static String nPairs(Collection l) { return nPairs(l(l)); }
static String nPairs(Map map) { return nPairs(l(map)); }
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 int indexOf(List l, A a, int startIndex) {
if (l == null) return -1;
int n = l(l);
for (int i = startIndex; i < n; i++)
if (eq(l.get(i), a))
return i;
return -1;
}
static int indexOf(List l, int startIndex, A a) {
return indexOf(l, a, startIndex);
}
static int indexOf(List l, A a) {
if (l == null) return -1;
return l.indexOf(a);
}
static int indexOf(String a, String b) {
return a == null || b == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, String b, int i) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, char b) {
return a == null ? -1 : a.indexOf(b);
}
static int indexOf(String a, int i, char b) {
return indexOf(a, b, i);
}
static int indexOf(String a, char b, int i) {
return a == null ? -1 : a.indexOf(b, i);
}
static int indexOf(String a, int i, String b) {
return a == null || b == null ? -1 : a.indexOf(b, i);
}
static int indexOf(A[] x, A a) {
int n = l(x);
for (int i = 0; i < n; i++)
if (eq(x[i], a))
return i;
return -1;
}
static List takeFirst(List l, int n) {
return l(l) <= n ? l : newSubListOrSame(l, 0, n);
}
static List takeFirst(int n, List l) {
return takeFirst(l, n);
}
static String takeFirst(int n, String s) { return substring(s, 0, n); }
static String takeFirst(String s, int n) { return substring(s, 0, n); }
static CharSequence takeFirst(int n, CharSequence s) { return subCharSequence(s, 0, n); }
static List takeFirst(int n, Iterable i) {
if (i == null) return null;
List l = new ArrayList();
Iterator it = i.iterator();
for (int _repeat_0 = 0; _repeat_0 < n; _repeat_0++) { if (it.hasNext()) l.add(it.next()); else break; }
return l;
}
static int[] takeFirst(int n, int[] a) {
return takeFirstOfIntArray(n, a);
}
static List compactIntList(List l) {
return l == null ? null : new IntBuffer(l).asVirtualList();
}
static List parseInts(Iterable l) {
return lambdaMap(__29 -> parseInt(__29),l);
}
static List splitAtSpace(String s) {
return empty(s) ? emptyList() : asList(s.split("\\s+"));
}
static String substring(String s, int x) {
return substring(s, x, strL(s));
}
static String substring(String s, int x, int y) {
if (s == null) return null;
if (x < 0) x = 0;
int n = s.length();
if (y < x) y = x;
if (y > n) y = n;
if (x >= y) return "";
return s.substring(x, y);
}
// convenience method for quickly dropping a prefix
static String substring(String s, CharSequence l) {
return substring(s, l(l));
}
static String getText(final AbstractButton c) {
return c == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret c.getText();"; }});
}
static String getText(final JTextComponent c) {
return c == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return c.getText(); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret c.getText();"; }});
}
static String getText(final JLabel l) {
return l == null ? "" : (String) swingAndWait(new F0() { public Object get() { try { return l.getText(); } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "ret l.getText();"; }});
}
// returns the contents of text field for editable combo box
static String getText(final JComboBox cb) {
if (cb == null) return null;
if (isEditableComboBox(cb))
return unnull((String) cb.getEditor().getItem());
else
return str(cb.getSelectedItem());
}
static int firstIntFromLong(long l) {
return (int) (l >> 32);
}
static int secondIntFromLong(long l) {
return (int) l;
}
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 int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return 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 long longSum(Iterable l) {
long sum = 0;
for (Long i : unnull(l))
if (i != null) sum += i;
return sum;
}
static A set(A o, String field, Object value) {
if (o == null) return null;
if (o instanceof Class) set((Class) o, field, value);
else try {
Field f = set_findField(o.getClass(), field);
makeAccessible(f);
smartSet(f, o, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
return o;
}
static void set(Class c, String field, Object value) {
if (c == null) return;
try {
Field f = set_findStaticField(c, field);
makeAccessible(f);
smartSet(f, null, value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field set_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 set_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 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 && c.addAll(Arrays.asList(b));
}
static Map addAll(Map a, Map extends A,? extends B> b) {
if (a != null) a.putAll(b);
return a;
}
static int lLongArray(long[] a) {
return a == null ? 0 : a.length;
}
static long[] resizeLongArray(long[] a, int n) {
if (n == lLongArray(a)) return a;
long[] b = new long[n];
arraycopy(a, 0, b, 0, Math.min(lLongArray(a), n));
return b;
}
static int maximumSafeArraySize() {
return Integer.MAX_VALUE-8;
}
static ArrayList longArrayToList(long[] a) {
if (a == null) return null;
return longArrayToList(a, 0, a.length);
}
// no range checking on from/to in the interest of S P E E E E E EEED
static ArrayList longArrayToList(long[] a, int from, int to) {
if (a == null) return null;
ArrayList < Long > l = new ArrayList<>(to-from);
for (int i = from; i < to; i++) l.add(a[i]);
return l;
}
static List asVirtualList(A[] a) {
return wrapArrayAsList(a);
}
static List listFromFunction(int n, IF1 f) {
return new RandomAccessAbstractList() {
public int size() { return n; }
public A get(int i) { return f.get(i); }
};
}
static List listFromFunction(IF1 f, int n) {
return listFromFunction(n, f);
}
static A popLast(List l) {
return liftLast(l);
}
static List popLast(int n, List l) {
return liftLast(n, l);
}
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 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 nextToLast(List l) {
return get(l, l(l)-2);
}
static String squareBracket(String s) {
return "[" + s + "]";
}
static Iterator iterator(Iterable c) {
return c == null ? emptyIterator() : c.iterator();
}
static Map putAll(Map a, Map extends A,? extends B> b) {
if (a != null && b != null) a.putAll(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 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 Set emptySet() {
return new HashSet();
}
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 keySet(Map map) {
return map == null ? new HashSet() : map.keySet();
}
static Set keySet(Object map) {
return keys((Map) map);
}
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 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(Object o) {
if (o instanceof Collection) return empty((Collection) o);
if (o instanceof String) return empty((String) o);
if (o instanceof Map) return empty((Map) o);
if (o instanceof Object[]) return empty((Object[]) o);
if (o instanceof byte[]) return empty((byte[]) o);
if (o == null) return true;
throw fail("unknown type for 'empty': " + getType(o));
}
static boolean empty(Iterator i) { return i == null || !i.hasNext(); }
static boolean empty(double[] a) { return a == null || a.length == 0; }
static boolean empty(float[] a) { return a == null || a.length == 0; }
static boolean empty(int[] a) { return a == null || a.length == 0; }
static boolean empty(long[] a) { return a == null || a.length == 0; }
static boolean empty(byte[] a) { return a == null || a.length == 0; }
static boolean empty(short[] a) { return a == null || a.length == 0; }
static boolean empty(File f) { return getFileSize(f) == 0; }
static boolean empty(IntBuffer b) { return b == null || b.isEmpty(); }
static boolean empty(LongBuffer b) { return b == null || b.isEmpty(); }
static String unquote(String s) {
if (s == null) return null;
if (startsWith(s, '[')) {
int i = 1;
while (i < s.length() && s.charAt(i) == '=') ++i;
if (i < s.length() && s.charAt(i) == '[') {
String m = s.substring(1, i);
if (s.endsWith("]" + m + "]"))
return s.substring(i+1, s.length()-i-1);
}
}
if (s.length() > 1) {
char c = s.charAt(0);
if (c == '\"' || c == '\'') {
int l = endsWith(s, c) ? s.length()-1 : s.length();
StringBuilder sb = new StringBuilder(l-1);
for (int i = 1; i < l; i++) {
char ch = s.charAt(i);
if (ch == '\\') {
char nextChar = (i == l - 1) ? '\\' : s.charAt(i + 1);
// Octal escape?
if (nextChar >= '0' && nextChar <= '7') {
String code = "" + nextChar;
i++;
if ((i < l - 1) && s.charAt(i + 1) >= '0'
&& s.charAt(i + 1) <= '7') {
code += s.charAt(i + 1);
i++;
if ((i < l - 1) && s.charAt(i + 1) >= '0'
&& s.charAt(i + 1) <= '7') {
code += s.charAt(i + 1);
i++;
}
}
sb.append((char) Integer.parseInt(code, 8));
continue;
}
switch (nextChar) {
case '\"': ch = '\"'; break;
case '\\': ch = '\\'; break;
case 'b': ch = '\b'; break;
case 'f': ch = '\f'; break;
case 'n': ch = '\n'; break;
case 'r': ch = '\r'; break;
case 't': ch = '\t'; break;
case '\'': ch = '\''; break;
// Hex Unicode: u????
case 'u':
if (i >= l - 5) {
ch = 'u';
break;
}
int code = Integer.parseInt(
"" + s.charAt(i + 2) + s.charAt(i + 3)
+ s.charAt(i + 4) + s.charAt(i + 5), 16);
sb.append(Character.toChars(code));
i += 5;
continue;
default:
ch = nextChar; // added by Stefan
}
i++;
}
sb.append(ch);
}
return sb.toString();
}
}
return s; // not quoted - return original
}
static String joinWithComma(Collection c) {
return join(", ", c);
}
static String joinWithComma(String... c) {
return join(", ", c);
}
static String joinWithComma(Pair p) {
return p == null ? "" : joinWithComma(str(p.a), str(p.b));
}
static List quoteAll(Collection l) {
List x = new ArrayList();
for (String s : l)
x.add(quote(s));
return x;
}
static boolean arraysEqual(Object[] a, Object[] b) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++)
if (neq(a[i], b[i])) return false;
return true;
}
static int lIntArray(int[] a) {
return a == null ? 0 : a.length;
}
static int[] resizeIntArray(int[] a, int n) {
if (n == lIntArray(a)) return a;
int[] b = new int[n];
arraycopy(a, 0, b, 0, Math.min(lIntArray(a), n));
return b;
}
static ArrayList intArrayToList(int[] a) {
if (a == null) return null;
return intArrayToList(a, 0, a.length);
}
// no range checking on from/to in the interest of S P E E E E E EEED
static ArrayList intArrayToList(int[] a, int from, int to) {
if (a == null) return null;
ArrayList < Integer > l = new ArrayList<>(to-from);
for (int i = from; i < to; i++) l.add(a[i]);
return l;
}
static IntegerIterator integerIterator(int[] l) {
return l == null ? null : new IntegerIterator() {
int i = 0;
public boolean hasNext() { return i < l.length; }
public int next() { return l[i++]; }
};
}
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 int iround(double d) {
return (int) Math.round(d);
}
static int iround(Number n) {
return iround(toDouble(n));
}
static UnsupportedOperationException unsupportedOperation() {
throw new UnsupportedOperationException();
}
static int hashCodeFor(Object a) {
return a == null ? 0 : a.hashCode();
}
static AutoCloseable tempInterceptPrintIfNotIntercepted(F1 f) {
return print_byThread().get() == null ? tempInterceptPrint(f) : null;
}
static String shortenClassName(String name) {
if (name == null) return null;
int i = lastIndexOf(name, "$");
if (i < 0) i = lastIndexOf(name, ".");
return i < 0 ? name : substring(name, i+1);
}
static List ll(A... a) {
ArrayList l = new ArrayList(a.length);
if (a != null) for (A x : a) l.add(x);
return l;
}
static Map> callF_cache = newDangerousWeakHashMap();
static A callF(F0 f) {
return f == null ? null : f.get();
}
static B callF(F1 f, A a) {
return f == null ? null : f.get(a);
}
static A callF(IF0 f) {
return f == null ? null : f.get();
}
static B callF(IF1 f, A a) {
return f == null ? null : f.get(a);
}
static C callF(IF2 f, A a, B b) {
return f == null ? null : f.get(a, b);
}
static void callF(VF1 f, A a) {
if (f != null) f.get(a);
}
static Object callF(Object f, Object... args) {
if (f instanceof String)
return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC
return safeCallF(f, args);
}
static Object safeCallF(Object f, Object... args) {
if (f instanceof Runnable) {
((Runnable) f).run();
return null;
}
if (f == null) return null;
Class c = f.getClass();
ArrayList methods;
synchronized(callF_cache) {
methods = callF_cache.get(c);
if (methods == null)
methods = callF_makeCache(c);
}
int n = l(methods);
if (n == 0) {
throw fail("No get method in " + getClassName(c));
}
if (n == 1) return invokeMethod(methods.get(0), f, args);
for (int i = 0; i < n; i++) {
Method m = methods.get(i);
if (call_checkArgs(m, args, false))
return invokeMethod(m, f, args);
}
throw fail("No matching get method in " + getClassName(c));
}
// used internally
static ArrayList callF_makeCache(Class c) {
ArrayList l = new ArrayList();
Class _c = c;
do {
for (Method m : _c.getDeclaredMethods())
if (m.getName().equals("get")) {
makeAccessible(m);
l.add(m);
}
if (!l.isEmpty()) break;
_c = _c.getSuperclass();
} while (_c != null);
callF_cache.put(c, l);
return l;
}
static 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)
x.add(callF(f, o));
return x;
}
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)
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) for (A o : l)
x.add(f.get(o));
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)
x.add(f.get(o));
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);
}
// 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()) {
Map.Entry e = (Map.Entry) _e;
x.add(callF(f, e.getKey(), e.getValue()));
}
return x;
}
static List map(Map map, IF2 f) {
return map(map, (Object) f);
}
public static String bytesToHex(byte[] bytes) {
return bytesToHex(bytes, 0, bytes.length);
}
public static String bytesToHex(byte[] bytes, int ofs, int len) {
StringBuilder stringBuilder = new StringBuilder(len*2);
for (int i = 0; i < len; i++) {
String s = "0" + Integer.toHexString(bytes[ofs+i]);
stringBuilder.append(s.substring(s.length()-2, s.length()));
}
return stringBuilder.toString();
}
static byte[] charToBytes(char c) {
return new byte[] {
(byte) (c >>> 8),
(byte) c
};
}
static List stringAsCharacterList(final String s) {
if (s == null) return null;
return new RandomAccessAbstractList() {
final int l = l(s);
public int size() { return l; }
public Character get(int i) { return s.charAt(i); }
};
}
// returns l(s) if not found
static int smartIndexOf(String s, String sub, int i) {
if (s == null) return 0;
i = s.indexOf(sub, min(i, l(s)));
return i >= 0 ? i : l(s);
}
static int smartIndexOf(String s, int i, char c) {
return smartIndexOf(s, c, i);
}
static int smartIndexOf(String s, char c, int i) {
if (s == null) return 0;
i = s.indexOf(c, min(i, l(s)));
return i >= 0 ? i : l(s);
}
static int smartIndexOf(String s, String sub) {
return smartIndexOf(s, sub, 0);
}
static int smartIndexOf(String s, char c) {
return smartIndexOf(s, c, 0);
}
static int smartIndexOf(List l, A sub) {
return smartIndexOf(l, sub, 0);
}
static int smartIndexOf(List l, int start, A sub) {
return smartIndexOf(l, sub, start);
}
static int smartIndexOf(List l, A sub, int start) {
int i = indexOf(l, sub, start);
return i < 0 ? l(l) : i;
}
static String formatFunctionCall(String fname, Object... args) {
return fname + "(" + joinWithComma(allToString(args)) + ")";
}
static String rtrim_fromLines(Collection lines) {
StringBuilder buf = new StringBuilder();
if (lines != null) {
boolean first = true;
for (Object line : lines) {
if (first) first = false; else buf.append('\n');
buf.append(str(line));
}
}
return buf.toString();
}
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 boolean endsWithLetterOrDigit(String s) {
return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1));
}
static void ping_okInCleanUp() {
if (ping_pauseAll || ping_anyActions)
ping_impl(true);
}
// this syntax should be removed...
static Object getThreadLocal(Object o, String name) {
ThreadLocal t = (ThreadLocal) (getOpt(o, name));
return t != null ? t.get() : null;
}
static A getThreadLocal(ThreadLocal tl) {
return tl == null ? null : tl.get();
}
static A getThreadLocal(ThreadLocal tl, A defaultValue) {
return or(getThreadLocal(tl), defaultValue);
}
static ThreadLocal print_byThread_dontCreate() {
return print_byThread;
}
static boolean isFalse(Object o) {
return eq(false, o);
}
static String fixNewLines(String s) {
int i = indexOf(s, '\r');
if (i < 0) return s;
int l = s.length();
StringBuilder out = new StringBuilder(l);
out.append(s, 0, i);
for (; i < l; i++) {
char c = s.charAt(i);
if (c != '\r')
out.append(c);
else {
out.append('\n');
if (i+1 < l && s.charAt(i+1) == '\n') ++i;
}
}
return out.toString();
}
static void print_append(Appendable buf, String s, int max) { try {
synchronized(buf) {
buf.append(s);
if (buf instanceof StringBuffer)
rotateStringBuffer(((StringBuffer) buf), max);
else if (buf instanceof StringBuilder)
rotateStringBuilder(((StringBuilder) buf), max);
}
} catch (Exception __e) { throw rethrow(__e); } }
static void vmBus_send(String msg, Object... args) {
Object arg = vmBus_wrapArgs(args);
pcallFAll(vm_busListeners_live(), msg, arg);
pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg);
}
static void vmBus_send(String msg) {
vmBus_send(msg, (Object) null);
}
static Class mc() {
return main.class;
}
static int countLines(String s) {
return l(toLines(s)); // yeah could be optimized :-)
}
static List uniquifyList(Collection l) {
if (l == null) return null;
if (l(l) < 2) return asList(l);
HashSet set = new HashSet();
List out = new ArrayList();
for (A a : l)
if (set.add(a))
out.add(a);
return out;
}
static String formatWithThousands(long l) {
return formatWithThousandsSeparator(l);
}
static double fraction(double d) {
return d % 1;
}
static String n_fancy2(long l, String singular, String plural) {
return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural);
}
static String n_fancy2(Collection l, String singular, String plural) {
return n_fancy2(l(l), singular, plural);
}
static String n_fancy2(Map m, String singular, String plural) {
return n_fancy2(l(m), singular, plural);
}
static String n_fancy2(Object[] a, String singular, String plural) {
return n_fancy2(l(a), singular, plural);
}
static List sortedInPlace(List l, final Object comparator) {
sort(l, makeComparator(comparator));
return l;
}
static List sortedInPlace(List l) {
sort(l);
return l;
}
static int iteratorCount_int_close(Iterator i) { try {
int n = 0;
if (i != null) while (i.hasNext()) { i.next(); ++n; }
if (i instanceof AutoCloseable) ((AutoCloseable) i).close();
return n;
} catch (Exception __e) { throw rethrow(__e); } }
static Object call(Object o) {
return callF(o);
}
// varargs assignment fixer for a single string array argument
static Object call(Object o, String method, String[] arg) {
return call(o, method, new Object[] {arg});
}
static Object call(Object o, String method, Object... args) {
//ret call_cached(o, method, args);
return call_withVarargs(o, method, args);
}
static List collectField(Iterable c, String field) {
List l = new ArrayList();
if (c != null) for (Object a : c)
l.add(getOpt(a, field));
return l;
}
static List collectField(String field, Iterable c) {
return collectField(c, field);
}
static Map