Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

54
LINES

< > BotCompany Repo | #1034121 // MultiBest - takes objects and scores (doubles) and keeps the highest-scoring objects

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4894L/27K).

// now synchronized
persistable sclass MultiBest<A> {
  Cl<A> best = new L;
  double score = negativeInfinity();
  bool verboseNewBest;
  
  synchronized double bestScore aka score aka getScore() {
    ret best == null ? minusInfinity() : score;
  }
  
  bool isEmpty() { ret best.isEmpty(); }
  
  synchronized float floatScoreOr(float defaultValue) {
    ret best == null ? defaultValue : (float) score;
  }
  
  void put(Pair<? extends A, Double> p) {
    if (p != null) put(p.a, p.b);
  }
  
  void put(A a, double score) {
    ping();
    bool newBest;
    if (a != null) synchronized(this) {
      if (score > this.score) {
        best.clear();
        this.score = score;
        set newBest;
      }
      
      if (score >= this.score) {
        best.add(a);
      }
    }
    if (newBest && verboseNewBest) print("New best! " + this);
  }
  
  synchronized Cl<A> get() { ret best; }
  synchronized bool has() { ret !isEmpty(); }
  
  toString {
    ret isEmpty() ? "-"
      : "Score " + formatDouble_significant2(score, 4)
        + (tied() ? ", " + nWinners(duplicity()) : "")
        + ": " + joinWithComma(map stringify(best));
  }
  
  S stringify(A a) { ret str(a); }
  
  bool tied() { ret duplicity() > 1; }
  int duplicity() { ret best.size(); }
  
  synchronized void clear() { best.clear(); score = 0; }
}

Author comment

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: 174 / 299
Version history: 6 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)