public interface LongIndexable extends PrimitiveLongIterable, Sizeable
The EMPTY
constant or emptyIndexable()
method provide the same
useful return value to indicate emptiness.
Simple implementations should extend LongIndexable.Base
because
that provides useful implementations for standard Object methods and implements
Comparable
.
FastLongSequenceSearch
Modifier and Type | Interface and Description |
---|---|
static class |
LongIndexable.Base
Abstract base class which provides useful implementations
for
Object.equals(Object) , Object.hashCode() ,
Object.toString() . |
static class |
LongIndexable.LongIndexableSpliterator
Spliterator for long indexables.
|
Modifier and Type | Field and Description |
---|---|
static LongIndexable.Base |
EMPTY
An empty indexable.
|
EMPTY_LONG_ITERATOR
Modifier and Type | Method and Description |
---|---|
default void |
addAllTo(java.util.Collection<? super java.lang.Long> collection)
Add all elements of this indexable to the given collection.
|
default int |
addToArray(long[] array,
int pos)
Add the content of this indexable to the given array.
|
default int |
addToArray(long[] array,
int arrayPos,
int index,
int length)
Add a part of the content of this indexable to the given array.
|
default LongIndexable.Base |
asBase()
Make this long integer indexable usable as a class with defined standard Object methods.
|
default java.util.Collection<java.lang.Long> |
asCollection()
Get a view on this indexable as an unmodifiable collection.
|
default Indexable<java.lang.Long> |
asIndexable()
Get a view of this indexable as a standard object-based indexable.
|
default java.util.List<java.lang.Long> |
asList()
Get a view on this indexable as an unmodifiable list.
|
default int |
binarySearch(long value)
Do a binary search in an indexable ordered in natural ascending order.
|
default int |
binarySearch(long value,
LongOrdering order)
Do a binary search in an ordered indexable.
|
static int |
compare(LongIndexable indexable1,
LongIndexable indexable2)
Compare two long indexables.
|
static int |
compareUnsigned(LongIndexable indexable1,
LongIndexable indexable2)
Compare two long indexables as if they contain unsigned long values.
|
static LongIndexable.Base |
emptyIndexable()
Get an empty indexable set.
|
static boolean |
equal(LongIndexable indexable1,
LongIndexable indexable2)
Are two long indexables equal?
|
static boolean |
equal(LongIndexable indexable1,
java.lang.Object indexable2)
Are two long indexables equal?
|
default int |
firstMatch(java.util.function.LongPredicate check)
Get the first index for which the given check is fulfilled.
|
default long |
foldLeft(long initialValue,
java.util.function.LongBinaryOperator foldOperation)
Go over all values and do a cumulative calculation.
|
default LongIndexable |
frozen()
Get a frozen version of this indexable.
|
default java.util.Spliterator.OfLong |
frozenLongSpliterator()
Get an immutable spliterator on a frozen copy of this indexable.
|
long |
get(int index)
Get the element at the given index.
|
default long |
getMod(int index)
Get an element modulo size.
|
default long |
gyt(int index)
Pythonesque get.
|
static int |
hash(LongIndexable indexable)
Calculate a hashcode for a long indexable.
|
default LongIndexable.Base |
headSet(int toIndex)
Create an indexable subset from the last elements of this indexable set.
|
default java.lang.Iterable<java.lang.Integer> |
indexes()
Get the indexes of this indexable as an iterable range.
|
static LongIndexable.Base |
init(int size,
long value)
Get a long indexable of a given size which always returns the same value.
|
static LongIndexable |
initByIndex(int size,
java.util.function.IntToLongFunction producer)
Get a long indexable of a given size which returns elements created by index.
|
default IntIndexable |
intIndexes()
Get the indexes of this indexable as an integer indexable.
|
default boolean |
isEmpty()
Is this indexable empty?
|
default boolean |
isOrdered(LongOrdering order)
Is this indexable sorted according to the given ordering?
|
default boolean |
isStrictlyOrdered(LongOrdering order)
Is this indexable strictly sorted according to the given ordering?
|
default java.util.Iterator<java.lang.Long> |
iterator()
Returns an iterator over elements of type
T . |
default int |
lastMatch(java.util.function.LongPredicate check)
Get the last index for which the given check is fulfilled.
|
default java.util.ListIterator<java.lang.Long> |
listIterator()
Returns a list iterator over elements of type
T . |
default java.util.PrimitiveIterator.OfLong |
longIterator()
Get an primitive long iterator.
|
default java.util.PrimitiveIterator.OfLong |
longIterator(int from,
int to)
Get a long iterator from the given sub set.
|
default java.util.Spliterator.OfLong |
longSpliterator()
Get a spliterator on this indexable.
|
default int |
nextMatch(int startIndex,
java.util.function.LongPredicate check)
Get the next index for which the given check is fulfilled.
|
default MutableLongIndexable |
ordered()
Create a (mutable) long indexable from the values of this indexable
which is ordered in natural order.
|
default MutableLongIndexable |
ordered(LongOrdering order)
Create a (mutable) long indexable from the values of this indexable
which is ordered as defined by the given ordering.
|
default int |
previousMatch(int startIndex,
java.util.function.LongPredicate check)
Get the previous index for which the given check is fulfilled.
|
default LongIndexable.Base |
reverse()
Get this indexable but with inverted order.
|
default LongIndexable |
rotated(int steps)
Get a rotated view of this long indexable.
|
static LongIndexable.Base |
singleton(long value)
View a single long value as a long indexable of size 1.
|
default LongIndexable.Base |
subSet(int fromIndex,
int toIndex)
Get an indexable subset.
|
default LongIndexable.Base |
sybSet(int fromIndex,
int toIndex)
Get an indexable subset.
|
default LongIndexable.Base |
tailSet(int fromIndex)
Create an indexable subset from the last elements of this indexable set.
|
default long[] |
toArray()
Convert this indexable into a Java array.
|
default java.util.ArrayList<java.lang.Long> |
toList()
Create a list from this long indexable.
|
static java.lang.String |
toString(LongIndexable indexable)
Create a string representation of the given indexable.
|
default <E> Indexable<E> |
view(java.util.function.LongFunction<E> convert)
View this raw long indexable as if it has another element type.
|
static LongIndexable.Base |
viewArray(long... array)
View an int array as an Indexable.
|
static LongIndexable.Base |
viewArray(long[] array,
int start,
int length)
View part of an integer array as an Indexable.
|
static <T> LongIndexable.Base |
viewArray(T[] array,
java.util.function.ToLongFunction<? super T> valueExtract)
View an object array as a long indexable.
|
default BooleanIndexable |
viewAsBoolean(java.util.function.LongPredicate convert)
View this indexable as a boolean indexable while using a conversion for
each value.
|
default ByteIndexable |
viewAsByte()
View this long indexable as a byte integer indexable.
|
default DoubleIndexable |
viewAsDouble()
View this long indexable as a double indexable.
|
default DoubleIndexable |
viewAsDouble(java.util.function.LongToDoubleFunction convert)
View this indexable as a double indexable while using a conversion for
each value.
|
default FloatIndexable |
viewAsFloat()
View this long indexable as a float indexable.
|
default IntIndexable |
viewAsInt()
View this long indexable as a short integer indexable.
|
default IntIndexable |
viewAsInt(java.util.function.LongToIntFunction convert)
View this indexable as an integer indexable while using a conversion for
each value.
|
default ShortIndexable |
viewAsShort()
View this long indexable as a short integer indexable.
|
static LongIndexable.Base |
viewByIndex(int size,
java.util.function.IntToLongFunction producer)
Get a long indexable of a given size which returns elements created by index.
|
static LongIndexable.Base |
viewIndexable(Indexable<? extends java.lang.Number> indexable)
View a generic Number indexable as long indexable.
|
static LongIndexable.Base |
viewIndexable(Indexable<? extends java.lang.Number> indexable,
long nullFallback)
View a generic Number indexable as long indexable.
|
static <T> LongIndexable.Base |
viewIndexable(Indexable<T> indexable,
java.util.function.ToLongFunction<? super T> extractor)
View a generic indexable as a long indexable.
|
static LongIndexable.Base |
viewList(java.util.List<? extends java.lang.Number> list)
View a list as an Indexable.
|
static LongIndexable.Base |
viewList(java.util.List<? extends java.lang.Number> list,
int nullValue)
View a list as an Indexable.
|
static <T> LongIndexable.Base |
viewList(java.util.List<T> list,
java.util.function.ToLongFunction<? super T> extractor)
View a generic list as a long indexable.
|
static LongIndexable.Base |
viewNumberArray(java.lang.Number... array)
View a Number array as an Indexable.
|
default LongIndexable |
viewOp(java.util.function.LongUnaryOperator op)
View this long indexable transformed by an operator.
|
default LongIndexable |
withAppendedValue(long value)
Create a view with of this indexable with another value added at the end.
|
static LongIndexable.Base |
withCachedHash(LongIndexable indexable)
Wrap an indexable with one which caches the hash value.
|
default LongIndexable |
withExchangedValueAt(int index,
long value)
Create a view of this long indexable with an exchanged value.
|
default LongIndexable |
withInsertedValueAt(int index,
long value)
Create a view of this long indexable with an inserted value.
|
default LongIndexable |
withRemovedValueAt(int index)
Create a view of this indexable with one value removed.
|
default LongIndexable |
withSwappedValuesAt(int index1,
int index2)
Create a view of this long indexable with two values swapped.
|
average, containsLong, forEachLong, forEachLongFragile, spliterator, stream, sum, sumX
@NotNull static final LongIndexable.Base EMPTY
long get(int index)
index
- index between 0
and size() - 1
default long gyt(int index)
-1
references the last element, -2
its predecessor, and so on.index
- index between -size()
and size() - 1
default long getMod(int index)
This maps the given index
into the range of this indexable
by applying a modulo Sizeable.size()
operation. For empty indexable
this will throw an IndexOutOfBoundsException
as there is no
possible index to get.
index
- index, possibly out of range, possibly even negative
for Pythonesque access@NotNull default LongIndexable.Base reverse()
default boolean isEmpty()
true
: there are no elements in this indexablefalse
: this indexable has elements@NotNull default java.util.PrimitiveIterator.OfLong longIterator()
longIterator
in interface PrimitiveLongIterable
@NotNull default java.util.PrimitiveIterator.OfLong longIterator(int from, int to)
from
- first index of iteratorto
- one past last index of iterator@NotNull default java.util.Iterator<java.lang.Long> iterator()
T
.iterator
in interface java.lang.Iterable<java.lang.Long>
iterator
in interface PrimitiveLongIterable
@NotNull default java.util.ListIterator<java.lang.Long> listIterator()
T
.@NotNull default LongIndexable.Base subSet(int fromIndex, int toIndex)
fromIndex
to toIndex - 1
.fromIndex
- start index of sub settoIndex
- index after last index@NotNull default LongIndexable.Base sybSet(int fromIndex, int toIndex)
fromIndex
- start index of sub settoIndex
- end index of sub set@NotNull default LongIndexable.Base tailSet(int fromIndex)
fromIndex
- index to start with, negative counts from the backdefault LongIndexable.Base headSet(int toIndex)
toIndex
- index one after the end (equal to the length of the returned set),
negative counts from the back@NotNull default java.util.Collection<java.lang.Long> asCollection()
Collection.addAll(Collection)
.@NotNull default java.util.List<java.lang.Long> asList()
Collection.addAll(Collection)
.toList()
@NotNull default java.util.ArrayList<java.lang.Long> toList()
asList()
default void addAllTo(@NotNull java.util.Collection<? super java.lang.Long> collection)
collection
- collection to add to@NotNull default Indexable<java.lang.Long> asIndexable()
Long
values@NotNull default long[] toArray()
default int addToArray(@NotNull long[] array, int pos)
array
- array where the content is addedpos
- position in the array where the content of this indexable is addeddefault int addToArray(@NotNull long[] array, int arrayPos, int index, int length)
array
- array where the content is addedarrayPos
- position in hte array where the content is addedindex
- start index of this indexable which is added firstlength
- number of entries of this indexable added to the array@NotNull default java.lang.Iterable<java.lang.Integer> indexes()
intIndexes()
,
Range.indexes(int)
@NotNull default IntIndexable intIndexes()
indexes()
,
IntIndexable.rangeFromSize(int)
default long foldLeft(long initialValue, @NotNull java.util.function.LongBinaryOperator foldOperation)
Eg calculate the maximum of this indexable use
long max = indexable.foldLeft(Long.MIN_VALUE, Math::max);
initialValue
- start value for the operation, will be returned if this iterable is emptyfoldOperation
- operation applied to each value, will get the accumulated value as its
first and the current element value as its second argumentdefault int nextMatch(int startIndex, @NotNull java.util.function.LongPredicate check)
startIndex
- start index for checking, Pythonesque
indexing supportedcheck
- check to perform on elements of this indexable until true
startIndex
for which the check returns true
,
Pythonesque.UNMAPPABLE
if nonedefault int firstMatch(@NotNull java.util.function.LongPredicate check)
check
- check to perform on elements of this indexable until true
true
,
-1
if nonedefault int previousMatch(int startIndex, @NotNull java.util.function.LongPredicate check)
startIndex
- start index for checking, Pythonesque
indexing supportedcheck
- check to perform on elements of this indexable until true
startIndex
for which the check returns true
,
Pythonesque.UNMAPPABLE
if nonedefault int lastMatch(@NotNull java.util.function.LongPredicate check)
check
- check to perform on elements of this indexable until true
true
,
-1
if none@NotNull default LongIndexable frozen()
Often Indexables are used as a view to underlying collections. Although this interface is immutable, the underlying colelction might nevertheless change. This copies the current state of this indexable into an unmodifiable state, and returns an Indexable which is stable in size and will return always the same element for a given index. Beware: it is still possible that any element itself changes when the elements are mutable.
Calling frozen()
again on the returned object will just return
the object itself, so you can safely call this method more than once.
@NotNull default java.util.Spliterator.OfLong longSpliterator()
Spliterator.IMMUTABLE
flag set,
see frozenLongSpliterator()
for an alternative.@NotNull default java.util.Spliterator.OfLong frozenLongSpliterator()
frozen()
returns this
)
then this is the same as PrimitiveLongIterable.spliterator()
with the difference that the spliterator
returned here will have its Spliterator.IMMUTABLE
flag set. If this is not immutable
a frozen copy will be created, and the spliterator will be operating on that. Please refer to
frozen()
to understand what level of immutabiliy it will provide.@NotNull default LongIndexable withInsertedValueAt(int index, long value)
ExpandableLongIndexable
if you want to carry out more
stuff like this.
As this creates a view of this indexable, changes to this indexable might result in a disaster when using the returned indexable!
index
- index where the value is inserted before the current index
(not Pythonesque, because it is possible to add an value at index == size()
,
and it is expected that most insertion will happen at 0 or at the end)value
- inserted value at that indexwithAppendedValue(long)
@NotNull default LongIndexable withAppendedValue(long value)
value
- value to addwithInsertedValueAt(int, long)
@NotNull default LongIndexable withExchangedValueAt(int index, long value)
ExpandableLongIndexable
if you want to carry out more
stuff like this.
As this creates a view of this indexable, changes to this indexable could lead to unexpected results when using the returned indexable!
index
- index where the element is exchanged, hiding the current element.
(Pythonesque)value
- exchanged value at that index@NotNull default LongIndexable withRemovedValueAt(int index)
index
- index of the removed value (Pythonesque)@NotNull default LongIndexable withSwappedValuesAt(int index1, int index2)
index1
- index of the first value (Pythonesque)index2
- index of the second value (Pythonesque)@NotNull default LongIndexable rotated(int steps)
steps
will rotate left, i.e. any index will be accessed
as if the number of steps is added before extraction (modulo length).
E.g. a rotation of -1
will return the last element when index 0
is
requested, and the first element for index 1
.steps
- steps to rotatefrozen()
to create an indexable which no longer depends on this one@NotNull default LongIndexable viewOp(@NotNull java.util.function.LongUnaryOperator op)
op
- transforming operator@NotNull default DoubleIndexable viewAsDouble()
frozen()
on the result to create an independent copy
if this seems like a problem to you.@NotNull default DoubleIndexable viewAsDouble(@NotNull java.util.function.LongToDoubleFunction convert)
Use frozen()
on the result to decouple it from this indexable.
convert
- converter applied to each value of this indexable before
it is returned from the resulting indexable@NotNull default FloatIndexable viewAsFloat()
frozen()
on the result to create an independent copy
if this seems like a problem to you.@NotNull default IntIndexable viewAsInt()
frozen()
on the result to create an independent copy
if this seems like a problem to you.@NotNull default IntIndexable viewAsInt(@NotNull java.util.function.LongToIntFunction convert)
Use frozen()
on the result to decouple it from this indexable.
convert
- converter applied to each value of this indexable before
it is returned from the resulting indexable@NotNull default ShortIndexable viewAsShort()
frozen()
on the result to create an independent copy
if this seems like a problem to you.@NotNull default ByteIndexable viewAsByte()
frozen()
on the result to create an independent copy
if this seems like a problem to you.@NotNull default BooleanIndexable viewAsBoolean(@NotNull java.util.function.LongPredicate convert)
Use frozen()
on the result to decouple it from this indexable.
convert
- converter applied to each value of this indexable before
it is returned from the resulting indexable@NotNull static LongIndexable.Base viewList(@NotNull java.util.List<? extends java.lang.Number> list)
list
- list used as a base, required to have only non-null elements@NotNull static LongIndexable.Base viewList(@NotNull java.util.List<? extends java.lang.Number> list, int nullValue)
list
- list used as a base, required to have only non-null elementsnullValue
- value returned for null
elements in the list@NotNull static <T> LongIndexable.Base viewList(@NotNull java.util.List<T> list, @NotNull java.util.function.ToLongFunction<? super T> extractor)
T
- element type of listlist
- generic listextractor
- extractor function which extracts a long from the elements of list
list
@NotNull static LongIndexable.Base viewIndexable(@NotNull Indexable<? extends java.lang.Number> indexable)
indexable
- generic indexable with Number elements, required to have only non-null elements@NotNull static LongIndexable.Base viewIndexable(@NotNull Indexable<? extends java.lang.Number> indexable, long nullFallback)
indexable
- generic indexable with Number elementsnullFallback
- fallback for null
elements@NotNull static <T> LongIndexable.Base viewIndexable(@NotNull Indexable<T> indexable, @NotNull java.util.function.ToLongFunction<? super T> extractor)
T
- element type of indexableindexable
- generic indexableextractor
- extractor function which extracts a long from the elements of indexable
indexable
@NotNull static LongIndexable.Base viewNumberArray(@NotNull java.lang.Number... array)
subSet(int, int)
.array
- array used as base, required to hold only non-null values@NotNull static LongIndexable.Base viewArray(@NotNull long... array)
viewArray(long[], int, int)
.array
- array used as base@NotNull static LongIndexable.Base viewArray(@NotNull long[] array, int start, int length)
array
- array used as basestart
- index of first byte to use from the arraylength
- number of elements to use from the array@NotNull static <T> LongIndexable.Base viewArray(@NotNull T[] array, @NotNull java.util.function.ToLongFunction<? super T> valueExtract)
T
- array element typearray
- viewed arrayvalueExtract
- converter from array elements to the long values of this indexable@NotNull static LongIndexable.Base singleton(long value)
value
- single value@NotNull static LongIndexable.Base init(int size, long value)
size
- size of the returned indexablevalue
- value returned for each elementsize
with always the same element@NotNull static LongIndexable.Base viewByIndex(int size, @NotNull java.util.function.IntToLongFunction producer)
producer
when an element is requested.
Use frozen()
on the result to create an indexable which avoids this.size
- size of the returned indexableproducer
- producer which is called with an index and expected to return the associated valueproducer
with the given index when an element is requestedinitByIndex(int, IntToLongFunction)
@NotNull static LongIndexable initByIndex(int size, @NotNull java.util.function.IntToLongFunction producer)
size
- size of the returned indexableproducer
- producer which is called with an index and expected to return the associated valueproducer
viewByIndex(int, IntToLongFunction)
@NotNull static LongIndexable.Base emptyIndexable()
@NotNull static java.lang.String toString(@NotNull LongIndexable indexable)
indexable
- indexablestatic boolean equal(@NotNull LongIndexable indexable1, @NotNull LongIndexable indexable2)
indexable1
- first indexableindexable2
- second indexabletrue
if both indexables contain the same values in the same sequencefalse
if sizes or values differstatic boolean equal(@NotNull LongIndexable indexable1, @Nullable java.lang.Object indexable2)
Object.equals(Object)
method.indexable1
- first indexableindexable2
- object expected to be an indexable itselftrue
if both indexables contain the same values in the same sequencefalse
if the second object is not an indexable, of if sizes or values differstatic int compare(@NotNull LongIndexable indexable1, @NotNull LongIndexable indexable2)
indexable1
- first indexableindexable2
- second indexable< 0>
if indexable1 < indxable2
,
0
if indexable1 == indexable2
, or
> 0
if indexable1 > indexable2
static int compareUnsigned(@NotNull LongIndexable indexable1, @NotNull LongIndexable indexable2)
indexable1
- first indexableindexable2
- second indexable< 0>
if indexable1 < indxable2
,
0
if indexable1 == indexable2
, or
> 0
if indexable1 > indexable2
static int hash(@NotNull LongIndexable indexable)
indexable
- indexable for which the hash code is required@NotNull static LongIndexable.Base withCachedHash(@NotNull LongIndexable indexable)
The wrapped indexable must not change after it is wrapped, otherwise strange things are expected to happen.
indexable
- wrapped indexable, must not changeindexable
, but also provides
useful implementations for Object.hashCode()
, Object.equals(Object)
,
and Object.toString()
default <E> Indexable<E> view(@NotNull java.util.function.LongFunction<E> convert)
E
- element type of return viewconvert
- converter between long and the requested element typedefault boolean isOrdered(@NotNull LongOrdering order)
order
- expected orderingtrue
if this indexable is sorted as defined by order
false
if notdefault boolean isStrictlyOrdered(@NotNull LongOrdering order)
order
- expected orderingtrue
if this indexable is sorted as defined by order
false
if notdefault int binarySearch(long value)
natural ascending order
,
i.e. isOrdered(LongOrdering) has to return true
for this ordering.
For efficiency this prerequisite is not checked, but not fulfilling
it will make this method return bogus results. If this indexable is only ordered,
but not strictly ordered
it is not defined
which index is returned when the looked up value appears in a sequence of equal values.value
- value to look up-(insertIndex + 1)
.default int binarySearch(long value, @NotNull LongOrdering order)
order
,
i.e. isOrdered(LongOrdering) has to return true
for this order.
For efficiency this prerequisite is not checked, but not fulfilling
it will make this method return bogus results. If this indexable is only ordered,
but not strictly ordered
it is not defined
which index is returned when the looked up value appears in a sequence of equal values.value
- value to look uporder
- sort order-(insertIndex + 1)
.@NotNull default MutableLongIndexable ordered(@NotNull LongOrdering order)
If natural ordering is required prefer calling ordered()
because that typically runs 1.5 times faster.
order
- sort order@NotNull default MutableLongIndexable ordered()
This can be some 1.5x faster than using ordered(LongOrdering)
with natural order
.
@NotNull default LongIndexable.Base asBase()