-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Dart 2 Map methods to BiMap #423
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,13 +70,18 @@ class HashBiMap<K, V> implements BiMap<K, V> { | |
int get length => _map.length; | ||
Iterable<V> get values => _inverse.keys; | ||
|
||
BiMap<V, K> get inverse { | ||
if (_cached == null) { | ||
_cached = new HashBiMap._from(_inverse, _map); | ||
} | ||
return _cached; | ||
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_method | ||
void addEntries(Iterable<Object> entries) { | ||
// Change Iterable<Object> to Iterable<MapEntry<K, V>> when | ||
// the MapEntry class has been added. | ||
throw new UnimplementedError("addEntries"); | ||
void addEntries(Iterable<MapEntry<K, V>> entries) { | ||
for (var entry in entries) { | ||
this[entry.key] = entry.value; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implement this using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
} | ||
|
||
@override | ||
|
@@ -87,28 +92,18 @@ class HashBiMap<K, V> implements BiMap<K, V> { | |
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_getter | ||
Iterable<Null> get entries { | ||
// Change Iterable<Null> to Iterable<MapEntry<K, V>> when | ||
// the MapEntry class has been added. | ||
throw new UnimplementedError("entries"); | ||
} | ||
|
||
BiMap<V, K> get inverse { | ||
if (_cached == null) { | ||
_cached = new HashBiMap._from(_inverse, _map); | ||
} | ||
return _cached; | ||
Iterable<MapEntry<K, V>> get entries { | ||
return keys.map((K key) => new MapEntry<K, V>(key, this[key])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not returning Iterable<MapEntry<K, V>> get entries => _map.entries; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_method | ||
Map<K2, V2> map<K2, V2>(Object transform(K key, V value)) { | ||
// Change Object to MapEntry<K2, V2> when | ||
// the MapEntry class has been added. | ||
throw new UnimplementedError("map"); | ||
Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> transform(K key, V value)) { | ||
var result = <K2, V2>{}; | ||
for (var key in this.keys) { | ||
var entry = transform(key, this[key]); | ||
result[entry.key] = entry.value; | ||
} | ||
return result; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not returning Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> transform(K key, V value)) {
return _map.map(transform);
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
V putIfAbsent(K key, V ifAbsent()) { | ||
|
@@ -124,10 +119,9 @@ class HashBiMap<K, V> implements BiMap<K, V> { | |
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_method | ||
void removeWhere(bool test(K key, V value)) { | ||
throw new UnimplementedError("removeWhere"); | ||
_map.removeWhere(test); | ||
_inverse.removeWhere((v, k) => test(k, v)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Let's do this in the same order as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
} | ||
|
||
@override | ||
|
@@ -138,17 +132,22 @@ class HashBiMap<K, V> implements BiMap<K, V> { | |
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_method | ||
V update(K key, V update(V value), {V ifAbsent()}) { | ||
throw new UnimplementedError("update"); | ||
var value = _map[key]; | ||
if (value != null) { | ||
return _add(key, update(value), true); | ||
} else { | ||
if (ifAbsent == null) | ||
throw new ArgumentError.value(key, 'key', 'Key not in map'); | ||
return _add(key, ifAbsent(), false); | ||
} | ||
} | ||
|
||
@override | ||
// TODO: Dart 2.0 requires this method to be implemented. | ||
// ignore: override_on_non_overriding_method | ||
void updateAll(V update(K key, V value)) { | ||
throw new UnimplementedError("updateAll"); | ||
for (var key in this.keys) { | ||
_add(key, update(key, _map[key]), true); | ||
} | ||
} | ||
|
||
void clear() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With null-aware assignment:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love it. Done.