private R handleLeft() { synchronized (root) { Connector<T, R> l = left; if (l == null) { return none(); } if (l.left == NONE && l.right == NONE && l.acc != NONE) { return l.drain(); } } if (source.tryAdvance(this)) { T first = this.a; T last = first; R acc = this.mapper.apply(first); while (source.tryAdvance(this)) { if (!this.mergeable.test(last, a)) return pushLeft(first, acc); last = a; acc = this.accumulator.apply(acc, last); } a = none(); return connectOne(first, acc, last); } return connectEmpty(); }
private R pushLeft(T first, R acc) { synchronized (root) { Connector<T, R> l = left; if (l == null) return acc; left = null; l.rhs = null; T laright = l.right; l.right = none(); if (l.acc == NONE) { l.acc = acc; l.left = first; return none(); } if (this.mergeable.test(laright, first)) { l.acc = this.combiner.apply(l.acc, acc); return l.drainLeft(); } if (l.left == NONE) { left = new Connector<>(null, acc, this); return l.drain(); } } return acc; }
private R connectOne(T first, R acc, T last) { synchronized (root) { Connector<T, R> l = left; if (l == null) { return pushRight(acc, last); } if (l.acc == NONE) { l.acc = acc; l.left = first; l.right = last; return connectEmpty(); } T laright = l.right; if (mergeable.test(laright, first)) { l.acc = combiner.apply(l.acc, acc); l.right = last; return connectEmpty(); } left = null; l.rhs = null; l.right = none(); if (l.left != NONE) { return pushRight(acc, last); } acc = pushRight(acc, last); if (acc != NONE) left = new Connector<>(null, acc, this); return l.drain(); } }
R drainLeft() { return left == NONE ? drain() : none(); }
R drainRight() { return right == NONE ? drain() : none(); } }