ConcurrentHashMap 原始碼分析03之內部類ReduceTask

cerish404發表於2020-12-02

1.3.35 ReduceKeysTask

@SuppressWarnings("serial")
static final class ReduceKeysTask<K,V>
    extends BulkTask<K,V,K> {
    final BiFunction<? super K, ? super K, ? extends K> reducer;
    K result;
    ReduceKeysTask<K,V> rights, nextRight;
    ReduceKeysTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceKeysTask<K,V> nextRight,
         BiFunction<? super K, ? super K, ? extends K> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final K getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super K, ? super K, ? extends K> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceKeysTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            K r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                K u = p.key;
                r = (r == null) ? u : u == null ? r : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceKeysTask<K,V>
                    t = (ReduceKeysTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    K tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.36 ReduceValuesTask

@SuppressWarnings("serial")
static final class ReduceValuesTask<K,V>
    extends BulkTask<K,V,V> {
    final BiFunction<? super V, ? super V, ? extends V> reducer;
    V result;
    ReduceValuesTask<K,V> rights, nextRight;
    ReduceValuesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceValuesTask<K,V> nextRight,
         BiFunction<? super V, ? super V, ? extends V> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final V getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super V, ? super V, ? extends V> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceValuesTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            V r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                V v = p.val;
                r = (r == null) ? v : reducer.apply(r, v);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceValuesTask<K,V>
                    t = (ReduceValuesTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    V tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.37 ReduceEntriesTask

@SuppressWarnings("serial")
static final class ReduceEntriesTask<K,V>
    extends BulkTask<K,V,Map.Entry<K,V>> {
    final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
    Map.Entry<K,V> result;
    ReduceEntriesTask<K,V> rights, nextRight;
    ReduceEntriesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         ReduceEntriesTask<K,V> nextRight,
         BiFunction<Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.reducer = reducer;
    }
    public final Map.Entry<K,V> getRawResult() { return result; }
    public final void compute() {
        final BiFunction<Map.Entry<K,V>, Map.Entry<K,V>, ? extends Map.Entry<K,V>> reducer;
        if ((reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new ReduceEntriesTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, reducer)).fork();
            }
            Map.Entry<K,V> r = null;
            for (Node<K,V> p; (p = advance()) != null; )
                r = (r == null) ? p : reducer.apply(r, p);
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                ReduceEntriesTask<K,V>
                    t = (ReduceEntriesTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    Map.Entry<K,V> tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.38 MapReduceKeysTask

@SuppressWarnings("serial")
static final class MapReduceKeysTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<? super K, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceKeysTask<K,V,U> rights, nextRight;
    MapReduceKeysTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceKeysTask<K,V,U> nextRight,
         Function<? super K, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<? super K, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceKeysTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p.key)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceKeysTask<K,V,U>
                    t = (MapReduceKeysTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.39 MapReduceValuesTask

@SuppressWarnings("serial")
static final class MapReduceValuesTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<? super V, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceValuesTask<K,V,U> rights, nextRight;
    MapReduceValuesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceValuesTask<K,V,U> nextRight,
         Function<? super V, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<? super V, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceValuesTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p.val)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceValuesTask<K,V,U>
                    t = (MapReduceValuesTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.40 MapReduceEntriesTask

@SuppressWarnings("serial")
static final class MapReduceEntriesTask<K,V,U>
    extends BulkTask<K,V,U> {
    final Function<Map.Entry<K,V>, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceEntriesTask<K,V,U> rights, nextRight;
    MapReduceEntriesTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceEntriesTask<K,V,U> nextRight,
         Function<Map.Entry<K,V>, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final Function<Map.Entry<K,V>, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceEntriesTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceEntriesTask<K,V,U>
                    t = (MapReduceEntriesTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.41 MapReduceMappingsTask

@SuppressWarnings("serial")
static final class MapReduceMappingsTask<K,V,U>
    extends BulkTask<K,V,U> {
    final BiFunction<? super K, ? super V, ? extends U> transformer;
    final BiFunction<? super U, ? super U, ? extends U> reducer;
    U result;
    MapReduceMappingsTask<K,V,U> rights, nextRight;
    MapReduceMappingsTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceMappingsTask<K,V,U> nextRight,
         BiFunction<? super K, ? super V, ? extends U> transformer,
         BiFunction<? super U, ? super U, ? extends U> reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.reducer = reducer;
    }
    public final U getRawResult() { return result; }
    public final void compute() {
        final BiFunction<? super K, ? super V, ? extends U> transformer;
        final BiFunction<? super U, ? super U, ? extends U> reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceMappingsTask<K,V,U>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, reducer)).fork();
            }
            U r = null;
            for (Node<K,V> p; (p = advance()) != null; ) {
                U u;
                if ((u = transformer.apply(p.key, p.val)) != null)
                    r = (r == null) ? u : reducer.apply(r, u);
            }
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceMappingsTask<K,V,U>
                    t = (MapReduceMappingsTask<K,V,U>)c,
                    s = t.rights;
                while (s != null) {
                    U tr, sr;
                    if ((sr = s.result) != null)
                        t.result = (((tr = t.result) == null) ? sr :
                                    reducer.apply(tr, sr));
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.42 MapReduceKeysToDoubleTask

@SuppressWarnings("serial")
staticfinal class MapReduceKeysToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<? super K> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceKeysToDoubleTask<K,V> rights, nextRight;
    MapReduceKeysToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceKeysToDoubleTask<K,V> nextRight,
         ToDoubleFunction<? super K> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<? super K> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceKeysToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceKeysToDoubleTask<K,V>
                    t = (MapReduceKeysToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.43 MapReduceValuesToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceValuesToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<? super V> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceValuesToDoubleTask<K,V> rights, nextRight;
    MapReduceValuesToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceValuesToDoubleTask<K,V> nextRight,
         ToDoubleFunction<? super V> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<? super V> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceValuesToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.val));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceValuesToDoubleTask<K,V>
                    t = (MapReduceValuesToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.44 MapReduceEntriesToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceEntriesToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleFunction<Map.Entry<K,V>> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceEntriesToDoubleTask<K,V> rights, nextRight;
    MapReduceEntriesToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceEntriesToDoubleTask<K,V> nextRight,
         ToDoubleFunction<Map.Entry<K,V>> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleFunction<Map.Entry<K,V>> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceEntriesToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceEntriesToDoubleTask<K,V>
                    t = (MapReduceEntriesToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.45 MapReduceMappingsToDoubleTask

@SuppressWarnings("serial")
static final class MapReduceMappingsToDoubleTask<K,V>
    extends BulkTask<K,V,Double> {
    final ToDoubleBiFunction<? super K, ? super V> transformer;
    final DoubleBinaryOperator reducer;
    final double basis;
    double result;
    MapReduceMappingsToDoubleTask<K,V> rights, nextRight;
    MapReduceMappingsToDoubleTask
        (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t,
         MapReduceMappingsToDoubleTask<K,V> nextRight,
         ToDoubleBiFunction<? super K, ? super V> transformer,
         double basis,
         DoubleBinaryOperator reducer) {
        super(p, b, i, f, t); this.nextRight = nextRight;
        this.transformer = transformer;
        this.basis = basis; this.reducer = reducer;
    }
    public final Double getRawResult() { return result; }
    public final void compute() {
        final ToDoubleBiFunction<? super K, ? super V> transformer;
        final DoubleBinaryOperator reducer;
        if ((transformer = this.transformer) != null &&
            (reducer = this.reducer) != null) {
            double r = this.basis;
            for (int i = baseIndex, f, h; batch > 0 &&
                     (h = ((f = baseLimit) + i) >>> 1) > i;) {
                addToPendingCount(1);
                (rights = new MapReduceMappingsToDoubleTask<K,V>
                 (this, batch >>>= 1, baseLimit = h, f, tab,
                  rights, transformer, r, reducer)).fork();
            }
            for (Node<K,V> p; (p = advance()) != null; )
                r = reducer.applyAsDouble(r, transformer.applyAsDouble(p.key, p.val));
            result = r;
            CountedCompleter<?> c;
            for (c = firstComplete(); c != null; c = c.nextComplete()) {
                @SuppressWarnings("unchecked")
                MapReduceMappingsToDoubleTask<K,V>
                    t = (MapReduceMappingsToDoubleTask<K,V>)c,
                    s = t.rights;
                while (s != null) {
                    t.result = reducer.applyAsDouble(t.result, s.result);
                    s = t.rights = s.nextRight;
                }
            }
        }
    }
}

1.3.46 ToLong、ToInt

  • ToLong、ToInt 的任務類與 ToDouble 基本一致,只是型別不同,這裡不做贅述。

相關文章