sclass Best { A best; double score; bool verboseNewBest, replaceIfSameScore; transient O onChange; transient O stringifier; // func(A) -> S synchronized bool isNewBest(double score) { ret best == null || !isNaN(score) && (replaceIfSameScore ? score >= this.score : score > this.score); } synchronized double bestScore() { ret best == null ? minusInfinity() : score; } double score() { ret bestScore(); } double getScore() { ret bestScore(); } synchronized float floatScoreOr(float defaultValue) { ret best == null ? defaultValue : (float) score; } bool put(Pair p) { ret p != null && put(p.a, p.b); } bool put(A a, double score) { ping(); bool change = false; if (a != null) synchronized(this) { if (isNewBest(score)) { best = a; this.score = score; change = true; } } if (change) { if (verboseNewBest) print("New best! " + this); pcallF(onChange); } ret change; } synchronized A get() { ret best; } synchronized bool has() { ret best != null; } synchronized Pair pair() { ret main.pair(best, bestScore()); } synchronized A getIfScoreAbove(double x) { ret score() >= x ? best : null; } toString { ret "Score " + formatDouble_significant2(score, 4) + ": " + callStringifier(stringifier, best); } bool putAndPrintIfNewBest(A a, double score) { if (!put(a, score)) false; ret true with print(this); } }