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.RandomAccessA finite, ordered view of a collection.A
Vectorguarantees 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
Vectorto newVectors, 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
Vectordoes implementIterable, it does not implementCollection. If you need to convert theVectorto ajava.util.Collection, you will need to make a copy. Consider using Lambda'stoCollectionfunction 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 newVectorBuilderwith an initial capacity hint.static <A> ImmutableVector<A>copyFrom(int maxCount, A[] source)Returns a newImmutableVectorthat is copied from an array.static <A> ImmutableVector<A>copyFrom(int maxCount, java.lang.Iterable<A> source)Creates anImmutableVectorthat is copied from anyIterable, but consuming a maximum number of elements.static <A> ImmutableVector<A>copyFrom(A[] source)Creates anImmutableVectorthat is copied from an array.static <A> ImmutableVector<A>copyFrom(java.lang.Iterable<A> source)Creates anImmutableVectorthat is copied from anyIterable.static <A> ImmutableVector<A>copySliceFrom(int startIndex, int endIndexExclusive, java.lang.Iterable<A> source)Creates anImmutableVectorby 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 thisVectorwith anotherVector.default Vector<A>drop(int count)Returns a newVectorthat drops the firstcountelements of thisVector.default Vector<A>dropRight(int count)Returns a newVectorthat drops all except the lastcountelements of thisVector.static <A> ImmutableVector<A>empty()Returns an emptyImmutableVector.static <A> ImmutableVector<A>fill(int size, A value)Creates anImmutableVectorthat repeats the same elementsizetimes.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 thisVectorthat 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 thisVectorat 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 aNonEmptyIterablecontaining the inits of thisVector.default booleanisEmpty()Tests whether thisVectoris 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 anImmutableVectorwhere elements are lazily evaluated.static <A> ImmutableNonEmptyVector<A>of(A first, A... more)Creates aImmutableNonEmptyVectorwith the given elements.static ImmutableVector<java.lang.Integer>range(int size)Creates anImmutableVector<Integer>containing elements 0..size - 1.default Vector<A>reverse()Creates aVectorwith thisVector's elements in reversed order.intsize()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 ofkelements across theVectorby one element at a time.default com.jnape.palatable.lambda.adt.hlist.Tuple2<? extends Vector<A>,? extends Vector<A>>splitAt(int index)Splits thisVectorinto two at a given position.default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>>tails()Returns aNonEmptyIterablecontaining the tails of thisVector.default Vector<A>take(int count)Returns a newVectorcontaining at most the firstcountelements of thisVector.default Vector<A>takeRight(int count)Returns a newVectorcontaining at most the lastcountelements of thisVector.default ImmutableVector<A>toImmutable()Converts thisVectorto anImmutableVector.default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>>toNonEmpty()Attempts to convert thisVectorto aNonEmptyVector.default NonEmptyVector<A>toNonEmptyOrThrow()Attempts to convert thisVectorto aNonEmptyVector.AunsafeGet(int index)Gets an element from thisVectorat an index.static <A> Vector<A>wrap(A[] underlying)Creates aVectorthat wraps an array.static <A> Vector<A>wrap(java.util.List<A> underlying)Creates aVectorthat 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 thisVectorwith anotherVectorby applying a zipping function.default Vector<com.jnape.palatable.lambda.adt.hlist.Tuple2<A,java.lang.Integer>>zipWithIndex()Zips thisVectorwith 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:
sizein interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Returns:
- the number of elements in this
Vector
-
unsafeGet
A unsafeGet(int index)
Gets an element from thisVectorat 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 thisVectorwith anotherVector.Does not make copies of any underlying collections.
The returned
Vectorwill 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- aVectorof any type- Returns:
- a
Vector<Tuple2<A, B>>
-
drop
default Vector<A> drop(int count)
Returns a newVectorthat drops the firstcountelements of thisVector.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vectorthat 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:
dropin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Specified by:
dropin 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 newVectorthat drops all except the lastcountelements of thisVector.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vectorthat 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 thisVectorthat 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
Vectorof the same size (but possibly a different type).Does not make any copies of underlying collections.
This method is stack-safe, so a
Vectorcan be mapped as many times as the heap permits.- Specified by:
fmapin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Specified by:
fmapin interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Specified by:
fmapin 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 fromAtoB. 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 thisVectorat 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 aNonEmptyIterablecontaining the inits of thisVector.The first value will be this
Vectorand the final one will be an emptyVector, with the intervening values the results of successive applications ofinit.- Specified by:
initsin interfacesoftware.kes.enhancediterables.FiniteIterable<A>- Returns:
- a
NonEmptyIterableover all the inits of thisVector
-
isEmpty
default boolean isEmpty()
Tests whether thisVectoris empty.Executes in O(1).
- Specified by:
isEmptyin interfacesoftware.kes.enhancediterables.EnhancedIterable<A>- Returns:
- true if this
Vectoris empty, false otherwise.
-
iterator
default java.util.Iterator<A> iterator()
Returns an iterator over thisVector's elements.- Specified by:
iteratorin interfacejava.lang.Iterable<A>- Returns:
- an Iterator
-
reverse
default Vector<A> reverse()
Creates aVectorwith thisVector's elements in reversed order.Does not make copies of any underlying collections.
- Specified by:
reversein 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
Vectorthat 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 emptyVectorwill 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 ofkelements across theVectorby 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 thisVectorinto 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
Tuple2contains ofVectors, one of which containing the firstindexelements, the second containing the other elements.
-
tails
default software.kes.enhancediterables.ImmutableNonEmptyFiniteIterable<? extends Vector<A>> tails()
Returns aNonEmptyIterablecontaining the tails of thisVector.The first value will be this
Vectorand 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 newVectorcontaining at most the firstcountelements of thisVector.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vectorthat 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:
takein 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 newVectorcontaining at most the lastcountelements of thisVector.Does not make copies of any underlying collections.
Use caution when taking a small slice of a huge
Vectorthat 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 thisVectorto anImmutableVector.This method will make a copy of the underlying data structure if necessary to guarantee immutability.
If this
Vectoris already anImmutableVector, no copies are made and this method is a no-op.- Returns:
- an
ImmutableVectorof the same type and containing the same elements
-
toNonEmpty
default com.jnape.palatable.lambda.adt.Maybe<? extends NonEmptyVector<A>> toNonEmpty()
Attempts to convert thisVectorto aNonEmptyVector.If successful, returns a
NonEmptyVectorcontaining the same elements as this one, wrapped in aMaybe.just(A).If this
Vectoris empty, returnsMaybe.nothing().Does not make copies of any underlying collections.
-
toNonEmptyOrThrow
default NonEmptyVector<A> toNonEmptyOrThrow()
Attempts to convert thisVectorto aNonEmptyVector.If successful, returns a
NonEmptyVectorcontaining the same elements as this one. Use this if you are confident that thisVectoris not empty.If this
Vectoris empty, throws anIllegalArgumentException.Does not make copies of any underlying collections.
- Returns:
- a
NonEmptyVector<A> - Throws:
java.lang.IllegalArgumentException- if thisVectoris empty
-
zipWith
default <B,C> Vector<C> zipWith(com.jnape.palatable.lambda.functions.Fn2<A,B,C> fn, Vector<B> other)
Zips together thisVectorwith anotherVectorby applying a zipping function.Applies the function to the successive elements of of each
Vectoruntil one of them runs out of elements.Does not make copies of any underlying collections.
- Type Parameters:
B- The element type of the otherVectorC- 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 thisVectorwith its indices.Does not make copies of any underlying collections.
- Returns:
- a new
Vectorcontaining pairs consisting of all elements of thisVectorpaired 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 aImmutableNonEmptyVectorwith 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 newVectorBuilderwith 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 anImmutableVectorthat repeats the same elementsizetimes.Uses O(1) memory.
See
NonEmptyVector.fill(int, A)if you require anImmutableNonEmptyVectorto 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>ofsizeelements, 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 anImmutableVectorwhere 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 aImmutableNonEmptyVectorto 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 returnedVectorwill contains its index.Uses O(1) memory.
- Parameters:
size- the number of elements. Must be >= 0. If 0, the returnedImmutableVectorwill be empty.- Returns:
- an
ImmutableVector<Integer>
-
wrap
static <A> Vector<A> wrap(A[] underlying)
Creates aVectorthat wraps an array.Does not make any copies of the given array. The created
Vectorwill hold on to a reference to the array, but will never alter it in any way.Bearers of the created
Vectorwill 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 aVectorthat wraps ajava.util.List.Does not make any copies of the given
List. The createdVectorwill hold a reference to the givenList, but will not alter it in any way.Bearers of the created
Vectorwill 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
Listcan still mutate it. Mutating theListis not advised. Operations that change the size of the underlyingListwill result in unpredictable behavior. UsecopyFrom(java.lang.Iterable<A>)if you want to avoid this situation.- Type Parameters:
A- the element type- Parameters:
underlying-Listto wrap; not null- Returns:
- a
Vector<A>
-
copyFrom
static <A> ImmutableVector<A> copyFrom(java.lang.Iterable<A> source)
Creates anImmutableVectorthat is copied from anyIterable.The entire
Iterablewill be eagerly iterated. Be careful not to pass in an infiniteIterableor this method will not terminate.If necessary to guarantee immutability, this method will make a copy of the data provided. If
sourceis 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 anImmutableVectorthat 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 anImmutableVectorthat is copied from anyIterable, but consuming a maximum number of elements.The
Iterablewill be eagerly iterated, but only up to a maximum ofmaxCountelements. IfmaxCountelements are not available, then the all of the elements available will be returned.This method will make a copy of the data provided, unless
sourceis an untransformedImmutableVectorand its size is less than or equal tomaxCount, in which case it will be returned directly.If
sourceis anImmutableVectorthat is greater thanmaxCountin size, a copy will always be made, therefore making it memory-safe to take a small slice of a hugeVectorthat 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 tomaxCountelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableVectorthat contains at mostmaxCountelements
-
copyFrom
static <A> ImmutableVector<A> copyFrom(int maxCount, A[] source)
Returns a newImmutableVectorthat 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 anImmutableVectorby copying a slice from anIterable.The
Iterablewill 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
startIndexis 0 andsourceis anImmutableVectorwhose 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
Vectorthat 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 toendIndexExclusiveelements. Not null. It is safe forsourceto be infinite.- Returns:
- an
ImmutableVector<A>
-
-