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

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

package fj.data.fingertrees;

import fj.F;
import fj.F2;
import fj.P2;
import fj.P3;
import fj.data.Option;

import static fj.Function.curry;

/**
 * An inner node of the 2-3 tree.
 */
public abstract class Node {
  private final Measured m;
  private final V measure;

  public abstract  B foldRight(final F> f, final B z);

  public abstract  B foldLeft(final F> f, final B z);

  public static  F, B>> foldLeft_(final F> bff) {
    return curry((b, node) -> node.foldLeft(bff, b));
  }

  public static  F, B>> foldRight_(final F> aff) {
    return curry((b, node) -> node.foldRight(aff, b));
  }

  public final  Node map(final F f, final Measured m) {
    return match(
        node2 -> new Node2(m, node2.toVector().map(f)),
        node3 -> new Node3(m, node3.toVector().map(f))
    );
  }

  public static  F, Node> liftM(final F f, final Measured m) {
    return node -> node.map(f, m);
  }

  public abstract Digit toDigit();

  Node(final Measured m, final V measure) {
    this.m = m;
    this.measure = measure;
  }

  public final V measure() {
    return measure;
  }

  Measured measured() {
    return m;
  }

  abstract P3>, A, Option>> split1(final F predicate, final V acc);

  public abstract P2 lookup(final F o, final int i);

  public abstract  B match(final F, B> n2, final F, B> n3);

  public abstract int length();
}