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

34
LINES

< > BotCompany Repo | #1028471 // WithProbability - probability (0 to 1) is first argument in constructor

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

Libraryless. Click here for Pure Java version (3469L/19K).

sclass WithProbability<A> extends Var<A> implements Comparable<WithProbability<A>> {
  double probability; // assumed between 0 and 1
  
  *() {}
  *(A a) { this(1, a); }
  *(double *probability, A a) { super(a); }
  
  toString {
    ret "p=" + renderedProbability() + ": " + str(get());
  }
  
  S renderedProbability() {
    ret formatDouble_noLeadingZero(probability, 2);
  }
  
  double probability() { ret probability; }
  
  public int hashCode() {
    ret boostHashCombine(main hashCode(probability), main hashCode(get()));
  }
  
  public bool equals(O o) {
    if (o cast WithProbability) {
      ret probability == o.probability && eq(get(), o!);
    }
    false;
  }
  
  // Higher probability comes first!
  public int compareTo(WithProbability<A> wp) {
    ret wp == null ? 1
      : cmp(wp.probability, probability);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1028471
Snippet name: WithProbability - probability (0 to 1) is first argument in constructor
Eternal ID of this version: #1028471/9
Text MD5: bc649173a90af0ac81b0431b44d891d5
Transpilation MD5: 3d86c307a787071852c5ace5abb7fda7
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-08-20 06:33:50
Source code size: 870 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 308 / 659
Version history: 8 change(s)
Referenced in: #1031504 - ProbabilisticList - list ordered by probability [OK!]
#1031512 - withProbability - make WithProbability
#1032030 - WithPreciseProbability - show full precision in toString
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)