| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /** | 5 /** |
| 6 * This class represents a pair of two objects, used by LinkedHashMap | 6 * This class represents a pair of two objects, used by LinkedHashMap |
| 7 * to store a {key, value} in a list. | 7 * to store a {key, value} in a list. |
| 8 */ | 8 */ |
| 9 class KeyValuePair<K, V> { | 9 class KeyValuePair<K, V> { |
| 10 KeyValuePair(this.key, this.value) {} | 10 KeyValuePair(this.key, this.value) {} |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 if (_map.containsKey(key)) { | 38 if (_map.containsKey(key)) { |
| 39 _map[key].element.value = value; | 39 _map[key].element.value = value; |
| 40 } else { | 40 } else { |
| 41 _list.addLast(new KeyValuePair<K, V>(key, value)); | 41 _list.addLast(new KeyValuePair<K, V>(key, value)); |
| 42 _map[key] = _list.lastEntry(); | 42 _map[key] = _list.lastEntry(); |
| 43 } | 43 } |
| 44 } | 44 } |
| 45 | 45 |
| 46 V operator [](K key) { | 46 V operator [](K key) { |
| 47 DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map[key]; | 47 DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map[key]; |
| 48 if (entry === null) return null; | 48 if (entry == null) return null; |
| 49 return entry.element.value; | 49 return entry.element.value; |
| 50 } | 50 } |
| 51 | 51 |
| 52 V remove(K key) { | 52 V remove(K key) { |
| 53 DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map.remove(key); | 53 DoubleLinkedQueueEntry<KeyValuePair<K, V>> entry = _map.remove(key); |
| 54 if (entry === null) return null; | 54 if (entry == null) return null; |
| 55 entry.remove(); | 55 entry.remove(); |
| 56 return entry.element.value; | 56 return entry.element.value; |
| 57 } | 57 } |
| 58 | 58 |
| 59 V putIfAbsent(K key, V ifAbsent()) { | 59 V putIfAbsent(K key, V ifAbsent()) { |
| 60 V value = this[key]; | 60 V value = this[key]; |
| 61 if ((this[key] === null) && !(containsKey(key))) { | 61 if ((this[key] == null) && !(containsKey(key))) { |
| 62 value = ifAbsent(); | 62 value = ifAbsent(); |
| 63 this[key] = value; | 63 this[key] = value; |
| 64 } | 64 } |
| 65 return value; | 65 return value; |
| 66 } | 66 } |
| 67 | 67 |
| 68 Collection<K> getKeys() { | 68 Collection<K> getKeys() { |
| 69 List<K> list = new List<K>(length); | 69 List<K> list = new List<K>(length); |
| 70 int index = 0; | 70 int index = 0; |
| 71 _list.forEach(void _(KeyValuePair<K, V> entry) { | 71 _list.forEach(void _(KeyValuePair<K, V> entry) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 112 |
| 113 void clear() { | 113 void clear() { |
| 114 _map.clear(); | 114 _map.clear(); |
| 115 _list.clear(); | 115 _list.clear(); |
| 116 } | 116 } |
| 117 | 117 |
| 118 String toString() { | 118 String toString() { |
| 119 return Maps.mapToString(this); | 119 return Maps.mapToString(this); |
| 120 } | 120 } |
| 121 } | 121 } |
| OLD | NEW |