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