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).

1  
// simple probabilistic version of AllOnAll
2  
// where you simply supply a function that calculates a probability for each pair
3  
// class should be persistable if a's, b's and probabilityForPair are persistable
4  
sclass AllOnAll_probabilistic<A, B> implements Producer<Pair<A, B>> {
5  
  new L<A> aList;
6  
  new L<B> bList;
7  
  LPair<IntRange> todo = new LinkedList; // this is still non-probabilistic
8  
  MultiSetMap<Double, Pair<A, B>> todo2 = multiSetMap_outerDescTreeMap_innerLinkedHashSet();
9  
  
10  
  persistently swappable double probabilityForPair(A a, B b) { ret 1.0; }
11  
12  
  void addA(A a) { newA(a); }
13  
  void addAIfNotNull(A a) { if (a != null) newA(a); }
14  
  
15  
  synchronized void newA(A a) {
16  
    add(aList, a);
17  
    IntRange rB = intRange(0, l(bList));
18  
    if (empty(rB)) ret;
19  
    addPair(todo, intRange_last(aList), rB);
20  
  }
21  
  
22  
  synchronized void newAs(Iterable<A> l) {
23  
    fOr (A a : l) newA(a);
24  
  }
25  
  
26  
  void addB(B b) { newB(b); }
27  
  void addBIfNotNull(B b) { if (b != null) newB(b); }
28  
  
29  
  synchronized void newB(B b) {
30  
    add(bList, b);
31  
    IntRange rA = intRange(0, l(aList));
32  
    if (empty(rA)) ret;
33  
    addPair(todo, rA, intRange_last(bList));
34  
  }
35  
  
36  
  synchronized void newBs(Iterable<B> l) {
37  
    fOr (B b : l) newB(b);
38  
  }
39  
  
40  
  public synchronized Pair<A, B> next() {
41  
    Pair<IntRange> p;
42  
    while ((p = popFirst(todo)) != null)
43  
      for (A a : subList(aList, p.a))
44  
        for (B b : subList(bList, p.b))
45  
          addAPair(a, b, probabilityForPair(a, b));
46  
47  
    ret popFirstValueFromMultiSetMap(todo2);
48  
  }
49  
  
50  
  private void addAPair(A a, B b, double probability) {
51  
    if (probability > 0)
52  
      todo2.put(probability, pair(a, b));
53  
  }
54  
  
55  
  synchronized L cloneBList() {
56  
    ret clonedList(bList);
57  
  }
58  
  
59  
  L<A> getAs() { ret aList; }
60  
  L<B> getBs() { ret bList; }
61  
}

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: 283 / 623
Version history: 12 change(s)
Referenced in: [show references]