-
Notifications
You must be signed in to change notification settings - Fork 8
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
CombinedMapView does not maintain keys uniqueness #595
Comments
My workaround was creating my own type on top of it: class SnapshotMap<K, V> extends CombinedMapView<K, V> {
Iterable<K> Function() _lazyKeys;
SnapshotMap(Map<K, V> topMap, Map<K, V> bottomMap)
: super([topMap, bottomMap]) {
_lazyKeys = lazy(() => super.keys.toSet());
}
UnmodifiableMapView<K, V> get view => UnmodifiableMapView(this);
@override
Iterable<K> get keys => _lazyKeys();
@override
int get length => keys.length;
} |
Ya, I am going to see if I can maintain the lazy behavior of the current implementation though if possible. Otherwise this would likely break some projects. |
No the prettiest thing, but my T Function() lazy<T>(T Function() f) {
T instance;
return () {
if (instance == null) {
instance = f();
if (instance == null) {
throw StateError('lazy function must not return null');
}
}
return instance;
};
} |
The problem there is the |
Note that in adding the test for this I also noticed |
(these all get fixed by fixing |
That's true, but if you avoid that you'll have to check for already-iterated keys on all maps except the first... which may be more costly?! Unless perhaps if you accumulate keys as you see them into a set? |
Yes I am accumulating them as i see them, the impl is actually really easy/small. |
Fixes https://github.com/dart-lang/collection/issues/109 Adds a custom iterable/iterator that can filter out duplicates and use that for the `CombineMapView.keys` getter. Updates tests to contain duplicates in maps, and ensure the keys/values from the earlier maps are the ones that are returned. Updates the changelog and docs to no longer claim `O(maps)` for the length getter. This now requires iteration of all items and is `O(total map entries)`. Prepare to publish as 1.14.12
@jakemac53 is this going to be released soon? |
Yes, should be very soon (waiting for an existing publisher to add me as an uploader or publish it themselves). |
This is now published as version 1.14.12 |
Fixes https://github.com/dart-lang/collection/issues/109 Adds a custom iterable/iterator that can filter out duplicates and use that for the `CombineMapView.keys` getter. Updates tests to contain duplicates in maps, and ensure the keys/values from the earlier maps are the ones that are returned. Updates the changelog and docs to no longer claim `O(maps)` for the length getter. This now requires iteration of all items and is `O(total map entries)`. Prepare to publish as 1.14.12
The following test fails:
The first expectation fails because
length
returns5
.The second, because
keys
returns['1', '2', '3', '2', '4']
.This seems to violate
Map
's interface, which says:This was a problem for me because I needed something that looks just like a normal
Map
but based on two other Maps, and which lets a map coming first "override" values on the map coming later in the list of maps. I believe that's the main purpose of combining maps (possibly unmodifiable), but due to this issue, it can't be used like that.The text was updated successfully, but these errors were encountered: