Approach 2: Split the Algorithm Into Two Parts

Learn how splitting the algorithm into two parts can improve performance by copying elements conditionally in parallel and then merging them sequentially into a continuous range.

The second approach is to split the algorithm into two parts. First, the conditional copying is performed in parallel chunks, and then the resulting sparse range is squeezed to a continuous range.

Part one: Copy elements in parallel into the destination range

The first part copies the elements in chunks, resulting in the sparse destination array illustrated below. Each chunk is conditionally copied in parallel, and the resulting range iterators are stored in std::future objects for later retrieval:

Get hands-on with 1200+ tech skills courses.