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

Add toUnorderedList to PriorityQueue. #152

Merged
merged 4 commits into from
Sep 18, 2020
Merged

Add toUnorderedList to PriorityQueue. #152

merged 4 commits into from
Sep 18, 2020

Conversation

lrhn
Copy link
Contributor

@lrhn lrhn commented Sep 10, 2020

Also change HeapPriorityQueue to use == for contains and remove checks.

Fix a bad null-safety migration for HeapPriorityQueue, and add tests to ensure it works with null values.

Fixes dart-lang/core#710, dart-lang/core#606

Also change `HeapPriorityQueue` to use `==` for
`contains` and `remove` checks.

Fix a bad null-safety migration for HeapPriorityQueue,
and add tests to ensure it works with `null` values.
@lrhn lrhn requested a review from jakemac53 September 10, 2020 14:08
@lrhn lrhn requested a review from natebosch September 10, 2020 14:08
Copy link
Contributor

@jakemac53 jakemac53 left a comment

Choose a reason for hiding this comment

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

Is it actually useful to store nulls in a queue like this? That seems really dubious to me. We could instead make it <E extends Object>?

Maybe not worth restricting though...

@@ -9,6 +9,18 @@ import 'utils.dart';
/// A priority queue is a priority based work-list of elements.
///
/// The queue allows adding elements, and removing them again in priority order.
/// The same object can be added to the queue more than once.
/// There is no specified oredering for objects with the same priority
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// There is no specified oredering for objects with the same priority
/// There is no specified ordering for objects with the same priority

}
return set;
}

@override
List<E> toUnorderedList() => _toUnorderedList();
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need the private one? Can we put the implementation here and skip the function?

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 call this method from toList too. It's an implementation detail that the two methods share code, so I don't want to risk someone overriding toUnorderedList and also affecting toList.
Generally, I avoid calling an overridable method from another method unless the latter method is explicitly documented as depending on the former. Code sharing is an implementation detail, so it shouldn't affect the visible API.

@lrhn
Copy link
Contributor Author

lrhn commented Sep 11, 2020

All our other collections accept null, so it would stand out if this one doesn't, and it would be a breaking change.
Using as E also works better than ! with legacy code which might store null values in a HeapPriorityQueue<int>, because null as int* doesn't throw, but null! does.

There is no real reason to not to accept null as a value. The null value is an object too.

The _elementAt code will only read a null if the E type actually accepts null - it's only used for indices in the range where we know we have stored elements. All the as E operations will only see null values actually stored into the heap, so it's safely compilable to JavaScript where we omit the as casts.

@mit-mit
Copy link
Contributor

mit-mit commented Sep 17, 2020

@lrhn ready to merge?

@lrhn lrhn merged commit 65d3e58 into master Sep 18, 2020
@natebosch natebosch deleted the heap-eq branch September 18, 2020 17:56
if (comp == 0) return index;
if (comp < 0) {
if (comp <= 0) {
if (comp == 0 && element == object) return index;
Copy link
Contributor

Choose a reason for hiding this comment

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

This turned out to break at least one project's tests.

I think it's the right thing to do, and they were able to work around it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Super. If you rely on compareTo for equality, and don't override == to match, then I guess this would affect you.

mosuem pushed a commit to dart-lang/core that referenced this pull request Oct 18, 2024
Add `toUnorderedList` to `PriorityQueue`.

Also change `HeapPriorityQueue` to use `==` for
`contains` and `remove` checks.

Fix a bad null-safety migration for HeapPriorityQueue,
and add tests to ensure it works with `null` values.
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.

PriorityQueue and comparison function inconsistent
5 participants