// 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 implements Producer> { new L aList; new L bList; LPair todo = new LinkedList; // this is still non-probabilistic MultiSetMap> 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 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 l) { fOr (B b : l) newB(b); } public synchronized Pair next() { Pair 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 getAs() { ret aList; } L getBs() { ret bList; } }