Libraryless. Click here for Pure Java version (9847L/54K).
sclass Best<A> is IBest<A> { A best; double score; bool verboseNewBest, replaceIfSameScore; settable bool lowerIsBetter; transient O onChange; transient O stringifier; // func(A) -> S synchronized bool isNewBest(double score) { ret best == null || !isNaN(score) && (replaceIfSameScore ? compareScores(score, this.score) >= 0 : compareScores(score, this.score) > 0); } double worstScore() { ret lowerIsBetter ? infinity() : minusInfinity(); } int compareScores(double a, double b) { ret lowerIsBetter ? -cmp(a, b) : cmp(a, b); } synchronized double bestScore() { ret best == null ? worstScore() : score; } double score() { ret bestScore(); } double getScore() { ret bestScore(); } synchronized float floatScoreOr(float defaultValue) { ret best == null ? defaultValue : (float) score; } bool put(Scored<? extends A> s) { ret s != null && put(s!, s.score()); } bool put(Pair<? extends A, Double> p) { ret p != null && put(p.a, p.b); } bool put(Best<? extends A> b) { ret b != null && put(b!, b.score); } public 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<A, Double> pair() { ret main pair(best, bestScore()); } synchronized Scored<A> scored() { ret best == null ?: new Scored<A>(best, bestScore()); } synchronized A getIfScoreAbove(double x) { ret compareScores(score(), x) >= 0 ? 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); } synchronized void clear() { best = null; score = 0; } }
download show line numbers debug dex old transpilations
Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1006090 |
Snippet name: | class Best - takes objects and scores (doubles) and keeps the highest-scoring object. now synchronized and persistable |
Eternal ID of this version: | #1006090/41 |
Text MD5: | 0a281df274f56a2d989336f8ddeef94a |
Transpilation MD5: | f59a673807121c573fc63812b53103ab |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-11-21 11:30:09 |
Source code size: | 2226 bytes / 83 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 919 / 3532 |
Version history: | 40 change(s) |
Referenced in: | [show references] |