summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-04-14 21:23:35 -0700
committerbors <bors@rust-lang.org>2016-04-14 21:23:35 -0700
commit4091cd0c5d0aa6e915867ce996f5b547dee2ff31 (patch)
treef6bf5290fc6aceb7b899d81ed1430c829daa1940 /src/libcore
parent76c1a0df2b59c56616bb44f9bee8ad394b391ba8 (diff)
parent62945b6ce3e2b504fdc14341e06d191d64788d72 (diff)
downloadrust-4091cd0c5d0aa6e915867ce996f5b547dee2ff31.tar.gz
rust-4091cd0c5d0aa6e915867ce996f5b547dee2ff31.zip
Auto merge of #32693 - kamalmarhubi:binary_search_by_key, r=alexcrichton
collections: Add slice::binary_search_by_key

This method adds to the family of `_by_key` methods, and is the
counterpart of `slice::sort_by_key`. It was mentioned on #30423 but
was not implemented at that time.

Refs #30423
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/slice.rs13
1 files changed, 13 insertions, 0 deletions
diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs
index 2e91238bff3..68f5a725b74 100644
--- a/src/libcore/slice.rs
+++ b/src/libcore/slice.rs
@@ -157,6 +157,11 @@ pub trait SliceExt {
     fn clone_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Clone;
     #[stable(feature = "copy_from_slice", since = "1.9.0")]
     fn copy_from_slice(&mut self, src: &[Self::Item]) where Self::Item: Copy;
+
+    #[unstable(feature = "slice_binary_search_by_key", reason = "recently added", issue = "0")]
+    fn binary_search_by_key<B, F>(&self, b: &B, f: F) -> Result<usize, usize>
+        where F: FnMut(&Self::Item) -> B,
+              B: Ord;
 }
 
 // Use macros to be generic over const/mut
@@ -507,6 +512,14 @@ impl<T> SliceExt for [T] {
                 src.as_ptr(), self.as_mut_ptr(), self.len());
         }
     }
+
+    #[inline]
+    fn binary_search_by_key<B, F>(&self, b: &B, mut f: F) -> Result<usize, usize>
+        where F: FnMut(&Self::Item) -> B,
+              B: Ord
+    {
+        self.binary_search_by(|k| f(k).cmp(b))
+    }
 }
 
 #[stable(feature = "rust1", since = "1.0.0")]