Skip to content
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

Adds [Heap]PriorityQueue.of constructor. #734

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Adds [Heap]PriorityQueue.of constructor. #734

wants to merge 2 commits into from

Conversation

lrhn
Copy link
Member

@lrhn lrhn commented Dec 18, 2024

Introduces efficient (linear-number of comparisons) "heapify" algorithm for converting an unsorted list to a heap-sorted list, using it for the of constructor, and after a large addAll operation, when it's presumed faster than just bubbling down all the new elements.

Also rewrites HeapPriorityQueue to use a growable list as backing array, instead of implementing the same thing using the double-when-full algorithm, and still having to deal with nullable cells. The platform growable list implementation is assumed to efficiently avoid some of those null checks.

Closes #732

Introduces efficient "heapify" algorithm for converting an unsorted list
to a heap-sorted list, using it for the `of` constructor, and after a
large `addAll` operation, when it's presumed faster than just bubbling
down all the new elements.

Also rewrites `HeapPriorityQueue` to use a growable list as backing
array, instead of implementing the same thing using the double-when-full
algorithm, and still having to deal with nullable cells.
The platform growable list implementation is assumed to efficiently
avoid some of those `null` checks.
Copy link

github-actions bot commented Dec 18, 2024

PR Health

Breaking changes ✔️
Package Change Current Version New Version Needed Version Looking good?
collection Non-Breaking 1.19.1 1.20.0-wip 1.20.0 ✔️
Changelog Entry ✔️
Package Changed Files

Changes to files need to be accounted for in their respective changelogs.

Coverage ⚠️
File Coverage
pkgs/collection/lib/src/priority_queue.dart 💔 98 % ⬇️ 0 %

This check for test coverage is informational (issues shown here will not fail the PR).

This check can be disabled by tagging the PR with skip-coverage-check.

API leaks ✔️

The following packages contain symbols visible in the public API, but not exported by the library. Export these symbols or remove them from your publicly visible API.

Package Leaked API symbols
License Headers ✔️
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
Files
no missing headers

All source files should start with a license header.

/// If [comparison] is omitted, it defaults to [Comparable.compare]. If this
/// is the case, `E` must implement [Comparable], and this is checked at
/// runtime for every comparison.
factory PriorityQueue.of(
Copy link
Member

Choose a reason for hiding this comment

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

What's the right way to easily support Comparable<T>

factory PriorityQueue.ofComparable<T extends Comparable<T>>(Iterable<T>)...something?

Copy link
Member Author

@lrhn lrhn Dec 20, 2024

Choose a reason for hiding this comment

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

That's an option, with static instead of factory since constructors cannot be generic.

static PriorityQueue<T> ofComparable<T extends Comparable<T>>([Iterable<T>? values]) =>
    PriorityQueue<T>.of(compareComparable<T>, values);

Or just have the user use Comparable.compare explicitly.

A problem with <T extends Comparable<T>> is that it currently fails to infer for int and double.
(Although that seems to be fixed in 3.7.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement heapify in HeapPriorityQueue.addAll
2 participants