forked from google/quiver-dart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bimap.dart
176 lines (145 loc) · 4.74 KB
/
bimap.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2013 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import 'dart:collection';
/// A bi-directional map whose key-value pairs form a one-to-one
/// correspondence. BiMaps support an `inverse` property which gives access to
/// an inverted view of the map, such that there is a mapping (v, k) for each
/// pair (k, v) in the original map. Since a one-to-one key-value invariant
/// applies, it is an error to insert duplicate values into this map.
abstract class BiMap<K, V> implements Map<K, V> {
/// Creates a BiMap instance with the default implementation.
factory BiMap() => HashBiMap();
/// Adds an association between key and value.
///
/// Throws [ArgumentError] if an association involving [value] exists in the
/// map; otherwise, the association is inserted, overwriting any existing
/// association for the key.
@override
void operator []=(K key, V value);
/// Replaces any existing associations(s) involving key and value.
///
/// If an association involving [key] or [value] exists in the map, it is
/// removed.
void replace(K key, V value);
/// Returns the inverse of this map, with key-value pairs (v, k) for each pair
/// (k, v) in this map.
BiMap<V, K> get inverse;
}
/// A hash-table based implementation of BiMap.
class HashBiMap<K, V> implements BiMap<K, V> {
HashBiMap() : this._from(HashMap<K, V>(), HashMap<V, K>());
HashBiMap._from(this._map, this._inverse);
final Map<K, V> _map;
final Map<V, K> _inverse;
BiMap<V, K>? _cached;
@override
V? operator [](Object? key) => _map[key];
@override
void operator []=(K key, V value) {
_add(key, value, false);
}
@override
void replace(K key, V value) {
_add(key, value, true);
}
@override
void addAll(Map<K, V> other) => other.forEach((k, v) => _add(k, v, false));
@override
bool containsKey(Object? key) => _map.containsKey(key);
@override
bool containsValue(Object? value) => _inverse.containsKey(value);
@override
void forEach(void Function(K key, V value) f) => _map.forEach(f);
@override
bool get isEmpty => _map.isEmpty;
@override
bool get isNotEmpty => _map.isNotEmpty;
@override
Iterable<K> get keys => _map.keys;
@override
int get length => _map.length;
@override
Iterable<V> get values => _inverse.keys;
@override
BiMap<V, K> get inverse => _cached ??= HashBiMap._from(_inverse, _map);
@override
void addEntries(Iterable<MapEntry<K, V>> entries) {
for (final entry in entries) {
_add(entry.key, entry.value, false);
}
}
@override
Map<K2, V2> cast<K2, V2>() {
// TODO(cbracken): Dart 2.0 requires this method to be implemented.
throw UnimplementedError('cast');
}
@override
Iterable<MapEntry<K, V>> get entries => _map.entries;
@override
Map<K2, V2> map<K2, V2>(
MapEntry<K2, V2> Function(K key, V value) transform) =>
_map.map(transform);
@override
V putIfAbsent(K key, V Function() ifAbsent) {
if (containsKey(key)) {
return _map[key]!;
}
return _add(key, ifAbsent(), false);
}
@override
V? remove(Object? key) {
_inverse.remove(_map[key]);
return _map.remove(key);
}
@override
void removeWhere(bool Function(K key, V value) test) {
_inverse.removeWhere((v, k) => test(k, v));
_map.removeWhere(test);
}
@override
V update(K key, V Function(V value) update, {V Function()? ifAbsent}) {
var value = _map[key];
if (value != null) {
return _add(key, update(value), true);
} else {
if (ifAbsent == null) {
throw ArgumentError.value(key, 'key', 'Key not in map');
}
return _add(key, ifAbsent(), false);
}
}
@override
void updateAll(V Function(K key, V value) update) {
for (final key in keys) {
_add(key, update(key, _map[key]!), true);
}
}
@override
void clear() {
_map.clear();
_inverse.clear();
}
V _add(K key, V value, bool replace) {
var oldValue = _map[key];
if (containsKey(key) && oldValue == value) return value;
if (_inverse.containsKey(value)) {
if (!replace) throw ArgumentError('Mapping for $value exists');
_map.remove(_inverse[value]);
}
_inverse.remove(oldValue);
_map[key] = value;
_inverse[value] = key;
return value;
}
}