Interface Vector<A>
-
- Type Parameters:
A
- the element type
- All Superinterfaces:
software.kes.enhancediterables.EnhancedIterable<A>
,software.kes.enhancediterables.FiniteIterable<A>
,com.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>
,java.lang.Iterable<A>
,java.util.RandomAccess
- All Known Subinterfaces:
ImmutableNonEmptyVector<A>
,ImmutableVector<A>
,NonEmptyVector<A>
public interface Vector<A> extends software.kes.enhancediterables.FiniteIterable<A>, java.util.RandomAccess
A finite, ordered view of a collection.A
Vector
guarantees the following:- A
size()
method that executes in O(1)*. - A
get(int)
method that retrieves an element by index in O(1)*. - An
isEmpty()
method that executes in O(1)*. - Iteration will always terminate.
- Protected from mutation by the bearer.
- The bearer cannot gain access to a reference to the underlying collection.
Additionally, the ability to transform the
Vector
to newVector
s, without affecting or copying the underlying collection, is provided through the following methods:fmap(com.jnape.palatable.lambda.functions.Fn1<? super A, ? extends B>)
take(int)
drop(int)
slice(int, int)
While
Vector
does implementIterable
, it does not implementCollection
. If you need to convert theVector
to ajava.util.Collection
, you will need to make a copy. Consider using Lambda'stoCollection
function to accomplish this.*The claim of O(1) means that the number of elements in the Vector has no bearing on performance. However, the number of transformations applied to the Vector, such as mapping and slicing, will. Technically, the complexity is O(k) where k is the number of transformations applied.
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description static <A> VectorBuilder<A>
builder()
Creates a newVectorBuilder
.static <A> VectorBuilder<A>
builder(int initialCapacity)
Creates a newVectorBuilder
with an initial capacity hint.static <A> ImmutableVector<A>
copyFrom(int maxCount, A[] source)
Returns a newImmutableVector
that is copied from an array.static <A> ImmutableVector<A>
copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableVector
that is copied from anyIterable
, but consuming a maximum number of elements.static <A> ImmutableVector<A>
copyFrom(A[] source)
Creates anImmutableVector
that is copied from an array.static <A> ImmutableVector<A>
copyFrom(java.lang.Iterable<A> source)
Creates anImmutableVector
that is copied from anyIterable
.static <A> ImmutableVector<A>
copySliceFrom(int startIndex, int endIndexExclusive, java.lang.Iterable<A> source)
Creates anImmutableVector
by copying a slice from anIterable
.default <B> Vector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>>
cross(Vector<B> other)
Returns the cartesian product of thisVector
with anotherVector
.default Vector<A>
drop(int count)
Returns a newVector
that drops the firstcount
elements of thisVector
.default Vector<A>
dropRight(int count)
Returns a newVector
that drops all except the lastcount
elements of thisVector
.static <A> ImmutableVector<A>
empty()
Returns an emptyImmutableVector
.static <A> ImmutableVector<A>
fill(int size, A value)
Creates anImmutableVector
that repeats the same elementsize
times.default com.jnape.palatable.lambda.adt.Maybe<java.lang.Integer>
findIndex(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Finds the first element of thisVector
that satisfies a predicate, if any, and returns its index.default <B> Vector<B>
fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisVector
.default com.jnape.palatable.lambda.adt.Maybe<A>
get(int index)
Gets an element from thisVector
at an index.default ImmutableVector<java.lang.Integer>
indices()
Returns anImmutableVector<Integer>
that contains all the indices of thisVector
.default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>>
inits()
Returns aNonEmptyIterable
containing the inits of thisVector
.default boolean
isEmpty()
Tests whether thisVector
is empty.default java.util.Iterator<A>
iterator()
Returns an iterator over thisVector
's elements.static <A> ImmutableVector<A>
lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)
Creates anImmutableVector
where elements are lazily evaluated.static <A> ImmutableNonEmptyVector<A>
of(A first, A... more)
Creates aImmutableNonEmptyVector
with the given elements.static ImmutableVector<java.lang.Integer>
range(int size)
Creates anImmutableVector<Integer>
containing elements 0..size - 1
.default Vector<A>
reverse()
Creates aVector
with thisVector
's elements in reversed order.int
size()
Returns the size of thisVector
.default Vector<A>
slice(int startIndex, int endIndexExclusive)
Creates a slice of thisVector
.default software.kes.enhancediterables.FiniteIterable<? extends NonEmptyVector<A>>
slide(int k)
"Slides" a window ofk
elements across theVector
by one element at a time.default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends Vector<A>,? extends Vector<A>>
splitAt(int index)
Splits thisVector
into two at a given position.default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>>
tails()
Returns aNonEmptyIterable
containing the tails of thisVector
.default Vector<A>
take(int count)
Returns a newVector
containing at most the firstcount
elements of thisVector
.default Vector<A>
takeRight(int count)
Returns a newVector
containing at most the lastcount
elements of thisVector
.default ImmutableVector<A>
toImmutable()
Converts thisVector
to anImmutableVector
.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>>
toNonEmpty()
Attempts to convert thisVector
to aNonEmptyVector
.default NonEmptyVector<A>
toNonEmptyOrThrow()
Attempts to convert thisVector
to aNonEmptyVector
.A
unsafeGet(int index)
Gets an element from thisVector
at an index.static <A> Vector<A>
wrap(A[] underlying)
Creates aVector
that wraps an array.static <A> Vector<A>
wrap(java.util.List<A> underlying)
Creates aVector
that wraps ajava.util.List
.default <B,C>
Vector<C>zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, Vector<B> other)
Zips together thisVector
with anotherVector
by applying a zipping function.default Vector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>>
zipWithIndex()
Zips thisVector
with its indices.-
Methods inherited from interface software.kes.enhancediterables.EnhancedIterable
concat, concat, find, toArray, toCollection, zipWith, zipWith
-
-
-
-
Method Detail
-
size
int size()
Returns the size of thisVector
.Executes in O(1).
- Specified by:
size
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Returns:
- the number of elements in this
Vector
-
unsafeGet
A unsafeGet(int index)
Gets an element from thisVector
at an index.Executes in O(1).
- Parameters:
index
- the index of the element to retrieve. Must be between 0 andsize() - 1
, otherwise will throw anIndexOutOfBoundsException
- Returns:
- the element at
index
. May be null, if the underlying data contains a null at that index. - Throws:
java.lang.IndexOutOfBoundsException
- if index is less than 0 or greater than or equal tosize()
-
cross
default <B> Vector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,B>> cross(Vector<B> other)
Returns the cartesian product of thisVector
with anotherVector
.Does not make copies of any underlying collections.
The returned
Vector
will have a size ofsize()
×other.size()
, but will allocate no extra memory (aside from a few bytes for housekeeping).- Type Parameters:
B
- the type of the otherVector
- Parameters:
other
- aVector
of any type- Returns:
- a
Vector<Tuple2<A, B>>
-
drop
default Vector<A> drop(int count)
Returns a newVector
that drops the firstcount
elements of thisVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed.- Specified by:
drop
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
drop
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Parameters:
count
- the number of elements to drop from thisVector
. Must be >= 0. May exceed size of thisVector
, in which case, the result will be an emptyVector
.- Returns:
- a
Vector<A>
-
dropRight
default Vector<A> dropRight(int count)
Returns a newVector
that drops all except the lastcount
elements of thisVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed.- Parameters:
count
- the number of elements to drop from the end of thisVector
. Must be >= 0. May exceed size of thisVector
, in which case, the result will be an emptyVector
.- Returns:
- a
Vector<A>
-
findIndex
default com.jnape.palatable.lambda.adt.Maybe<java.lang.Integer> findIndex(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends java.lang.Boolean> predicate)
Finds the first element of thisVector
that satisfies a predicate, if any, and returns its index.- Parameters:
predicate
- a predicate; not null- Returns:
- an index wrapped in a
Maybe.just(A)
if a matching element is found;Maybe.nothing()
otherwise.
-
fmap
default <B> Vector<B> fmap(com.jnape.palatable.lambda.functions.Fn1<? super A,? extends B> f)
Maps a function over thisVector
.Returns a new
Vector
of the same size (but possibly a different type).Does not make any copies of underlying collections.
This method is stack-safe, so a
Vector
can be mapped as many times as the heap permits.- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Specified by:
fmap
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Specified by:
fmap
in interfacecom.jnape.palatable.lambda.functor.Functor<A,software.kes.enhancediterables.EnhancedIterable<?>>
- Type Parameters:
B
- The type of the elements contained in the output Vector.- Parameters:
f
- a function fromA
toB
. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- a
Vector<B>
of the same size
-
get
default com.jnape.palatable.lambda.adt.Maybe<A> get(int index)
Gets an element from thisVector
at an index.Executes in O(1). Will never return null.
- Parameters:
index
- the index of the element to retrieve- Returns:
- an element wrapped in a
Maybe.just(A)
if the index is in range and the element is not null.Maybe.nothing()
otherwise.
-
indices
default ImmutableVector<java.lang.Integer> indices()
Returns anImmutableVector<Integer>
that contains all the indices of thisVector
.- Returns:
- an
ImmutableVector<Integer>
-
inits
default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>> inits()
Returns aNonEmptyIterable
containing the inits of thisVector
.The first value will be this
Vector
and the final one will be an emptyVector
, with the intervening values the results of successive applications ofinit
.- Specified by:
inits
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Returns:
- a
NonEmptyIterable
over all the inits of thisVector
-
isEmpty
default boolean isEmpty()
Tests whether thisVector
is empty.Executes in O(1).
- Specified by:
isEmpty
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Returns:
- true if this
Vector
is empty, false otherwise.
-
iterator
default java.util.Iterator<A> iterator()
Returns an iterator over thisVector
's elements.- Specified by:
iterator
in interfacejava.lang.Iterable<A>
- Returns:
- an Iterator
-
reverse
default Vector<A> reverse()
Creates aVector
with thisVector
's elements in reversed order.Does not make copies of any underlying collections.
- Specified by:
reverse
in interfacesoftware.kes.enhancediterables.FiniteIterable<A>
- Returns:
- a
Vector<A>
-
slice
default Vector<A> slice(int startIndex, int endIndexExclusive)
Creates a slice of thisVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, usecopySliceFrom(int, int, java.lang.Iterable<A>)
instead.- Parameters:
startIndex
- the index of the element to begin the slice. Must be >= 0. May exceed the size of thisVector
, in which case an emptyVector
will be returned.endIndexExclusive
- the end index (exclusive) of the slice. Must be >=startIndex
. May exceed the size of thisVector
, in which case the slice will contain as many elements as available.- Returns:
- a
Vector<A>
-
slide
default software.kes.enhancediterables.FiniteIterable<? extends NonEmptyVector<A>> slide(int k)
"Slides" a window ofk
elements across theVector
by one element at a time.Example:
Vector.of(1, 2, 3, 4, 5).slide(2); // [[1, 2], [2, 3], [3, 4], [4, 5]]
-
splitAt
default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends Vector<A>,? extends Vector<A>> splitAt(int index)
Splits thisVector
into two at a given position.Does not make copies of any underlying collections.
Note that
vector.splitAt(n)
is equivalent to, but possibly more efficient thantuple(vector.take(n), vector.drop(n))
- Parameters:
index
- the position at which to split. Must be >= 0;- Returns:
- a
Tuple2
contains ofVector
s, one of which containing the firstindex
elements, the second containing the other elements.
-
tails
default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>> tails()
Returns aNonEmptyIterable
containing the tails of thisVector
.The first value will be this
Vector
and the final one will be an emptyVector
, with the intervening values the results of successive applications oftail
.
-
take
default Vector<A> take(int count)
Returns a newVector
containing at most the firstcount
elements of thisVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, usecopyFrom(int, Iterable)
instead.- Specified by:
take
in interfacesoftware.kes.enhancediterables.EnhancedIterable<A>
- Parameters:
count
- the maximum number of elements to take from thisVector
. Must be >= 0. May exceed size of thisVector
.- Returns:
- a
Vector<A>
-
takeRight
default Vector<A> takeRight(int count)
Returns a newVector
containing at most the lastcount
elements of thisVector
.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vector
that you no longer need. The smaller slice will hold onto a reference of the larger one, and will prevent it from being GC'ed. To avoid this situation, usecopyFrom(int, Iterable)
instead.- Parameters:
count
- the maximum number of elements to take from thisVector
. Must be >= 0. May exceed size of thisVector
.- Returns:
- a
Vector<A>
-
toImmutable
default ImmutableVector<A> toImmutable()
Converts thisVector
to anImmutableVector
.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
Vector
is already anImmutableVector
, no copies are made and this method is a no-op.- Returns:
- an
ImmutableVector
of the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>> toNonEmpty()
Attempts to convert thisVector
to aNonEmptyVector
.If successful, returns a
NonEmptyVector
containing the same elements as this one, wrapped in aMaybe.just(A)
.If this
Vector
is empty, returnsMaybe.nothing()
.Does not make copies of any underlying collections.
-
toNonEmptyOrThrow
default NonEmptyVector<A> toNonEmptyOrThrow()
Attempts to convert thisVector
to aNonEmptyVector
.If successful, returns a
NonEmptyVector
containing the same elements as this one. Use this if you are confident that thisVector
is not empty.If this
Vector
is empty, throws anIllegalArgumentException
.Does not make copies of any underlying collections.
- Returns:
- a
NonEmptyVector<A>
- Throws:
java.lang.IllegalArgumentException
- if thisVector
is empty
-
zipWith
default <B,C> Vector<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, Vector<B> other)
Zips together thisVector
with anotherVector
by applying a zipping function.Applies the function to the successive elements of of each
Vector
until one of them runs out of elements.Does not make copies of any underlying collections.
- Type Parameters:
B
- The element type of the otherVector
C
- The element type of the result- Parameters:
fn
- The zipping function. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.other
- The otherVector
- Returns:
- A
Vector<C>
-
zipWithIndex
default Vector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>> zipWithIndex()
Zips thisVector
with its indices.Does not make copies of any underlying collections.
- Returns:
- a new
Vector
containing pairs consisting of all elements of thisVector
paired with their index. Indices start at 0.
-
empty
static <A> ImmutableVector<A> empty()
Returns an emptyImmutableVector
.- Type Parameters:
A
- the element type- Returns:
- an empty
ImmutableVector<A>
-
of
@SafeVarargs static <A> ImmutableNonEmptyVector<A> of(A first, A... more)
Creates aImmutableNonEmptyVector
with the given elements.- Type Parameters:
A
- the element type- Parameters:
first
- the first elementmore
- the remaining elements- Returns:
- an
ImmutableNonEmptyVector<A>
-
builder
static <A> VectorBuilder<A> builder()
Creates a newVectorBuilder
.- Type Parameters:
A
- the element type- Returns:
- an empty
VectorBuilder
-
builder
static <A> VectorBuilder<A> builder(int initialCapacity)
Creates a newVectorBuilder
with an initial capacity hint.- Type Parameters:
A
- the element type- Parameters:
initialCapacity
- an initial capacity hint. Must be >= 0.- Returns:
- an empty
VectorBuilder
-
fill
static <A> ImmutableVector<A> fill(int size, A value)
Creates anImmutableVector
that repeats the same elementsize
times.Uses O(1) memory.
See
NonEmptyVector.fill(int, A)
if you require anImmutableNonEmptyVector
to be returned.- Type Parameters:
A
- the element type- Parameters:
size
- the number of elements. Must be >= 0.value
- the value that will be repeated for all elements of theImmutableVector
- Returns:
- an
ImmutableVector<A>
ofsize
elements, with each element having the valuevalue
-
lazyFill
static <A> ImmutableVector<A> lazyFill(int size, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer,A> valueSupplier)
Creates anImmutableVector
where elements are lazily evaluated.Uses O(1) memory.
See
NonEmptyVector.lazyFill(int, com.jnape.palatable.lambda.functions.Fn1<java.lang.Integer, A>)
if you require aImmutableNonEmptyVector
to be returned.- Type Parameters:
A
- the element type- Parameters:
size
- the number of elements. Must be >= 0.valueSupplier
- a function that accepts an index and returns the computed value for that index. Not null. This function should be referentially transparent and not perform side-effects. It may be called zero or more times for each element.- Returns:
- an
ImmutableVector<A>
-
range
static ImmutableVector<java.lang.Integer> range(int size)
Creates anImmutableVector<Integer>
containing elements 0..size - 1
. In other words, each element of the returnedVector
will contains its index.Uses O(1) memory.
- Parameters:
size
- the number of elements. Must be >= 0. If 0, the returnedImmutableVector
will be empty.- Returns:
- an
ImmutableVector<Integer>
-
wrap
static <A> Vector<A> wrap(A[] underlying)
Creates aVector
that wraps an array.Does not make any copies of the given array. The created
Vector
will hold on to a reference to the array, but will never alter it in any way.Bearers of the created
Vector
will be unable to gain access to the underlying array, so it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the array can still mutate it. Use
copyFrom(java.lang.Iterable<A>)
instead if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
- array to wrap; not null- Returns:
- a
Vector<A>
-
wrap
static <A> Vector<A> wrap(java.util.List<A> underlying)
Creates aVector
that wraps ajava.util.List
.Does not make any copies of the given
List
. The createdVector
will hold a reference to the givenList
, but will not alter it in any way.Bearers of the created
Vector
will be unable to gain access to the underlyingList
, so it is safe to share.Since no copy is made, be aware that anyone that holds a direct reference to the
List
can still mutate it. Mutating theList
is not advised. Operations that change the size of the underlyingList
will result in unpredictable behavior. UsecopyFrom(java.lang.Iterable<A>)
if you want to avoid this situation.- Type Parameters:
A
- the element type- Parameters:
underlying
-List
to wrap; not null- Returns:
- a
Vector<A>
-
copyFrom
static <A> ImmutableVector<A> copyFrom(java.lang.Iterable<A> source)
Creates anImmutableVector
that is copied from anyIterable
.The entire
Iterable
will be eagerly iterated. Be careful not to pass in an infiniteIterable
or this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
source
is an untransformedImmutableVector
, it will be returned directly.- Type Parameters:
A
- the element type- Parameters:
source
- anIterable<A>
that will be iterated eagerly in its entirety; not null- Returns:
- an
ImmutableVector<A>
-
copyFrom
static <A> ImmutableVector<A> copyFrom(A[] source)
Creates anImmutableVector
that is copied from an array.- Type Parameters:
A
- the element type- Parameters:
source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableVector<A>
-
copyFrom
static <A> ImmutableVector<A> copyFrom(int maxCount, java.lang.Iterable<A> source)
Creates anImmutableVector
that is copied from anyIterable
, but consuming a maximum number of elements.The
Iterable
will be eagerly iterated, but only up to a maximum ofmaxCount
elements. IfmaxCount
elements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
source
is an untransformedImmutableVector
and its size is less than or equal tomaxCount
, in which case it will be returned directly.If
source
is anImmutableVector
that is greater thanmaxCount
in size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVector
that you no longer need.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to consume from the source. Must be >= 0.source
- anIterable<A>
that will be iterated eagerly for up tomaxCount
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableVector
that contains at mostmaxCount
elements
-
copyFrom
static <A> ImmutableVector<A> copyFrom(int maxCount, A[] source)
Returns a newImmutableVector
that is copied from an array.- Type Parameters:
A
- the element type- Parameters:
maxCount
- the maximum number of elements to copy from the array. Must be >= 0.source
- the array to copy from. Not null. This method will not alter or hold on to a reference of this array.- Returns:
- an
ImmutableVector<A>
-
copySliceFrom
static <A> ImmutableVector<A> copySliceFrom(int startIndex, int endIndexExclusive, java.lang.Iterable<A> source)
Creates anImmutableVector
by copying a slice from anIterable
.The
Iterable
will be eagerly iterated, but only for the number of elements needed to fulfill the requested slice. If not enough elements are not available, then this method yields as many elements that were available.This method will make a copy of the data provided, except in the case
startIndex
is 0 andsource
is anImmutableVector
whose size is less than or equal tocount
, in which case it will be returned directly.It is memory-safe to use this method to take a small slice of a huge
Vector
that you no longer need.- Type Parameters:
A
- the element type- Parameters:
startIndex
- the index of the element to begin the slice. Must be >= 0. May exceed the size of theIterable
.endIndexExclusive
- the end index (exclusive) of the slice. Must be >=startIndex
. May exceed the size of theIterable
.source
- anIterable<A>
that will be iterated eagerly for up toendIndexExclusive
elements. Not null. It is safe forsource
to be infinite.- Returns:
- an
ImmutableVector<A>
-
-