dart.collection


Classes
DoubleLinkedQueue
DoubleLinkedQueueEntry
HasNextIterator
HashMap
HashSet
IterableBase
IterableMixin
LinkedHashMap
LinkedHashSet
LinkedList
LinkedListEntry
ListBase
ListMixin
ListQueue
Maps
Queue
SplayTreeMap
UnmodifiableListView

Classes and utilities that supplement the collection support in dart:core.


Class DoubleLinkedQueue extends IterableBase<E> implements Queue<E>

Fields
first: E
isEmpty: bool
iterator: _DoubleLinkedQueueIterator
last: E
length: int
single: E
Getters and Setters
first: E
isEmpty: bool
iterator: _DoubleLinkedQueueIterator<E>
last: E
length: int
single: E
Constructors
DoubleLinkedQueue()
DoubleLinkedQueue.from(Iterable<E> other)
Methods
add(E value): void
addAll(Iterable<E> iterable): void
addFirst(E value): void
addLast(E value): void
clear(): void
firstEntry(): DoubleLinkedQueueEntry<E>
forEachEntry(<E> f): void
lastEntry(): DoubleLinkedQueueEntry<E>
remove(Object o): bool
removeFirst(): E
removeLast(): E
removeWhere(<E> test): void
retainWhere(<E> test): void
toString(): String

A Queue implementation based on a double-linked list.

Allows constant time add, remove-at-ends and peek operations.

Can do removeAll and retainAll in linear time.

Fields

final E first
final bool isEmpty
final _DoubleLinkedQueueIterator iterator
final E last
final int length
final E single

Getters and Setters

E get first
bool get isEmpty
_DoubleLinkedQueueIterator<E> get iterator
E get last
int get length
E get single

Constructors

DoubleLinkedQueue()
factory DoubleLinkedQueue.from(Iterable<E> other)

Methods

void add(E value)

Adds value at the end of the queue.

void addAll(Iterable<E> iterable)

Adds all elements of iterable at the end of the queue. The length of the queue is extended by the length of iterable.

void addFirst(E value)

Adds value at the beginning of the queue.

void addLast(E value)

Adds value at the end of the queue.

void clear()

Removes all elements in the queue. The size of the queue becomes zero.

DoubleLinkedQueueEntry<E> firstEntry()
void forEachEntry(<E> f)
DoubleLinkedQueueEntry<E> lastEntry()
bool remove(Object o)

Remove a single instance of value from the queue.

Returns true if a value was removed, or false if the queue contained no element equal to value.

E removeFirst()

Removes and returns the first element of this queue. Throws an StateError exception if this queue is empty.

E removeLast()

Removes and returns the last element of the queue. Throws an StateError exception if this queue is empty.

void removeWhere(<E> test)
void retainWhere(<E> test)
String toString()

Returns a string representation of this object.


Class DoubleLinkedQueueEntry

Fields
element: E
Getters and Setters
element: E
element=(E e)
Constructors
DoubleLinkedQueueEntry(E e)
Methods
append(E e): void
nextEntry(): DoubleLinkedQueueEntry<E>
prepend(E e): void
previousEntry(): DoubleLinkedQueueEntry<E>
remove(): E

An entry in a doubly linked list. It contains a pointer to the next entry, the previous entry, and the boxed element.

WARNING: This class is temporary located in dart:core. It'll be removed at some point in the near future.

Fields

E element

Getters and Setters

E get element
set element=(E e)

Constructors

DoubleLinkedQueueEntry(E e)

Methods

void append(E e)
DoubleLinkedQueueEntry<E> nextEntry()
void prepend(E e)
DoubleLinkedQueueEntry<E> previousEntry()
E remove()

Class HasNextIterator

Fields
hasNext: bool
Getters and Setters
hasNext: bool
Constructors
HasNextIterator(Iterator<dynamic> _iterator)
Methods
next(): E

The HasNextIterator class wraps an Iterator and provides methods to iterate over an object using hasNext and next.

An HasNextIterator does not implement the Iterator interface.

Fields

final bool hasNext

Getters and Setters

bool get hasNext

Constructors

HasNextIterator(Iterator<dynamic> _iterator)

Methods

E next()

Abstract class HashMap implements Map<K, V>

Constructors
HashMap(<K, V> equals, <K, V> hashCode, <K, V> isValidKey)
HashMap.from(Map<K, V> other)
HashMap.fromIterable(Iterable<dynamic> iterable, <K, V> key, <K, V> value)
HashMap.fromIterables(Iterable<K> keys, Iterable<V> values)

A hash-table based implementation of Map.

