| 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 569 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 | 580 |
| 581 /** | 581 /** |
| 582 * Rounds [number] up to the nearest power of 2. | 582 * Rounds [number] up to the nearest power of 2. |
| 583 * | 583 * |
| 584 * If [number] is a power of 2 already, it is returned. | 584 * If [number] is a power of 2 already, it is returned. |
| 585 * | 585 * |
| 586 * Only works for positive numbers. | 586 * Only works for positive numbers. |
| 587 */ | 587 */ |
| 588 static int _nextPowerOf2(int number) { | 588 static int _nextPowerOf2(int number) { |
| 589 assert(number > 0); | 589 assert(number > 0); |
| 590 number = (number << 2) - 1; | 590 number = (number << 1) - 1; |
| 591 for(;;) { | 591 for(;;) { |
| 592 int nextNumber = number & (number - 1); | 592 int nextNumber = number & (number - 1); |
| 593 if (nextNumber == 0) return number; | 593 if (nextNumber == 0) return number; |
| 594 number = nextNumber; | 594 number = nextNumber; |
| 595 } | 595 } |
| 596 } | 596 } |
| 597 | 597 |
| 598 /** Check if the queue has been modified during iteration. */ | 598 /** Check if the queue has been modified during iteration. */ |
| 599 void _checkModification(int expectedModificationCount) { | 599 void _checkModification(int expectedModificationCount) { |
| 600 if (expectedModificationCount != _modificationCount) { | 600 if (expectedModificationCount != _modificationCount) { |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 int firstPartSize = _table.length - _head; | 671 int firstPartSize = _table.length - _head; |
| 672 target.setRange(0, firstPartSize, _table, _head); | 672 target.setRange(0, firstPartSize, _table, _head); |
| 673 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0); | 673 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0); |
| 674 return _tail + firstPartSize; | 674 return _tail + firstPartSize; |
| 675 } | 675 } |
| 676 } | 676 } |
| 677 | 677 |
| 678 /** Grows the table even if it is not full. */ | 678 /** Grows the table even if it is not full. */ |
| 679 void _preGrow(int newElementCount) { | 679 void _preGrow(int newElementCount) { |
| 680 assert(newElementCount >= length); | 680 assert(newElementCount >= length); |
| 681 |
| 682 // Add some extra room to ensure that there's room for more elements after |
| 683 // expansion. |
| 684 newElementCount += newElementCount >> 1; |
| 681 int newCapacity = _nextPowerOf2(newElementCount); | 685 int newCapacity = _nextPowerOf2(newElementCount); |
| 682 List<E> newTable = new List<E>(newCapacity); | 686 List<E> newTable = new List<E>(newCapacity); |
| 683 _tail = _writeToList(newTable); | 687 _tail = _writeToList(newTable); |
| 684 _table = newTable; | 688 _table = newTable; |
| 685 _head = 0; | 689 _head = 0; |
| 686 } | 690 } |
| 687 } | 691 } |
| 688 | 692 |
| 689 /** | 693 /** |
| 690 * Iterator for a [ListQueue]. | 694 * Iterator for a [ListQueue]. |
| (...skipping 19 matching lines...) Expand all Loading... |
| 710 _queue._checkModification(_modificationCount); | 714 _queue._checkModification(_modificationCount); |
| 711 if (_position == _end) { | 715 if (_position == _end) { |
| 712 _current = null; | 716 _current = null; |
| 713 return false; | 717 return false; |
| 714 } | 718 } |
| 715 _current = _queue._table[_position]; | 719 _current = _queue._table[_position]; |
| 716 _position = (_position + 1) & (_queue._table.length - 1); | 720 _position = (_position + 1) & (_queue._table.length - 1); |
| 717 return true; | 721 return true; |
| 718 } | 722 } |
| 719 } | 723 } |
| OLD | NEW |