Slice<T> class
final
A contiguous sequence of elements in a List. Slices are a view into a list without allocating and copying to a new list, as such, they do not own their own data. Note: Shrinking the original list can cause the slices range to become invalid, which may cause an exception or unintended behavior.
- Implemented types
-
- List<
T>
- List<
- Available extensions
- AnyhowIterableResultExtension
- Array$IterableExtension
- Array$ListExtension
- EnumByName
- FutureIterable
- Iter$IterableExtension
- IterableExtensions
- ListToJSArray
- NullableIterableExtensions
- OpsListExtension
- Result$IterableFutureResultExtension
- Result$IterableResultExtension
- Slice$ListExtension
- Slice$SliceComparableSelfExtension
- Slice$SliceNumExtension
- Vec$IterableExtension
- Vec$ListExtension
- Vec$ListListExtension
Constructors
-
Slice(List<
T> _list, [int _start = 0, int? end]) -
Slice.fromList(List<
T> list) -
Slice.fromSlice(Slice<
T> slice, [int start = 0, int? end])
Properties
- first ↔ T
-
The first element.
getter/setter pairoverride
-
firstOrOption
→ Option<
T> -
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- isEmpty → bool
-
Whether this collection has no elements.
no setteroverride
- isNotEmpty → bool
-
Whether this collection has at least one element.
no setteroverride
-
iterator
→ Iterator<
T> -
A new
Iterator
that allows iterating the elements of thisIterable
.no setteroverride - last ↔ T
-
Returns the last element of the slice, can throw.
getter/setter pairoverride
-
lastOrOption
→ Option<
T> -
Returns the last element of the slice, or None if it is empty.
no setter
- length ↔ int
-
The number of objects in this list.
getter/setter pairoverride
-
reversed
→ Iterable<
T> -
An Iterable of the objects in this list in reverse order.
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- single → T
-
Checks that this iterable has only one element, and returns that element.
no setteroverride
Methods
-
add(
T value) → void -
Adds
value
to the end of the slice, extending the length of the underlying list and this slice by one. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
addAll(
Iterable< T> iterable) → void -
Appends all objects of
iterable
to the end of this slice. Extends the length of the underlying list and this slice by the number of objects initerable
. The underlying list must be growable. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
any(
bool f(T)) → bool -
Checks whether any element of this iterable satisfies
test
.override -
arrayWindows(
int size) → Iter< Arr< T> > - Returns an iterator over all contiguous windows of length size. The windows overlap. If the array is shorter than size, the iterator returns no values. Panics if size is zero or less.
-
asChunks(
int chunkSize) → (Arr< Arr< , Arr<T> >T> ) -
Splits the slice into a slice of N-element arrays, starting at the beginning of the slice,
and a remainder slice with length strictly less than N.
Panics if
chunkSize
is 0 or less. -
asMap(
) → Map< int, T> -
An unmodifiable Map view of this list.
override
-
asRchunks(
int chunkSize) → (Arr< T> , Arr<Arr< )T> > -
Splits the slice into a slice of N-element arrays, starting at the end of the slice,
and a remainder slice with length strictly less than N.
Panics if
chunkSize
is 0 or less. -
asSlice(
) → Slice< T> -
binarySearchBy(
int comparator(T)) → Result< int, int> -
Binary searches this slice with a comparator function. See
SliceOnComparableSliceExtension.binarySearch
for more. -
binarySearchByKey<
K extends Comparable> (K key, K keyExtractor(T)) → Result< int, int> -
Binary searches this slice with a key extraction function. See
SliceOnComparableSliceExtension.binarySearch
for more. -
call(
RangeBounds range) → Iterable< T> -
cast<
U> () → Slice< U> -
Returns a slice view where the elements are
U
.override -
chunkBy(
bool compare(T, T)) → Iter< Slice< T> > -
Returns an iterator over the slice producing non-overlapping runs of elements
using the predicate to separate them.
[1, 1, 1, 3, 3]
=>[[1, 1, 1], [3, 3]]
for(a, b) => a == b
The predicate is called for every pair of consecutive elements. -
chunks(
int n) → Iter< Slice< T> > -
Returns an iterator over
chunkSize
elements of the slice at a time, starting at the beginning of the slice. The chunks are slices and do not overlap. IfchunkSize
does not divide the length of the slice, then the last chunk will not have length chunkSize. Panics ifchunkSize
is 0 or less. -
clear(
) → void -
Clears the underlying data the slice encompasses. This slice's length after will be zero.
Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.
override
-
contains(
Object? element) → bool -
Whether the collection contains an element equal to
element
.override -
copyFromSlice(
Slice< T> src) → void - Copies the elements from src into self. The length of src must be the same as this.
-
copyWithin(
int start, int end, int dst, {bool sInc = true, bool enInc = false}) → void -
Copies elements from one part of the slice to another part of itself
The edge conditions can be changes with
sInc
andeInc
.sInc
is whether the start is inclusive andeInc
is whether the end is inclusive. -
elementAt(
int index) → T -
Returns the
index
th element.override -
endsWith(
Slice< T> needle) → bool - Returns true if needle is a suffix of the slice.
-
every(
bool f(T)) → bool -
Checks whether every element of this iterable satisfies
test
.override -
expand<
U> (Iterable< U> f(T)) → Iter<U> -
Expands each element of this Iterable into zero or more elements.
override
-
fill(
T value) → void -
Fills this slice with
value
. -
fillRange(
int start, int end, [T? fillValue]) → void -
Overwrites a range of elements with
fillValue
.override -
fillWith(
T f()) → void -
Fills this slice with the value return by calling
f
repeatedly. -
firstWhere(
bool f(T), {T orElse()?}) → T -
The first element that satisfies the given predicate
test
.override -
fold<
U> (U initialValue, U f(U previousValue, T element)) → U -
Reduces a collection to a single value by iteratively combining each
element of the collection with an existing value
override
-
followedBy(
Iterable< T> other) → Iter<T> -
Creates the lazy concatenation of this iterable and
other
.override -
forEach(
void f(T)) → void -
Invokes
action
on each element of this iterable in iteration order.override -
get(
int index) → Option< T> -
getMany(
Arr< int> indices) → Result<Arr< T> , GetManyError> - Returns many indices at once. Returns an error if any index is out-of-bounds.
-
getManyUnchecked(
Arr< int> indices) → Arr<T> - Returns mutable references to many indices at once, without doing any checks.
-
getRange(
int start, int end) → Iterable< T> -
Creates an Iterable that iterates over a range of elements.
override
-
getUnchecked(
int index) → T - Returns the element at the given index without doing bounds checking.
-
indexOf(
T element, [int start = 0]) → int -
The first index of
element
in this list.override -
indexWhere(
bool test(T element), [int start = 0]) → int -
The first index in the list that satisfies the provided
test
.override -
insert(
int index, T element) → void -
Inserts
element
at positionindex
in this slice. This increases the length of the underlying list and this slice by one and shifts all objects at or after the index towards the end. The underlying list must be growable. Theindex
value must be non-negative and no greater than length. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
insertAll(
int index, Iterable< T> iterable) → void -
Inserts all objects of
iterable
at positionindex
in this slice. This increases the length of the slice by the length ofiterable
and shifts all later objects towards the end of the slice. The underlying list must be growable. Theindex
value must be non-negative and no greater than length. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
isSortedBy(
Comparator< T> compare) → bool - Checks if the elements of this slice are sorted using the given comparator function.
-
isSortedByKey<
K extends Comparable< (K> >K key(T)) → bool - Checks if the elements of this slice are sorted using the given key extraction function.
-
iter(
) → Iter< T> -
join(
[String separator = '']) → String -
Converts each element to a String and concatenates the strings.
override
-
lastChunk(
int n) → Option< Arr< T> > - Return an array with the last N items in the slice. If the slice is not at least N in length, this will return None.
-
lastIndexOf(
T element, [int? start]) → int -
The last index of
element
in this list.override -
lastIndexWhere(
bool test(T element), [int? start]) → int -
The last index in the list that satisfies the provided
test
.override -
lastWhere(
bool f(T), {T orElse()?}) → T -
The last element that satisfies the given predicate
test
.override -
len(
) → int - Returns the length of the slice.
-
map<
U> (U f(T)) → Iter< U> -
The current elements of this iterable modified by
toElement
.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
partitionDedupBy(
bool sameBucket(T, T)) → (Slice< T> , Slice<T> ) -
Moves all consecutive repeated elements to the end of the slice according to Comparable.
Returns two slices. The first contains no consecutive repeated elements. The second contains all the duplicates in no specified order.
The
sameBucket
function is passed the to two elements from the slice and must determine if the elements compare equal. The elements are passed in opposite order from their order in the slice, so if same_bucket(a, b) returns true, a is moved at the end of the slice. If the slice is sorted, the first returned slice contains no duplicates. -
partitionDedupByKey<
K extends Comparable< (K> >K key(T)) → (Slice< T> , Slice<T> ) - Moves all but the first of consecutive elements to the end of the list that resolve to the same key. Returns two slices. The first contains no consecutive repeated elements. The second contains all the duplicates in no specified order. If the list is sorted, the first returned list contains no duplicates.
-
partitionPoint(
bool predicate(T)) → int -
Returns the index of the partition point according to the given predicate (the index of the first element of the second partition).
The slice is assumed to be partitioned according to the given predicate. This means that all elements for which the predicate returns
true are at the start of the slice and all elements for which the predicate returns false are at the end.
For example,
7, 15, 3, 5, 4, 12, 6
is partitioned under the predicate x % 2 != 0 (all odd numbers are at the start, all even at the end). If this slice is not partitioned, the returned result is unspecified and meaningless, as this method performs a kind of binary search. -
rchunks(
int chunkSize) → Iter< Slice< T> > -
Returns an iterator over
chunkSize
elements of the slice at a time, starting at the end of the slice. The chunks are slices and do not overlap. If chunk_size does not divide the length of the slice, then the last chunk will not have lengthchunkSize
. Panics ifchunkSize
is less than or equal to zero -
reduce(
T f(T, T)) → T -
Reduces a collection to a single value by iteratively combining elements
of the collection using the provided function.
override
-
remove(
Object? value) → bool -
Removes the first occurrence of
value
from this slice. Returns true ifvalue
was in the slice, false otherwise. The slice must be growable. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
removeAt(
int index) → T -
Removes the object at position
index
from this slice. This method reduces the length of this slice and the underlying list by one and moves all later objects down by one position. Returns the removed value. Theindex
must be in the range 0 ≤ index < length. The underlying list must be growable. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
removeLast(
) → T -
Removes and returns the last object in this list. The list must be growable and non-empty.
Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.
override
-
removeRange(
int start, int end) → void -
Removes a range of elements from the slice and the underlying list.
Removes the elements with positions greater than or equal to
start
and less thanend
, from the slice and underlying list. This reduces the slice and underlying list's length by end - start. The provided range, given bystart
andend
, must be valid. A range fromstart
toend
is valid if 0 ≤ start ≤ end ≤ length. An empty range (with end == start) is valid. The list must be growable. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
removeWhere(
bool test(T element)) → void -
Removes all objects from this slice and the underlying list that satisfy
test
. An object o satisfiestest
if test(o) is true. The slice's range shrinks by the number of elements removed. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
replaceRange(
int start, int end, Iterable< T> replacements) → void -
Replaces a range of elements with the elements of
replacements
. Removes the objects in the range fromstart
toend
, then inserts the elements ofreplacements
atstart
. This will change the size of this slice byreplacements.length - (end - start)
. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
retainWhere(
bool test(T element)) → void -
Removes all objects from this list that fail to satisfy
test
. An object o satisfiestest
if test(o) is true. The slice's range shrinks by the number of elements removed. Note, the ranges of other slices on the underlying list will not change, therefore use with care as this may shift the underlying data in other slices.override -
reverse(
) → void - Reverses the order of elements in the slice, in place.
-
rotateLeft(
int mid) → void -
Rotates the slice in-place such that the first mid elements of the slice move to the end while the
last
this.len() - mid
elements move to the front. After calling rotateLeft, the element previously at index mid will become the first element in the slice. -
rotateRight(
int k) → void -
Rotates the slice in-place such that the first
this.len() - k
elements of the slice move to the end while the last k elements move to the front. After calling rotate_right, the element previously at indexthis.len() - k
will become the first element in the slice. -
rsplit(
bool pred(T)) → Iter< Slice< T> > - Returns an iterator over slices separated by elements that match pred, starting at the end of the slice and working backwards. The matched element is not contained in the slices.
-
rsplitAt(
int index) → (Slice< T> , Slice<T> ) - Divides one slice into a slice and a remainder slice at an index from the end. The slice will contain all indices from [0, len - N) (excluding the index N itself) and the second slice will contain all indices from [len - N, len) (excluding the index len itself).
-
rsplitn(
int n, bool pred(T)) → Iter< Slice< T> > - Returns an iterator over slices separated by elements that match pred, limited to returning at most n items, starting from the end. The matched element is not contained in the slices. The last element returned, if any, will contain the remainder of the slice.
-
rsplitOnce(
bool pred(T)) → Option< (Slice< T> , Slice<T> )> - Splits the slice on the last element that matches the specified predicate. If any matching elements are resent in the slice, returns the prefix before the match and suffix after. The matching element itself is not included. If no elements match, returns None.
-
setAll(
int index, Iterable< T> iterable) → void -
Overwrites elements with the objects of
iterable
. The elements ofiterable
are written into this list, starting at positionindex
. This operation does not increase the length of the slice or the underlying list. Theindex
must be non-negative and no greater than length. Theiterable
must not have more elements than what can fit fromindex
to length. If iterable is based on this slice, its values may change during the setAlloverride -
setRange(
int start, int end, Iterable< T> iterable, [int skipCount = 0]) → void -
Writes some elements of
iterable
into a range of this list. Copies the objects ofiterable
, skippingskipCount
objects first, into the range fromstart
, inclusive, toend
, exclusive, of this slice.override -
setUnchecked(
int index, T value) → void - Sets the element at the given index without doing bounds checking.
-
shuffle(
[Random? random]) → void -
Shuffles the elements of this list randomly.
override
-
singleWhere(
bool f(T), {T orElse()?}) → T -
The single element that satisfies
test
.override -
skip(
int count) → Iter< T> -
Creates an Iterable that provides all but the first
count
elements.override -
skipWhile(
bool f(T)) → Iter< T> -
Creates an
Iterable
that skips leading elements whiletest
is satisfied.override -
slice(
[int start = 0, int? end]) → Slice< T> -
sort(
[int compare(T a, T b)?]) → void -
Sorts this slice according to the order specified by the
compare
function.override -
sortUnstableBy(
int compare(T a, T b)) → void - Sorts the slice with a comparator function, but might not preserve the order of equal elements.
-
sortUnstableByKey<
K extends Comparable< (K> >K key(T a)) → void - Sorts the slice with a key extraction function, but might not preserve the order of equal elements.
-
split(
bool pred(T)) → Iter< Slice< T> > - Returns an iterator over subslices separated by elements that match pred. The matched element is not contained in the subslices. see splitInclusive also.
-
splitAt(
int index) → (Slice< T> , Slice<T> ) - Divides one mutable slice into a slice and a remainder slice at an index. The slice will contain all indices from [0, N) (excluding the index N itself) and the second slice will contain all indices from [N, len) (excluding the index len itself).
-
splitFirst(
) → Option< (T, Slice< T> )> - Returns the first and all the rest of the elements of the slice, or None if it is empty.
-
splitInclusive(
bool pred(T)) → Iter< Slice< T> > - Returns an iterator over subslices separated by elements that match pred. The matched element is contained in the end of the previous subslice as a terminator. see split also.
-
splitLast(
) → Option< (T, Slice< T> )> - Returns the last and all the rest of the elements of the slice, or None if it is empty.
-
splitn(
int n, bool pred(T)) → Iter< Slice< T> > - Returns an iterator over subslices separated by elements that match pred, limited to returning at most n items. e.g. n == 1 will return the whole slice. The matched element is not contained in the subslices. The last element returned, if any, will contain the remainder of the slice.
-
splitOnce(
bool pred(T)) → Option< (Slice< T> , Slice<T> )> - Splits the slice on the first element that matches the specified predicate. If any matching elements are resent in the slice, returns the prefix before the match and suffix after. The matching element itself is not included. If no elements match, returns None.
-
startsWith(
Slice< T> needle) → bool - Returns true if needle is a prefix of the slice.
-
stripPrefix(
Slice< T> prefix) → Option<Slice< T> > - Returns a subslice with the prefix removed. Returns none if the prefix is not present.
-
stripSuffix(
Slice< T> suffix) → Option<Slice< T> > -
sublist(
int start, [int? end]) → List< T> -
Returns a new list containing the elements between
start
andend
.override -
swap(
int i, int j) → void - Swaps two elements in the slice. Will throw if the indices are out of bounds.
-
swapWithSlice(
Slice< T> other) → void - Swaps all elements in this with those in other. The length of other must be the same as this. Will panic if the length of other is not the same as this.
-
take(
int count) → Iter< T> -
Creates a lazy iterable of the
count
first elements of this iterable.override -
takeEnd(
int from) → Option< Slice< T> > - Returns a new slice of this slice from the end, and removes those elements from this slice. Returns None and does not modify the slice if the given range is out of bounds.
-
takeFirst(
) → Option< T> -
takeLast(
) → Option< T> -
takeStart(
int to) → Option< Slice< T> > - Returns a new slice of this slice until to, and removes those elements from this slice. Returns None and does not modify the slice if the given range is out of bounds.
-
takeWhile(
bool f(T)) → Iter< T> -
Creates a lazy iterable of the leading elements satisfying
test
.override -
toList(
{bool growable = true}) → List< T> -
growable
is ignore, always returns a growable list.override -
toSet(
) → Set< T> -
Creates a Set containing the same elements as this iterable.
override
-
toString(
) → String -
A string representation of this object.
override
-
where(
bool f(T)) → Iter< T> -
Creates a new lazy Iterable with all elements that satisfy the
predicate
test
.override -
whereType<
U> () → Iter< U> -
Creates a new lazy Iterable with all elements that have type
T
.override -
windows(
int size) → Iter< Slice< T> > - Returns an iterator of slices of this slice over all contiguous windows of length size. The windows overlap. If the slice is shorter than size, the iterator returns no values. Panics if size is zero or less.
Operators
-
operator +(
List< T> other) → List<T> -
Returns the concatenation of this list and
other
.override -
operator ==(
Object other) → bool -
The equality operator.
override
-
operator [](
int index) → T -
The object at the given
index
in the list.override -
operator []=(
int index, T value) → void -
Sets the value at the given
index
in the list tovalue
.override