The keys of a HashMap must have consistent Object.operator== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the keys (reflexive, anti-symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.

The map allows null as a key.

Constructors

factory HashMap(<K, V> equals, <K, V> hashCode, <K, V> isValidKey)

Creates a hash-table based Map.

The created map is not ordered in any way. When iterating the keys or values, the iteration order is unspecified except that it will stay the same as long as the map isn't changed.

If equals is provided, it is used to compare the keys in the table with new keys. If equals is omitted, the key's own Object.operator== is used instead.

Similar, if hashCode is provided, it is used to produce a hash value for keys in order to place them in the hash table. If it is omitted, the key's own Object.hashCode is used.

If using methods like operator[], remove and containsKey together with a custom equality and hashcode, an extra isValidKey function can be supplied. This function is called before calling equals or hashCode with an argument that may not be a K instance, and if the call returns false, the key is assumed to not be in the set. The isValidKey function defaults to just testing if the object is a K instance.

The used equals and hashCode method should always be consistent, so that if equals(a, b) then hashCode(a) == hashCode(b). The hash of an object, or what it compares equal to, should not change while the object is in the table. If it does change, the result is unpredictable.

It is generally the case that if you supply one of equals and hashCode, you also want to supply the other. The only common exception is to pass identical as the equality and use the default hash code.

factory HashMap.from(Map<K, V> other)

Creates a HashMap that contains all key value pairs of other.

factory HashMap.fromIterable(Iterable<dynamic> iterable, <K, V> key, <K, V> value)

Creates a HashMap where the keys and values are computed from the iterable.

For each element of the iterable this constructor computes a key/value pair, by applying key and value respectively.

The keys of the key/value pairs do not need to be unique. The last occurrence of a key will simply overwrite any previous value.

If no values are specified for key and value the default is the identity function.

factory HashMap.fromIterables(Iterable<K> keys, Iterable<V> values)

Creates a HashMap associating the given keys to values.

This constructor iterates over keys and values and maps each element of keys to the corresponding element of values.

If keys contains the same object multiple times, the last occurrence overwrites the previous value.

It is an error if the two Iterables don't have the same length.


Class HashSet implements Set<E>

Constructors
HashSet(<E> equals, <E> hashCode, <E> isValidKey)
HashSet.from(Iterable<E> iterable)

A HashSet is a hash-table based Set implementation.

The elements of a HashSet must have consistent equality and hashCode implementations. This means that the equals operation must define a stable equivalence relation on the elements (reflexive, anti-symmetric, transitive, and consistent over time), and that the hashCode must consistent with equality, so that the same for objects that are considered equal.

The set allows null as an element.

Most simple operations on HashSet are done in constant time: add, contains, remove, and length.

Constructors

factory HashSet(<E> equals, <E> hashCode, <E> isValidKey)

Create a hash set using the provided equals as equality.

The provided equals must define a stable equivalence relation, and hashCode must be consistent with equals. If the equals or hashCode methods won't work on all objects, but only to instances of E, the isValidKey predicate can be used to restrict the keys that they are applied to. Any key for which isValidKey returns false is automatically assumed to not be in the set.

If equals, hashCode and isValidKey are omitted, the set uses the objects' intrinsic Object.operator== and Object.hashCode.

If isValidKey is omitted, it defaults to testing if the object is an E instance.

If equals is identical, this creates an identity set. Any hashCode is compatible with identical, and it applies to all objects, so hashCode and isValidKey can safely be omitted.

factory HashSet.from(Iterable<E> iterable)

Create a hash set containing the elements of iterable.

Creates a hash set as by new HashSet<E>() and adds each element of iterable to this set in the order they are iterated.


Abstract class IterableBase implements Iterable<E>

Fields
first: E
isEmpty: bool
isNotEmpty: bool
last: E
length: int
single: E
Getters and Setters
first: E
isEmpty: bool
isNotEmpty: bool
last: E
length: int
single: E
Constructors
IterableBase()
Methods
any(<E> f): bool
contains(Object element): bool
elementAt(int index): E
every(<E> f): bool
expand(<E> f): Iterable<dynamic>
firstWhere(<E> test, <E> orElse): dynamic
fold(dynamic initialValue, <E> combine): dynamic
forEach(<E> f): void
join(String separator): String
lastWhere(<E> test, <E> orElse): dynamic
map(<E> f): Iterable<dynamic>
reduce(<E> combine): E
singleWhere(<E> test): E
skip(int n): Iterable<E>
skipWhile(<E> test): Iterable<E>
take(int n): Iterable<E>
takeWhile(<E> test): Iterable<E>
toList(bool growable): List<E>
toSet(): Set<E>
where(<E> f): Iterable<E>

Base class for implementing Iterable.

This class implements all methods of Iterable except Iterable.iterator in terms of iterator.

Fields

final E first
final bool isEmpty
final bool isNotEmpty
final E last
final int length
final E single

Getters and Setters

E get first
bool get isEmpty
bool get isNotEmpty
E get last
int get length
E get single

Constructors

IterableBase()

Methods

bool any(<E> f)

Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.

bool contains(Object element)

Returns true if the collection contains an element equal to element.

E elementAt(int index)

Returns the indexth element.

If this has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

bool every(<E> f)

Returns true if every elements of this collection satisify the predicate test. Returns false otherwise.

Iterable<dynamic> expand(<E> f)

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and calls f for each element of this every time it's iterated.

dynamic firstWhere(<E> test, <E> orElse)

Returns the first element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

dynamic fold(dynamic initialValue, <E> combine)

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.

Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of an iterable:

iterable.fold(0, (prev, element) => prev + element);

void forEach(<E> f)

Applies the function f to each element of this collection.

String join(String separator)

Converts each element to a String and concatenates the strings.

Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.

dynamic lastWhere(<E> test, <E> orElse)

Returns the last element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

Iterable<dynamic> map(<E> f)

Returns a lazy Iterable where each element e of this is replaced by the result of f(e).

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

E reduce(<E> combine)

Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.

Example of calculating the sum of an iterable:

iterable.reduce((value, element) => value + element);

E singleWhere(<E> test)

Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.

Iterable<E> skip(int n)

Returns an Iterable that skips the first n elements.

If this has fewer than n elements, then the resulting Iterable is empty.

It is an error if n is negative.

Iterable<E> skipWhile(<E> test)

Returns an Iterable that skips elements while test is satisfied.

The filtering happens lazily. Every new Iterator of the returned Iterable iterates over all elements of this.

As long as the iterator's elements satisfy test they are discarded. Once an element does not satisfy the test the iterator stops testing and uses every later element unconditionally. That is, the elements of the returned Iterable are the elements of this starting from the first element that does not satisfy test.

Iterable<E> take(int n)

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

Iterable<E> takeWhile(<E> test)

Returns an Iterable that stops once test is not satisfied anymore.

The filtering happens lazily. Every new Iterator of the returned Iterable starts iterating over the elements of this.

When the iterator encounters an element e that does not satisfy test, it discards e and moves into the finished state. That is, it does not get or provide any more elements.

List<E> toList(bool growable)

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

Set<E> toSet()

Creates a Set containing the elements of this Iterable.

Iterable<E> where(<E> f)

Returns a lazy Iterable with all elements that satisfy the predicate test.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable will invoke the supplied function test multiple times on the same element.


Abstract class IterableMixin implements Iterable<E>

Fields
first: E
isEmpty: bool
isNotEmpty: bool
last: E
length: int
single: E
Getters and Setters
first: E
isEmpty: bool
isNotEmpty: bool
last: E
length: int
single: E
Constructors
IterableMixin()
Methods
any(<E> f): bool
contains(Object element): bool
elementAt(int index): E
every(<E> f): bool
expand(<E> f): Iterable<dynamic>
firstWhere(<E> test, <E> orElse): dynamic
fold(dynamic initialValue, <E> combine): dynamic
forEach(<E> f): void
join(String separator): String
lastWhere(<E> test, <E> orElse): dynamic
map(<E> f): Iterable<dynamic>
reduce(<E> combine): E
singleWhere(<E> test): E
skip(int n): Iterable<E>
skipWhile(<E> test): Iterable<E>
take(int n): Iterable<E>
takeWhile(<E> test): Iterable<E>
toList(bool growable): List<E>
toSet(): Set<E>
where(<E> f): Iterable<E>

This Iterable mixin implements all Iterable members except iterator.

All other methods are implemented in terms of iterator.

Fields

final E first
final bool isEmpty
final bool isNotEmpty
final E last
final int length
final E single

Getters and Setters

E get first
bool get isEmpty
bool get isNotEmpty
E get last
int get length
E get single

Constructors

IterableMixin()

Methods

bool any(<E> f)

Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.

bool contains(Object element)

Returns true if the collection contains an element equal to element.

E elementAt(int index)

Returns the indexth element.

If this has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

bool every(<E> f)

Returns true if every elements of this collection satisify the predicate test. Returns false otherwise.

Iterable<dynamic> expand(<E> f)

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and calls f for each element of this every time it's iterated.

dynamic firstWhere(<E> test, <E> orElse)

Returns the first element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

dynamic fold(dynamic initialValue, <E> combine)

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.

Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of an iterable:

iterable.fold(0, (prev, element) => prev + element);

void forEach(<E> f)

Applies the function f to each element of this collection.

String join(String separator)

Converts each element to a String and concatenates the strings.

Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.

dynamic lastWhere(<E> test, <E> orElse)

Returns the last element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

Iterable<dynamic> map(<E> f)

Returns a lazy Iterable where each element e of this is replaced by the result of f(e).

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

E reduce(<E> combine)

Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.

Example of calculating the sum of an iterable:

iterable.reduce((value, element) => value + element);

E singleWhere(<E> test)

Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.

Iterable<E> skip(int n)

Returns an Iterable that skips the first n elements.

If this has fewer than n elements, then the resulting Iterable is empty.

It is an error if n is negative.

Iterable<E> skipWhile(<E> test)

Returns an Iterable that skips elements while test is satisfied.

The filtering happens lazily. Every new Iterator of the returned Iterable iterates over all elements of this.

As long as the iterator's elements satisfy test they are discarded. Once an element does not satisfy the test the iterator stops testing and uses every later element unconditionally. That is, the elements of the returned Iterable are the elements of this starting from the first element that does not satisfy test.

Iterable<E> take(int n)

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

Iterable<E> takeWhile(<E> test)

Returns an Iterable that stops once test is not satisfied anymore.

The filtering happens lazily. Every new Iterator of the returned Iterable starts iterating over the elements of this.

When the iterator encounters an element e that does not satisfy test, it discards e and moves into the finished state. That is, it does not get or provide any more elements.

List<E> toList(bool growable)

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

Set<E> toSet()

Creates a Set containing the elements of this Iterable.

Iterable<E> where(<E> f)

Returns a lazy Iterable with all elements that satisfy the predicate test.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable will invoke the supplied function test multiple times on the same element.


Abstract class LinkedHashMap implements HashMap<K, V>

Constructors
LinkedHashMap(<K, V> equals, <K, V> hashCode, <K, V> isValidKey)
LinkedHashMap.from(Map<K, V> other)
LinkedHashMap.fromIterable(Iterable<dynamic> iterable, <K, V> key, <K, V> value)
LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values)

