| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A [Queue] is a collection that can be manipulated at both ends. One | 8 * A [Queue] is a collection that can be manipulated at both ends. One |
| 9 * can iterate over the elements of a queue through [forEach] or with | 9 * can iterate over the elements of a queue through [forEach] or with |
| 10 * an [Iterator]. | 10 * an [Iterator]. |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 } | 451 } |
| 452 | 452 |
| 453 void addAll(Iterable<E> elements) { | 453 void addAll(Iterable<E> elements) { |
| 454 if (elements is List) { | 454 if (elements is List) { |
| 455 List list = elements; | 455 List list = elements; |
| 456 int addCount = list.length; | 456 int addCount = list.length; |
| 457 int length = this.length; | 457 int length = this.length; |
| 458 if (length + addCount >= _table.length) { | 458 if (length + addCount >= _table.length) { |
| 459 _preGrow(length + addCount); | 459 _preGrow(length + addCount); |
| 460 // After preGrow, all elements are at the start of the list. | 460 // After preGrow, all elements are at the start of the list. |
| 461 _table.setRange(length, addCount, list, 0); | 461 _table.setRange(length, length + addCount, list, 0); |
| 462 _tail += addCount; | 462 _tail += addCount; |
| 463 } else { | 463 } else { |
| 464 // Adding addCount elements won't reach _head. | 464 // Adding addCount elements won't reach _head. |
| 465 int endSpace = _table.length - _tail; | 465 int endSpace = _table.length - _tail; |
| 466 if (addCount < endSpace) { | 466 if (addCount < endSpace) { |
| 467 _table.setRange(_tail, addCount, list, 0); | 467 _table.setRange(_tail, _tail + addCount, list, 0); |
| 468 _tail += addCount; | 468 _tail += addCount; |
| 469 } else { | 469 } else { |
| 470 int preSpace = addCount - endSpace; | 470 int preSpace = addCount - endSpace; |
| 471 _table.setRange(_tail, endSpace, list, 0); | 471 _table.setRange(_tail, _tail + endSpace, list, 0); |
| 472 _table.setRange(0, preSpace, list, endSpace); | 472 _table.setRange(0, preSpace, list, endSpace); |
| 473 _tail = preSpace; | 473 _tail = preSpace; |
| 474 } | 474 } |
| 475 } | 475 } |
| 476 _modificationCount++; | 476 _modificationCount++; |
| 477 } else { | 477 } else { |
| 478 for (E element in elements) _add(element); | 478 for (E element in elements) _add(element); |
| 479 } | 479 } |
| 480 } | 480 } |
| 481 | 481 |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 } | 654 } |
| 655 } | 655 } |
| 656 | 656 |
| 657 /** | 657 /** |
| 658 * Grow the table when full. | 658 * Grow the table when full. |
| 659 */ | 659 */ |
| 660 void _grow() { | 660 void _grow() { |
| 661 List<E> newTable = new List<E>(_table.length * 2); | 661 List<E> newTable = new List<E>(_table.length * 2); |
| 662 int split = _table.length - _head; | 662 int split = _table.length - _head; |
| 663 newTable.setRange(0, split, _table, _head); | 663 newTable.setRange(0, split, _table, _head); |
| 664 newTable.setRange(split, _head, _table, 0); | 664 newTable.setRange(split, split + _head, _table, 0); |
| 665 _head = 0; | 665 _head = 0; |
| 666 _tail = _table.length; | 666 _tail = _table.length; |
| 667 _table = newTable; | 667 _table = newTable; |
| 668 } | 668 } |
| 669 | 669 |
| 670 int _writeToList(List<E> target) { | 670 int _writeToList(List<E> target) { |
| 671 assert(target.length >= length); | 671 assert(target.length >= length); |
| 672 if (_head <= _tail) { | 672 if (_head <= _tail) { |
| 673 int length = _tail - _head; | 673 int length = _tail - _head; |
| 674 target.setRange(0, length, _table, _head); | 674 target.setRange(0, length, _table, _head); |
| 675 return length; | 675 return length; |
| 676 } else { | 676 } else { |
| 677 int firstPartSize = _table.length - _head; | 677 int firstPartSize = _table.length - _head; |
| 678 target.setRange(0, firstPartSize, _table, _head); | 678 target.setRange(0, firstPartSize, _table, _head); |
| 679 target.setRange(firstPartSize, _tail, _table, 0); | 679 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0); |
| 680 return _tail + firstPartSize; | 680 return _tail + firstPartSize; |
| 681 } | 681 } |
| 682 } | 682 } |
| 683 | 683 |
| 684 /** Grows the table even if it is not full. */ | 684 /** Grows the table even if it is not full. */ |
| 685 void _preGrow(int newElementCount) { | 685 void _preGrow(int newElementCount) { |
| 686 assert(newElementCount >= length); | 686 assert(newElementCount >= length); |
| 687 int newCapacity = _nextPowerOf2(newElementCount); | 687 int newCapacity = _nextPowerOf2(newElementCount); |
| 688 List<E> newTable = new List<E>(newCapacity); | 688 List<E> newTable = new List<E>(newCapacity); |
| 689 _tail = _writeToList(newTable); | 689 _tail = _writeToList(newTable); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 716 _queue._checkModification(_modificationCount); | 716 _queue._checkModification(_modificationCount); |
| 717 if (_position == _end) { | 717 if (_position == _end) { |
| 718 _current = null; | 718 _current = null; |
| 719 return false; | 719 return false; |
| 720 } | 720 } |
| 721 _current = _queue._table[_position]; | 721 _current = _queue._table[_position]; |
| 722 _position = (_position + 1) & (_queue._table.length - 1); | 722 _position = (_position + 1) & (_queue._table.length - 1); |
| 723 return true; | 723 return true; |
| 724 } | 724 } |
| 725 } | 725 } |
| OLD | NEW |