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

50
LINES

< > BotCompany Repo | #1010411 // PosNeg - positive MultiSet + negative MultiSet

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

Libraryless. Click here for Pure Java version (3201L/20K).

sclass PosNeg<A> {
  new MultiSet<A> pos;
  new MultiSet<A> neg;
  
  *() {}
  *(Iterable<A> pos, Iterable<A> neg) {
    this.pos.addAll(pos);
    this.neg.addAll(neg);
  }
  
  *(Iterable<Pair<A, Bool>> pairs) {
    fOr (Pair<A, Bool> p : pairs)
      add(p.a, p.b);
  }
  
  *(Map<A, Bool> map) {
    fOr (A a, Bool b : map)
      add(a, b);
  }
  
  void add(A a, int score) {
    if (score > 0)
      pos.add(a, score);
    else if (score < 0)
      neg.add(a, -score);
  }
  
  void add(A a, Bool b) {
    if (isTrue(b))
      pos.add(a);
    else if (isFalse(b))
      neg.add(a);
  }
  
  int get(A a) {
    ret pos.get(a)-neg.get(a);
  }
  
  int score() { ret l(pos)-l(neg); }
  int n() { ret l(pos)+l(neg); }
  int nPos() { ret l(pos); }
  int nNeg() { ret l(neg); }
  bool isEmpty() { ret n() == 0; }
  bool perfect() { ret nPos() != 0 && nNeg() == 0; }
  bool antiPerfect() { ret nPos() == 0 && nNeg() != 0; }
  
  toString {
    ret "+" + nPos() + "/-" + nNeg() + ": " + pos + " / " + neg;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1010411
Snippet name: PosNeg - positive MultiSet + negative MultiSet
Eternal ID of this version: #1010411/15
Text MD5: d4ea5ae779490183441d7d171fd848b0
Transpilation MD5: 847b61b3f5036b7c666268795057f9f6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-06 00:32:30
Source code size: 1057 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 529 / 1236
Version history: 14 change(s)
Referenced in: #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)