/**
  * このベクトルに対して二項演算を行う。
  *
  * <p>デフォルトの実装では、渡された演算をそれぞれの次元に適用し、得られた値を{@link #set(Object, Object, Object, Object)}に委譲します。
  *
  * @param operator 現在の設定値を受け取り、新しい設定値を返す関数。
  * @param x 右辺となるx成分の値。
  * @param y 右辺となるy成分の値。
  * @param z 右辺となるz成分の値。
  * @param w 右辺となるw成分の値。
  */
 default void calculate(BinaryOperator<T> operator, T x, T y, T z, T w) {
   set(
       operator.apply(getX(), x),
       operator.apply(getY(), y),
       operator.apply(getZ(), z),
       operator.apply(getW(), w));
 }
 // l + <first|acc|last> + r
 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();
   }
 }
 // <?|acc|last> + r
 private R pushRight(R acc, T last) {
   cur = none();
   if (right == null) return acc;
   synchronized (root) {
     Connector<T, R> r = right;
     if (r == null) return acc;
     right = null;
     r.lhs = null;
     T raleft = r.left;
     r.left = none();
     if (r.acc == NONE) {
       if (acc == NONE) {
         r.drain();
       } else {
         r.acc = acc;
         r.right = last;
       }
       return none();
     }
     if (acc == NONE) {
       return r.drainRight();
     }
     if (mergeable.test(last, raleft)) {
       r.acc = combiner.apply(acc, r.acc);
       return r.drainRight();
     }
     if (r.right == NONE) right = new Connector<>(this, r.drain(), null);
     return acc;
   }
 }
Esempio n. 4
0
  default U reduce(U identity, BinaryOperator<U> accumulator) {
    if (getLastActive().isSequential()) {
      Object[] result = {identity};
      forEach(
          r -> {
            if (result[0] == null) result[0] = r;
            else {
              result[0] = accumulator.apply((U) result[0], r);
            }
          });
      return (U) result[0];
    }
    Function<FastFuture, U> safeJoin =
        (FastFuture cf) -> (U) BlockingStreamHelper.getSafe(cf, getErrorHandler());
    IncrementalReducer<U> collector =
        new IncrementalReducer(
            this.getLazyCollector().get().withResults(new ArrayList<>()),
            this,
            getParallelReduction());
    Object[] result = {identity};
    try {
      this.getLastActive()
          .injectFutures()
          .forEach(
              next -> {
                collector.getConsumer().accept(next);

                result[0] = collector.reduce(safeJoin, (U) result[0], accumulator);
              });
    } catch (SimpleReactProcessingException e) {

    }
    return collector.reduceResults(
        collector.getConsumer().getAllResults(), safeJoin, (U) result[0], accumulator);
  }
Esempio n. 5
0
 private T evaluate(int level) {
   Deque<T> q = elements[level];
   BinaryOperator<T> op = operations[level];
   T result = q.pollFirst();
   while (!q.isEmpty()) {
     result = op.apply(result, q.pollFirst());
   }
   return result;
 }
 // l + r
 private R connectEmpty() {
   synchronized (root) {
     Connector<T, R> l = left, r = right;
     if (l == null) {
       return pushRight(none(), none());
     }
     left = right = null;
     l.rhs = null;
     T laright = l.right;
     l.right = none();
     if (l.acc == NONE) {
       if (r == null) l.drain();
       else {
         if (l.lhs != null) {
           l.lhs.right = r;
           r.lhs = l.lhs;
         }
       }
       return none();
     }
     if (r == null) {
       return l.drainLeft();
     }
     r.lhs = null;
     if (r.acc == NONE) {
       if (r.rhs != null) {
         r.rhs.left = l;
         l.rhs = r.rhs;
         l.right = laright;
       }
       return none();
     }
     T raleft = r.left;
     r.left = none();
     if (mergeable.test(laright, raleft)) {
       R acc = combiner.apply(l.acc, r.acc);
       if (l.left == NONE && r.right == NONE) {
         l.drain();
         r.drain();
         return acc;
       }
       l.acc = acc;
       l.right = r.right;
       if (r.rhs != null) {
         r.rhs.left = l;
         l.rhs = r.rhs;
       }
       return none();
     }
     if (l.left == NONE) {
       if (r.right == NONE) right = new Connector<>(this, r.drain(), null);
       return l.drain();
     }
     return r.drainRight();
   }
 }
 @Override
 public Set<Integer> onIntermediateResult(Address address, R results) {
   if (results != null) {
     synchronized (this) {
       if (currentValue != null) {
         currentValue = binaryOperator.apply(currentValue, results);
       } else {
         currentValue = results;
       }
     }
   }
   return null;
 }
Esempio n. 8
0
  default Optional<U> reduce(BinaryOperator<U> accumulator) {
    if (getLastActive().isSequential()) {
      Object[] result = {null};
      forEach(
          r -> {
            if (result[0] == null) result[0] = r;
            else {
              result[0] = accumulator.apply((U) result[0], r);
            }
          });
      return (Optional) Optional.ofNullable(result[0]);
    }
    Function<FastFuture, U> safeJoin =
        (FastFuture cf) -> (U) BlockingStreamHelper.getSafe(cf, getErrorHandler());
    IncrementalReducer<U> collector =
        new IncrementalReducer(
            this.getLazyCollector().get().withResults(new ArrayList<>()),
            this,
            getParallelReduction());
    Optional[] result = {Optional.empty()};
    try {
      this.getLastActive()
          .injectFutures()
          .forEach(
              next -> {
                collector.getConsumer().accept(next);
                if (!result[0].isPresent()) result[0] = collector.reduce(safeJoin, accumulator);
                else result[0] = result[0].map(v -> collector.reduce(safeJoin, (U) v, accumulator));
              });
    } catch (SimpleReactProcessingException e) {

    }

    if (result[0].isPresent())
      return result[0].map(
          v ->
              collector.reduceResults(
                  collector.getConsumer().getAllResults(), safeJoin, (U) v, accumulator));

    return collector.reduceResults(collector.getConsumer().getAllResults(), safeJoin, accumulator);
  }
Esempio n. 9
0
 public void test() {
   BinaryOperator<Long> operator = (l1, l2) -> l1 + l2;
   System.out.println(operator.apply(1L, 2L));
 }