sclass Best {
A best;
double score;
transient O onChange;
O stringifier; // func(A) -> S
synchronized bool isNewBest(double score) {
ret best == null || !isNaN(score) && score > this.score;
}
synchronized double bestScore() {
ret best == null ? minusInfinity() : score;
}
double score() { ret bestScore(); }
double getScore() { ret bestScore(); }
bool put(A a, double score) {
ping();
bool change = false;
synchronized(this) {
if (a != null && isNewBest(score)) {
best = a;
this.score = score;
change = true;
}
}
if (change)
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);
}
}