| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.collection; | |
| 6 | |
| 7 class HashMap<K, V> extends _HashTable<K> implements Map<K, V> { | |
| 8 static const int _INITIAL_CAPACITY = 8; | |
| 9 static const int _VALUE_INDEX = 1; | |
| 10 | |
| 11 HashMap() : super(_INITIAL_CAPACITY); | |
| 12 | |
| 13 factory HashMap.from(Map<K, V> other) { | |
| 14 return new HashMap<K, V>()..addAll(other); | |
| 15 } | |
| 16 | |
| 17 int get _entrySize => 2; | |
| 18 | |
| 19 V _value(int offset) => _table[offset + _VALUE_INDEX]; | |
| 20 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } | |
| 21 | |
| 22 _copyEntry(List fromTable, int fromOffset, int toOffset) { | |
| 23 _table[toOffset + _VALUE_INDEX] = fromTable[fromOffset + _VALUE_INDEX]; | |
| 24 } | |
| 25 | |
| 26 bool containsKey(K key) { | |
| 27 return _get(key) >= 0; | |
| 28 } | |
| 29 | |
| 30 bool containsValue(V value) { | |
| 31 for (int offset = 0; offset < _table.length; offset += _entrySize) { | |
| 32 if (!_isFree(_table[offset]) && _value(offset) == value) { | |
| 33 return true; | |
| 34 } | |
| 35 } | |
| 36 return false; | |
| 37 } | |
| 38 | |
| 39 void addAll(Map<K, V> other) { | |
| 40 other.forEach((K key, V value) { | |
| 41 int offset = _put(key); | |
| 42 _setValue(offset, value); | |
| 43 _checkCapacity(); | |
| 44 }); | |
| 45 } | |
| 46 | |
| 47 V operator [](K key) { | |
| 48 int offset = _get(key); | |
| 49 if (offset >= 0) return _value(offset); | |
| 50 return null; | |
| 51 } | |
| 52 | |
| 53 void operator []=(K key, V value) { | |
| 54 int offset = _put(key); | |
| 55 _setValue(offset, value); | |
| 56 _checkCapacity(); | |
| 57 } | |
| 58 | |
| 59 V putIfAbsent(K key, V ifAbsent()) { | |
| 60 int offset = _probeForAdd(_hashCodeOf(key), key); | |
| 61 Object entry = _table[offset]; | |
| 62 if (!_isFree(entry)) { | |
| 63 return _value(offset); | |
| 64 } | |
| 65 int modificationCount = _modificationCount; | |
| 66 V value = ifAbsent(); | |
| 67 _checkModification(modificationCount); | |
| 68 _setKey(offset, key); | |
| 69 _setValue(offset, value); | |
| 70 if (entry == null) { | |
| 71 _entryCount++; | |
| 72 _checkCapacity(); | |
| 73 } else { | |
| 74 assert(identical(entry, _TOMBSTONE)); | |
| 75 _deletedCount--; | |
| 76 } | |
| 77 _recordModification(); | |
| 78 return value; | |
| 79 } | |
| 80 | |
| 81 V remove(K key) { | |
| 82 int offset = _remove(key); | |
| 83 if (offset < 0) return null; | |
| 84 V oldValue = _value(offset); | |
| 85 _setValue(offset, null); | |
| 86 _checkCapacity(); | |
| 87 return oldValue; | |
| 88 } | |
| 89 | |
| 90 void clear() { | |
| 91 _clear(); | |
| 92 } | |
| 93 | |
| 94 void forEach(void action (K key, V value)) { | |
| 95 int modificationCount = _modificationCount; | |
| 96 for (int offset = 0; offset < _table.length; offset += _entrySize) { | |
| 97 Object entry = _table[offset]; | |
| 98 if (!_isFree(entry)) { | |
| 99 K key = entry; | |
| 100 V value = _value(offset); | |
| 101 action(key, value); | |
| 102 _checkModification(modificationCount); | |
| 103 } | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 Iterable<K> get keys => new _HashTableKeyIterable<K>(this); | |
| 108 Iterable<V> get values => | |
| 109 new _HashTableValueIterable<V>(this, _VALUE_INDEX); | |
| 110 | |
| 111 int get length => _elementCount; | |
| 112 | |
| 113 bool get isEmpty => _elementCount == 0; | |
| 114 | |
| 115 String toString() => Maps.mapToString(this); | |
| 116 } | |
| OLD | NEW |