Add Iterator::partition_in_place() and is_partitioned() by cuviper · Pull Request #62278 · rust-lang/rust (original) (raw)
These are inspired by C++ partition and is_partitioned, spurred by this question about mimicking #include .
This requires a
DoubleEndedIteratorso we can search from front and back for items that need swapping.
Implementation note: C++ allows partition with only ForwardIt, but they have to use two mutable iterators (cursors) to implement it, which Rust can't really allow. The best I could imagine is keeping a queue of &mut T we've already seen, eligible for swapping, which would require allocation (non-core) and seems generally distasteful.
C++ does talk about bi-directional modes too, which I imagine would work like what I have here.
inplace_partitionpersonally sounds like a better name to me thanpartition_mut.
I'm open to renaming. I don't think an inplace_ prefix is used anywhere else, but there are examples of ptr::drop_in_place and Alloc::grow_in_place and shrink_in_place.