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