Libraryless. Click here for Pure Java version (9847L/54K).
1 | sclass Best<A> is IBest<A> { |
2 | A best; |
3 | double score; |
4 | bool verboseNewBest, replaceIfSameScore; |
5 | settable bool lowerIsBetter; |
6 | |
7 | transient O onChange; |
8 | transient O stringifier; // func(A) -> S |
9 | |
10 | synchronized bool isNewBest(double score) { |
11 | ret best == null || !isNaN(score) |
12 | && (replaceIfSameScore |
13 | ? compareScores(score, this.score) >= 0 |
14 | : compareScores(score, this.score) > 0); |
15 | } |
16 | |
17 | double worstScore() { |
18 | ret lowerIsBetter ? infinity() : minusInfinity(); |
19 | } |
20 | |
21 | int compareScores(double a, double b) { |
22 | ret lowerIsBetter ? -cmp(a, b) : cmp(a, b); |
23 | } |
24 | |
25 | synchronized double bestScore() { |
26 | ret best == null ? worstScore() : score; |
27 | } |
28 | |
29 | double score() { ret bestScore(); } |
30 | double getScore() { ret bestScore(); } |
31 | |
32 | synchronized float floatScoreOr(float defaultValue) { |
33 | ret best == null ? defaultValue : (float) score; |
34 | } |
35 | |
36 | bool put(Scored<? extends A> s) { |
37 | ret s != null && put(s!, s.score()); |
38 | } |
39 | |
40 | bool put(Pair<? extends A, Double> p) { |
41 | ret p != null && put(p.a, p.b); |
42 | } |
43 | |
44 | bool put(Best<? extends A> b) { |
45 | ret b != null && put(b!, b.score); |
46 | } |
47 | |
48 | public bool put(A a, double score) { |
49 | ping(); |
50 | bool change = false; |
51 | if (a != null) synchronized(this) { |
52 | if (isNewBest(score)) { |
53 | best = a; |
54 | this.score = score; |
55 | change = true; |
56 | } |
57 | } |
58 | if (change) { |
59 | if (verboseNewBest) print("New best! " + this); |
60 | pcallF(onChange); |
61 | } |
62 | ret change; |
63 | } |
64 | |
65 | synchronized A get() { ret best; } |
66 | synchronized bool has() { ret best != null; } |
67 | |
68 | synchronized Pair<A, Double> pair() { ret main pair(best, bestScore()); } |
69 | synchronized Scored<A> scored() { ret best == null ?: new Scored<A>(best, bestScore()); } |
70 | |
71 | synchronized A getIfScoreAbove(double x) { ret compareScores(score(), x) >= 0 ? best : null; } |
72 | |
73 | toString { |
74 | ret "Score " + formatDouble_significant2(score, 4) + ": " + callStringifier(stringifier, best); |
75 | } |
76 | |
77 | bool putAndPrintIfNewBest(A a, double score) { |
78 | if (!put(a, score)) false; |
79 | ret true with print(this); |
80 | } |
81 | |
82 | synchronized void clear() { best = null; score = 0; } |
83 | } |
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: | 920 / 3535 |
Version history: | 40 change(s) |
Referenced in: | [show references] |