Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(86)

Side by Side Diff: packages/quiver/lib/src/collection/bimap.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 Google Inc. All Rights Reserved. 1 // Copyright 2013 Google Inc. All Rights Reserved.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 part of quiver.collection; 15 part of quiver.collection;
16 16
17 /** 17 /// A bi-directional map whose key-value pairs form a one-to-one
18 * A bi-directional map whose key-value pairs form a one-to-one correspondence. 18 /// correspondence. BiMaps support an `inverse` property which gives access to
19 * BiMaps support an `inverse` property which gives access to an inverted view 19 /// an inverted view of the map, such that there is a mapping (v, k) for each
20 * of the map, such that there is a mapping (v, k) for each pair (k, v) in the 20 /// pair (k, v) in the original map. Since a one-to-one key-value invariant
21 * original map. Since a one-to-one key-value invariant applies, it is an error 21 /// applies, it is an error to insert duplicate values into this map. It is
22 * to insert duplicate values into this map. It is also an error to insert null 22 /// also an error to insert null keys or values into this map.
23 * keys or values into this map.
24 */
25 abstract class BiMap<K, V> implements Map<K, V> { 23 abstract class BiMap<K, V> implements Map<K, V> {
26 /** 24 /// Creates a BiMap instance with the default implementation.
27 * Creates a BiMap instance with the default implementation.
28 */
29 factory BiMap() => new HashBiMap(); 25 factory BiMap() => new HashBiMap();
30 26
31 /** 27 /// Adds an association between key and value.
32 * Adds an association between key and value. 28 ///
33 * 29 /// Throws [ArgumentError] if an association involving [value] exists in the
34 * Throws [ArgumentError] if an association involving [value] exists in the 30 /// map; otherwise, the association is inserted, overwriting any existing
35 * map; otherwise, the association is inserted, overwriting any existing 31 /// association for the key.
36 * association for the key.
37 */
38 void operator []=(K key, V value); 32 void operator []=(K key, V value);
39 33
40 /** 34 /// Replaces any existing associations(s) involving key and value.
41 * Replaces any existing associations(s) involving key and value. 35 ///
42 * 36 /// If an association involving [key] or [value] exists in the map, it is
43 * If an association involving [key] or [value] exists in the map, it is 37 /// removed.
44 * removed.
45 */
46 void replace(K key, V value); 38 void replace(K key, V value);
47 39
48 /** 40 /// Returns the inverse of this map, with key-value pairs (v, k) for each pair
49 * Returns the inverse of this map, with key-value pairs (v, k) for each 41 /// (k, v) in this map.
50 * pair (k, v) in this map.
51 */
52 BiMap<V, K> get inverse; 42 BiMap<V, K> get inverse;
53 } 43 }
54 44
55 /** 45 /// A hash-table based implementation of BiMap.
56 * A hash-table based implementation of BiMap.
57 */
58 class HashBiMap<K, V> implements BiMap<K, V> { 46 class HashBiMap<K, V> implements BiMap<K, V> {
59 final Map<K, V> _map; 47 final Map<K, V> _map;
60 final Map<V, K> _inverse; 48 final Map<V, K> _inverse;
61 BiMap<V, K> _cached; 49 BiMap<V, K> _cached;
62 50
63 HashBiMap() : this._from(new HashMap(), new HashMap()); 51 HashBiMap() : this._from(new HashMap(), new HashMap());
64 HashBiMap._from(this._map, this._inverse); 52 HashBiMap._from(this._map, this._inverse);
65 53
66 V operator [](Object key) => _map[key]; 54 V operator [](Object key) => _map[key];
67 void operator []=(K key, V value) { 55 void operator []=(K key, V value) {
68 _add(key, value, false); 56 _add(key, value, false);
69 } 57 }
58
70 void replace(K key, V value) { 59 void replace(K key, V value) {
71 _add(key, value, true); 60 _add(key, value, true);
72 } 61 }
62
73 void addAll(Map<K, V> other) => other.forEach((k, v) => _add(k, v, false)); 63 void addAll(Map<K, V> other) => other.forEach((k, v) => _add(k, v, false));
74 bool containsKey(Object key) => _map.containsKey(key); 64 bool containsKey(Object key) => _map.containsKey(key);
75 bool containsValue(Object value) => _inverse.containsKey(value); 65 bool containsValue(Object value) => _inverse.containsKey(value);
76 void forEach(void f(K key, V value)) => _map.forEach(f); 66 void forEach(void f(K key, V value)) => _map.forEach(f);
77 bool get isEmpty => _map.isEmpty; 67 bool get isEmpty => _map.isEmpty;
78 bool get isNotEmpty => _map.isNotEmpty; 68 bool get isNotEmpty => _map.isNotEmpty;
79 Iterable<K> get keys => _map.keys; 69 Iterable<K> get keys => _map.keys;
80 int get length => _map.length; 70 int get length => _map.length;
81 Iterable<V> get values => _inverse.keys; 71 Iterable<V> get values => _inverse.keys;
82 72
(...skipping 29 matching lines...) Expand all
112 if (_inverse.containsKey(value)) { 102 if (_inverse.containsKey(value)) {
113 if (!replace) throw new ArgumentError("Mapping for $value exists"); 103 if (!replace) throw new ArgumentError("Mapping for $value exists");
114 _map.remove(_inverse[value]); 104 _map.remove(_inverse[value]);
115 } 105 }
116 _inverse.remove(oldValue); 106 _inverse.remove(oldValue);
117 _map[key] = value; 107 _map[key] = value;
118 _inverse[value] = key; 108 _inverse[value] = key;
119 return value; 109 return value;
120 } 110 }
121 } 111 }
OLDNEW
« no previous file with comments | « packages/quiver/lib/src/cache/map_cache.dart ('k') | packages/quiver/lib/src/collection/delegates/iterable.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698