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 int length = other.length; | |
44 _ensureCapacity(length); // Is this really wise if there is an overlap? | |
45 other.forEach((K key, V value) { | |
46 int offset = _put(key); | |
47 _setValue(offset, value); | |
48 }); | |
49 } | |
50 | |
51 V operator [](K key) { | |
52 int offset = _get(key); | |
53 if (offset >= 0) return _value(offset); | |
54 return null; | |
55 } | |
56 | |
57 void operator []=(K key, V value) { | |
58 _ensureCapacity(1); | |
59 int offset = _put(key); | |
60 _setValue(offset, value); | |
61 } | |
62 | |
63 V putIfAbsent(K key, V ifAbsent()) { | |
64 _ensureCapacity(1); | |
65 int offset = _probeForAdd(_hashCodeOf(key), key); | |
66 Object entry = _table[offset]; | |
67 if (entry == null) { | |
68 _entryCount++; | |
69 } else if (identical(entry, _TOMBSTONE)) { | |
70 _deletedCount--; | |
71 } else { | |
72 return _value(offset); | |
73 } | |
74 V value = ifAbsent(); | |
floitsch
2013/02/08 16:48:35
You already changed the _entryCount and _deletedCo
Lasse Reichstein Nielsen
2013/02/11 14:07:37
Done.
| |
75 _setKey(offset, key); | |
76 _setValue(offset, value); | |
77 _linkLast(offset); | |
78 _recordModification(); | |
79 return value; | |
80 } | |
81 | |
82 V remove(K key) { | |
83 int offset = _remove(key); | |
84 if (offset < 0) return null; | |
85 Object oldValue = _value(offset); | |
86 _setValue(offset, null); | |
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 = _next(_HEAD_OFFSET); | |
97 offset != _HEAD_OFFSET; | |
98 offset = _next(offset)) { | |
99 action(_key(offset), _value(offset)); | |
100 _checkModification(modificationCount); | |
101 } | |
102 } | |
103 | |
104 Iterable<K> get keys => new _LinkedHashTableKeyIterable<K>(this); | |
105 Iterable<V> get values => | |
106 new _LinkedHashTableValueIterable<V>(this, _VALUE_INDEX); | |
107 | |
108 int get length => _elementCount; | |
109 | |
110 bool get isEmpty => _elementCount == 0; | |
111 | |
112 String toString() => Maps.mapToString(this); | |
113 } | |
114 | |
OLD | NEW |