Package arc.struct
Class Seq<T>
java.lang.Object
arc.struct.Seq<T>
- Direct Known Subclasses:
DelayedRemovalSeq
,Jval.JsonArray
,SnapshotSeq
A resizable, ordered or unordered array of objects. If unordered, this class avoids a memory copy when removing elements (the
last element is moved to the removed element's position).
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionT[]
Provides direct access to the underlying array.static int
Debugging variable to count total number of iterators allocated.boolean
int
-
Constructor Summary
ConstructorDescriptionSeq()
Creates an ordered array with a capacity of 16.Seq
(boolean ordered) Creates an ordered/unordered array with the specified capacity.Seq
(boolean ordered, int capacity) Creates a new array withitems
of the specified type.Creates a new array containing the elements in the specified array.Seq
(int capacity) Creates an ordered array with the specified capacity.Creates a new array containing the elements in the specified array.Creates an ordered array withitems
of the specified type and a capacity of 16.Creates a new ordered array containing the elements in the specified array. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Adds a value if it was not already in this sequence.boolean
boolean
any()
<R> Seq<R>
as()
<K,
V> ObjectMap<K, V> asSet()
clear()
boolean
boolean
boolean
Returns if this array contains value.boolean
containsAll
(Seq<T> seq) boolean
containsAll
(Seq<T> seq, boolean identity) copy()
int
distinct()
Note that this allocates a new set.<E extends T>
voidvoid
T[]
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
Deprecated.This name is misleading, as it modifies the collection! If you want a newly allocated Seq, use select.first()
Returns the first item.firstOpt()
Returns the first item, or null if this Seq is empty.<R> Seq<R>
Returns a new array with the mapped values.<R> Seq<R>
flatten()
Flattens this array of arrays into one array.get
(int index) getFrac
(float index) int
hashCode()
int
int
int
Returns the index of first occurrence of value in the array, or -1 if no such value exists.void
boolean
isEmpty()
Returns true if the array is empty.iterator()
Returns an iterator for the items in the array.int
lastIndexOf
(T value, boolean identity) Returns an index of last occurrence of value in array or -1 if no such value exists.list()
<R> Seq<R>
Returns a new array with the mapped values.max
(Comparator<T> func) min
(Comparator<T> func) peek()
Returns the last item.pop()
Removes and returns the last item.If this array is empty, returns an object specified by the constructor.random()
Returns a random item from the array, or null if the array is empty.Returns a random item from the array, excluding the specified element.<R> R
remove
(int index) Removes and returns the item at the specified index.boolean
Removes a single value by predicate.boolean
Removes a value, without using identity.boolean
Removes the first instance of the specified value in the array.boolean
boolean
Removes from this array all of elements contained in the specified array.void
removeRange
(int start, int end) Removes the items between the specified indices, inclusive.void
Replaces values without creating a new array.boolean
Replaces the first occurrence of 'from' with 'to'.protected T[]
resize
(int newSize) Creates a new backing array with the specified size containing the current items.Removes everything that does not match this predicate.reverse()
Allocates a new array with all elements that match the predicate.static <T> Seq<T>
selectFrom
(Seq<T> base, Boolf<T> predicate) selectRanked
(Comparator<T> comparator, int kthLowest) Selects the nth-lowest element from the Seq according to Comparator ranking.int
selectRankedIndex
(Comparator<T> comparator, int kthLowest) void
void
Sets this array's contents to the specified array.void
Sets this array's contents to the specified array.T[]
setSize
(int newSize) Sets the array size, leaving any values beyond the current size null.T[]
shrink()
Reduces the size of the backing array to the size of the actual items.shuffle()
sort()
Sorts this array.sort
(Comparator<? super T> comparator) Sorts the array.<U extends Comparable<? super U>>
Seq<T>sortComparing
(Func<? super T, ? extends U> keyExtractor) int
float
void
swap
(int first, int second) T[]
toArray()
Returns the items as an array.<V> V[]
toString()
void
truncate
(int newSize) Reduces the size of the array to the specified size.static <T> Seq<T>
static <T> Seq<T>
with
(T... array) static <T> Seq<T>
withArrays
(Object... arrays) Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
iteratorsAllocated
public static int iteratorsAllocatedDebugging variable to count total number of iterators allocated. -
items
Provides direct access to the underlying array. If the Array's generic type is not Object, this field may only be accessed if theSeq(boolean, int, Class)
constructor was used. -
size
public int size -
ordered
public boolean ordered
-
-
Constructor Details
-
Seq
public Seq()Creates an ordered array with a capacity of 16. -
Seq
public Seq(int capacity) Creates an ordered array with the specified capacity. -
Seq
public Seq(boolean ordered) Creates an ordered/unordered array with the specified capacity. -
Seq
public Seq(boolean ordered, int capacity) - Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing array to be grown.
-
Seq
Creates a new array withitems
of the specified type.- Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.capacity
- Any elements added beyond this will cause the backing array to be grown.
-
Seq
Creates an ordered array withitems
of the specified type and a capacity of 16. -
Seq
Creates a new array containing the elements in the specified array. The new array will have the same type of backing array and will be ordered if the specified array is ordered. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown. -
Seq
Creates a new ordered array containing the elements in the specified array. The new array will have the same type of backing array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown. -
Seq
Creates a new array containing the elements in the specified array. The new array will have the same type of backing array. The capacity is set to the number of elements, so any subsequent elements added will cause the backing array to be grown.- Parameters:
ordered
- If false, methods that remove elements may change the order of other elements in the array, which avoids a memory copy.
-
-
Method Details
-
withArrays
-
with
- See Also:
-
with
-
select
- See Also:
-
asMap
-
asMap
-
asSet
-
copy
-
list
-
sumf
-
sum
-
each
-
each
-
replace
Replaces values without creating a new array. -
flatten
Flattens this array of arrays into one array. Allocates a new instance. -
flatMap
Returns a new array with the mapped values. -
map
Returns a new array with the mapped values. -
mapInt
- Returns:
- a new int array with the mapped values.
-
mapFloat
- Returns:
- a new float array with the mapped values.
-
reduce
-
allMatch
-
contains
-
min
-
max
-
min
-
min
-
max
-
find
-
with
-
addUnique
Adds a value if it was not already in this sequence.- Returns:
- whether this value was added successfully.
-
add
-
add
-
add
-
add
-
add
-
add
-
addAll
-
addAll
-
addAll
-
addAll
-
addAll
-
set
Sets this array's contents to the specified array. -
set
Sets this array's contents to the specified array. -
getFrac
-
get
-
set
-
insert
-
swap
public void swap(int first, int second) -
replace
Replaces the first occurrence of 'from' with 'to'.- Returns:
- whether anything was replaced.
-
containsAll
- Returns:
- whether this sequence contains every other element in the other sequence.
-
containsAll
- Returns:
- whether this sequence contains every other element in the other sequence.
-
contains
-
contains
Returns if this array contains value.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if array contains value, false if it doesn't
-
indexOf
-
indexOf
Returns the index of first occurrence of value in the array, or -1 if no such value exists.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of first occurrence of value in array or -1 if no such value exists
-
indexOf
-
lastIndexOf
Returns an index of last occurrence of value in array or -1 if no such value exists. Search is started from the end of an array.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- An index of last occurrence of value in array or -1 if no such value exists
-
remove
Removes a value, without using identity. -
remove
Removes a single value by predicate.- Returns:
- whether the item was found and removed.
-
remove
Removes the first instance of the specified value in the array.- Parameters:
value
- May be null.identity
- If true, == comparison will be used. If false, .equals() comparison will be used.- Returns:
- true if value was found and removed, false otherwise
-
remove
Removes and returns the item at the specified index. -
removeRange
public void removeRange(int start, int end) Removes the items between the specified indices, inclusive. -
removeAll
- Returns:
- this object
-
removeAll
-
removeAll
Removes from this array all of elements contained in the specified array.- Parameters:
identity
- True to use ==, false to use .equals().- Returns:
- true if this array was modified.
-
pop
If this array is empty, returns an object specified by the constructor. Otherwise, acts like pop(). -
pop
Removes and returns the last item. -
peek
Returns the last item. -
first
Returns the first item. -
firstOpt
Returns the first item, or null if this Seq is empty. -
isEmpty
public boolean isEmpty()Returns true if the array is empty. -
any
public boolean any() -
clear
-
shrink
Reduces the size of the backing array to the size of the actual items. This is useful to release memory when many items have been removed, or if it is known that more items will not be added.- Returns:
items
-
ensureCapacity
Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes.- Returns:
items
-
setSize
Sets the array size, leaving any values beyond the current size null.- Returns:
items
-
resize
Creates a new backing array with the specified size containing the current items. -
sort
Sorts this array. The array elements must implementComparable
. This method is not thread safe (usesSort.instance()
). -
sort
Sorts the array. This method is not thread safe (usesSort.instance()
). -
sort
-
sortComparing
public <U extends Comparable<? super U>> Seq<T> sortComparing(Func<? super T, ? extends U> keyExtractor) -
selectFrom
-
distinct
Note that this allocates a new set. Mutates. -
as
-
select
Allocates a new array with all elements that match the predicate. -
retainAll
Removes everything that does not match this predicate. -
filter
Deprecated.This name is misleading, as it modifies the collection! If you want a newly allocated Seq, use select. For a direct replacement, use retainAll.Removes everything that does not match this predicate. -
count
-
selectRanked
Selects the nth-lowest element from the Seq according to Comparator ranking. This might partially sort the Array. The array must have a size greater than 0, or aArcRuntimeException
will be thrown.- Parameters:
comparator
- used for comparisonkthLowest
- rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.- Returns:
- the value of the Nth lowest ranked object.
- See Also:
-
selectRankedIndex
- Parameters:
comparator
- used for comparisonkthLowest
- rank of desired object according to comparison, n is based on ordinal numbers, not array indices. for min value use 1, for max value use size of array, using 0 results in runtime exception.- Returns:
- the index of the Nth lowest ranked object.
- See Also:
-
reverse
-
shuffle
-
truncate
public void truncate(int newSize) Reduces the size of the array to the specified size. If the array is already smaller than the specified size, no action is taken. -
random
-
random
Returns a random item from the array, or null if the array is empty. -
random
Returns a random item from the array, excluding the specified element. If the array is empty, returns null. If this array only has one element, returns that element. -
toArray
Returns the items as an array. Note the array is typed, so theSeq(Class)
constructor must have been used. Otherwise usetoArray(Class)
to specify the array type. -
toArray
-
hashCode
public int hashCode() -
equals
-
toString
-
toString
-
toString
-
iterator
Returns an iterator for the items in the array. Remove is supported. Note that the same iterator instance is returned each time this method is called, unless you are using nested loops. Never, ever access this iterator's method manually, e.g. hasNext()/next(). Note that calling 'break' while iterating will permanently clog this iterator, falling back to an implementation that allocates new ones.
-