Skip to content

std: Make PriorityQueue and PriorityDequeue unmanaged containers #23431

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

allocgator
Copy link
Contributor

@allocgator allocgator commented Apr 2, 2025

Edit:

Revived #21433

Solves: #21432


Can I merge

/// Pop the highest priority element from the queue. Returns `null` if empty.
pub fn removeOrNull(self: *Self) ?T {
    return if (self.items.len > 0) self.remove() else null;
}

and

/// Remove and return the highest priority element from the queue
pub fn remove(self: *Self) T {
    return self.removeIndex(0);
}

into a single method

/// Pop the highest priority element from the queue. Returns `null` if empty.
pub fn remove(self: *Self) ?T {
    return if (self.items.len > 0) self.removeIndex(0) else null;
}

?

This would make the new remove method similar to the peek method.

@allocgator allocgator marked this pull request as ready for review April 2, 2025 01:05
@allocgator allocgator changed the title std: priority queue - Convert priority queue to unmanaged container std: PriorityQueue and PriorityDequeue are unmanaged containers Apr 2, 2025
@allocgator allocgator changed the title std: PriorityQueue and PriorityDequeue are unmanaged containers std: Make PriorityQueue and PriorityDequeue unmanaged containers Apr 2, 2025
@bndbsh
Copy link
Contributor

bndbsh commented Apr 5, 2025

Note that the comment still mentions pop which doesn't exist. Would be nice to update that (or maybe rename remove)

@allocgator
Copy link
Contributor Author

allocgator commented Apr 5, 2025

Note that the comment still mentions pop which doesn't exist. Would be nice to update that (or maybe rename remove)

pop makes sense as its a queue data structure and its functionality is currently provided by removeOrNull.

Furthermore, if there's a pop method, it may also involve renaming add related methods to push.

Not sure if that comment is from one of the reviewers.

Looping in @alexrp for feedback on:

  1. Renaming

    • insert methods to push methods
    • removeIndex to popIndex
  2. Combining removeOrNull and remove into pop, where the new pop method looks like:

pub fn pop(self: *Self) ?T {
    return if (self.items.len > 0) self.popIndex(0) else null;
}

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

Successfully merging this pull request may close these issues.

2 participants