OLD | NEW |
| (Empty) |
1 // Copyright 2013 Google Inc. All Rights Reserved. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 | |
15 part of quiver.collection; | |
16 | |
17 /** | |
18 * A bi-directional map whose key-value pairs form a one-to-one correspondence. | |
19 * BiMaps support an `inverse` property which gives access to an inverted view | |
20 * of the map, such that there is a mapping (v, k) for each pair (k, v) in the | |
21 * original map. Since a one-to-one key-value invariant applies, it is an error | |
22 * to insert duplicate values into this map. It is also an error to insert null | |
23 * keys or values into this map. | |
24 */ | |
25 abstract class BiMap<K, V> implements Map<K, V> { | |
26 /** | |
27 * Creates a BiMap instance with the default implementation. | |
28 */ | |
29 factory BiMap() => new HashBiMap(); | |
30 | |
31 /** | |
32 * Adds an association between key and value. | |
33 * | |
34 * Throws [ArgumentError] if an association involving [value] exists in the | |
35 * map; otherwise, the association is inserted, overwriting any existing | |
36 * association for the key. | |
37 */ | |
38 void operator []=(K key, V value); | |
39 | |
40 /** | |
41 * Replaces any existing associations(s) involving key and value. | |
42 * | |
43 * If an association involving [key] or [value] exists in the map, it is | |
44 * removed. | |
45 */ | |
46 void replace(K key, V value); | |
47 | |
48 /** | |
49 * Returns the inverse of this map, with key-value pairs (v, k) for each | |
50 * pair (k, v) in this map. | |
51 */ | |
52 BiMap<V, K> get inverse; | |
53 } | |
54 | |
55 /** | |
56 * A hash-table based implementation of BiMap. | |
57 */ | |
58 class HashBiMap<K, V> implements BiMap<K, V> { | |
59 final Map<K, V> _map; | |
60 final Map<V, K> _inverse; | |
61 BiMap<V, K> _cached; | |
62 | |
63 HashBiMap() : this._from(new HashMap(), new HashMap()); | |
64 HashBiMap._from(this._map, this._inverse); | |
65 | |
66 V operator [](Object key) => _map[key]; | |
67 void operator []=(K key, V value) { | |
68 _add(key, value, false); | |
69 } | |
70 void replace(K key, V value) { | |
71 _add(key, value, true); | |
72 } | |
73 void addAll(Map<K, V> other) => other.forEach((k, v) => _add(k, v, false)); | |
74 bool containsKey(Object key) => _map.containsKey(key); | |
75 bool containsValue(Object value) => _inverse.containsKey(value); | |
76 void forEach(void f(K key, V value)) => _map.forEach(f); | |
77 bool get isEmpty => _map.isEmpty; | |
78 bool get isNotEmpty => _map.isNotEmpty; | |
79 Iterable<K> get keys => _map.keys; | |
80 int get length => _map.length; | |
81 Iterable<V> get values => _inverse.keys; | |
82 | |
83 BiMap<V, K> get inverse { | |
84 if (_cached == null) { | |
85 _cached = new HashBiMap._from(_inverse, _map); | |
86 } | |
87 return _cached; | |
88 } | |
89 | |
90 V putIfAbsent(K key, V ifAbsent()) { | |
91 var value = _map[key]; | |
92 if (value != null) return value; | |
93 if (!_map.containsKey(key)) return _add(key, ifAbsent(), false); | |
94 return null; | |
95 } | |
96 | |
97 V remove(Object key) { | |
98 _inverse.remove(_map[key]); | |
99 return _map.remove(key); | |
100 } | |
101 | |
102 void clear() { | |
103 _map.clear(); | |
104 _inverse.clear(); | |
105 } | |
106 | |
107 V _add(K key, V value, bool replace) { | |
108 if (key == null) throw new ArgumentError("null key"); | |
109 if (value == null) throw new ArgumentError("null value"); | |
110 var oldValue = _map[key]; | |
111 if (oldValue == value) return value; | |
112 if (_inverse.containsKey(value)) { | |
113 if (!replace) throw new ArgumentError("Mapping for $value exists"); | |
114 _map.remove(_inverse[value]); | |
115 } | |
116 _inverse.remove(oldValue); | |
117 _map[key] = value; | |
118 _inverse[value] = key; | |
119 return value; | |
120 } | |
121 } | |
OLD | NEW |