ListQueue<E>.from constructor

ListQueue<E>.from(
  1. Iterable elements
)

Create a ListQueue containing all elements.

The elements are added to the queue, as by addLast, in the order given by elements.iterator.

All the elements should be instances of E. The elements iterable itself may have any element type, so this constructor can be used to down-cast a Queue, for example as:

Queue<SuperType> superQueue = ...;
Queue<SubType> subQueue =
    ListQueue<SubType>.from(superQueue.whereType<SubType>());

Example:

final numbers = <num>[10, 20, 30];
final queue = ListQueue<int>.from(numbers);
print(queue); // {10, 20, 30}

Implementation

factory ListQueue.from(Iterable<dynamic> elements) {
  if (elements is List<dynamic>) {
    int length = elements.length;
    ListQueue<E> queue = ListQueue<E>(length + 1);
    assert(queue._table.length > length);
    for (int i = 0; i < length; i++) {
      queue._table[i] = elements[i] as E;
    }
    queue._tail = length;
    return queue;
  } else {
    int capacity = _INITIAL_CAPACITY;
    if (elements is EfficientLengthIterable) {
      capacity = elements.length;
    }
    ListQueue<E> result = ListQueue<E>(capacity);
    for (final element in elements) {
      result.addLast(element as E);
    }
    return result;
  }
}