]> git.lizzy.rs Git - rust.git/commitdiff
Fully generalize `BTree{Map, Set}` range iterators
authorAndrew Paseltiner <apaseltiner@gmail.com>
Wed, 5 Aug 2015 19:12:10 +0000 (15:12 -0400)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Wed, 5 Aug 2015 20:06:12 +0000 (16:06 -0400)
This permits collections with `String` keys to be ranged over with
`&str` bounds. The `K` defaults for `Min` and `Max` permit the default
type parameter fallback to work with things like

```rust
use std::collections::{BTreeSet, Bound};
let set = BTreeSet::<String>::new();
set.range(Bound::Included("a"), Bound::Unbounded);
```

Without the defaults, the type of the maximum bound would be
unconstrained.

src/libcollections/btree/map.rs
src/libcollections/btree/node.rs
src/libcollections/btree/set.rs

index 9fa54c6ca2facc624f42d5fafe123e2f3104bbbc..f5d1d44b4048295b12f1bb7fd60416a17ad2245a 100644 (file)
@@ -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, [])
     }
 
@@ -1542,7 +1546,7 @@ pub fn range<'a>(&'a self, min: Bound<&K>, max: Bound<&K>) -> Range<'a, 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 {
@@ -1551,7 +1555,11 @@ pub fn range<'a>(&'a self, min: Bound<&K>, max: Bound<&K>) -> Range<'a, 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])
     }
index b9cd73470b62b4aa46e066292c0a5b94eb17f1ce..9f2e1db42c938b5c46e4546b1545892928ac7170 100644 (file)
@@ -1528,7 +1528,9 @@ fn search_linear<Q: ?Sized>(&self, key: &Q) -> (usize, bool)
             }
 
             /// 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_|
                 // |   |   |   |   |
@@ -1556,7 +1558,9 @@ pub fn slice_from(self, min_key: &K) -> $NodeSlice<'a, K, V> {
             }
 
             /// 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_|
                 // |   |   |   |   |
index b9430b2d003bbc7e27482412522c8d4bba96fa34..e31147b730830d1d92907ba05e2352403d9d8720 100644 (file)
@@ -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