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

47
LINES

< > BotCompany Repo | #1006448 // class Lowest - like Best, but collects item with lowest score

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

Libraryless. Click here for Pure Java version (9729L/53K).

sclass Lowest<A> is IBest<A> {
  A best;
  double score;
  transient O onChange;
  
  synchronized bool isNewBest(double score) {
    ret best == null || score < this.score;
  }
  
  synchronized double bestScore() {
    ret best == null ? Double.NaN : score;
  }
  
  double score() { ret bestScore(); }
  
  synchronized float floatScore() {
    ret best == null ? Float.NaN : (float) score;
  }
  
  synchronized float floatScoreOr(float defaultValue) {
    ret best == null ? defaultValue : (float) score;
  }
  
  public bool put(A a, double score) {
    bool change = false;
    synchronized(this) {
      if (a != null && isNewBest(score)) {
        best = a;
        this.score = score;
        change = true;
      }
    }
    if (change)
      pcallF(onChange);
    ret change;
  }
  
  synchronized void clear() { best = null; score = 0; }
  
  synchronized A get() { ret best; }
  synchronized bool has() { ret best != null; }
  synchronized Pair<A, Double> pair() { ret best == null ? null : Pair(best, bestScore()); }
  
  toString {
    ret "Score " + formatDouble_significant2(score, 4) + ": " + best;
  }
}

Author comment

Began life as a copy of #1006090

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1006448
Snippet name: class Lowest - like Best, but collects item with lowest score
Eternal ID of this version: #1006448/12
Text MD5: de0be6e56f8549f507a3782e57312c45
Transpilation MD5: 1af45cd24a20e0abac3edd7eacbb005d
Author: stefan
Category: javax / genetic optimization
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-21 11:29:55
Source code size: 1169 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 667 / 4365
Version history: 11 change(s)
Referenced in: [show references]