Chromium Code Reviews| 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 | |
| 7 int get _entrySize => 4; | |
| 8 | |
| 9 LinkedHashMap() : super(_INITIAL_CAPACITY); | |
| 10 | |
| 11 factory LinkedHashMap.from(Map<K, V> other) { | |
| 12 return new LinkedHashMap<K, V>()..addAll(other); | |
| 13 } | |
| 14 | |
| 15 V _value(int offset) => _table[offset + _VALUE_INDEX]; | |
| 16 void _setValue(int offset, V value) { _table[offset + _VALUE_INDEX] = value; } | |
| 17 | |
| 18 _copyEntry(List oldTable, int fromOffset, int toOffset) { | |
| 19 _setValue(toOffset, oldTable[fromOffset + _VALUE_INDEX]); | |
|
floitsch
2013/02/06 10:43:58
don't use "_setValue".
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done.
| |
| 20 } | |
| 21 | |
| 22 bool containsKey(K key) { | |
| 23 return _get(key) >= 0; | |
| 24 } | |
| 25 | |
| 26 bool containsValue(V value) { | |
| 27 for (int offset = _next(_LinkedHashTable._HEAD_OFFSET); | |
| 28 offset != _LinkedHashTable._HEAD_OFFSET; | |
| 29 offset = _next(offset)) { | |
| 30 if (_value(offset) == value) { | |
| 31 return true; | |
| 32 } | |
| 33 } | |
| 34 return false; | |
| 35 } | |
| 36 | |
| 37 void addAll(Map<K, V> other) { | |
| 38 _ensureCapacity(other.length); | |
| 39 other.forEach((K key, V value) { | |
| 40 int offset = _put(key); | |
| 41 _setValue(offset, value); | |
| 42 _modificationCount++; | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 V operator [](K key) { | |
| 47 int offset = _get(key); | |
| 48 if (offset >= 0) return _value(offset); | |
| 49 return null; | |
| 50 } | |
| 51 | |
| 52 void operator []=(K key, V value) { | |
| 53 _ensureCapacity(1); | |
| 54 int offset = _put(key); | |
| 55 _setValue(offset, value); | |
| 56 } | |
| 57 | |
| 58 V putIfAbsent(K key, V ifAbsent()) { | |
| 59 _ensureCapacity(1); | |
| 60 int offset = _probeForAdd(_hashCodeOf(key), key); | |
| 61 Object prevKey = _table[offset]; | |
| 62 if (_isFree(prevKey)) { | |
| 63 V value = ifAbsent(); | |
| 64 _table[offset] = key; | |
| 65 _setValue(offset, value); | |
| 66 _linkLast(offset); | |
| 67 if (prevKey == null) { | |
| 68 _entryCount++; | |
| 69 } else { | |
| 70 assert(identical(prevKey, _TOMBSTONE)); | |
| 71 _deletedCount--; | |
| 72 } | |
| 73 _modificationCount++; | |
| 74 return value; | |
| 75 } | |
| 76 return _value(offset); | |
| 77 } | |
| 78 | |
| 79 V remove(K key) { | |
| 80 int offset = _remove(key); | |
| 81 if (offset < 0) return null; | |
| 82 Object oldValue = _value(offset); | |
|
floitsch
2013/02/06 10:43:58
modification counter.
Lasse Reichstein Nielsen
2013/02/08 13:53:01
Done by _remove now.
| |
| 83 _setValue(offset, null); | |
| 84 return oldValue; | |
| 85 } | |
| 86 | |
| 87 void clear() { | |
| 88 _clear(); | |
| 89 } | |
| 90 | |
| 91 void forEach(void action (K key, V value)) { | |
| 92 int modCount = _modificationCount; | |
| 93 for (int offset = _next(0); offset != 0; offset = _next(offset)) { | |
| 94 if (modCount != _modificationCount) { | |
| 95 throw new ConcurrentModificationError(this); | |
| 96 } | |
| 97 Object key = _table[offset]; | |
| 98 assert(!_isFree(key)); | |
| 99 action(key, _value(offset)); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 Iterable<K> get keys => new _LinkedHashTableKeyIterable<K>(this); | |
| 104 Iterable<V> get values => | |
| 105 new _LinkedHashTableValueIterable<V>(this, _VALUE_INDEX); | |
| 106 | |
| 107 int get length => _elementCount; | |
| 108 | |
| 109 bool get isEmpty => _elementCount == 0; | |
| 110 | |
| 111 String toString() => Maps.mapToString(this); | |
| 112 } | |
| 113 | |
| OLD | NEW |