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

dev.marksman.collectionviews.ImmutableCrossJoinVector 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 ImmutableCrossJoinVector extends ConcreteVector>
        implements ImmutableNonEmptyVector> {

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

    private ImmutableCrossJoinVector(ImmutableNonEmptyVector first, ImmutableNonEmptyVector 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  ImmutableCrossJoinVector immutableCrossJoinVector(ImmutableNonEmptyVector first, ImmutableNonEmptyVector second) {
        return new ImmutableCrossJoinVector<>(first, second);
    }

}