Skip to content

Re-pushing with the highest priority does not always put the item in the top #6

@da-x

Description

@da-x

The following example re-pushes "d" with the highest priority among all already pushed items. We expect to see "d" as the first time in .pop(), but we see "a" instead, and "d" comes second.

extern crate priority_queue;

use priority_queue::PriorityQueue;

fn main() {
    let mut pq : PriorityQueue<&'static str, usize> = PriorityQueue::new();

    pq.push("a", 9);
    pq.push("b", 8);
    pq.push("c", 7);
    pq.push("d", 6);

    // Re-pushing with the highest priority
    pq.push("d", 20);

    let mut prev_opt = None;
    for (item, x) in pq.into_sorted_iter() {
        println!("{:?} {:?}", item, x);
        if let Some(prev) = prev_opt {
            if prev < x {
                println!("WTF");
            }
        }
        prev_opt = Some(x);
    }
}

Output:

"a" 9
"d" 20
WTF
"b" 8
"c" 7

Tested along with:

[dependencies]
priority-queue = { git = "https://github.com/garro95/priority-queue" }

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions