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

Wrong first element popped in pop_first() for BTreeSet #68829

Closed
SetTheorist opened this issue Feb 4, 2020 · 8 comments
Closed

Wrong first element popped in pop_first() for BTreeSet #68829

SetTheorist opened this issue Feb 4, 2020 · 8 comments
Labels
A-collections Area: std::collections. C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@SetTheorist
Copy link

Summary:
When popping elements using pop_first() from a BTreeSet, the first element popped is not the min element, though remaining elements are popped in expected order.

I tried this code:

#![feature(map_first_last)]
use std::collections::BTreeSet;

type D = isize;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct T(pub D, pub D);

pub fn main() {
    let mut s = BTreeSet::new();
    let v = vec![
        T(0, 0),
        T(63, 63),
        T(63, 252),
        T(126, 126),
        T(189, 189),
        T(252, 252),
        T(252, 441),
        T(315, 315),
        T(378, 378),
        T(441, 441),
        T(441, 503),
        T(504, 504),
    ];
    for x in &v {
        s.insert(x);
    }
    while let Some(x) = s.pop_first() {
        print!(" {:?}", x);
    }
    println!("");
}

I expected to see this happen:

 T(0, 0) T(63, 63) T(63, 252) T(126, 126) T(189, 189) T(252, 252) T(252, 441) T(315, 315) T(378, 378) T(441, 441) T(441, 503) T(504, 504)

Instead, this happened: (Note the first result).

 T(252, 441) T(0, 0) T(63, 63) T(63, 252) T(126, 126) T(189, 189) T(252, 252) T(315, 315) T(378, 378) T(441, 441) T(441, 503) T(504, 504)

For various subsets of the given inputs I tried, it appears to give the expected behaviour.

Meta

rustc --version --verbose:
rustc 1.42.0-nightly (8417d68 2020-02-03)
binary: rustc
commit-hash: 8417d68
commit-date: 2020-02-03
host: x86_64-unknown-linux-gnu
release: 1.42.0-nightly
LLVM version: 9.0

@jonas-schievink
Copy link
Contributor

Thanks for the report! Do you know if this is a recent regression, or did this always happen? cc @ssomers in any case

@jonas-schievink jonas-schievink added A-collections Area: std::collections. C-bug Category: This is a bug. I-nominated T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. and removed I-nominated labels Feb 4, 2020
@jonas-schievink
Copy link
Contributor

Oh this is nightly-only

@SetTheorist
Copy link
Author

It was there in [rustc 1.42.0-nightly (31dd4f4 2020-01-13)], as that is what I was using initially, but I don't know when it appeared before that.

@ssomers
Copy link
Contributor

ssomers commented Feb 4, 2020

Well, your expectations are right, and play.rust-lang.org confirms it's not what happens.

@ssomers
Copy link
Contributor

ssomers commented Feb 4, 2020

The implementation is completely wrong. Apparently I did not test this on anything with more than 1 node (11 elements)!?

@ssomers
Copy link
Contributor

ssomers commented Feb 4, 2020

So it's always been wrong (picking the first element from the root node) and it will be right in some future nightly, hopefully a day or two from now. Thank you for the clear report.

@SetTheorist
Copy link
Author

Thanks for the rapid response!

Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 8, 2020
…r=KodrAus

Fix and test implementation of BTreeMap's first/last_entry, pop_first/last

Properly implement and test `first_entry` & `last_entry` to fix problem report rust-lang#68829
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 8, 2020
…r=KodrAus

Fix and test implementation of BTreeMap's first/last_entry, pop_first/last

Properly implement and test `first_entry` & `last_entry` to fix problem report rust-lang#68829
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue Feb 8, 2020
…r=KodrAus

Fix and test implementation of BTreeMap's first/last_entry, pop_first/last

Properly implement and test `first_entry` & `last_entry` to fix problem report rust-lang#68829
@runiq
Copy link

runiq commented Feb 13, 2020

Drive-by triage: This should apparently have been closed by #68834.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-collections Area: std::collections. C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants