20#if (MANIFOLD_PAR == 1)
21#include <tbb/combinable.h>
22#include <tbb/parallel_for.h>
23#include <tbb/parallel_invoke.h>
24#include <tbb/parallel_reduce.h>
25#include <tbb/parallel_scan.h>
30#include "manifold/iters.h"
33enum class ExecutionPolicy {
38constexpr size_t kSeqThreshold = 1e4;
42inline constexpr ExecutionPolicy autoPolicy(
size_t size,
43 size_t threshold = kSeqThreshold) {
44 if (size <= threshold) {
45 return ExecutionPolicy::Seq;
47 return ExecutionPolicy::Par;
50template <
typename Iter,
51 typename Dummy = std::enable_if_t<!std::is_integral_v<Iter>>>
52inline constexpr ExecutionPolicy autoPolicy(Iter first, Iter last,
53 size_t threshold = kSeqThreshold) {
54 if (
static_cast<size_t>(std::distance(first, last)) <= threshold) {
55 return ExecutionPolicy::Seq;
57 return ExecutionPolicy::Par;
60template <
typename InputIter,
typename OutputIter>
61void copy(ExecutionPolicy policy, InputIter first, InputIter last,
63template <
typename InputIter,
typename OutputIter>
64void copy(InputIter first, InputIter last, OutputIter d_first);
66#if (MANIFOLD_PAR == 1)
68using manifold::kSeqThreshold;
73template <
typename SrcIter,
typename DestIter,
typename Comp>
74void mergeRec(SrcIter src, DestIter dest,
size_t p1,
size_t r1,
size_t p2,
75 size_t r2,
size_t p3, Comp comp) {
76 size_t length1 = r1 - p1;
77 size_t length2 = r2 - p2;
78 if (length1 < length2) {
81 std::swap(length1, length2);
83 if (length1 == 0)
return;
84 if (length1 + length2 <= kSeqThreshold) {
85 std::merge(src + p1, src + r1, src + p2, src + r2, dest + p3, comp);
87 size_t q1 = p1 + length1 / 2;
89 std::distance(src, std::lower_bound(src + p2, src + r2, src[q1], comp));
90 size_t q3 = p3 + (q1 - p1) + (q2 - p2);
93 [=] { mergeRec(src, dest, p1, q1, p2, q2, p3, comp); },
94 [=] { mergeRec(src, dest, q1 + 1, r1, q2, r2, q3 + 1, comp); });
98template <
typename SrcIter,
typename DestIter,
typename Comp>
99void mergeSortRec(SrcIter src, DestIter dest,
size_t begin,
size_t end,
101 size_t numElements = end - begin;
102 if (numElements <= kSeqThreshold) {
103 std::copy(src + begin, src + end, dest + begin);
104 std::stable_sort(dest + begin, dest + end, comp);
106 size_t middle = begin + numElements / 2;
107 tbb::parallel_invoke([=] { mergeSortRec(dest, src, begin, middle, comp); },
108 [=] { mergeSortRec(dest, src, middle, end, comp); });
109 mergeRec(src, dest, begin, middle, middle, end, begin, comp);
113template <
typename T,
typename InputIter,
typename OutputIter,
typename BinOp>
121 ScanBody(T sum, T identity, BinOp &f, InputIter input, OutputIter output)
122 : sum(sum), identity(identity), f(f), input(input), output(output) {}
123 ScanBody(ScanBody &b, tbb::split)
125 identity(b.identity),
129 template <
typename Tag>
130 void operator()(
const tbb::blocked_range<size_t> &r, Tag) {
132 for (
size_t i = r.begin(); i < r.end(); ++i) {
133 T inputTmp = input[i];
134 if (Tag::is_final_scan()) output[i] = temp;
135 temp = f(temp, inputTmp);
139 T get_sum()
const {
return sum; }
140 void reverse_join(ScanBody &a) { sum = f(a.sum, sum); }
141 void assign(ScanBody &b) { sum = b.sum; }
144template <
typename InputIter,
typename OutputIter,
typename P>
145struct CopyIfScanBody {
151 CopyIfScanBody(P &pred, InputIter input, OutputIter output)
152 : sum(0), pred(pred), input(input), output(output) {}
153 CopyIfScanBody(CopyIfScanBody &b, tbb::split)
154 : sum(0), pred(b.pred), input(b.input), output(b.output) {}
155 template <
typename Tag>
156 void operator()(
const tbb::blocked_range<size_t> &r, Tag) {
158 for (
size_t i = r.begin(); i < r.end(); ++i) {
161 if (Tag::is_final_scan()) output[temp - 1] = input[i];
166 size_t get_sum()
const {
return sum; }
167 void reverse_join(CopyIfScanBody &a) { sum = a.sum + sum; }
168 void assign(CopyIfScanBody &b) { sum = b.sum; }
171template <
typename N, const
int K>
174 static constexpr int k = K;
175 N hist[k][256] = {{0}};
176 void merge(
const Hist<N, K> &other) {
177 for (
int i = 0; i < k; ++i)
178 for (
int j = 0; j < 256; ++j) hist[i][j] += other.hist[i][j];
180 void prefixSum(N total,
bool *canSkip) {
181 for (
int i = 0; i < k; ++i) {
183 for (
int j = 0; j < 256; ++j) {
187 if (tmp == total) canSkip[i] =
true;
193template <
typename T,
typename H>
194void histogram(T *ptr,
typename H::SizeType n, H &hist) {
195 auto worker = [](T *ptr,
typename H::SizeType n, H &hist) {
196 for (
typename H::SizeType i = 0; i < n; ++i)
197 for (
int k = 0; k < hist.k; ++k)
198 ++hist.hist[k][(ptr[i] >> (8 * k)) & 0xFF];
200 if (n < kSeqThreshold) {
201 worker(ptr, n, hist);
203 tbb::combinable<H> store;
205 tbb::blocked_range<typename H::SizeType>(0, n, kSeqThreshold),
206 [&worker, &store, ptr](
const auto &r) {
207 worker(ptr + r.begin(), r.end() - r.begin(), store.local());
209 store.combine_each([&hist](
const H &h) { hist.merge(h); });
213template <
typename T,
typename H>
214void shuffle(T *src, T *target,
typename H::SizeType n, H &hist,
int k) {
215 for (
typename H::SizeType i = 0; i < n; ++i)
216 target[hist.hist[k][(src[i] >> (8 * k)) & 0xFF]++] = src[i];
219template <
typename T,
typename SizeType>
220bool LSB_radix_sort(T *input, T *tmp, SizeType n) {
221 Hist<SizeType,
sizeof(T) /
sizeof(
char)> hist;
222 if (std::is_sorted(input, input + n))
return false;
223 histogram(input, n, hist);
224 bool canSkip[hist.k] = {0};
225 hist.prefixSum(n, canSkip);
226 T *a = input, *b = tmp;
227 for (
int k = 0; k < hist.k; ++k) {
229 shuffle(a, b, n, hist, k);
237template <
typename T,
typename SizeType>
240 SizeType offset = 0, length = 0;
243 SortedRange(T *input, T *tmp, SizeType offset = 0, SizeType length = 0)
244 : input(input), tmp(tmp), offset(offset), length(length) {}
245 SortedRange(SortedRange<T, SizeType> &r, tbb::split)
246 : input(r.input), tmp(r.tmp) {}
247 void operator()(
const tbb::blocked_range<SizeType> &range) {
248 SortedRange<T, SizeType> rhs(input, tmp, range.begin(),
249 range.end() - range.begin());
251 LSB_radix_sort(input + rhs.offset, tmp + rhs.offset, rhs.length);
257 bool swapBuffer()
const {
258 T *src = input, *target = tmp;
259 if (inTmp) std::swap(src, target);
260 copy(src + offset, src + offset + length, target + offset);
263 void join(
const SortedRange<T, SizeType> &rhs) {
264 if (inTmp != rhs.inTmp) {
265 if (length < rhs.length)
266 inTmp = swapBuffer();
270 T *src = input, *target = tmp;
271 if (inTmp) std::swap(src, target);
272 if (src[offset + length - 1] > src[rhs.offset]) {
273 mergeRec(src, target, offset, offset + length, rhs.offset,
274 rhs.offset + rhs.length, offset, std::less<T>());
277 length += rhs.length;
281template <
typename T,
typename SizeTy>
282void radix_sort(T *input, SizeTy n) {
284 SizeTy blockSize = std::max(n / tbb::this_task_arena::max_concurrency() / 4,
285 static_cast<SizeTy
>(kSeqThreshold /
sizeof(T)));
286 SortedRange<T, SizeTy> result(input, aux);
287 tbb::parallel_reduce(tbb::blocked_range<SizeTy>(0, n, blockSize), result);
288 if (result.inTmp) copy(aux, aux + n, input);
292template <
typename Iterator,
293 typename T =
typename std::iterator_traits<Iterator>::value_type,
294 typename Comp =
decltype(std::less<T>())>
295void mergeSort(ExecutionPolicy policy, Iterator first, Iterator last,
297#if (MANIFOLD_PAR == 1)
298 if (policy == ExecutionPolicy::Par) {
300 tbb::this_task_arena::isolate([&] {
301 size_t length = std::distance(first, last);
302 T *tmp =
new T[length];
303 copy(policy, first, last, tmp);
304 details::mergeSortRec(tmp, first, 0, length, comp);
310 std::stable_sort(first, last, comp);
317template <
typename Iterator,
318 typename T =
typename std::iterator_traits<Iterator>::value_type,
319 typename Dummy =
void>
321 void operator()(ExecutionPolicy policy, Iterator first, Iterator last) {
323 std::is_convertible_v<
324 typename std::iterator_traits<Iterator>::iterator_category,
325 std::random_access_iterator_tag>,
326 "You can only parallelize RandomAccessIterator.");
327 static_assert(std::is_trivially_destructible_v<T>,
328 "Our simple implementation does not support types that are "
329 "not trivially destructable.");
330 return mergeSort(policy, first, last, std::less<T>());
336template <
typename Iterator,
typename T>
340 std::is_integral_v<T> &&
341 std::is_pointer_v<typename std::iterator_traits<Iterator>::pointer>>> {
342 void operator()(ExecutionPolicy policy, Iterator first, Iterator last) {
344 std::is_convertible_v<
345 typename std::iterator_traits<Iterator>::iterator_category,
346 std::random_access_iterator_tag>,
347 "You can only parallelize RandomAccessIterator.");
348 static_assert(std::is_trivially_destructible_v<T>,
349 "Our simple implementation does not support types that are "
350 "not trivially destructable.");
351#if (MANIFOLD_PAR == 1)
352 if (policy == ExecutionPolicy::Par) {
353 radix_sort(&*first,
static_cast<size_t>(std::distance(first, last)));
357 stable_sort(policy, first, last, std::less<T>());
366template <
typename Iter,
typename F>
367void for_each(ExecutionPolicy policy, Iter first, Iter last, F f) {
368 static_assert(std::is_convertible_v<
369 typename std::iterator_traits<Iter>::iterator_category,
370 std::random_access_iterator_tag>,
371 "You can only parallelize RandomAccessIterator.");
372#if (MANIFOLD_PAR == 1)
373 if (policy == ExecutionPolicy::Par) {
374 tbb::parallel_for(tbb::blocked_range<Iter>(first, last),
375 [&f](
const tbb::blocked_range<Iter> &range) {
376 for (Iter i = range.begin(); i != range.end(); i++)
382 std::for_each(first, last, f);
386template <
typename Iter,
typename F>
387void for_each_n(ExecutionPolicy policy, Iter first,
size_t n, F f) {
388 static_assert(std::is_convertible_v<
389 typename std::iterator_traits<Iter>::iterator_category,
390 std::random_access_iterator_tag>,
391 "You can only parallelize RandomAccessIterator.");
392 for_each(policy, first, first + n, f);
400template <
typename InputIter,
typename BinaryOp,
401 typename T =
typename std::iterator_traits<InputIter>::value_type>
402T reduce(ExecutionPolicy policy, InputIter first, InputIter last, T init,
404 static_assert(std::is_convertible_v<
405 typename std::iterator_traits<InputIter>::iterator_category,
406 std::random_access_iterator_tag>,
407 "You can only parallelize RandomAccessIterator.");
408#if (MANIFOLD_PAR == 1)
409 if (policy == ExecutionPolicy::Par) {
411 return tbb::parallel_reduce(
412 tbb::blocked_range<InputIter>(first, last, details::kSeqThreshold),
414 [&f](
const tbb::blocked_range<InputIter> &range, T value) {
415 return std::reduce(range.begin(), range.end(), value, f);
420 return std::reduce(first, last, init, f);
428template <
typename InputIter,
typename BinaryOp,
429 typename T =
typename std::iterator_traits<InputIter>::value_type>
430T reduce(InputIter first, InputIter last, T init, BinaryOp f) {
431 return reduce(autoPolicy(first, last, 1e5), first, last, init, f);
440template <
typename InputIter,
typename BinaryOp,
typename UnaryOp,
441 typename T = std::invoke_result_t<
442 UnaryOp,
typename std::iterator_traits<InputIter>::value_type>>
443T transform_reduce(ExecutionPolicy policy, InputIter first, InputIter last,
444 T init, BinaryOp f, UnaryOp g) {
445 return reduce(policy, TransformIterator(first, g), TransformIterator(last, g),
455template <
typename InputIter,
typename BinaryOp,
typename UnaryOp,
456 typename T = std::invoke_result_t<
457 UnaryOp,
typename std::iterator_traits<InputIter>::value_type>>
458T transform_reduce(InputIter first, InputIter last, T init, BinaryOp f,
460 return manifold::reduce(TransformIterator(first, g),
461 TransformIterator(last, g), init, f);
471template <
typename InputIter,
typename OutputIter,
472 typename T =
typename std::iterator_traits<InputIter>::value_type>
473void inclusive_scan(ExecutionPolicy policy, InputIter first, InputIter last,
474 OutputIter d_first) {
475 static_assert(std::is_convertible_v<
476 typename std::iterator_traits<InputIter>::iterator_category,
477 std::random_access_iterator_tag>,
478 "You can only parallelize RandomAccessIterator.");
480 std::is_convertible_v<
481 typename std::iterator_traits<OutputIter>::iterator_category,
482 std::random_access_iterator_tag>,
483 "You can only parallelize RandomAccessIterator.");
484#if (MANIFOLD_PAR == 1)
485 if (policy == ExecutionPolicy::Par) {
487 tbb::blocked_range<size_t>(0, std::distance(first, last)),
489 [&](
const tbb::blocked_range<size_t> &range, T sum,
490 bool is_final_scan) {
492 for (
size_t i = range.begin(); i < range.end(); ++i) {
493 temp = temp + first[i];
494 if (is_final_scan) d_first[i] = temp;
502 std::inclusive_scan(first, last, d_first);
512template <
typename InputIter,
typename OutputIter,
513 typename T =
typename std::iterator_traits<InputIter>::value_type>
514void inclusive_scan(InputIter first, InputIter last, OutputIter d_first) {
515 return inclusive_scan(autoPolicy(first, last, 1e5), first, last, d_first);
530template <
typename InputIter,
typename OutputIter,
531 typename BinOp =
decltype(std::plus<
typename std::iterator_traits<
532 InputIter>::value_type>()),
533 typename T =
typename std::iterator_traits<InputIter>::value_type>
534void exclusive_scan(ExecutionPolicy policy, InputIter first, InputIter last,
535 OutputIter d_first, T init =
static_cast<T
>(0),
536 BinOp f = std::plus<T>(), T identity =
static_cast<T
>(0)) {
537 static_assert(std::is_convertible_v<
538 typename std::iterator_traits<InputIter>::iterator_category,
539 std::random_access_iterator_tag>,
540 "You can only parallelize RandomAccessIterator.");
542 std::is_convertible_v<
543 typename std::iterator_traits<OutputIter>::iterator_category,
544 std::random_access_iterator_tag>,
545 "You can only parallelize RandomAccessIterator.");
546#if (MANIFOLD_PAR == 1)
547 if (policy == ExecutionPolicy::Par) {
548 details::ScanBody<T, InputIter, OutputIter, BinOp> body(init, identity, f,
551 tbb::blocked_range<size_t>(0, std::distance(first, last)), body);
555 std::exclusive_scan(first, last, d_first, init, f);
570template <
typename InputIter,
typename OutputIter,
571 typename BinOp =
decltype(std::plus<
typename std::iterator_traits<
572 InputIter>::value_type>()),
573 typename T =
typename std::iterator_traits<InputIter>::value_type>
574void exclusive_scan(InputIter first, InputIter last, OutputIter d_first,
575 T init =
static_cast<T
>(0), BinOp f = std::plus<T>(),
576 T identity =
static_cast<T
>(0)) {
577 exclusive_scan(autoPolicy(first, last, 1e5), first, last, d_first, init, f,
587template <
typename InputIter,
typename OutputIter,
typename F>
588void transform(ExecutionPolicy policy, InputIter first, InputIter last,
589 OutputIter d_first, F f) {
590 static_assert(std::is_convertible_v<
591 typename std::iterator_traits<InputIter>::iterator_category,
592 std::random_access_iterator_tag>,
593 "You can only parallelize RandomAccessIterator.");
595 std::is_convertible_v<
596 typename std::iterator_traits<OutputIter>::iterator_category,
597 std::random_access_iterator_tag>,
598 "You can only parallelize RandomAccessIterator.");
599#if (MANIFOLD_PAR == 1)
600 if (policy == ExecutionPolicy::Par) {
601 tbb::parallel_for(tbb::blocked_range<size_t>(
602 0,
static_cast<size_t>(std::distance(first, last))),
603 [&](
const tbb::blocked_range<size_t> &range) {
604 std::transform(first + range.begin(),
606 d_first + range.begin(), f);
611 std::transform(first, last, d_first, f);
620template <
typename InputIter,
typename OutputIter,
typename F>
621void transform(InputIter first, InputIter last, OutputIter d_first, F f) {
622 transform(autoPolicy(first, last, 1e5), first, last, d_first, f);
631template <
typename InputIter,
typename OutputIter>
632void copy(ExecutionPolicy policy, InputIter first, InputIter last,
633 OutputIter d_first) {
634 static_assert(std::is_convertible_v<
635 typename std::iterator_traits<InputIter>::iterator_category,
636 std::random_access_iterator_tag>,
637 "You can only parallelize RandomAccessIterator.");
639 std::is_convertible_v<
640 typename std::iterator_traits<OutputIter>::iterator_category,
641 std::random_access_iterator_tag>,
642 "You can only parallelize RandomAccessIterator.");
643#if (MANIFOLD_PAR == 1)
644 if (policy == ExecutionPolicy::Par) {
645 tbb::parallel_for(tbb::blocked_range<size_t>(
646 0,
static_cast<size_t>(std::distance(first, last)),
647 details::kSeqThreshold),
648 [&](
const tbb::blocked_range<size_t> &range) {
649 std::copy(first + range.begin(), first + range.end(),
650 d_first + range.begin());
655 std::copy(first, last, d_first);
664template <
typename InputIter,
typename OutputIter>
665void copy(InputIter first, InputIter last, OutputIter d_first) {
666 copy(autoPolicy(first, last, 1e6), first, last, d_first);
675template <
typename InputIter,
typename OutputIter>
676void copy_n(ExecutionPolicy policy, InputIter first,
size_t n,
677 OutputIter d_first) {
678 copy(policy, first, first + n, d_first);
687template <
typename InputIter,
typename OutputIter>
688void copy_n(InputIter first,
size_t n, OutputIter d_first) {
689 copy(autoPolicy(n, 1e6), first, first + n, d_first);
693template <
typename OutputIter,
typename T>
694void fill(ExecutionPolicy policy, OutputIter first, OutputIter last, T value) {
696 std::is_convertible_v<
697 typename std::iterator_traits<OutputIter>::iterator_category,
698 std::random_access_iterator_tag>,
699 "You can only parallelize RandomAccessIterator.");
700#if (MANIFOLD_PAR == 1)
701 if (policy == ExecutionPolicy::Par) {
702 tbb::parallel_for(tbb::blocked_range<OutputIter>(first, last),
703 [&](
const tbb::blocked_range<OutputIter> &range) {
704 std::fill(range.begin(), range.end(), value);
709 std::fill(first, last, value);
713template <
typename OutputIter,
typename T>
714void fill(OutputIter first, OutputIter last, T value) {
715 fill(autoPolicy(first, last, 5e5), first, last, value);
720template <
typename InputIter,
typename P>
721size_t count_if(ExecutionPolicy policy, InputIter first, InputIter last,
723#if (MANIFOLD_PAR == 1)
724 if (policy == ExecutionPolicy::Par) {
725 return reduce(policy, TransformIterator(first, pred),
726 TransformIterator(last, pred), 0, std::plus<size_t>());
729 return std::count_if(first, last, pred);
734template <
typename InputIter,
typename P>
735size_t count_if(InputIter first, InputIter last, P pred) {
736 return count_if(autoPolicy(first, last, 1e4), first, last, pred);
741template <
typename InputIter,
typename P>
742bool all_of(ExecutionPolicy policy, InputIter first, InputIter last, P pred) {
743 static_assert(std::is_convertible_v<
744 typename std::iterator_traits<InputIter>::iterator_category,
745 std::random_access_iterator_tag>,
746 "You can only parallelize RandomAccessIterator.");
747#if (MANIFOLD_PAR == 1)
748 if (policy == ExecutionPolicy::Par) {
750 return tbb::parallel_reduce(
751 tbb::blocked_range<InputIter>(first, last),
true,
752 [&](
const tbb::blocked_range<InputIter> &range,
bool value) {
753 if (!value)
return false;
754 for (InputIter i = range.begin(); i != range.end(); i++)
755 if (!pred(*i))
return false;
758 [](
bool a,
bool b) {
return a && b; });
761 return std::all_of(first, last, pred);
766template <
typename InputIter,
typename P>
767bool all_of(InputIter first, InputIter last, P pred) {
768 return all_of(autoPolicy(first, last, 1e5), first, last, pred);
782template <
typename InputIter,
typename OutputIter,
typename P>
783OutputIter copy_if(ExecutionPolicy policy, InputIter first, InputIter last,
784 OutputIter d_first, P pred) {
785 static_assert(std::is_convertible_v<
786 typename std::iterator_traits<InputIter>::iterator_category,
787 std::random_access_iterator_tag>,
788 "You can only parallelize RandomAccessIterator.");
790 std::is_convertible_v<
791 typename std::iterator_traits<OutputIter>::iterator_category,
792 std::random_access_iterator_tag>,
793 "You can only parallelize RandomAccessIterator.");
794#if (MANIFOLD_PAR == 1)
795 if (policy == ExecutionPolicy::Par) {
796 auto pred2 = [&](
size_t i) {
return pred(first[i]); };
797 details::CopyIfScanBody body(pred2, first, d_first);
799 tbb::blocked_range<size_t>(0, std::distance(first, last)), body);
800 return d_first + body.get_sum();
803 return std::copy_if(first, last, d_first, pred);
817template <
typename InputIter,
typename OutputIter,
typename P>
818OutputIter copy_if(InputIter first, InputIter last, OutputIter d_first,
820 return copy_if(autoPolicy(first, last, 1e5), first, last, d_first, pred);
831template <
typename Iter,
typename P,
832 typename T =
typename std::iterator_traits<Iter>::value_type>
833Iter remove_if(ExecutionPolicy policy, Iter first, Iter last, P pred) {
834 static_assert(std::is_convertible_v<
835 typename std::iterator_traits<Iter>::iterator_category,
836 std::random_access_iterator_tag>,
837 "You can only parallelize RandomAccessIterator.");
838 static_assert(std::is_trivially_destructible_v<T>,
839 "Our simple implementation does not support types that are "
840 "not trivially destructable.");
841#if (MANIFOLD_PAR == 1)
842 if (policy == ExecutionPolicy::Par) {
843 T *tmp =
new T[std::distance(first, last)];
845 copy_if(policy, first, last, tmp, [&](T v) {
return !pred(v); });
846 copy(policy, tmp, back, first);
847 auto d = std::distance(tmp, back);
852 return std::remove_if(first, last, pred);
864template <
typename Iter,
typename P,
865 typename T =
typename std::iterator_traits<Iter>::value_type>
866Iter remove_if(Iter first, Iter last, P pred) {
867 return remove_if(autoPolicy(first, last, 1e4), first, last, pred);
878template <
typename Iter,
879 typename T =
typename std::iterator_traits<Iter>::value_type>
880Iter remove(ExecutionPolicy policy, Iter first, Iter last, T value) {
881 static_assert(std::is_convertible_v<
882 typename std::iterator_traits<Iter>::iterator_category,
883 std::random_access_iterator_tag>,
884 "You can only parallelize RandomAccessIterator.");
885 static_assert(std::is_trivially_destructible_v<T>,
886 "Our simple implementation does not support types that are "
887 "not trivially destructable.");
888#if (MANIFOLD_PAR == 1)
889 if (policy == ExecutionPolicy::Par) {
890 T *tmp =
new T[std::distance(first, last)];
892 copy_if(policy, first, last, tmp, [&](T v) {
return v != value; });
893 copy(policy, tmp, back, first);
894 auto d = std::distance(tmp, back);
899 return std::remove(first, last, value);
910template <
typename Iter,
911 typename T =
typename std::iterator_traits<Iter>::value_type>
912Iter remove(Iter first, Iter last, T value) {
913 return remove(autoPolicy(first, last, 1e4), first, last, value);
925template <
typename Iter,
926 typename T =
typename std::iterator_traits<Iter>::value_type>
927Iter unique(ExecutionPolicy policy, Iter first, Iter last) {
928 static_assert(std::is_convertible_v<
929 typename std::iterator_traits<Iter>::iterator_category,
930 std::random_access_iterator_tag>,
931 "You can only parallelize RandomAccessIterator.");
932 static_assert(std::is_trivially_destructible_v<T>,
933 "Our simple implementation does not support types that are "
934 "not trivially destructable.");
935#if (MANIFOLD_PAR == 1)
936 if (policy == ExecutionPolicy::Par && first != last) {
937 Iter newSrcStart = first;
940 constexpr size_t MAX_BUFFER_SIZE = 1 << 16;
941 T *tmp =
new T[std::min(MAX_BUFFER_SIZE,
942 static_cast<size_t>(std::distance(first, last)))];
943 auto pred = [&](
size_t i) {
return tmp[i] != tmp[i + 1]; };
946 std::min(MAX_BUFFER_SIZE,
947 static_cast<size_t>(std::distance(newSrcStart, last)));
948 copy(policy, newSrcStart, newSrcStart + length, tmp);
949 *first = *newSrcStart;
952 details::CopyIfScanBody body(pred, tmp + 1, first + 1);
953 tbb::parallel_scan(tbb::blocked_range<size_t>(0, length - 1), body);
954 first += body.get_sum() + 1;
955 newSrcStart += length;
956 }
while (newSrcStart != last);
961 return std::unique(first, last);
973template <
typename Iter,
974 typename T =
typename std::iterator_traits<Iter>::value_type>
975Iter unique(Iter first, Iter last) {
976 return unique(autoPolicy(first, last, 1e4), first, last);
985template <
typename Iterator,
986 typename T =
typename std::iterator_traits<Iterator>::value_type>
987void stable_sort(ExecutionPolicy policy, Iterator first, Iterator last) {
988#if (MANIFOLD_PAR == 1)
989 details::SortFunctor<Iterator, T>()(policy, first, last);
991 std::stable_sort(first, last);
1001template <
typename Iterator,
1002 typename T =
typename std::iterator_traits<Iterator>::value_type>
1003void stable_sort(Iterator first, Iterator last) {
1004 stable_sort(autoPolicy(first, last, 1e4), first, last);
1014template <
typename Iterator,
1015 typename T =
typename std::iterator_traits<Iterator>::value_type,
1016 typename Comp =
decltype(std::less<T>())>
1017void stable_sort(ExecutionPolicy policy, Iterator first, Iterator last,
1019#if (MANIFOLD_PAR == 1)
1020 details::mergeSort(policy, first, last, comp);
1022 std::stable_sort(first, last, comp);
1033template <
typename Iterator,
1034 typename T =
typename std::iterator_traits<Iterator>::value_type,
1035 typename Comp =
decltype(std::less<T>())>
1036void stable_sort(Iterator first, Iterator last, Comp comp) {
1037 stable_sort(autoPolicy(first, last, 1e4), first, last, comp);
1047template <
typename InputIterator1,
typename InputIterator2,
1048 typename OutputIterator>
1049void scatter(ExecutionPolicy policy, InputIterator1 first, InputIterator1 last,
1050 InputIterator2 mapFirst, OutputIterator outputFirst) {
1051 for_each(policy, countAt(0),
1052 countAt(
static_cast<size_t>(std::distance(first, last))),
1053 [first, mapFirst, outputFirst](
size_t i) {
1054 outputFirst[mapFirst[i]] = first[i];
1065template <
typename InputIterator1,
typename InputIterator2,
1066 typename OutputIterator>
1067void scatter(InputIterator1 first, InputIterator1 last, InputIterator2 mapFirst,
1068 OutputIterator outputFirst) {
1069 scatter(autoPolicy(first, last, 1e5), first, last, mapFirst, outputFirst);
1078template <
typename InputIterator,
typename RandomAccessIterator,
1079 typename OutputIterator>
1080void gather(ExecutionPolicy policy, InputIterator mapFirst,
1081 InputIterator mapLast, RandomAccessIterator inputFirst,
1082 OutputIterator outputFirst) {
1083 for_each(policy, countAt(0),
1084 countAt(
static_cast<size_t>(std::distance(mapFirst, mapLast))),
1085 [mapFirst, inputFirst, outputFirst](
size_t i) {
1086 outputFirst[i] = inputFirst[mapFirst[i]];
1096template <
typename InputIterator,
typename RandomAccessIterator,
1097 typename OutputIterator>
1098void gather(InputIterator mapFirst, InputIterator mapLast,
1099 RandomAccessIterator inputFirst, OutputIterator outputFirst) {
1100 gather(autoPolicy(std::distance(mapFirst, mapLast), 1e5), mapFirst, mapLast,
1101 inputFirst, outputFirst);
1105template <
typename Iterator>
1106void sequence(ExecutionPolicy policy, Iterator first, Iterator last) {
1107 for_each(policy, countAt(0),
1108 countAt(
static_cast<size_t>(std::distance(first, last))),
1109 [first](
size_t i) { first[i] = i; });
1113template <
typename Iterator>
1114void sequence(Iterator first, Iterator last) {
1115 sequence(autoPolicy(first, last, 1e5), first, last);