class Vector<+T>
A mutable, growable sequence of items with O(1) lookup/update and amortized O(1) appending of items.
Notes
Vector is represented as a contiguous block of memory and supports random access by integer index for indices in the range 0 (inclusive) up to the vector's size() (exclusive).
Static Methods
Creating a Vector
static fun sourcemcreate(capacity: Int): mutable thisCreate a mutable Vector with the given capacity.
Create a Vector containing the given items.
Create a Vector filled with the items of an iterator.
Create a Vector filled with the items of an iterator in reverse order. Throws if the iterator does not yield exactly its reported number of items.
Create a vector of the given size with all indices set to the given value.
Create a vector of the given size with each index set to the result of calling the function with the index.
Methods
Copying a Vector
readonly fun sourceclone(reserveCapacity: Int): mutable thisReturns a mutable, shallow copy of this Vector. The returned Vector is sized to fit exactly the number of items in this vector, plus the optional reserveCapacity number of additional items.
Returns the total number of items that can fit in the Vector's internal storage without allocating new space. To determine the number of additional items that can be added without allocating, use capacity() - size(). NOTE: Making capacity observable on frozen values prevents optimizations such as shrinking to fit on freeze. By making it mutable we preserve option value to add such optimizations in the future.
Grow the Vector's internal storage as necessary to accommodate the given (absolute) capacity. This method may allocate more storage than requested: if the method succeeds the Vector's capacity will be equal or greater than the given capacity.
Remove all items from this Vector. The Vector's internal storage is not affected (ie the Vector's capacity does not change).
Expand or shrink the vector to the given size. If the new size is larger than the current size, indices between the old and new size are set to the given filler value.
Accessing Items
readonly fun sourceget(index: Int): TGet the item at the given index, throws if the index is out of bounds.
Get the item at the given index as Some() or return None() if the index is out of bounds.
Adding/Changing/Removing Items
mutable fun sourceset(index: Int, value: T): VoidSet the value at the given index. Throws if the index is out of bounds. NOTE: To add items to the end of a container use push().
Add the value to the end of the container, growing the container's internal storage if necessary to make space for the item.
Remove the last item of the container and return it. Throws if the container is empty.
Remove the last item of the container and return it as Some() if non-empty, otherwise returns None().
Append all of items from the given sequence to the end of this container.
Inserts the value at the given index, shifting over existing elements to make room for the new element. Grows internal storage if necessary: throws if the index is out of bounds. NOTE: unlike set(), insert() allows inserting at index == size
Deletes the value at the given index, shifting over elements after that index to fill the gap. Throws if the index is out of bounds.
Deletes the items in the range of start (inclusive) to end (exclusive) and inserts the items from the second sequence at the start index. If second is smaller than the length of the slice denoted by start/end, then subsequent items will be shifted left to fill any gaps. If second is larger than the start-end slice, then subsequent items will be shifted right to make room.
Visual Example:
Before:
[ ...prefix, ...start-end, ...suffix ]
After:
[ ...prefix, ...second, ...suffix ]
Remove all items from this sequence for which the predicate returns false, keeping only those items for which the predicate returns true.
Randomize the order of items in this Vector using a pseudo-random generator initialized with the given seed. For a collection of Hashable values, a deterministic random value can be initialized with:
rng = Random::mcreate(vector.hash());
Sorts the items in place, using the given predicate to determine ordering. To sort a collection of non-Orderable items, use Orderable.create():
a: Vector
Selecting Portions of a Sequence
readonly fun sourceslice(start: Int, end: Int): thisReturns the items of this container from the start index (inclusive) up to
the end index (exclusive). The start/end values may be negative, in which
case they refer to the nth index before the end of the container.
For example, x.slice(0, -2)
is equivalent to x.slice(0, x.size() - 2)
.
Returns the items of this container that do not appear in the second sequence.
Return the items of this container that appear in the second sequence.
Returns up to N items randomly sampled from this Vector using a pseudo-random generator initialized with the given seed. Returns all items if N is greater than or equal to the size of the container. For a collection of Hashable values, a deterministic random value can be initialized with:
rng = Random::mcreate(vector.hash());
Iteration
readonly fun sourceeach(f: (T) -> Void): VoidCalls the given function once per element of this sequence.
Calls the given function once per element of this sequence.
Aggregation
readonly fun sourcefind(p: (T) -> Bool): Option<T>Returns Some(x) for the first item in this sequence that matches the predicate, or None() if no items match.
Composition
readonly fun sourcemap<U>(s: (T) -> U): Vector<U>Returns a new sequence representing the results of calling the selection function on each element of this sequence.
Returns a new sequence representing the results of calling the selection function on each element of this sequence.
Similar to map(), but calls the callback with (index, item) for each item in this sequence.
Returns a new sequence representing the results of calling the selection function on each element to map each element to a sequence, and (conceptually) flattening the resulting sequences.
When the items of this sequence are themselves sequences, returns a new sequence that contains all the items from all of the inner sequences.
Returns a new sequence representing all the elements of this sequence for which the predicate returns true.
When the items of this sequence are Options, returns a new sequence containing the inner values of items that are Some():
Before: Vector[None(), None(), Some(2), None(), Some(4)] After: Vector[2, 4]
Returns a new sequence representing the results of calling the selection function with tuples of the ith elements of this and the second sequence.
Returns a new sequence representing the elements of this sequence after the nth element.
Returns a new sequence representing the elements of this sequence after the contiguous prefix for which the predicate returns true.
Returns a new sequence representing the first n elements of this.
Returns a new sequence representing the contiguous prefix of elements from this for which the predicate returns true.
Returns a new Vector containing the items of this sequence followed by the items of the second sequence.
Returns a new sequence representing the items of this sequence in reverse order.
Returns a new sequence representing the items of this sequence in sorted order, using their default ordering. To sort a collection of non-Orderable items, use sortedBy().
Returns a new sequence representing the items of this sequence in sorted order, using the given predicate to determine ordering. To sort a collection of non-Orderable items, use Orderable.create():
a: Vector
Extension Methods
frozen fun sourceparallelMap<U>(f: (T) ~> U): Vector<U>Methods unique to Vector and not inherited from Seq/IndexedSequence. Returns a new Vector with the results of applying the selection function in parallel to each item of this Vector.
Iterators
readonly fun sourcekeys(): mutable Iterator<Int>Returns an Iterator that yields the range of numbers from 0 (inclusive)
to the size of this vector (exclusive) - ie of [0, size)
.
Returns an Iterator that yields the values contained in this vector.
Returns an Iterator that yields tuples of (index, value), where index is the zero-based index of the value within this vector.
Returns an Iterator that yields the values of this Vector in reverse order.
Internal helper for iteration guarding against concurrent modifiction.
Internal helper for iteration guarding against concurrent modifiction.