Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Add CombinedMapView #53

Merged
merged 4 commits into from
Mar 23, 2017
Merged

Conversation

matanlurey
Copy link
Contributor

Branched from #52, which needs to be merged first.

Finally closes dart-lang/core#711.

@matanlurey matanlurey requested a review from nex3 February 9, 2017 04:17
@override
V operator [](Object key) {
for (var map in _maps) {
// Avoid two hash look ups on a positive hit.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

positive non-null hit 😄

@matanlurey
Copy link
Contributor Author

I'll rebase against master and ping when ready.

@matanlurey
Copy link
Contributor Author

Ready for review @nex3!

class CombinedMapView<K, V> extends UnmodifiableMapBase<K, V> {
final Iterable<Map<K, V>> _maps;

CombinedMapView(this._maps);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document that the maps iterable is accessed repeatedly, so it should be a collection type like List or Set rather than a lazy iterable produced by map() et al.

I really wish we had a distinct Collection type so we could express that statically.

return null;
}

Iterable<K> get keys => new CombinedIterableView<K>(_maps.map((m) => m.keys));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the UnmodifiableMapBase docs:

The keys iterable should have efficient length and contains operations, and it should catch concurrent modifications of the keys while iterating.

CombinedIterableView doesn't have an efficient contains() (maybe that should be special-cased as well). It also won't necessarily throw if a later map is modified while an earlier one is being traversed, but that may not realistically be fixable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a special case for contains in CombinedIterableView.

I don't think I can do anything about concurrent modifications. I can document that concurrent modifications should be avoided because we don't throw?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documenting that would be good. Be sure to mention that it will sometimes throw, but maybe don't guarantee the specific circumstances so we can change it in the future if we need to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


V operator [](Object key) {
for (var map in _maps) {
// Avoid two hash look ups on a positive hit.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: "lookups"

final map1 = const {1: 1, 2: 2, 3: 3};
final map2 = const {4: 4, 5: 5, 6: 6};
final map3 = const {7: 7, 8: 8, 9: 9};
final concat = {}..addAll(map1)..addAll(map2)..addAll(map3);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: var

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

nex3
nex3 previously requested changes Mar 6, 2017
@@ -24,6 +24,8 @@ class CombinedIterableView<T> extends IterableBase<T> {
// Special cased isEmpty/length since many iterables have an efficient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mention that contains is special-cased as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

return null;
}

Iterable<K> get keys => new CombinedIterableView<K>(_maps.map((m) => m.keys));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documenting that would be good. Be sure to mention that it will sometimes throw, but maybe don't guarantee the specific circumstances so we can change it in the future if we need to.

@matanlurey matanlurey dismissed nex3’s stale review March 16, 2017 03:44

nex3 is OOO for a bit.

@matanlurey
Copy link
Contributor Author

@kevmoo Do you mind approving? I think I've addressed all of @nex3's feedback but it would be nice not to have to wait several weeks to merge something already vetted :)

@matanlurey
Copy link
Contributor Author

One more tiny ping for @kevmoo

@matanlurey matanlurey merged commit 546123b into dart-archive:master Mar 23, 2017
@matanlurey matanlurey deleted the combined_map branch March 23, 2017 16:40
mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 18, 2024
* Add CombinedMapView.

* Mistype.

* Address feedback.

* Address feedback.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Add top-level combine<Iterables|Lists|Maps>
4 participants