A hash-table based implementation of Map.

Keys insertion order is remembered, and keys are iterated in insertion order. Values are iterated in their corresponding key's order.

The keys of a LinkedHashMap must have consistent Object.operator== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the keys (reflexive, anti-symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.

The map allows null as a key.

Constructors

factory LinkedHashMap(<K, V> equals, <K, V> hashCode, <K, V> isValidKey)
factory LinkedHashMap.from(Map<K, V> other)

Creates a LinkedHashMap that contains all key value pairs of other.

factory LinkedHashMap.fromIterable(Iterable<dynamic> iterable, <K, V> key, <K, V> value)

Creates a LinkedHashMap where the keys and values are computed from the iterable.

For each element of the iterable this constructor computes a key/value pair, by applying key and value respectively.

The keys of the key/value pairs do not need to be unique. The last occurrence of a key will simply overwrite any previous value.

If no values are specified for key and value the default is the identity function.

factory LinkedHashMap.fromIterables(Iterable<K> keys, Iterable<V> values)

Creates a LinkedHashMap associating the given keys to values.

This constructor iterates over keys and values and maps each element of keys to the corresponding element of values.

If keys contains the same object multiple times, the last occurrence overwrites the previous value.

It is an error if the two Iterables don't have the same length.


Class LinkedHashSet implements HashSet<E>

Constructors
LinkedHashSet(<E> equals, <E> hashCode, <E> isValidKey)
LinkedHashSet.from(Iterable<E> iterable)

A LinkedHashSet is a hash-table based Set implementation.

The LinkedHashSet also keep track of the order that elements were inserted in, and iteration happens in first-to-last insertion order.

The elements of a LinkedHashSet must have consistent Object.operator== and Object.hashCode implementations. This means that the == operator must define a stable equivalence relation on the elements (reflexive, anti-symmetric, transitive, and consistent over time), and that hashCode must be the same for objects that are considered equal by ==.

The set allows null as an element.

Most simple operations on HashSet are done in constant time: add, contains, remove, and length.

Constructors

factory LinkedHashSet(<E> equals, <E> hashCode, <E> isValidKey)
factory LinkedHashSet.from(Iterable<E> iterable)

Class LinkedList extends IterableBase<E> implements _LinkedListLink

Fields
first: E
isEmpty: bool
iterator: Iterator
last: E
length: int
single: E
Getters and Setters
first: E
isEmpty: bool
iterator: Iterator<E>
last: E
length: int
single: E
Constructors
LinkedList()
Methods
add(E entry): void
addAll(Iterable<E> entries): void
addFirst(E entry): void
clear(): void
forEach(<E> action): void
remove(E entry): bool
toString(): String

A linked list implementation, providing O(1) removal(unlink) of elements and manual traversal through next and previous.

The list elements must extend LinkedListEntry.

Fields

final E first
final bool isEmpty
final Iterator iterator
final E last
final int length
final E single

Getters and Setters

E get first
bool get isEmpty
Iterator<E> get iterator
E get last
int get length
E get single

Constructors

LinkedList()

Construct a new empty linked list.

Methods

void add(E entry)

Add entry to the end of the list.

void addAll(Iterable<E> entries)

Add entries to the end of the list.

void addFirst(E entry)

Add entry to the beginning of the list.

void clear()
void forEach(<E> action)

Call action with each entry in the list.

It's an error if action modify the list.

bool remove(E entry)

Remove entry from the list. This is the same as calling entry.unlink().

If entry is not in the list, false is returned.

String toString()

Returns a string representation of this object.


Abstract class LinkedListEntry implements _LinkedListLink

Fields
list: LinkedList
next: E
previous: E
Getters and Setters
list: LinkedList<E>
next: E
previous: E
Constructors
LinkedListEntry()
Methods
insertAfter(E entry): void
insertBefore(E entry): void
unlink(): void

Entry element for a LinkedList. Any entry must extend this class.

Fields

final LinkedList list
final E next
final E previous

Getters and Setters

LinkedList<E> get list

Get the list containing this element.

E get next

Return the succeeding element in the list.

E get previous

Return the preceeding element in the list.

Constructors

LinkedListEntry()

Methods

void insertAfter(E entry)

insert an element after this.

void insertBefore(E entry)

Insert an element before this.

void unlink()

Unlink the element from the list.


Class ListBase with ListMixin<E>

Constructors
ListBase()

Abstract implementation of a list.

All operations are defined in terms of length, operatorListBase(), operatorListBase()= and length=, which need to be implemented.

Constructors

ListBase()

Abstract class ListMixin implements List<E>

Fields
first: E
isEmpty: bool
isNotEmpty: bool
iterator: Iterator
last: E
reversed: Iterable
single: E
Getters and Setters
first: E
isEmpty: bool
isNotEmpty: bool
iterator: Iterator<E>
last: E
reversed: Iterable<E>
single: E
Constructors
ListMixin()
Methods
add(E element): void
addAll(Iterable<E> iterable): void
any(<E> test): bool
asMap(): Map<int, E>
clear(): void
contains(Object element): bool
elementAt(int index): E
every(<E> test): bool
expand(<E> f): Iterable<dynamic>
fillRange(int start, int end, E fill): void
firstWhere(<E> test, <E> orElse): dynamic
fold(dynamic initialValue, <E> combine): dynamic
forEach(<E> action): void
getRange(int start, int end): Iterable<E>
indexOf(Object element, int startIndex): int
insert(int index, E element): void
insertAll(int index, Iterable<E> iterable): void
join(String separator): String
lastIndexOf(Object element, int startIndex): int
lastWhere(<E> test, <E> orElse): dynamic
map(<E> f): Iterable<dynamic>
reduce(<E> combine): E
remove(Object element): bool
removeAt(int index): E
removeLast(): E
removeRange(int start, int end): void
removeWhere(<E> test): void
replaceRange(int start, int end, Iterable<E> newContents): void
retainWhere(<E> test): void
setAll(int index, Iterable<E> iterable): void
setRange(int start, int end, Iterable<E> iterable, int skipCount): void
singleWhere(<E> test): E
skip(int count): Iterable<E>
skipWhile(<E> test): Iterable<E>
sort(<E> compare): void
sublist(int start, int end): List<E>
take(int count): Iterable<E>
takeWhile(<E> test): Iterable<E>
toList(bool growable): List<E>
toSet(): Set<E>
toString(): String
where(<E> test): Iterable<E>

Base implementation of a List class.

This class can be used as a mixin.

This implements all read operations using only the length and operatorListMixin() members. It implements write operations using those and length= and operatorListMixin()=

Fields

final E first
final bool isEmpty
final bool isNotEmpty
final Iterator iterator
final E last
final Iterable reversed
final E single

Getters and Setters

E get first
bool get isEmpty
bool get isNotEmpty
Iterator<E> get iterator
E get last
Iterable<E> get reversed
E get single

Constructors

ListMixin()

Methods

void add(E element)

Adds value to the end of this list, extending the length by one.

Throws an UnsupportedError if the list is fixed-length.

void addAll(Iterable<E> iterable)

Appends all objects of iterable to the end of this list.

Extends the length of the list by the number of objects in iterable. Throws an UnsupportedError if this list is fixed-length.

bool any(<E> test)

Returns true if one element of this collection satisfies the predicate test. Returns false otherwise.

Map<int, E> asMap()

Returns an unmodifiable Map view of this.

The map uses the indices of this list as keys and the corresponding objects as values. The Map.keys Iterable iterates the indices of this list in numerical order.

void clear()

Removes all objects from this list; the length of the list becomes zero.

Throws an UnsupportedError, and retains all objects, if this is a fixed-length list.

bool contains(Object element)

Returns true if the collection contains an element equal to element.

E elementAt(int index)

Returns the indexth element.

If this has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

bool every(<E> test)

Returns true if every elements of this collection satisify the predicate test. Returns false otherwise.

Iterable<dynamic> expand(<E> f)

Expands each element of this Iterable into zero or more elements.

The resulting Iterable runs through the elements returned by f for each element of this, in order.

The returned Iterable is lazy, and calls f for each element of this every time it's iterated.

void fillRange(int start, int end, E fill)

Sets the objects in the range start inclusive to end exclusive to the given fillValue.

An error occurs if start..end is not a valid range for this.

dynamic firstWhere(<E> test, <E> orElse)

Returns the first element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

dynamic fold(dynamic initialValue, <E> combine)

Reduces a collection to a single value by iteratively combining each element of the collection with an existing value using the provided function.

Use initialValue as the initial value, and the function combine to create a new value from the previous one and an element.

Example of calculating the sum of an iterable:

iterable.fold(0, (prev, element) => prev + element);

void forEach(<E> action)

Applies the function f to each element of this collection.

Iterable<E> getRange(int start, int end)

Returns an Iterable that iterates over the objects in the range start inclusive to end exclusive.

An error occurs if end is before start.

An error occurs if the start and end are not valid ranges at the time of the call to this method. The returned Iterable behaves like skip(start).take(end - start). That is, it does not throw exceptions if this changes size.

Example:

var list = [1, 2, 3, 4, 5];
var range = list.getRange(1, 4);
print(range.join(', '));  // => 2, 3, 4
list.length = 3;
print(range.join(', '));  // => 2, 3

int indexOf(Object element, int startIndex)

Returns the first index of element in this list.

Searches the list from index start to the length of the list. The first time an object o is encountered so that o == element, the index of o is returned. Returns -1 if element is not found.

void insert(int index, E element)

Inserts the object at position index in this list.

This increases the length of the list by one and shifts all objects at or after the index towards the end of the list.

An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.

void insertAll(int index, Iterable<E> iterable)

Inserts all objects of iterable at position index in this list.

This increases the length of the list by the length of iterable and shifts all later objects towards the end of the list.

An error occurs if the index is less than 0 or greater than length. An UnsupportedError occurs if the list is fixed-length.

String join(String separator)

Converts each element to a String and concatenates the strings.

Converts each element to a String by calling Object.toString on it. Then concatenates the strings, optionally separated by the separator string.

int lastIndexOf(Object element, int startIndex)

Returns the last index in the list a of the given element, starting the search at index startIndex to 0. Returns -1 if element is not found.

dynamic lastWhere(<E> test, <E> orElse)

Returns the last element that satisfies the given predicate test.

If none matches, the result of invoking the orElse function is returned. By default, when orElse is null, a StateError is thrown.

Iterable<dynamic> map(<E> f)

Returns a lazy Iterable where each element e of this is replaced by the result of f(e).

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function f will not be invoked. The transformed elements will not be cached. Iterating multiple times over the the returned Iterable will invoke the supplied function f multiple times on the same element.

E reduce(<E> combine)

Reduces a collection to a single value by iteratively combining elements of the collection using the provided function.

Example of calculating the sum of an iterable:

iterable.reduce((value, element) => value + element);

bool remove(Object element)

Removes the first occurence of value from this list.

Returns true if value was in the list. Returns false otherwise.

The method has no effect if value was not in the list.

An UnsupportedError occurs if the list is fixed-length.

E removeAt(int index)

Removes the object at position index from this list.

This method reduces the length of this by one and moves all later objects down by one position.

Returns the removed object.

  • Throws an ArgumentError if index is not an int.
  • Throws a RangeError if the index is out of range for this list.
  • Throws an UnsupportedError, and doesn't remove the object,
if this is a fixed-length list.

E removeLast()

Pops and returns the last object in this list.

Throws an UnsupportedError if this is a fixed-length list.

void removeRange(int start, int end)

Removes the objects in the range start inclusive to end exclusive.

An error occurs if start..end is not a valid range for this. Throws an UnsupportedError if this is a fixed-length list.

void removeWhere(<E> test)

Removes all objects from this list that satisfy test.

An object o satisfies test if test(o) is true.

Throws an UnsupportedError if this is a fixed-length list.

void replaceRange(int start, int end, Iterable<E> newContents)

Removes the objects in the range start inclusive to end exclusive and replaces them with the contents of the iterable.

An error occurs if start..end is not a valid range for this.

Example:

var list = [1, 2, 3, 4, 5];
list.replaceRange(1, 3, [6, 7, 8, 9]);
print(list);  // [1, 6, 7, 8, 9, 4, 5]

void retainWhere(<E> test)

Removes all objects from this list that fail to satisfy test.

An object o satisfies test if test(o) is true.

Throws an UnsupportedError if this is a fixed-length list.

void setAll(int index, Iterable<E> iterable)

Overwrites objects of this with the objects of iterable, starting at position index in this list.

This operation does not increase the length of this.

An error occurs if the index is less than 0 or greater than length. An error occurs if the iterable is longer than length - index.

void setRange(int start, int end, Iterable<E> iterable, int skipCount)

Copies the objects of iterable, skipping skipCount objects first, into the range start inclusive to end exclusive of this.

If start equals end and start..end represents a legal range, this method has no effect.

An error occurs if start..end is not a valid range for this. An error occurs if the iterable does not have enough objects after skipping skipCount objects.

Example:

var list = [1, 2, 3, 4];
var list2 = [5, 6, 7, 8, 9];
list.setRange(1, 3, list2, 3);
print(list);  // => [1, 8, 9, 4]

E singleWhere(<E> test)

Returns the single element that satisfies test. If no or more than one element match then a StateError is thrown.

Iterable<E> skip(int count)

Returns an Iterable that skips the first n elements.

If this has fewer than n elements, then the resulting Iterable is empty.

It is an error if n is negative.

Iterable<E> skipWhile(<E> test)

Returns an Iterable that skips elements while test is satisfied.

The filtering happens lazily. Every new Iterator of the returned Iterable iterates over all elements of this.

As long as the iterator's elements satisfy test they are discarded. Once an element does not satisfy the test the iterator stops testing and uses every later element unconditionally. That is, the elements of the returned Iterable are the elements of this starting from the first element that does not satisfy test.

void sort(<E> compare)

Sorts this list according to the order specified by the compare function.

The compare function must act as a Comparator.

The default List implementations use Comparable.compare if compare is omitted.

List<E> sublist(int start, int end)

Returns a new list containing the objects from start inclusive to end exclusive.

If end is omitted, the length of this is used.

An error occurs if start is outside the range 0 .. length or if end is outside the range start .. length.

Iterable<E> take(int count)

Returns an Iterable with at most n elements.

The returned Iterable may contain fewer than n elements, if this contains fewer than n elements.

It is an error if n is negative.

Iterable<E> takeWhile(<E> test)

Returns an Iterable that stops once test is not satisfied anymore.

The filtering happens lazily. Every new Iterator of the returned Iterable starts iterating over the elements of this.

When the iterator encounters an element e that does not satisfy test, it discards e and moves into the finished state. That is, it does not get or provide any more elements.

List<E> toList(bool growable)

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

Set<E> toSet()

Creates a Set containing the elements of this Iterable.

String toString()

Returns a string representation of this object.

Iterable<E> where(<E> test)

Returns a lazy Iterable with all elements that satisfy the predicate test.

This method returns a view of the mapped elements. As long as the returned Iterable is not iterated over, the supplied function test will not be invoked. Iterating will not cache results, and thus iterating multiple times over the returned Iterable will invoke the supplied function test multiple times on the same element.


Class ListQueue extends IterableBase<E> implements Queue<E>

Fields
first: E
isEmpty: bool
iterator: Iterator
last: E
length: int
single: E
Getters and Setters
first: E
isEmpty: bool
iterator: Iterator<E>
last: E
length: int
single: E
Constructors
ListQueue(int initialCapacity)
ListQueue.from(Iterable<E> source)
Methods
add(E element): void
addAll(Iterable<E> elements): void
addFirst(E element): void
addLast(E element): void
clear(): void
elementAt(int index): E
forEach(<E> action): void
remove(Object object): bool
removeFirst(): E
removeLast(): E
removeWhere(<E> test): void
retainWhere(<E> test): void
toList(bool growable): List<E>
toString(): String

List based Queue.

Keeps a cyclic buffer of elements, and grows to a larger buffer when it fills up. This guarantees constant time peek and remove operations, and amortized constant time add operations.

The structure is efficient for any queue or stack usage.

Operations like removeAll and removeWhere() are very inefficient. If those are needed, use a DoubleLinkedQueue instead.

Fields

final E first
final bool isEmpty
final Iterator iterator
final E last
final int length
final E single

Getters and Setters

E get first
bool get isEmpty
Iterator<E> get iterator
E get last
int get length
E get single

Constructors

ListQueue(int initialCapacity)

Create an empty queue.

If initialCapacity is given, prepare the queue for at least that many elements.

factory ListQueue.from(Iterable<E> source)

Create a queue initially containing the elements of source.

Methods

void add(E element)

Adds value at the end of the queue.

void addAll(Iterable<E> elements)

Adds all elements of iterable at the end of the queue. The length of the queue is extended by the length of iterable.

void addFirst(E element)

Adds value at the beginning of the queue.

void addLast(E element)

Adds value at the end of the queue.

void clear()

Removes all elements in the queue. The size of the queue becomes zero.

E elementAt(int index)

Returns the indexth element.

If this has fewer than index elements throws a RangeError.

Note: if this does not have a deterministic iteration order then the function may simply return any element without any iteration if there are at least index elements in this.

void forEach(<E> action)

Applies the function f to each element of this collection.

bool remove(Object object)

Remove a single instance of value from the queue.

Returns true if a value was removed, or false if the queue contained no element equal to value.

E removeFirst()

Removes and returns the first element of this queue. Throws an StateError exception if this queue is empty.

E removeLast()

Removes and returns the last element of the queue. Throws an StateError exception if this queue is empty.

void removeWhere(<E> test)

Remove all elements matched by test.

This method is inefficient since it works by repeatedly removing single elements, each of which can take linear time.

void retainWhere(<E> test)

Remove all elements not matched by test.

This method is inefficient since it works by repeatedly removing single elements, each of which can take linear time.

List<E> toList(bool growable)

Creates a List containing the elements of this Iterable.

The elements are in iteration order. The list is fixed-length if growable is false.

String toString()

Returns a string representation of this object.


Class Maps

Constructors
Maps()
Methods
clear(Map<dynamic, dynamic> map): dynamic
containsKey(Map<dynamic, dynamic> map, dynamic key): bool
containsValue(Map<dynamic, dynamic> map, dynamic value): bool
forEach(Map<dynamic, dynamic> map, f): dynamic
getValues(Map<dynamic, dynamic> map): Iterable<dynamic>
isEmpty(Map<dynamic, dynamic> map): bool
isNotEmpty(Map<dynamic, dynamic> map): bool
length(Map<dynamic, dynamic> map): int
mapToString(Map<dynamic, dynamic> m): String
putIfAbsent(Map<dynamic, dynamic> map, dynamic key, ifAbsent): dynamic

Constructors

Maps()

Methods

static dynamic clear(Map<dynamic, dynamic> map)
static bool containsKey(Map<dynamic, dynamic> map, dynamic key)
static bool containsValue(Map<dynamic, dynamic> map, dynamic value)
static dynamic forEach(Map<dynamic, dynamic> map, f)
static Iterable<dynamic> getValues(Map<dynamic, dynamic> map)
static bool isEmpty(Map<dynamic, dynamic> map)
static bool isNotEmpty(Map<dynamic, dynamic> map)
static int length(Map<dynamic, dynamic> map)
static String mapToString(Map<dynamic, dynamic> m)

Returns a string representing the specified map. The returned string looks like this: '{key0: value0, key1: value1, ... keyN: valueN}'. The value returned by its toString method is used to represent each key or value.

If the map collection contains a reference to itself, either directly as a key or value, or indirectly through other collections or maps, the contained reference is rendered as '{...}'. This prevents the infinite regress that would otherwise occur. So, for example, calling this method on a map whose sole entry maps the string key 'me' to a reference to the map would return '{me: {...}}'.

A typical implementation of a map's toString method will simply return the results of this method applied to the collection.

static dynamic putIfAbsent(Map<dynamic, dynamic> map, dynamic key, ifAbsent)

Abstract class Queue implements Iterable<E>

Constructors
Queue()
Queue.from(Iterable<E> other)
Methods
add(E value): void
addAll(Iterable<E> iterable): void
addFirst(E value): void
addLast(E value): void
clear(): void
remove(Object object): bool
removeFirst(): E
removeLast(): E

A Queue is a collection that can be manipulated at both ends. One can iterate over the elements of a queue through forEach or with an Iterator.

Constructors

factory Queue()

Creates a queue.

factory Queue.from(Iterable<E> other)

Creates a queue with the elements of other. The order in the queue will be the order provided by the iterator of other.

Methods

void add(E value)

Adds value at the end of the queue.

void addAll(Iterable<E> iterable)

Adds all elements of iterable at the end of the queue. The length of the queue is extended by the length of iterable.

void addFirst(E value)

Adds value at the beginning of the queue.

void addLast(E value)

Adds value at the end of the queue.

void clear()

Removes all elements in the queue. The size of the queue becomes zero.

bool remove(Object object)

Remove a single instance of value from the queue.

Returns true if a value was removed, or false if the queue contained no element equal to value.

E removeFirst()

Removes and returns the first element of this queue. Throws an StateError exception if this queue is empty.

E removeLast()

Removes and returns the last element of the queue. Throws an StateError exception if this queue is empty.


Class SplayTreeMap extends _SplayTree<K> implements Map<K, V>

Fields
isEmpty: bool
isNotEmpty: bool
keys: Iterable
length: int
values: Iterable
Getters and Setters
isEmpty: bool
isNotEmpty: bool
keys: Iterable<K>
length: int
values: Iterable<V>
Constructors
SplayTreeMap(<K, V> compare, <K, V> isValidKey)
SplayTreeMap.from(Map<K, V> other, <K, V> compare, <K, V> isValidKey)
SplayTreeMap.fromIterable(Iterable<K> iterable, <K, V> key, <K, V> value, <K, V> compare, <K, V> isValidKey)
SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values, <K, V> compare, <K, V> isValidKey)
Methods
[](Object key): V
[]=(K key, V value): void
addAll(Map<K, V> other): void
clear(): void
containsKey(Object key): bool
containsValue(Object value): bool
firstKey(): K
firstKeyAfter(K key): K
forEach(<K, V> f): void
lastKey(): K
lastKeyBefore(K key): K
putIfAbsent(K key, <K, V> ifAbsent): V
remove(Object key): V
toString(): String

