Skip to content
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::is_partitioned #62544

Open
2 tasks
cuviper opened this issue Jul 9, 2019 · 4 comments
Open
2 tasks

Tracking issue for Iterator::is_partitioned #62544

cuviper opened this issue Jul 9, 2019 · 4 comments

Comments

@cuviper
Copy link
Member

@cuviper cuviper commented Jul 9, 2019

/// Checks if the elements of this iterator are partitioned according to the given predicate,
/// such that all those that return `true` precede all those that return `false`.
fn is_partitioned<P>(mut self, mut predicate: P) -> bool
where
    Self: Sized,
    P: FnMut(Self::Item) -> bool,

feature = "iter_is_partitioned"

ref: #62278

Unresolved questions

  • Do we want this function at all? (Motivating use cases would be useful.)
  • Would is_sorted_by_key already cover all use cases?
@TheWastl
Copy link
Contributor

@TheWastl TheWastl commented Dec 15, 2020

Is there anything blocking this from stabiilization?

Loading

@cuviper
Copy link
Member Author

@cuviper cuviper commented Dec 16, 2020

No blocker that I know of. There's some question about the related partition_in_place, but is_partitioned is straightforward.

Loading

@m-ou-se
Copy link
Member

@m-ou-se m-ou-se commented Dec 20, 2020

Comments from the stabilization PR:

I've looked at the various linked issues but didn't see any motivating use cases for this routine.

Thinking a bit more about this, I suppose that pretty much all use cases of this function are also covered by is_sorted_by_key.

I've added this as an unresolved question above.

Loading

@jayaddison
Copy link
Contributor

@jayaddison jayaddison commented Sep 21, 2021

(arrived here thanks to an initially unrelated curiosity re: iterator::partition (specifically, that it only provides binary partitions))

About use cases: one could be to aid (perhaps semi-automated) porting of existing C++ code that uses stdlib partition (which maps to partition_in_place in Rust, as I understand it?) in combination with stdlib is_partitioned.

I'm a very small Rustacean so I don't know if I understand correctly, but it does seem like all use cases for is_partitioned could theoretically be translated to an implementation in terms of is_sorted_by_key -- but that doing that could require careful sort order / partition function handling, and perhaps more care than automated porting tools could achieve easily / performantly. Basically I think they'd tend to rewrite it as a a check for is_sorted_by ( negation ( partition_func ) ) (because the left-side partition is the true values).

Current implementation notes: is_sorted_by_key performs a (lazy) map across the iterator whereas the current is_partitioned implementation short-circuits by applying all until failure, and then any for remaining elements. Those seem to both have O(n) worst-case?

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants