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

dev.marksman.collectionviews.CrossJoinVector Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package dev.marksman.collectionviews;

import com.jnape.palatable.lambda.adt.hlist.Tuple2;

import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;

final class CrossJoinVector extends ConcreteVector>
        implements NonEmptyVector> {

    private final NonEmptyVector first;
    private final NonEmptyVector second;
    private final int size;
    private final int stride;

    private CrossJoinVector(NonEmptyVector first, NonEmptyVector second) {
        this.first = first;
        this.second = second;
        this.stride = second.size();
        this.size = first.size() * stride;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public Tuple2 unsafeGet(int index) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException();
        }
        return tuple(first.unsafeGet(index / stride),
                second.unsafeGet(index % stride));
    }

    static  CrossJoinVector crossJoinVector(NonEmptyVector first, NonEmptyVector second) {
        return new CrossJoinVector<>(first, second);
    }

}