sclass AllOnAllWithUpdates { new TreeMap aList; new TreeMap bList; LPair todo = new LinkedList; LPair todo2 = new LinkedList; synchronized void newA(A a) { aList.put(lastKeyOr0(aList)+1, a); addPair(todo, longRange_one(lastKey(aList)), longRange(0, lastKeyOr0(bList)+1)); } synchronized void newB(B b) { bList.put(lastKeyOr0(bList)+1, b); addPair(todo, longRange(0, lastKeyOr0(aList)+1), longRange_one(lastKey(bList))); } synchronized void updateA(A a) { removeValue(aList, a); newA(a); } synchronized void updateB(B b) { removeValue(bList, b); newB(b); } synchronized Pair next() { while licensed { while (empty(todo2)) { if (empty(todo)) null; Pair p = popFirst(todo); addAll(todo2, outerProduct_pairs(keys(subMap_longRange(aList, p.a)), keys(subMap_longRange(bList, p.b)))); } Pair p = popFirst(todo2); A a = aList.get(p.a); B b = bList.get(p.b); if (a != null && b != null) ret pair(a, b); } null; } }