rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.97k stars 12.69k forks source link

max and min of treeset/treemap? #55975

Closed alok closed 1 year ago

alok commented 5 years ago

Since trees are naturally based on ordered types, it would be nice to have a method to access the min and max values of a tree (or keys in the case of a treemap). This isn't high priority, but is something I noticed when trying to implement some data structures in Rust.

scottmcm commented 5 years ago

Since the iterators are sorted and double-ended, can't you just do .iter().next() for .min() and .iter().next_back() for .max()? Come to think of it, the iterators could even override the iterator .min() and .max() methods to be O(1)...

kadiwa4 commented 1 year ago

The requested methods have been stabilized in 1.66.0 (first* and last* on BTreeMap/BTreeSet).