NumCpp  2.16.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
StlAlgorithms.hpp
Go to the documentation of this file.
1
28#pragma once
29
30#include <algorithm>
31#include <complex>
32#include <iterator>
33#include <numeric>
34#include <utility>
35
36#if defined(__cpp_lib_parallel_algorithm) && defined(NUMCPP_USE_MULTITHREAD)
37#define PARALLEL_ALGORITHMS_SUPPORTED
38#define CONDITIONAL_NO_EXCEPT
39#include <execution>
40#else
41#define CONDITIONAL_NO_EXCEPT noexcept
42#endif
43
45{
46 //============================================================================
47 // Method Description:
55 template<class InputIt, class UnaryPredicate>
56 bool all_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
57 {
58 return std::all_of(
59#ifdef PARALLEL_ALGORITHMS_SUPPORTED
60 std::execution::par_unseq,
61#endif
62 first,
63 last,
64 p);
65 }
66
67 //============================================================================
68 // Method Description:
76 template<class InputIt, class UnaryPredicate>
77 bool any_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
78 {
79 return std::any_of(
80#ifdef PARALLEL_ALGORITHMS_SUPPORTED
81 std::execution::par_unseq,
82#endif
83 first,
84 last,
85 p);
86 }
87
88 //============================================================================
89 // Method Description:
97 template<class InputIt, class OutputIt>
98 OutputIt copy(InputIt first, InputIt last, OutputIt destination) CONDITIONAL_NO_EXCEPT
99 {
100 return std::copy(
101#ifdef PARALLEL_ALGORITHMS_SUPPORTED
102 std::execution::par_unseq,
103#endif
104 first,
105 last,
106 destination);
107 }
108
109 //============================================================================
110 // Method Description:
118 template<class InputIt, class T>
119 typename std::iterator_traits<InputIt>::difference_type
120 count(InputIt first, InputIt last, const T& value) CONDITIONAL_NO_EXCEPT
121 {
122 return std::count(
123#ifdef PARALLEL_ALGORITHMS_SUPPORTED
124 std::execution::par_unseq,
125#endif
126 first,
127 last,
128 value);
129 }
130
131 //============================================================================
132 // Method Description:
140 template<class InputIt1, class InputIt2>
141 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) CONDITIONAL_NO_EXCEPT
142 {
143 return std::equal(
144#ifdef PARALLEL_ALGORITHMS_SUPPORTED
145 std::execution::par_unseq,
146#endif
147 first1,
148 last1,
149 first2);
150 }
151
152 //============================================================================
153 // Method Description:
162 template<class InputIt1, class InputIt2, class BinaryPredicate>
163 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) CONDITIONAL_NO_EXCEPT
164 {
165 return std::equal(
166#ifdef PARALLEL_ALGORITHMS_SUPPORTED
167 std::execution::par_unseq,
168#endif
169 first1,
170 last1,
171 first2,
172 p);
173 }
174
175 //============================================================================
176 // Method Description:
183 template<class ForwardIt, class T>
184 void fill(ForwardIt first, ForwardIt last, const T& value) CONDITIONAL_NO_EXCEPT
185 {
186 return std::fill(
187#ifdef PARALLEL_ALGORITHMS_SUPPORTED
188 std::execution::par_unseq,
189#endif
190 first,
191 last,
192 value);
193 }
194
195 //============================================================================
196 // Method Description:
205 template<class InputIt, class T>
206 InputIt find(InputIt first, InputIt last, const T& value) CONDITIONAL_NO_EXCEPT
207 {
208 return std::find(
209#ifdef PARALLEL_ALGORITHMS_SUPPORTED
210 std::execution::par_unseq,
211#endif
212 first,
213 last,
214 value);
215 }
216
217 //============================================================================
218 // Method Description:
225 template<class InputIt, class UnaryFunction>
226 void for_each(InputIt first, InputIt last, UnaryFunction f)
227 {
229#ifdef PARALLEL_ALGORITHMS_SUPPORTED
230 std::execution::par_unseq,
231#endif
232 first,
233 last,
234 f);
235 }
236
237 //============================================================================
238 // Method Description:
245 template<class ForwardIt>
246 bool is_sorted(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
247 {
248 return std::is_sorted(
249#ifdef PARALLEL_ALGORITHMS_SUPPORTED
250 std::execution::par_unseq,
251#endif
252 first,
253 last);
254 }
255
256 //============================================================================
257 // Method Description:
265 template<class ForwardIt, class Compare>
266 bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
267 {
268 return std::is_sorted(
269#ifdef PARALLEL_ALGORITHMS_SUPPORTED
270 std::execution::par_unseq,
271#endif
272 first,
273 last,
274 comp);
275 }
276
277 //============================================================================
278 // Method Description:
285 template<class ForwardIt>
286 ForwardIt max_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
287 {
288 return std::max_element(
289#ifdef PARALLEL_ALGORITHMS_SUPPORTED
290 std::execution::par_unseq,
291#endif
292 first,
293 last);
294 }
295
296 //============================================================================
297 // Method Description:
305 template<class ForwardIt, class Compare>
306 ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
307 {
308 return std::max_element(
309#ifdef PARALLEL_ALGORITHMS_SUPPORTED
310 std::execution::par_unseq,
311#endif
312 first,
313 last,
314 comp);
315 }
316
317 //============================================================================
318 // Method Description:
324 template<class ForwardIt>
325 ForwardIt min_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
326 {
327 return std::min_element(
328#ifdef PARALLEL_ALGORITHMS_SUPPORTED
329 std::execution::par_unseq,
330#endif
331 first,
332 last);
333 }
334
335 //============================================================================
336 // Method Description:
344 template<class ForwardIt, class Compare>
345 ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
346 {
347 return std::min_element(
348#ifdef PARALLEL_ALGORITHMS_SUPPORTED
349 std::execution::par_unseq,
350#endif
351 first,
352 last,
353 comp);
354 }
355
356 //============================================================================
357 // Method Description:
364 template<class ForwardIt>
365 std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
366 {
367 return std::minmax_element(
368#ifdef PARALLEL_ALGORITHMS_SUPPORTED
369 std::execution::par_unseq,
370#endif
371 first,
372 last);
373 }
374
375 //============================================================================
376 // Method Description:
384 template<class ForwardIt, class Compare>
385 std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
386 {
387 return std::minmax_element(
388#ifdef PARALLEL_ALGORITHMS_SUPPORTED
389 std::execution::par_unseq,
390#endif
391 first,
392 last,
393 comp);
394 }
395
396 //============================================================================
397 // Method Description:
405 template<class InputIt, class UnaryPredicate>
406 bool none_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
407 {
408 return std::none_of(
409#ifdef PARALLEL_ALGORITHMS_SUPPORTED
410 std::execution::par_unseq,
411#endif
412 first,
413 last,
414 p);
415 }
416
417 //============================================================================
418 // Method Description:
425 template<class RandomIt>
426 void nth_element(RandomIt first, RandomIt nth, RandomIt last) CONDITIONAL_NO_EXCEPT
427 {
429#ifdef PARALLEL_ALGORITHMS_SUPPORTED
430 std::execution::par_unseq,
431#endif
432 first,
433 nth,
434 last);
435 }
436
437 //============================================================================
438 // Method Description:
446 template<class RandomIt, class Compare>
447 void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
448 {
450#ifdef PARALLEL_ALGORITHMS_SUPPORTED
451 std::execution::par_unseq,
452#endif
453 first,
454 nth,
455 last,
456 comp);
457 }
458
459 //============================================================================
460 // Method Description:
468 template<class ForwardIt, class T>
469 void replace(ForwardIt first, ForwardIt last, const T& oldValue, const T& newValue) CONDITIONAL_NO_EXCEPT
470 {
472#ifdef PARALLEL_ALGORITHMS_SUPPORTED
473 std::execution::par_unseq,
474#endif
475 first,
476 last,
477 oldValue,
478 newValue);
479 }
480
481 //============================================================================
482 // Method Description:
488 template<class BidirIt>
489 void reverse(BidirIt first, BidirIt last) CONDITIONAL_NO_EXCEPT
490 {
492#ifdef PARALLEL_ALGORITHMS_SUPPORTED
493 std::execution::par_unseq,
494#endif
495 first,
496 last);
497 }
498
499 //============================================================================
500 // Method Description:
507 template<class ForwardIt>
508 void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) CONDITIONAL_NO_EXCEPT
509 {
511#ifdef PARALLEL_ALGORITHMS_SUPPORTED
512 std::execution::par_unseq,
513#endif
514 first,
515 firstN,
516 last);
517 }
518
519 //============================================================================
520 // Method Description:
530 template<class InputIt1, class InputIt2, class OutputIt>
531 OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
532 {
533 return std::set_difference(
534#ifdef PARALLEL_ALGORITHMS_SUPPORTED
535 std::execution::par_unseq,
536#endif
537 first1,
538 last1,
539 first2,
540 last2,
541 destination);
542 }
543
544 //============================================================================
545 // Method Description:
556 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
557 OutputIt set_difference(InputIt1 first1,
558 InputIt1 last1,
559 InputIt2 first2,
560 InputIt2 last2,
561 OutputIt destination,
562 Compare comp) CONDITIONAL_NO_EXCEPT
563 {
564 return std::set_difference(
565#ifdef PARALLEL_ALGORITHMS_SUPPORTED
566 std::execution::par_unseq,
567#endif
568 first1,
569 last1,
570 first2,
571 last2,
572 destination,
573 comp);
574 }
575
576 //============================================================================
577 // Method Description:
587 template<class InputIt1, class InputIt2, class OutputIt>
588 OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
590 {
592#ifdef PARALLEL_ALGORITHMS_SUPPORTED
593 std::execution::par_unseq,
594#endif
595 first1,
596 last1,
597 first2,
598 last2,
599 destination);
600 }
601
602 //============================================================================
603 // Method Description:
614 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
615 OutputIt set_intersection(InputIt1 first1,
616 InputIt1 last1,
617 InputIt2 first2,
618 InputIt2 last2,
619 OutputIt destination,
620 Compare comp) CONDITIONAL_NO_EXCEPT
621 {
623#ifdef PARALLEL_ALGORITHMS_SUPPORTED
624 std::execution::par_unseq,
625#endif
626 first1,
627 last1,
628 first2,
629 last2,
630 destination,
631 comp);
632 }
633
634 //============================================================================
635 // Method Description:
645 template<class InputIt1, class InputIt2, class OutputIt>
646 OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
648 {
649 return std::set_union(
650#ifdef PARALLEL_ALGORITHMS_SUPPORTED
651 std::execution::par_unseq,
652#endif
653 first1,
654 last1,
655 first2,
656 last2,
657 destination);
658 }
659
660 //============================================================================
661 // Method Description:
672 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
673 OutputIt
674 set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp)
676 {
677 return std::set_union(
678#ifdef PARALLEL_ALGORITHMS_SUPPORTED
679 std::execution::par_unseq,
680#endif
681 first1,
682 last1,
683 first2,
684 last2,
685 destination,
686 comp);
687 }
688
689 //============================================================================
690 // Method Description:
696 template<class RandomIt>
697 void sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
698 {
699 return std::sort(
700#ifdef PARALLEL_ALGORITHMS_SUPPORTED
701 std::execution::par_unseq,
702#endif
703 first,
704 last);
705 }
706
707 //============================================================================
708 // Method Description:
715 template<class RandomIt, class Compare>
716 void sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
717 {
718 return std::sort(
719#ifdef PARALLEL_ALGORITHMS_SUPPORTED
720 std::execution::par_unseq,
721#endif
722 first,
723 last,
724 comp);
725 }
726
727 //============================================================================
728 // Method Description:
734 template<class RandomIt>
735 void stable_sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
736 {
738#ifdef PARALLEL_ALGORITHMS_SUPPORTED
739 std::execution::par_unseq,
740#endif
741 first,
742 last);
743 }
744
745 //============================================================================
746 // Method Description:
753 template<class RandomIt, class Compare>
754 void stable_sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
755 {
757#ifdef PARALLEL_ALGORITHMS_SUPPORTED
758 std::execution::par_unseq,
759#endif
760 first,
761 last,
762 comp);
763 }
764
765 //============================================================================
766 // Method Description:
775 template<class InputIt, class OutputIt, class UnaryOperation>
776 OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
777 {
778 return std::transform(
779#ifdef PARALLEL_ALGORITHMS_SUPPORTED
780 std::execution::par_unseq,
781#endif
782 first,
783 last,
784 destination,
785 unaryFunction);
786 }
787
788 //============================================================================
789 // Method Description:
799 template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
800 OutputIt
801 transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt destination, BinaryOperation unaryFunction)
802 {
803 return std::transform(
804#ifdef PARALLEL_ALGORITHMS_SUPPORTED
805 std::execution::par_unseq,
806#endif
807 first1,
808 last1,
809 first2,
810 destination,
811 unaryFunction);
812 }
813
814 //============================================================================
815 // Method Description:
824 template<class ForwardIt1, class ForwardIt2, class T>
825 T transform_reduce(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, T init)
826 {
828#ifdef PARALLEL_ALGORITHMS_SUPPORTED
829 std::execution::par_unseq,
830#endif
831 first1,
832 last1,
833 first2,
834 init);
835 }
836
837 //============================================================================
838 // Method Description:
847 template<class ForwardIt1, class ForwardIt2, class T>
848 std::complex<T>
849 transform_reduce(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const std::complex<T>& init)
850 {
852#ifdef PARALLEL_ALGORITHMS_SUPPORTED
853 std::execution::par_unseq,
854#endif
855 first1,
856 last1,
857 first2,
858 init,
859 std::plus<std::complex<T>>(),
860 [](const auto a, const auto& b) { return std::complex<T>(a * b); });
861 }
862
863 //============================================================================
864 // Method Description:
872 template<class InputIt, class OutputIt>
873 constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) CONDITIONAL_NO_EXCEPT
874 {
875 return std::unique_copy(
876#ifdef PARALLEL_ALGORITHMS_SUPPORTED
877 std::execution::par_unseq,
878#endif
879 first,
880 last,
881 destination);
882 }
883
884 //============================================================================
885 // Method Description:
894 template<class InputIt, class OutputIt, class BinaryPredicate>
895 constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination, BinaryPredicate binaryFunction)
897 {
898 return std::unique_copy(
899#ifdef PARALLEL_ALGORITHMS_SUPPORTED
900 std::execution::par_unseq,
901#endif
902 first,
903 last,
904 destination,
905 binaryFunction);
906 }
907} // namespace nc::stl_algorithms
#define CONDITIONAL_NO_EXCEPT
Definition: StlAlgorithms.hpp:41
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: StlAlgorithms.hpp:45
bool any_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:77
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:120
T transform_reduce(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, T init)
Definition: StlAlgorithms.hpp:825
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:697
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:266
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:646
bool none_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:406
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:306
ForwardIt max_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:286
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:674
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:735
void sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:716
void reverse(BidirIt first, BidirIt last) noexcept
Definition: StlAlgorithms.hpp:489
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:776
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:163
bool all_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:56
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:385
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:226
InputIt find(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:206
void stable_sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:754
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:873
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:557
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
Definition: StlAlgorithms.hpp:531
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:447
void replace(ForwardIt first, ForwardIt last, const T &oldValue, const T &newValue) noexcept
Definition: StlAlgorithms.hpp:469
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:588
std::complex< T > transform_reduce(ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, const std::complex< T > &init)
Definition: StlAlgorithms.hpp:849
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:141
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:365
bool is_sorted(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:246
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:345
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:508
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:615
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:98
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination, BinaryPredicate binaryFunction) noexcept
Definition: StlAlgorithms.hpp:895
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt destination, BinaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:801
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:426
ForwardIt min_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:325
void fill(ForwardIt first, ForwardIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:184