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 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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 for (final e in other) { | 285 for (final e in other) { |
286 set.add(e); | 286 set.add(e); |
287 } | 287 } |
288 return set; | 288 return set; |
289 } | 289 } |
290 | 290 |
291 void clear() { | 291 void clear() { |
292 _backingMap.clear(); | 292 _backingMap.clear(); |
293 } | 293 } |
294 | 294 |
295 void add(E value) { | 295 bool add(E value) { |
| 296 if (_backingMap.containsKey(value)) return false; |
296 _backingMap[value] = value; | 297 _backingMap[value] = value; |
| 298 return true; |
297 } | 299 } |
298 | 300 |
299 bool contains(E value) { | 301 bool contains(E value) { |
300 return _backingMap.containsKey(value); | 302 return _backingMap.containsKey(value); |
301 } | 303 } |
302 | 304 |
303 bool remove(E value) { | 305 bool remove(E value) { |
304 if (!_backingMap.containsKey(value)) return false; | 306 if (!_backingMap.containsKey(value)) return false; |
305 _backingMap.remove(value); | 307 _backingMap.remove(value); |
306 return true; | 308 return true; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
440 | 442 |
441 /** | 443 /** |
442 * A singleton sentinel used to represent when a key is deleted from the map. | 444 * A singleton sentinel used to represent when a key is deleted from the map. |
443 * We can't use [: const Object() :] as a sentinel because it would end up | 445 * We can't use [: const Object() :] as a sentinel because it would end up |
444 * canonicalized and then we cannot distinguish the deleted key from the | 446 * canonicalized and then we cannot distinguish the deleted key from the |
445 * canonicalized [: Object() :]. | 447 * canonicalized [: Object() :]. |
446 */ | 448 */ |
447 class _DeletedKeySentinel { | 449 class _DeletedKeySentinel { |
448 const _DeletedKeySentinel(); | 450 const _DeletedKeySentinel(); |
449 } | 451 } |
OLD | NEW |