| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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, V> implements HashMap<K, V> { | 6 class HashMapImplementation<K, 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 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 231 int length = _keys.length; | 231 int length = _keys.length; |
| 232 for (int i = 0; i < length; i++) { | 232 for (int i = 0; i < length; i++) { |
| 233 var key = _keys[i]; | 233 var key = _keys[i]; |
| 234 if ((key !== null) && (key !== _DELETED_KEY)) { | 234 if ((key !== null) && (key !== _DELETED_KEY)) { |
| 235 f(key, _values[i]); | 235 f(key, _values[i]); |
| 236 } | 236 } |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 | 239 |
| 240 | 240 |
| 241 Collection<K> getKeys() { | 241 Collection<K> get keys { |
| 242 List<K> list = new List<K>(length); | 242 List<K> list = new List<K>(length); |
| 243 int i = 0; | 243 int i = 0; |
| 244 forEach(void _(K key, V value) { | 244 forEach(void _(K key, V value) { |
| 245 list[i++] = key; | 245 list[i++] = key; |
| 246 }); | 246 }); |
| 247 return list; | 247 return list; |
| 248 } | 248 } |
| 249 | 249 |
| 250 Collection<V> getValues() { | 250 Collection<V> get values { |
| 251 List<V> list = new List<V>(length); | 251 List<V> list = new List<V>(length); |
| 252 int i = 0; | 252 int i = 0; |
| 253 forEach(void _(K key, V value) { | 253 forEach(void _(K key, V value) { |
| 254 list[i++] = value; | 254 list[i++] = value; |
| 255 }); | 255 }); |
| 256 return list; | 256 return list; |
| 257 } | 257 } |
| 258 | 258 |
| 259 bool containsKey(K key) { | 259 bool containsKey(K key) { |
| 260 return (_probeForLookup(key) != -1); | 260 return (_probeForLookup(key) != -1); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 359 | 359 |
| 360 Set<E> filter(bool f(E element)) { | 360 Set<E> filter(bool f(E element)) { |
| 361 Set<E> result = new Set<E>(); | 361 Set<E> result = new Set<E>(); |
| 362 _backingMap.forEach(void _(E key, E value) { | 362 _backingMap.forEach(void _(E key, E value) { |
| 363 if (f(key)) result.add(key); | 363 if (f(key)) result.add(key); |
| 364 }); | 364 }); |
| 365 return result; | 365 return result; |
| 366 } | 366 } |
| 367 | 367 |
| 368 bool every(bool f(E element)) { | 368 bool every(bool f(E element)) { |
| 369 Collection<E> keys = _backingMap.getKeys(); | 369 Collection<E> keys = _backingMap.keys; |
| 370 return keys.every(f); | 370 return keys.every(f); |
| 371 } | 371 } |
| 372 | 372 |
| 373 bool some(bool f(E element)) { | 373 bool some(bool f(E element)) { |
| 374 Collection<E> keys = _backingMap.getKeys(); | 374 Collection<E> keys = _backingMap.keys; |
| 375 return keys.some(f); | 375 return keys.some(f); |
| 376 } | 376 } |
| 377 | 377 |
| 378 bool get isEmpty { | 378 bool get isEmpty { |
| 379 return _backingMap.isEmpty; | 379 return _backingMap.isEmpty; |
| 380 } | 380 } |
| 381 | 381 |
| 382 int get length { | 382 int get length { |
| 383 return _backingMap.length; | 383 return _backingMap.length; |
| 384 } | 384 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 447 |
| 448 /** | 448 /** |
| 449 * A singleton sentinel used to represent when a key is deleted from the map. | 449 * A singleton sentinel used to represent when a key is deleted from the map. |
| 450 * We can't use [: const Object() :] as a sentinel because it would end up | 450 * We can't use [: const Object() :] as a sentinel because it would end up |
| 451 * canonicalized and then we cannot distinguish the deleted key from the | 451 * canonicalized and then we cannot distinguish the deleted key from the |
| 452 * canonicalized [: Object() :]. | 452 * canonicalized [: Object() :]. |
| 453 */ | 453 */ |
| 454 class _DeletedKeySentinel { | 454 class _DeletedKeySentinel { |
| 455 const _DeletedKeySentinel(); | 455 const _DeletedKeySentinel(); |
| 456 } | 456 } |
| OLD | NEW |