OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import 'dart:collection'; |
| 6 |
| 7 /** |
| 8 * A class that efficiently implements both [Queue] and [List]. |
| 9 */ |
| 10 // TODO(nweiz): Currently this code is copied almost verbatim from |
| 11 // dart:collection. The only changes are to implement List and to remove methods |
| 12 // that are redundant with ListMixin. Remove or simplify it when issue 21330 is |
| 13 // fixed. |
| 14 class QueueList<E> extends Object with ListMixin<E> implements Queue<E> { |
| 15 static const int _INITIAL_CAPACITY = 8; |
| 16 List<E> _table; |
| 17 int _head; |
| 18 int _tail; |
| 19 |
| 20 /** |
| 21 * Create an empty queue. |
| 22 * |
| 23 * If [initialCapacity] is given, prepare the queue for at least that many |
| 24 * elements. |
| 25 */ |
| 26 QueueList([int initialCapacity]) : _head = 0, _tail = 0 { |
| 27 if (initialCapacity == null || initialCapacity < _INITIAL_CAPACITY) { |
| 28 initialCapacity = _INITIAL_CAPACITY; |
| 29 } else if (!_isPowerOf2(initialCapacity)) { |
| 30 initialCapacity = _nextPowerOf2(initialCapacity); |
| 31 } |
| 32 assert(_isPowerOf2(initialCapacity)); |
| 33 _table = new List<E>(initialCapacity); |
| 34 } |
| 35 |
| 36 /** |
| 37 * Create a queue initially containing the elements of [source]. |
| 38 */ |
| 39 factory QueueList.from(Iterable<E> source) { |
| 40 if (source is List) { |
| 41 int length = source.length; |
| 42 QueueList<E> queue = new QueueList(length + 1); |
| 43 assert(queue._table.length > length); |
| 44 List sourceList = source; |
| 45 queue._table.setRange(0, length, sourceList, 0); |
| 46 queue._tail = length; |
| 47 return queue; |
| 48 } else { |
| 49 return new QueueList<E>()..addAll(source); |
| 50 } |
| 51 } |
| 52 |
| 53 // Collection interface. |
| 54 |
| 55 void add(E element) { |
| 56 _add(element); |
| 57 } |
| 58 |
| 59 void addAll(Iterable<E> elements) { |
| 60 if (elements is List) { |
| 61 List list = elements; |
| 62 int addCount = list.length; |
| 63 int length = this.length; |
| 64 if (length + addCount >= _table.length) { |
| 65 _preGrow(length + addCount); |
| 66 // After preGrow, all elements are at the start of the list. |
| 67 _table.setRange(length, length + addCount, list, 0); |
| 68 _tail += addCount; |
| 69 } else { |
| 70 // Adding addCount elements won't reach _head. |
| 71 int endSpace = _table.length - _tail; |
| 72 if (addCount < endSpace) { |
| 73 _table.setRange(_tail, _tail + addCount, list, 0); |
| 74 _tail += addCount; |
| 75 } else { |
| 76 int preSpace = addCount - endSpace; |
| 77 _table.setRange(_tail, _tail + endSpace, list, 0); |
| 78 _table.setRange(0, preSpace, list, endSpace); |
| 79 _tail = preSpace; |
| 80 } |
| 81 } |
| 82 } else { |
| 83 for (E element in elements) _add(element); |
| 84 } |
| 85 } |
| 86 |
| 87 String toString() => IterableBase.iterableToFullString(this, "{", "}"); |
| 88 |
| 89 // Queue interface. |
| 90 |
| 91 void addLast(E element) { _add(element); } |
| 92 |
| 93 void addFirst(E element) { |
| 94 _head = (_head - 1) & (_table.length - 1); |
| 95 _table[_head] = element; |
| 96 if (_head == _tail) _grow(); |
| 97 } |
| 98 |
| 99 E removeFirst() { |
| 100 if (_head == _tail) throw new StateError("No element"); |
| 101 E result = _table[_head]; |
| 102 _table[_head] = null; |
| 103 _head = (_head + 1) & (_table.length - 1); |
| 104 return result; |
| 105 } |
| 106 |
| 107 E removeLast() { |
| 108 if (_head == _tail) throw new StateError("No element"); |
| 109 _tail = (_tail - 1) & (_table.length - 1); |
| 110 E result = _table[_tail]; |
| 111 _table[_tail] = null; |
| 112 return result; |
| 113 } |
| 114 |
| 115 // List interface. |
| 116 |
| 117 int get length => (_tail - _head) & (_table.length - 1); |
| 118 |
| 119 void set length(int value) { |
| 120 if (value < 0) throw new RangeError("Length $value may not be negative."); |
| 121 |
| 122 if (value <= length) { |
| 123 while (length > value) { |
| 124 removeLast(); |
| 125 } |
| 126 } else { |
| 127 while (length < value) { |
| 128 add(null); |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 133 E operator [](int index) => _table[(_head + index) & (_table.length - 1)]; |
| 134 |
| 135 void operator[]=(int index, E value) { |
| 136 _table[(_head + index) & (_table.length - 1)] = value; |
| 137 } |
| 138 |
| 139 // Internal helper functions. |
| 140 |
| 141 /** |
| 142 * Whether [number] is a power of two. |
| 143 * |
| 144 * Only works for positive numbers. |
| 145 */ |
| 146 static bool _isPowerOf2(int number) => (number & (number - 1)) == 0; |
| 147 |
| 148 /** |
| 149 * Rounds [number] up to the nearest power of 2. |
| 150 * |
| 151 * If [number] is a power of 2 already, it is returned. |
| 152 * |
| 153 * Only works for positive numbers. |
| 154 */ |
| 155 static int _nextPowerOf2(int number) { |
| 156 assert(number > 0); |
| 157 number = (number << 2) - 1; |
| 158 for(;;) { |
| 159 int nextNumber = number & (number - 1); |
| 160 if (nextNumber == 0) return number; |
| 161 number = nextNumber; |
| 162 } |
| 163 } |
| 164 |
| 165 /** Adds element at end of queue. Used by both [add] and [addAll]. */ |
| 166 void _add(E element) { |
| 167 _table[_tail] = element; |
| 168 _tail = (_tail + 1) & (_table.length - 1); |
| 169 if (_head == _tail) _grow(); |
| 170 } |
| 171 |
| 172 /** |
| 173 * Grow the table when full. |
| 174 */ |
| 175 void _grow() { |
| 176 List<E> newTable = new List<E>(_table.length * 2); |
| 177 int split = _table.length - _head; |
| 178 newTable.setRange(0, split, _table, _head); |
| 179 newTable.setRange(split, split + _head, _table, 0); |
| 180 _head = 0; |
| 181 _tail = _table.length; |
| 182 _table = newTable; |
| 183 } |
| 184 |
| 185 int _writeToList(List<E> target) { |
| 186 assert(target.length >= length); |
| 187 if (_head <= _tail) { |
| 188 int length = _tail - _head; |
| 189 target.setRange(0, length, _table, _head); |
| 190 return length; |
| 191 } else { |
| 192 int firstPartSize = _table.length - _head; |
| 193 target.setRange(0, firstPartSize, _table, _head); |
| 194 target.setRange(firstPartSize, firstPartSize + _tail, _table, 0); |
| 195 return _tail + firstPartSize; |
| 196 } |
| 197 } |
| 198 |
| 199 /** Grows the table even if it is not full. */ |
| 200 void _preGrow(int newElementCount) { |
| 201 assert(newElementCount >= length); |
| 202 int newCapacity = _nextPowerOf2(newElementCount); |
| 203 List<E> newTable = new List<E>(newCapacity); |
| 204 _tail = _writeToList(newTable); |
| 205 _table = newTable; |
| 206 _head = 0; |
| 207 } |
| 208 } |
OLD | NEW |