-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Tracking issue for Iterator::partition_in_place #62543
Comments
What's blocking this from stabilization? |
There's no blocker as far as I know. It would be nice to see some real use for experience, e.g. in the compiler or standard library itself, but I don't think that's required. |
I've rewritten exactly this function before to avoid depending on nightly features. Is there any way we can put this up for stabilization? |
There's a documented process for stabilizing language features, but I'm not aware of anything special for libraries. AFAIK, you can just open a pull request with the change, and the libs team will put the proposal through FCP. For example, #71886 stabilized a couple numeric methods. |
Maybe a stupid question, but why is the method in the |
It's a reasonable question. There's a little prior art in I guess the current But that's pretty loose justification, and I think it would also be reasonable to move it. 🤷 |
Naming thought: should this be |
IMO, "unstable" is only useful if we think there might be a stable variant, but you would need a side buffer of displaced items to manage that. The documentation does say that relative order is not maintained. |
Well, |
True, |
@cuviper Would it be possible to also offer an |
@sanmai-NL partitioning is a binary decision on each individual item, which we can do in a linear sweep. Asking for |
@cuviper I agree, but I had another application in mind. I was using it to implement Quicksort, and would like be able to control the sorting order through the predicate function. Since it got hairy with all the lifetimes in combination with recursion and mutable references in to the (sub)arrays, I was wondering whether I could use the natural ordering or some But otherwise, how would I pass the predicate from the caller for use here? It's probably easy, but the docs/signature wasn't helpful enough for me. |
@sanmai-NL So right now you have this, which already uses .partition_in_place(|&element| element < pivot) If you have a custom parameter .partition_in_place(|&element| matches!(cmp(element, pivot), Ordering::Less)) Or you might have a parameter .partition_in_place(|&element| lt(element, pivot)) |
Wouldn't returning a pair of iterators be better? ATM you return an index in reference to the where the partition happened, then you likely have to use that index to constuct iterators again. let mut a = [1, 2, 3, 4, 5, 6, 7];
let offset = 2;
let i = a[offset..].iter_mut().partition_in_place(|&n| n % 2 == 0);
assert!(a[offset..i+offset].iter().all(|&n| n % 2 == 0)); // evens
assert!(a[i+offset..].iter().all(|&n| n % 2 == 1)); // odds vs let mut a = [1, 2, 3, 4, 5, 6, 7];
let offset = 2;
let (bottom, top) = a[offset..].iter_mut().partition_in_place(|&n| n % 2 == 0);
assert!(bottom.all(|&n| n % 2 == 0)); // evens
assert!(top.all(|&n| n % 2 == 1)); // odds (forgive me for any mistakes, coming from C++, learning Rust ATM) |
@Ignition that's not possible with an arbitrary For your C++ background, it may help to think of C++ iterators as cursors, whereas Rust iterators are more like ranges that shrink each time an item is retrieved. |
Status update: The stabilization PR was not merged. See #76505 (comment) and #76505 (comment) for the relevant feedback. |
I commented on #54279 about the weirdness of not finding this as an inherent method on slice; while after seeing the algorithm explained in the docs, it makes sense to me why this doesn't have to be a slice-specific method, it's not at all obvious before that explanation. I understand that there are theoretical data structures that would not be represented internally as slices for which this could be implemented, but would said data structures expose a mutable iterator? Like, applying this to a binary tree would destroy the interior structure of the tree, for example, and it seems like this would only be done if you're destroying the structure and converting it to something else anyway. Like, it seems like this would be useful as a slice method simply for the ability to combine it with a |
|
...right, I dunno why I forgot the obvious ones. Still, beyond those, I wonder whether it's that worth it to expose on |
I think this function is misguided on |
I'd like to see this applicable to iterators and not only on specific data structure. There are many data structures out there that would exposed a mutable iterator but cannot be a slice. I'm thinking of scientific libraries: a strided iterator on a matrix, a chunked array in Apache Arrow / Polars, ... |
feature = "iter_partition_in_place"
ref: #62278
The text was updated successfully, but these errors were encountered: