-
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
Conversation
lib/src/collection/bimap.dart
Outdated
_cached = new HashBiMap._from(_inverse, _map); | ||
} | ||
return _cached; | ||
} |
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:
BiMap<V, K> get inverse => _cached ??= new HashBiMap._from(_inverse, _map);
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.
lib/src/collection/bimap.dart
Outdated
} | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Why not returning _map.entries
?
Iterable<MapEntry<K, V>> get entries => _map.entries;
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.
Done.
lib/src/collection/bimap.dart
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Why not returning _map.map(transform)
?
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
* master: Add new Dart 2 Iterable and List methods (google#422) Document google#373 as breaking change (google#424) Add tests of TreeSet removal operations (google#420)
lib/src/collection/bimap.dart
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Implement this using _add
since it should apply the same invariants.
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.
Done.
throw new UnimplementedError("map"); | ||
} | ||
Map<K2, V2> map<K2, V2>(MapEntry<K2, V2> transform(K key, V value)) => | ||
_map.map(transform); |
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.
Does Map.map
document the expected return type? If it's expected to be a BiMap
, we need to apply the invariants from _add
(i.e. that not only must the keys be unique, but so must the values, since they're the keys of the inverse).
If it's not, then I think this is fine as-is.
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.
My thought is it looks just like List.map
, Set.map`, ...: you don't expect a List or a Set out the other side; you just expect an Iterable.
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.
Cool. That sounds perfectly reasonable to me.
lib/src/collection/bimap.dart
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Let's do this in the same order as remove
... not that it really matters :)
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.
Done.
R rrr rrrrrrready to merge! |
* master: Add new Dart 2 Map methods to BiMap (google#423) Add new Dart 2 Map, Iterable, and List methods to multimap stuff (google#425) Add .dart_tool to gitignore (google#431) Update changelog for LruMap fix (google#430) Ensure that LruMap linkage is preserved (google#429) Update CHANGELOG.md (google#428) Update CHANGELOG/pubspec to include 0.28.1 (google#427)
Partial fix to #418. |
No description provided.