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 (2693L/17K).

sclass Lowest<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;
  }
  
  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   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, ppjhyzlbdabe, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, 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/11
Text MD5: 5e0d7ed68d0d8c05befecf9f37b6fe6b
Transpilation MD5: 2bfa9e33fa975ad70f529083bb6bf660
Author: stefan
Category: javax / genetic optimization
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-22 14:31:09
Source code size: 1150 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 367 / 3801
Version history: 10 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1006448 & http://1006448.tinybrain.de