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

61
LINES

< > BotCompany Repo | #1028466 // AllOnAll_probabilistic [works]

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

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

// simple probabilistic version of AllOnAll
// where you simply supply a function that calculates a probability for each pair
// class should be persistable if a's, b's and probabilityForPair are persistable
sclass AllOnAll_probabilistic<A, B> implements Producer<Pair<A, B>> {
  new L<A> aList;
  new L<B> bList;
  LPair<IntRange> todo = new LinkedList; // this is still non-probabilistic
  MultiSetMap<Double, Pair<A, B>> todo2 = multiSetMap_outerDescTreeMap_innerLinkedHashSet();
  
  persistently swappable double probabilityForPair(A a, B b) { ret 1.0; }

  void addA(A a) { newA(a); }
  void addAIfNotNull(A a) { if (a != null) newA(a); }
  
  synchronized void newA(A a) {
    add(aList, a);
    IntRange rB = intRange(0, l(bList));
    if (empty(rB)) ret;
    addPair(todo, intRange_last(aList), rB);
  }
  
  synchronized void newAs(Iterable<A> l) {
    fOr (A a : l) newA(a);
  }
  
  void addB(B b) { newB(b); }
  void addBIfNotNull(B b) { if (b != null) newB(b); }
  
  synchronized void newB(B b) {
    add(bList, b);
    IntRange rA = intRange(0, l(aList));
    if (empty(rA)) ret;
    addPair(todo, rA, intRange_last(bList));
  }
  
  synchronized void newBs(Iterable<B> l) {
    fOr (B b : l) newB(b);
  }
  
  public synchronized Pair<A, B> next() {
    Pair<IntRange> p;
    while ((p = popFirst(todo)) != null)
      for (A a : subList(aList, p.a))
        for (B b : subList(bList, p.b))
          addAPair(a, b, probabilityForPair(a, b));

    ret popFirstValueFromMultiSetMap(todo2);
  }
  
  private void addAPair(A a, B b, double probability) {
    if (probability > 0)
      todo2.put(probability, pair(a, b));
  }
  
  synchronized L cloneBList() {
    ret clonedList(bList);
  }
  
  L<A> getAs() { ret aList; }
  L<B> getBs() { ret bList; }
}

Author comment

Began life as a copy of #1023892

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: #1028466
Snippet name: AllOnAll_probabilistic [works]
Eternal ID of this version: #1028466/13
Text MD5: 49233992d652cd58c5ce8ceb3a34465c
Transpilation MD5: e2bbf14ba810268252df1c40ddfcd069
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-06-21 01:31:59
Source code size: 1830 bytes / 61 lines
Pitched / IR pitched: No / No
Views / Downloads: 284 / 624
Version history: 12 change(s)
Referenced in: #1028478 - AllOnAll_probabilistic_forTransformers [dev., possibly faster than AllOnAll_probabilistic, not that easy to make]
#1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674)