| 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 // Hash map implementation with open addressing and quadratic probing. | 5 // Hash map implementation with open addressing and quadratic probing. |
| 6 class HashMapImplementation<K extends Hashable, V> implements HashMap<K, V> { | 6 class HashMapImplementation<K extends Hashable, V> implements HashMap<K, V> { |
| 7 | 7 |
| 8 // The [_keys] list contains the keys inserted in the map. | 8 // The [_keys] list contains the keys inserted in the map. |
| 9 // The [_keys] list must be a raw list because it | 9 // The [_keys] list must be a raw list because it |
| 10 // will contain both elements of type K, and the [_DELETED_KEY] of type | 10 // will contain both elements of type K, and the [_DELETED_KEY] of type |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 static final int _INITIAL_CAPACITY = 8; // must be power of 2 | 43 static final int _INITIAL_CAPACITY = 8; // must be power of 2 |
| 44 | 44 |
| 45 HashMapImplementation() { | 45 HashMapImplementation() { |
| 46 _numberOfEntries = 0; | 46 _numberOfEntries = 0; |
| 47 _numberOfDeleted = 0; | 47 _numberOfDeleted = 0; |
| 48 _loadLimit = _computeLoadLimit(_INITIAL_CAPACITY); | 48 _loadLimit = _computeLoadLimit(_INITIAL_CAPACITY); |
| 49 _keys = new List(_INITIAL_CAPACITY); | 49 _keys = new List(_INITIAL_CAPACITY); |
| 50 _values = new List<V>(_INITIAL_CAPACITY); | 50 _values = new List<V>(_INITIAL_CAPACITY); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // See bug 5257789. Works in the vm, fails in dartc and frog. | 53 // See issue 417. Works in the vm, fails in dartc and frog. |
| 54 factory HashMapImplementation/* <K, V> */.from(Map/* <K, V> */other) { | 54 factory HashMapImplementation/*<K, V>*/.from(Map/*<K, V>*/ other) { |
| 55 Map/* <K, V> */ result = new HashMapImplementation/* <K, V> */(); | 55 Map/*<K, V>*/ result = new HashMapImplementation/*<K, V>*/(); |
| 56 other.forEach((/* K */ key, /* V */ value) { result[key] = value; }); | 56 other.forEach((/*K*/ key, /*V*/ value) { result[key] = value; }); |
| 57 return result; | 57 return result; |
| 58 } | 58 } |
| 59 | 59 |
| 60 static int _computeLoadLimit(int capacity) { | 60 static int _computeLoadLimit(int capacity) { |
| 61 return (capacity * 3) ~/ 4; | 61 return (capacity * 3) ~/ 4; |
| 62 } | 62 } |
| 63 | 63 |
| 64 static int _firstProbe(int hashCode, int length) { | 64 static int _firstProbe(int hashCode, int length) { |
| 65 return hashCode & (length - 1); | 65 return hashCode & (length - 1); |
| 66 } | 66 } |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 return false; | 268 return false; |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 | 271 |
| 272 class HashSetImplementation<E extends Hashable> implements HashSet<E> { | 272 class HashSetImplementation<E extends Hashable> implements HashSet<E> { |
| 273 | 273 |
| 274 HashSetImplementation() { | 274 HashSetImplementation() { |
| 275 _backingMap = new HashMapImplementation<E, E>(); | 275 _backingMap = new HashMapImplementation<E, E>(); |
| 276 } | 276 } |
| 277 | 277 |
| 278 // See bug 5257789. | 278 // See issue 417. Works in the vm, fails in dartc and frog. |
| 279 factory HashSetImplementation/* <E> */.from(Iterable/* <E> */ other) { | 279 factory HashSetImplementation/*<E>*/.from(Iterable/*<E>*/ other) { |
| 280 Set/* <E> */ set = new HashSetImplementation/* <E> */(); | 280 Set/*<E>*/ set = new HashSetImplementation/*<E>*/(); |
| 281 for (final e in other) { | 281 for (final e in other) { |
| 282 set.add(e); | 282 set.add(e); |
| 283 } | 283 } |
| 284 return set; | 284 return set; |
| 285 } | 285 } |
| 286 | 286 |
| 287 void clear() { | 287 void clear() { |
| 288 _backingMap.clear(); | 288 _backingMap.clear(); |
| 289 } | 289 } |
| 290 | 290 |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 | 424 |
| 425 /** | 425 /** |
| 426 * A singleton sentinel used to represent when a key is deleted from the map. | 426 * A singleton sentinel used to represent when a key is deleted from the map. |
| 427 * We can't use [: const Object() :] as a sentinel because it would end up | 427 * We can't use [: const Object() :] as a sentinel because it would end up |
| 428 * canonicalized and then we cannot distinguish the deleted key from the | 428 * canonicalized and then we cannot distinguish the deleted key from the |
| 429 * canonicalized [: Object() :]. | 429 * canonicalized [: Object() :]. |
| 430 */ | 430 */ |
| 431 class _DeletedKeySentinel { | 431 class _DeletedKeySentinel { |
| 432 const _DeletedKeySentinel(); | 432 const _DeletedKeySentinel(); |
| 433 } | 433 } |
| OLD | NEW |