Fields

final bool isEmpty
final bool isNotEmpty
final Iterable keys
final int length
final Iterable values

Getters and Setters

bool get isEmpty
bool get isNotEmpty
Iterable<K> get keys
int get length
Iterable<V> get values

Constructors

SplayTreeMap(<K, V> compare, <K, V> isValidKey)
factory SplayTreeMap.from(Map<K, V> other, <K, V> compare, <K, V> isValidKey)

Creates a SplayTreeMap that contains all key value pairs of other.

factory SplayTreeMap.fromIterable(Iterable<K> iterable, <K, V> key, <K, V> value, <K, V> compare, <K, V> isValidKey)

Creates a SplayTreeMap where the keys and values are computed from the iterable.

For each element of the iterable this constructor computes a key/value pair, by applying key and value respectively.

The keys of the key/value pairs do not need to be unique. The last occurrence of a key will simply overwrite any previous value.

If no values are specified for key and value the default is the identity function.

factory SplayTreeMap.fromIterables(Iterable<K> keys, Iterable<V> values, <K, V> compare, <K, V> isValidKey)

Creates a SplayTreeMap associating the given keys to values.

This constructor iterates over keys and values and maps each element of keys to the corresponding element of values.

If keys contains the same object multiple times, the last occurrence overwrites the previous value.

