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 issue 417. Works in the vm, fails in dartc and frog. | 53 factory HashMapImplementation.from(Map<K, V> other) { |
54 factory HashMapImplementation.from(Map/*<K, V>*/ other) { | 54 Map<K, V> result = new HashMapImplementation<K, V>(); |
55 Map/*<K, V>*/ result = new HashMapImplementation/*<K, V>*/(); | 55 other.forEach((K key, V value) { result[key] = value; }); |
56 other.forEach((/*K*/ key, /*V*/ value) { result[key] = value; }); | |
57 return result; | 56 return result; |
58 } | 57 } |
59 | 58 |
60 static int _computeLoadLimit(int capacity) { | 59 static int _computeLoadLimit(int capacity) { |
61 return (capacity * 3) ~/ 4; | 60 return (capacity * 3) ~/ 4; |
62 } | 61 } |
63 | 62 |
64 static int _firstProbe(int hashCode, int length) { | 63 static int _firstProbe(int hashCode, int length) { |
65 return hashCode & (length - 1); | 64 return hashCode & (length - 1); |
66 } | 65 } |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 return false; | 267 return false; |
269 } | 268 } |
270 } | 269 } |
271 | 270 |
272 class HashSetImplementation<E extends Hashable> implements HashSet<E> { | 271 class HashSetImplementation<E extends Hashable> implements HashSet<E> { |
273 | 272 |
274 HashSetImplementation() { | 273 HashSetImplementation() { |
275 _backingMap = new HashMapImplementation<E, E>(); | 274 _backingMap = new HashMapImplementation<E, E>(); |
276 } | 275 } |
277 | 276 |
278 // See issue 417. Works in the vm, fails in dartc and frog. | 277 factory HashSetImplementation.from(Iterable<E> other) { |
279 factory HashSetImplementation.from(Iterable/*<E>*/ other) { | 278 Set<E> set = new HashSetImplementation<E>(); |
280 Set/*<E>*/ set = new HashSetImplementation/*<E>*/(); | |
281 for (final e in other) { | 279 for (final e in other) { |
282 set.add(e); | 280 set.add(e); |
283 } | 281 } |
284 return set; | 282 return set; |
285 } | 283 } |
286 | 284 |
287 void clear() { | 285 void clear() { |
288 _backingMap.clear(); | 286 _backingMap.clear(); |
289 } | 287 } |
290 | 288 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
424 | 422 |
425 /** | 423 /** |
426 * A singleton sentinel used to represent when a key is deleted from the map. | 424 * 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 | 425 * 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 | 426 * canonicalized and then we cannot distinguish the deleted key from the |
429 * canonicalized [: Object() :]. | 427 * canonicalized [: Object() :]. |
430 */ | 428 */ |
431 class _DeletedKeySentinel { | 429 class _DeletedKeySentinel { |
432 const _DeletedKeySentinel(); | 430 const _DeletedKeySentinel(); |
433 } | 431 } |
OLD | NEW |