DoubleLinkedQueue<E> class
Implementation of a double linked list that box list elements into DoubleLinkedQueueEntry objects.
class DoubleLinkedQueue<E> implements Queue<E> { _DoubleLinkedQueueEntrySentinel<E> _sentinel; DoubleLinkedQueue() { _sentinel = new _DoubleLinkedQueueEntrySentinel<E>(); } factory DoubleLinkedQueue.from(Iterable<E> other) { Queue<E> list = new DoubleLinkedQueue(); for (final e in other) { list.addLast(e); } return list; } void addLast(E value) { _sentinel.prepend(value); } void addFirst(E value) { _sentinel.append(value); } void add(E value) { addLast(value); } void addAll(Collection<E> collection) { for (final e in collection) { add(e); } } E removeLast() { return _sentinel._previous.remove(); } E removeFirst() { return _sentinel._next.remove(); } E first() { return _sentinel._next.element; } E last() { return _sentinel._previous.element; } DoubleLinkedQueueEntry<E> lastEntry() { return _sentinel.previousEntry(); } DoubleLinkedQueueEntry<E> firstEntry() { return _sentinel.nextEntry(); } int get length { int counter = 0; forEach(void _(E element) { counter++; }); return counter; } bool isEmpty() { return (_sentinel._next === _sentinel); } void clear() { _sentinel._next = _sentinel; _sentinel._previous = _sentinel; } void forEach(void f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; f(entry._element); entry = nextEntry; } } void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; f(entry); entry = nextEntry; } } bool every(bool f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (!f(entry._element)) return false; entry = nextEntry; } return true; } bool some(bool f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (f(entry._element)) return true; entry = nextEntry; } return false; } Queue map(f(E element)) { Queue other = new Queue(); DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; other.addLast(f(entry._element)); entry = nextEntry; } return other; } Dynamic reduce(Dynamic initialValue, Dynamic combine(Dynamic previousValue, E element)) { return Collections.reduce(this, initialValue, combine); } Queue<E> filter(bool f(E element)) { Queue<E> other = new Queue<E>(); DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (f(entry._element)) other.addLast(entry._element); entry = nextEntry; } return other; } _DoubleLinkedQueueIterator<E> iterator() { return new _DoubleLinkedQueueIterator<E>(_sentinel); } String toString() { return Collections.collectionToString(this); } }
Implements
Constructors
new DoubleLinkedQueue() #
DoubleLinkedQueue() { _sentinel = new _DoubleLinkedQueueEntrySentinel<E>(); }
Properties
final int length #
Returns the number of elements in this collection.
int get length { int counter = 0; forEach(void _(E element) { counter++; }); return counter; }
Methods
void addAll(Collection<E> collection) #
void clear() #
bool contains(E element) #
Check whether the collection contains an element equal to element.
bool contains(E element) { for (E e in this) { if (e == element) return true; } return false; }
bool every(bool f(E element)) #
Returns true if every elements of this collection satisify the predicate f. Returns false otherwise.
bool every(bool f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (!f(entry._element)) return false; entry = nextEntry; } return true; }
Queue<E> filter(bool f(E element)) #
Returns a collection with the elements of this collection that satisfy the predicate f.
The returned collection should be of the same type as the collection creating it.
An element satisfies the predicate
f if f(element)
returns true.
Queue<E> filter(bool f(E element)) { Queue<E> other = new Queue<E>(); DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (f(entry._element)) other.addLast(entry._element); entry = nextEntry; } return other; }
DoubleLinkedQueueEntry<E> firstEntry() #
DoubleLinkedQueueEntry<E> firstEntry() { return _sentinel.nextEntry(); }
void forEach(void f(E element)) #
Applies the function f to each element of this collection.
void forEach(void f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; f(entry._element); entry = nextEntry; } }
void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) #
void forEachEntry(void f(DoubleLinkedQueueEntry<E> element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; f(entry); entry = nextEntry; } }
bool isEmpty() #
Returns true if there is no element in this collection.
bool isEmpty() { return (_sentinel._next === _sentinel); }
_DoubleLinkedQueueIterator<E> iterator() #
DoubleLinkedQueueEntry<E> lastEntry() #
DoubleLinkedQueueEntry<E> lastEntry() { return _sentinel.previousEntry(); }
Queue map(f(E element)) #
Returns a new collection with the elements f(e)
for each element e
of this collection.
Subclasses of Collection
should implement the map method
to return a collection of the same general type as themselves.
E.g., List.map
should return a List
.
Queue map(f(E element)) { Queue other = new Queue(); DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; other.addLast(f(entry._element)); entry = nextEntry; } return other; }
reduce(initialValue, combine(previousValue, E element)) #
Reduce 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 a collection:
collection.reduce(0, (prev, element) => prev + element);
Dynamic reduce(Dynamic initialValue, Dynamic combine(Dynamic previousValue, E element)) { return Collections.reduce(this, initialValue, combine); }
bool some(bool f(E element)) #
Returns true if one element of this collection satisfies the predicate f. Returns false otherwise.
bool some(bool f(E element)) { DoubleLinkedQueueEntry<E> entry = _sentinel._next; while (entry !== _sentinel) { DoubleLinkedQueueEntry<E> nextEntry = entry._next; if (f(entry._element)) return true; entry = nextEntry; } return false; }