It is an error if the two Iterables don't have the same length.

Methods

V [](Object key)

Returns the value for the given key or null if key is not in the map. Because null values are supported, one should either use containsKey to distinguish between an absent key and a null value, or use the putIfAbsent method.

void []=(K key, V value)

Associates the key with the given value.

void addAll(Map<K, V> other)

Adds all key-value pairs of other to this map.

If a key of other is already in this map, its value is overwritten.

The operation is equivalent to doing thiskey = value for each key and associated value in other. It iterates over other, which must therefore not change during the iteration.

void clear()

Removes all pairs from the map.

bool containsKey(Object key)

Returns true if this map contains the given key.

bool containsValue(Object value)

Returns true if this map contains the given value.

K firstKey()

Get the first key in the map. Returns null if the map is empty.

K firstKeyAfter(K key)

Get the first key in the map that is strictly larger than key. Returns null if no key was not found.

void forEach(<K, V> f)

Applies f to each {key, value} pair of the map.

It is an error to add or remove keys from the map during iteration.

K lastKey()

Get the last key in the map. Returns null if the map is empty.

K lastKeyBefore(K key)

Get the last key in the map that is strictly smaller than key. Returns null if no key was not found.

V putIfAbsent(K key, <K, V> ifAbsent)

If key is not associated to a value, calls ifAbsent and updates the map by mapping key to the value returned by ifAbsent. Returns the value in the map.

It is an error to add or remove keys from the map during the call to ifAbsent.

V remove(Object key)

Removes the association for the given key. Returns the value for key in the map or null if key is not in the map. Note that values can be null and a returned null value does not always imply that the key is absent.

String toString()

Returns a string representation of this object.


Class UnmodifiableListView extends UnmodifiableListBase<E>

Fields
length: int
Getters and Setters
length: int
Constructors
UnmodifiableListView(Iterable<E> source)
Methods
[](int index): E

An unmodifiable List view of another List.

The source of the elements may be a List or any Iterable with efficient Iterable.length and Iterable.elementAt.

Fields

final int length

Getters and Setters

int get length

Constructors

UnmodifiableListView(Iterable<E> source)

Create an unmodifiable list backed by source.

Methods

E [](int index)