Skip to content

support find_first, find_last, position_first, and position_last #151

@nikomatsakis

Description

@nikomatsakis

We currently have find_any and position_any. These are similar to the find and position methods on Iterator but they don't always find the first match. It'd be nice to support compatible versions. I would want to give them different names so as to discourage people from using them, since find_any will be more efficient.

Implementing this is similar to the existing find_any code but a bit trickier. The way that find_any works is that it makes a consumer with a full() method. full() returns true if execution should stop. We use a single shared atomic and, when a matching item is found, we set this flag to true, signaling others to halt.

To make this work for find_first, we need to halt not when any solution is found, but if a solution has been found in the sections to your left. The easiest way I can think to do this is to have each call to split_off, which breaks the consumer into two pieces, allocate an Arc<AtomicBool> (or something) for those two pieces to share. The left half will signal if it finds something by writing true; the right half will stop when either it finds something, or it sees that true has been written. The reduce method will then act just like it does now, preferring a value from the left half over the right half.

If you've not hacked on Rayon before, also check out the guide to development.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions