Libraryless. Click here for Pure Java version (4894L/27K).
1 | // now synchronized |
2 | persistable sclass MultiBest<A> {
|
3 | Cl<A> best = new L; |
4 | double score = negativeInfinity(); |
5 | bool verboseNewBest; |
6 | |
7 | synchronized double bestScore aka score aka getScore() {
|
8 | ret best == null ? minusInfinity() : score; |
9 | } |
10 | |
11 | bool isEmpty() { ret best.isEmpty(); }
|
12 | |
13 | synchronized float floatScoreOr(float defaultValue) {
|
14 | ret best == null ? defaultValue : (float) score; |
15 | } |
16 | |
17 | void put(Pair<? extends A, Double> p) {
|
18 | if (p != null) put(p.a, p.b); |
19 | } |
20 | |
21 | void put(A a, double score) {
|
22 | ping(); |
23 | bool newBest; |
24 | if (a != null) synchronized(this) {
|
25 | if (score > this.score) {
|
26 | best.clear(); |
27 | this.score = score; |
28 | set newBest; |
29 | } |
30 | |
31 | if (score >= this.score) {
|
32 | best.add(a); |
33 | } |
34 | } |
35 | if (newBest && verboseNewBest) print("New best! " + this);
|
36 | } |
37 | |
38 | synchronized Cl<A> get() { ret best; }
|
39 | synchronized bool has() { ret !isEmpty(); }
|
40 | |
41 | toString {
|
42 | ret isEmpty() ? "-" |
43 | : "Score " + formatDouble_significant2(score, 4) |
44 | + (tied() ? ", " + nWinners(duplicity()) : "") |
45 | + ": " + joinWithComma(map stringify(best)); |
46 | } |
47 | |
48 | S stringify(A a) { ret str(a); }
|
49 | |
50 | bool tied() { ret duplicity() > 1; }
|
51 | int duplicity() { ret best.size(); }
|
52 | |
53 | synchronized void clear() { best.clear(); score = 0; }
|
54 | } |
Began life as a copy of #1006090
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
| Snippet ID: | #1034121 |
| Snippet name: | MultiBest - takes objects and scores (doubles) and keeps the highest-scoring objects |
| Eternal ID of this version: | #1034121/7 |
| Text MD5: | c1b1aca94bb48b6652c367f5f4ca8e35 |
| Transpilation MD5: | d81afa0cc28d5ee4ad91e86348af9509 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2022-01-22 22:37:18 |
| Source code size: | 1381 bytes / 54 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 405 / 576 |
| Version history: | 6 change(s) |
| Referenced in: | [show references] |