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

Side by Side Diff: runtime/lib/collection_patch.dart

Issue 13599005: Add JS implementation of LinkedHashMap. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix checked mode. Created 7 years, 8 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 patch class HashMap<K, V> { 5 patch class HashMap<K, V> {
6 final _HashMapTable<K, V> _hashTable = new _HashMapTable<K, V>(); 6 final _HashMapTable<K, V> _hashTable = new _HashMapTable<K, V>();
7 7
8 /* patch */ HashMap() { 8 /* patch */ HashMap() {
9 _hashTable._container = this; 9 _hashTable._container = this;
10 } 10 }
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 } 182 }
183 183
184 /* patch */ void retainWhere(bool test(E element)) { 184 /* patch */ void retainWhere(bool test(E element)) {
185 _filterWhere(test, false); 185 _filterWhere(test, false);
186 } 186 }
187 187
188 /* patch */ void clear() { 188 /* patch */ void clear() {
189 _table._clear(); 189 _table._clear();
190 } 190 }
191 } 191 }
192
193 class _LinkedHashMapTable<K, V> extends _LinkedHashTable<K> {
194 static const int _INITIAL_CAPACITY = 8;
195 static const int _VALUE_INDEX = 3;
196
197 int get _entrySize => 4;
198
199 _LinkedHashMapTable() : super(_INITIAL_CAPACITY);
200
201 V _value(int offset) => _table[offset + _VALUE_INDEX];
202 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; }
203
204 _copyEntry(List oldTable, int fromOffset, int toOffset) {
205 _table[toOffset + _VALUE_INDEX] = oldTable[fromOffset + _VALUE_INDEX];
206 }
207 }
208
209 /**
210 * A hash-based map that iterates keys and values in key insertion order.
211 */
212 patch class LinkedHashMap<K, V> {
213 final _LinkedHashMapTable _hashTable;
214
215 /* patch */ LinkedHashMap() : _hashTable = new _LinkedHashMapTable<K, V>() {
216 _hashTable._container = this;
217 }
218
219 /* patch */ bool containsKey(K key) {
220 return _hashTable._get(key) >= 0;
221 }
222
223 /* patch */ bool containsValue(V value) {
224 int modificationCount = _hashTable._modificationCount;
225 for (int offset = _hashTable._next(_LinkedHashTable._HEAD_OFFSET);
226 offset != _LinkedHashTable._HEAD_OFFSET;
227 offset = _hashTable._next(offset)) {
228 if (_hashTable._value(offset) == value) {
229 return true;
230 }
231 // The == call may modify the table.
232 _hashTable._checkModification(modificationCount);
233 }
234 return false;
235 }
236
237 /* patch */ void addAll(Map<K, V> other) {
238 other.forEach((K key, V value) {
239 int offset = _hashTable._put(key);
240 _hashTable._setValue(offset, value);
241 _hashTable._checkCapacity();
242 });
243 }
244
245 /* patch */ V operator [](K key) {
246 int offset = _hashTable._get(key);
247 if (offset >= 0) return _hashTable._value(offset);
248 return null;
249 }
250
251 /* patch */ void operator []=(K key, V value) {
252 int offset = _hashTable._put(key);
253 _hashTable._setValue(offset, value);
254 _hashTable._checkCapacity();
255 }
256
257 /* patch */ V putIfAbsent(K key, V ifAbsent()) {
258 int offset = _hashTable._probeForAdd(_hashTable._hashCodeOf(key), key);
259 Object entry = _hashTable._table[offset];
260 if (!_hashTable._isFree(entry)) {
261 return _hashTable._value(offset);
262 }
263 int modificationCount = _hashTable._modificationCount;
264 V value = ifAbsent();
265 if (modificationCount == _hashTable._modificationCount) {
266 _hashTable._setKey(offset, key);
267 _hashTable._setValue(offset, value);
268 _hashTable._linkLast(offset);
269 if (entry == null) {
270 _hashTable._entryCount++;
271 _hashTable._checkCapacity();
272 } else {
273 assert(identical(entry, _TOMBSTONE));
274 _hashTable._deletedCount--;
275 }
276 _hashTable._recordModification();
277 } else {
278 // The table might have changed, so we can't trust [offset] any more.
279 // Do another lookup before setting the value.
280 offset = _hashTable._put(key);
281 _hashTable._setValue(offset, value);
282 _hashTable._checkCapacity();
283 }
284 return value;
285 }
286
287 /* patch */ V remove(K key) {
288 int offset = _hashTable._remove(key);
289 if (offset < 0) return null;
290 Object oldValue = _hashTable._value(offset);
291 _hashTable._setValue(offset, null);
292 _hashTable._checkCapacity();
293 return oldValue;
294 }
295
296 /* patch */ void clear() {
297 _hashTable._clear();
298 }
299
300 /* patch */ void forEach(void action (K key, V value)) {
301 int modificationCount = _hashTable._modificationCount;
302 for (int offset = _hashTable._next(_LinkedHashTable._HEAD_OFFSET);
303 offset != _LinkedHashTable._HEAD_OFFSET;
304 offset = _hashTable._next(offset)) {
305 action(_hashTable._key(offset), _hashTable._value(offset));
306 _hashTable._checkModification(modificationCount);
307 }
308 }
309
310 /* patch */ Iterable<K> get keys =>
311 new _LinkedHashTableKeyIterable<K>(_hashTable);
312
313 /* patch */ Iterable<V> get values =>
314 new _LinkedHashTableValueIterable<V>(_hashTable,
315 _LinkedHashMapTable._VALUE_INDEX);
316
317 /* patch */ int get length => _hashTable._elementCount;
318
319 /* patch */ bool get isEmpty => _hashTable._elementCount == 0;
320 }
OLDNEW
« no previous file with comments | « no previous file | sdk/lib/_internal/compiler/implementation/lib/collection_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698