Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Unified Diff: sdk/lib/collection/queue.dart

Issue 838463002: Change List/Set/Map/Queue.from constructrs to accept any iterable. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sdk/lib/collection/queue.dart
diff --git a/sdk/lib/collection/queue.dart b/sdk/lib/collection/queue.dart
index eb92c7128ccc9846a12b4305194235a4a69e9509..9236c7923c62c59518e72ab0878e7c1f5494540e 100644
--- a/sdk/lib/collection/queue.dart
+++ b/sdk/lib/collection/queue.dart
@@ -25,10 +25,12 @@ abstract class Queue<E> implements Iterable<E>, EfficientLength {
factory Queue() = ListQueue<E>;
/**
- * Creates a queue with the elements of [other]. The order in
- * the queue will be the order provided by the iterator of [other].
+ * Creates a queue containing all of [elements].
+ *
+ * The element order in the queue is as if the elements were added using
+ * [addLast] in the order provided by [elements.iterator].
*/
- factory Queue.from(Iterable<E> other) = ListQueue<E>.from;
+ factory Queue.from(Iterable elements) = ListQueue<E>.from;
/**
* Removes and returns the first element of this queue.
@@ -195,9 +197,15 @@ class DoubleLinkedQueue<E> extends IterableBase<E> implements Queue<E> {
_sentinel = new _DoubleLinkedQueueEntrySentinel<E>();
}
- factory DoubleLinkedQueue.from(Iterable<E> other) {
+ /**
+ * Creates a double-linked queue containing all of [elements].
+ *
+ * The element order in the queue is as if the elements were added using
+ * [addLast] in the order provided by [elements.iterator].
+ */
+ factory DoubleLinkedQueue.from(Iterable elements) {
Queue<E> list = new DoubleLinkedQueue();
- for (final e in other) {
+ for (final E e in elements) {
list.addLast(e);
}
return list;
@@ -378,19 +386,32 @@ class ListQueue<E> extends IterableBase<E> implements Queue<E> {
}
/**
- * Create a queue initially containing the elements of [source].
+ * Create a `ListQueue` containing all of [elements].
+ *
+ * The elements are added to the queue, as by [addLast], in the order given by
+ * `elements.iterator`.
+ *
+ * All `elements` should be assignable to [E].
*/
- factory ListQueue.from(Iterable<E> source) {
- if (source is List) {
- int length = source.length;
+ factory ListQueue.from(Iterable elements) {
+ if (elements is List) {
+ int length = elements.length;
ListQueue<E> queue = new ListQueue(length + 1);
assert(queue._table.length > length);
- List sourceList = source;
+ List sourceList = elements;
queue._table.setRange(0, length, sourceList, 0);
queue._tail = length;
return queue;
} else {
- return new ListQueue<E>()..addAll(source);
+ int capacity = _INITIAL_CAPACITY;
+ if (elements is EfficientLength) {
+ capacity = elements.length;
+ }
+ ListQueue<E> result = new ListQueue<E>(capacity);
+ for (final E element in elements) {
+ result.addLast(element);
+ }
+ return result;
}
}

Powered by Google App Engine
This is Rietveld 408576698