Dart API Referencedart:coreimplDoubleLinkedQueue<E>

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

Queue<E>

Constructors

new DoubleLinkedQueue() #

DoubleLinkedQueue() {
  _sentinel = new _DoubleLinkedQueueEntrySentinel<E>();
}

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

factory DoubleLinkedQueue.from(Iterable<E> other) {
  Queue<E> list = new DoubleLinkedQueue();
  for (final e in other) {
    list.addLast(e);
  }
  return list;
}

Properties

final int length #

Returns the number of elements in this collection.

docs inherited from Collection<E>
int get length {
  int counter = 0;
  forEach(void _(E element) { counter++; });
  return counter;
}

Methods

void add(E value) #

Adds value at the end of the queue.

docs inherited from Queue<E>
void add(E value) {
  addLast(value);
}

void addAll(Collection<E> collection) #

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

docs inherited from Queue<E>
void addAll(Collection<E> collection) {
  for (final e in collection) {
    add(e);
  }
}

void addFirst(E value) #

Adds value at the beginning of the queue.

docs inherited from Queue<E>
void addFirst(E value) {
  _sentinel.append(value);
}

void addLast(E value) #

Adds value at the end of the queue.

docs inherited from Queue<E>
void addLast(E value) {
  _sentinel.prepend(value);
}

void clear() #

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

docs inherited from Queue<E>
void clear() {
  _sentinel._next = _sentinel;
  _sentinel._previous = _sentinel;
}

bool contains(E element) #

inherited from Collection

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.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
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;
}

E first() #

Returns the first element of the queue. Throws an EmptyQueueException exception if this queue is empty.

docs inherited from Queue<E>
E first() {
  return _sentinel._next.element;
}

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.

docs inherited from Collection<E>
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.

docs inherited from Collection<E>
bool isEmpty() {
  return (_sentinel._next === _sentinel);
}

_DoubleLinkedQueueIterator<E> iterator() #

Returns an Iterator that iterates over this Iterable object.

docs inherited from Iterable<E>
_DoubleLinkedQueueIterator<E> iterator() {
  return new _DoubleLinkedQueueIterator<E>(_sentinel);
}

E last() #

Returns the last element of the queue. Throws an EmptyQueueException exception if this queue is empty.

docs inherited from Queue<E>
E last() {
  return _sentinel._previous.element;
}

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.

docs inherited from Collection<E>
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);

docs inherited from Collection<E>
Dynamic reduce(Dynamic initialValue,
               Dynamic combine(Dynamic previousValue, E element)) {
  return Collections.reduce(this, initialValue, combine);
}

E removeFirst() #

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

docs inherited from Queue<E>
E removeFirst() {
  return _sentinel._next.remove();
}

E removeLast() #

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

docs inherited from Queue<E>
E removeLast() {
  return _sentinel._previous.remove();
}

bool some(bool f(E element)) #

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

docs inherited from Collection<E>
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;
}

String toString() #

Returns a string representation of this object.

docs inherited from Object
String toString() {
  return Collections.collectionToString(this);
}