-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add partition_point #73577
Add partition_point #73577
Changes from all commits
4c8ce48
c9b4915
27b06f1
8cc6998
52f9762
9335787
83d5998
d720a19
60f2ba2
6f8ad3b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2663,6 +2663,60 @@ impl<T> [T] { | |
{ | ||
self.iter().is_sorted_by_key(f) | ||
} | ||
|
||
/// Returns the index of the partition point according to the given predicate | ||
/// (the index of the first element of the second partition). | ||
/// | ||
/// The slice is assumed to be partitioned according to the given predicate. | ||
/// This means that all elements for which the predicate returns true are at the start of the slice | ||
/// and all elements for which the predicate returns false are at the end. | ||
/// For example, [7, 15, 3, 5, 4, 12, 6] is a partitioned under the predicate x % 2 != 0 | ||
/// (all odd numbers are at the start, all even at the end). | ||
/// | ||
/// If this slice is not partitioned, the returned result is unspecified and meaningless, | ||
/// as this method performs a kind of binary search. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// #![feature(partition_point)] | ||
/// | ||
/// let v = [1, 2, 3, 3, 5, 6, 7]; | ||
/// let i = v.partition_point(|&x| x < 5); | ||
/// | ||
/// assert_eq!(i, 4); | ||
/// assert!(v[..i].iter().all(|&x| x < 5)); | ||
/// assert!(v[i..].iter().all(|&x| !(x < 5))); | ||
/// ``` | ||
#[unstable(feature = "partition_point", reason = "new API", issue = "73831")] | ||
pub fn partition_point<P>(&self, mut pred: P) -> usize | ||
where | ||
P: FnMut(&T) -> bool, | ||
{ | ||
let mut left = 0; | ||
let mut right = self.len(); | ||
|
||
while left != right { | ||
let mid = left + (right - left) / 2; | ||
// SAFETY: | ||
// When left < right, left <= mid < right. | ||
// Therefore left always increases and right always decreases, | ||
// and eigher of them is selected. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops thank you. |
||
// In both cases left <= right is satisfied. | ||
// Therefore if left < right in a step, | ||
// left <= right is satisfied in the next step. | ||
// Therefore as long as left != right, 0 <= left < right <= len is satisfied | ||
// and if this case 0 <= mid < len is satisfied too. | ||
let value = unsafe { self.get_unchecked(mid) }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment about why this |
||
if pred(value) { | ||
left = mid + 1; | ||
} else { | ||
right = mid; | ||
} | ||
} | ||
|
||
left | ||
} | ||
} | ||
|
||
#[lang = "slice_u8"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd better change to "If this slice is not sorted ..." as we actually want to tell the end-user an ordered slice is required for this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is right -- the slice has to be partitioned, and not ordered. Raison d'être of
partition_point
is that it works with non-Ord
things. To make this more precise, we might link to https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.is_partitioned.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed in 95% cases
partition_point
is used to getlower/upper_bound
. However in 5% cases it looks more partition point than lower bound.For example, we can get all valid values from
[3, 1, 4, 1, 5, -1, -1, -1, -1, -1]
.Actually this array is sorted by
|&x| x != -1
in descending order (true is larger than false), but many user would think that the array is partitioned at index 5 by whether -1 or not.What do you think about adding like this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the kind explanation and sorry for my misunderstanding. Yes, the slice requires partitioned instead of sorted.
However, I still confused by the comment
If this slice is not partitioned, the returned result is unspecified and meaningless
. If we did call this method on a not partitioned slice, the result should be anOption<size>
rather than an unspecified or meaningless value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Binary search runs in O(log n) by assuming ordered. If it checks whether is sorted, it must slower than O(n).