172 |
|
* class MapReducer<E> extends CountedCompleter { |
173 |
|
* final E[] array; final MyMapper<E> mapper; |
174 |
|
* final MyReducer<E> reducer; final int lo, hi; |
175 |
< |
* MapReducer sibling; |
175 |
> |
* MapReducer leftSibling, rightSibling; |
176 |
|
* E result; |
177 |
|
* MapReducer(CountedCompleter p, E[] array, MyMapper<E> mapper, |
178 |
|
* MyReducer<E> reducer, int lo, int hi) { |
185 |
|
* int mid = (lo + hi) >>> 1; |
186 |
|
* MapReducer<E> left = new MapReducer(this, array, mapper, reducer, lo, mid); |
187 |
|
* MapReducer<E> right = new MapReducer(this, array, mapper, reducer, mid, hi); |
188 |
< |
* left.sibling = right; |
189 |
< |
* right.sibling = left; |
188 |
> |
* left.rightSibling = right; |
189 |
> |
* right.leftSibling = left; |
190 |
|
* setPendingCount(1); // only right is pending |
191 |
|
* right.fork(); |
192 |
|
* left.compute(); // directly execute left |
200 |
|
* public void onCompletion(CountedCompleter caller) { |
201 |
|
* if (caller != this) { |
202 |
|
* MapReducer<E> child = (MapReducer<E>)caller; |
203 |
< |
* MapReducer<E> sib = child.sibling; |
204 |
< |
* if (sib == null || sib.result == null) |
205 |
< |
* result = child.result; |
203 |
> |
* MapReducer<E> left = (t.leftSibling == null) ? t : t.leftSibling; |
204 |
> |
* MapReducer<E> right = (t.rightSibling == null) ? t : t.rightSibling; |
205 |
> |
* if (left == null) |
206 |
> |
* result = right.result; |
207 |
> |
* else if (right == null) |
208 |
> |
* result = left.result; |
209 |
|
* else |
210 |
< |
* result = reducer.apply(child.result, sib.result); |
210 |
> |
* result = reducer.apply(left.result, right.result); |
211 |
|
* } |
212 |
|
* } |
213 |
|
* |