Skip to content

Fully generalize BTree{Map, Set} range iterators #27545

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

Merged
merged 1 commit into from
Aug 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,7 +1522,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// ```
#[unstable(feature = "btree_range",
reason = "matches collection reform specification, waiting for dust to settle")]
pub fn range<'a>(&'a self, min: Bound<&K>, max: Bound<&K>) -> Range<'a, K, V> {
pub fn range<Min: ?Sized + Ord = K, Max: ?Sized + Ord = K>(&self, min: Bound<&Min>,
max: Bound<&Max>)
-> Range<K, V> where
K: Borrow<Min> + Borrow<Max>,
{
range_impl!(&self.root, min, max, as_slices_internal, iter, Range, edges, [])
}

Expand All @@ -1542,7 +1546,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// let mut map: BTreeMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"].iter()
/// .map(|&s| (s, 0))
/// .collect();
/// for (_, balance) in map.range_mut(Included(&"B"), Excluded(&"Cheryl")) {
/// for (_, balance) in map.range_mut(Included("B"), Excluded("Cheryl")) {
/// *balance += 100;
/// }
/// for (name, balance) in &map {
Expand All @@ -1551,7 +1555,11 @@ impl<K: Ord, V> BTreeMap<K, V> {
/// ```
#[unstable(feature = "btree_range",
reason = "matches collection reform specification, waiting for dust to settle")]
pub fn range_mut<'a>(&'a mut self, min: Bound<&K>, max: Bound<&K>) -> RangeMut<'a, K, V> {
pub fn range_mut<Min: ?Sized + Ord = K, Max: ?Sized + Ord = K>(&mut self, min: Bound<&Min>,
max: Bound<&Max>)
-> RangeMut<K, V> where
K: Borrow<Min> + Borrow<Max>,
{
range_impl!(&mut self.root, min, max, as_slices_internal_mut, iter_mut, RangeMut,
edges_mut, [mut])
}
Expand Down
8 changes: 6 additions & 2 deletions src/libcollections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1528,7 +1528,9 @@ macro_rules! node_slice_impl {
}

/// Returns a sub-slice with elements starting with `min_key`.
pub fn slice_from(self, min_key: &K) -> $NodeSlice<'a, K, V> {
pub fn slice_from<Q: ?Sized + Ord>(self, min_key: &Q) -> $NodeSlice<'a, K, V> where
K: Borrow<Q>,
{
// _______________
// |_1_|_3_|_5_|_7_|
// | | | | |
Expand Down Expand Up @@ -1556,7 +1558,9 @@ macro_rules! node_slice_impl {
}

/// Returns a sub-slice with elements up to and including `max_key`.
pub fn slice_to(self, max_key: &K) -> $NodeSlice<'a, K, V> {
pub fn slice_to<Q: ?Sized + Ord>(self, max_key: &Q) -> $NodeSlice<'a, K, V> where
K: Borrow<Q>,
{
// _______________
// |_1_|_3_|_5_|_7_|
// | | | | |
Expand Down
6 changes: 5 additions & 1 deletion src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,11 @@ impl<T: Ord> BTreeSet<T> {
/// ```
#[unstable(feature = "btree_range",
reason = "matches collection reform specification, waiting for dust to settle")]
pub fn range<'a>(&'a self, min: Bound<&T>, max: Bound<&T>) -> Range<'a, T> {
pub fn range<'a, Min: ?Sized + Ord = T, Max: ?Sized + Ord = T>(&'a self, min: Bound<&Min>,
max: Bound<&Max>)
-> Range<'a, T> where
T: Borrow<Min> + Borrow<Max>,
{
fn first<A, B>((a, _): (A, B)) -> A { a }
let first: fn((&'a T, &'a ())) -> &'a T = first; // coerce to fn pointer

Expand Down