All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fj.data.fingertrees.Three Maven / Gradle / Ivy

Go to download

Functional Java is an open source library that supports closures for the Java programming language

There is a newer version: 5.0
Show newest version
package fj.data.fingertrees;

import fj.P;
import fj.P2;
import fj.P3;
import fj.Show;
import fj.data.Option;
import fj.data.Stream;
import fj.data.vector.V3;
import fj.F;

import static fj.data.Option.none;
import static fj.data.Option.some;
import static fj.data.fingertrees.FingerTree.mkTree;

/**
 * A three-element prefix or suffix of a finger tree.
 */
public final class Three extends Digit {
  private final V3 as;

  Three(final Measured m, final V3 as) {
    super(m);
    this.as = as;
  }

  public  B foldRight(final F> aff, final B z) {
    return aff.f(as._1()).f(aff.f(as._2()).f(aff.f(as._3()).f(z)));
  }

  public  B foldLeft(final F> bff, final B z) {
    return as.toStream().foldLeft(bff, z);
  }

  @Override public  B match(
      final F, B> one, final F, B> two, final F, B> three,
      final F, B> four) {
    return three.f(this);
  }

  /**
   * Returns the elements of this digit as a vector.
   *
   * @return the elements of this digit as a vector.
   */
  public V3 values() {
    return as;
  }

  @Override P3>, A, Option>> split1(final F predicate, final V acc) {
    final Measured m = measured();
    final MakeTree mk = mkTree(m);
    final F measure = m.measure();
    final V acc1 = m.sum(acc, measure.f(as._1()));
    if (predicate.f(acc1)) {
      return P.p(none(), as._1(), some(mk.two(as._2(), as._3())));
    } else if (predicate.f(m.sum(acc1, measure.f(as._2())))) {
      return P.p(some(mk.one(as._1())), as._2(), some(mk.one(as._3())));
    } else {
      return P.p(some(mk.two(as._1(), as._2())), as._3(), none());
    }
  }

  @Override public P2 lookup(F o, int i) {
    final F m = measured().measure();
    final int s1 = o.f(m.f(as._1()));
    if (i < s1) {
      return P.p(i, as._1());
    } else {
      final int s2 = s1 + o.f(m.f(as._2()));
      if (i < s2) {
        return P.p(i - s1, as._2());
      } else {
        return P.p(i - s2, as._3());
      }
    }
  }

    @Override
    public int length() {
        return 3;
    }

  public String toString() {
    return Show.digitShow(Show.anyShow(), Show.anyShow()).showS(this);
  }
  public Stream toStream() {
    return values().toStream();
  }

}