NumCpp  2.16.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Loading...
Searching...
No Matches
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>
57 {
58 return std::all_of(
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>
78 {
79 return std::any_of(
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>
99 {
100 return std::copy(
102 std::execution::par_unseq,
103#endif
104 first,
105 last,
107 }
108
109 //============================================================================
110 // Method Description:
118 template<class InputIt, class T>
119 typename std::iterator_traits<InputIt>::difference_type
121 {
122 return std::count(
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>
142 {
143 return std::equal(
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>
164 {
165 return std::equal(
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>
185 {
186 return std::fill(
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>
207 {
208 return std::find(
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>
227 {
228 std::for_each(
230 std::execution::par_unseq,
231#endif
232 first,
233 last,
234 f);
235 }
236
237 //============================================================================
238 // Method Description:
245 template<class ForwardIt>
247 {
248 return std::is_sorted(
250 std::execution::par_unseq,
251#endif
252 first,
253 last);
254 }
255
256 //============================================================================
257 // Method Description:
265 template<class ForwardIt, class Compare>
267 {
268 return std::is_sorted(
270 std::execution::par_unseq,
271#endif
272 first,
273 last,
274 comp);
275 }
276
277 //============================================================================
278 // Method Description:
285 template<class ForwardIt>
287 {
288 return std::max_element(
290 std::execution::par_unseq,
291#endif
292 first,
293 last);
294 }
295
296 //============================================================================
297 // Method Description:
305 template<class ForwardIt, class Compare>
307 {
308 return std::max_element(
310 std::execution::par_unseq,
311#endif
312 first,
313 last,
314 comp);
315 }
316
317 //============================================================================
318 // Method Description:
324 template<class ForwardIt>
326 {
327 return std::min_element(
329 std::execution::par_unseq,
330#endif
331 first,
332 last);
333 }
334
335 //============================================================================
336 // Method Description:
344 template<class ForwardIt, class Compare>
346 {
347 return std::min_element(
349 std::execution::par_unseq,
350#endif
351 first,
352 last,
353 comp);
354 }
355
356 //============================================================================
357 // Method Description:
364 template<class ForwardIt>
366 {
367 return std::minmax_element(
369 std::execution::par_unseq,
370#endif
371 first,
372 last);
373 }
374
375 //============================================================================
376 // Method Description:
384 template<class ForwardIt, class Compare>
386 {
387 return std::minmax_element(
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>
407 {
408 return std::none_of(
410 std::execution::par_unseq,
411#endif
412 first,
413 last,
414 p);
415 }
416
417 //============================================================================
418 // Method Description:
425 template<class RandomIt>
427 {
428 std::nth_element(
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>
448 {
449 std::nth_element(
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>
470 {
471 std::replace(
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>
490 {
491 std::reverse(
493 std::execution::par_unseq,
494#endif
495 first,
496 last);
497 }
498
499 //============================================================================
500 // Method Description:
507 template<class ForwardIt>
509 {
510 std::rotate(
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>
532 {
533 return std::set_difference(
535 std::execution::par_unseq,
536#endif
537 first1,
538 last1,
539 first2,
540 last2,
542 }
543
544 //============================================================================
545 // Method Description:
556 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
563 {
564 return std::set_difference(
566 std::execution::par_unseq,
567#endif
568 first1,
569 last1,
570 first2,
571 last2,
573 comp);
574 }
575
576 //============================================================================
577 // Method Description:
587 template<class InputIt1, class InputIt2, class OutputIt>
590 {
591 return std::set_intersection(
593 std::execution::par_unseq,
594#endif
595 first1,
596 last1,
597 first2,
598 last2,
600 }
601
602 //============================================================================
603 // Method Description:
614 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
621 {
622 return std::set_intersection(
624 std::execution::par_unseq,
625#endif
626 first1,
627 last1,
628 first2,
629 last2,
631 comp);
632 }
633
634 //============================================================================
635 // Method Description:
645 template<class InputIt1, class InputIt2, class OutputIt>
648 {
649 return std::set_union(
651 std::execution::par_unseq,
652#endif
653 first1,
654 last1,
655 first2,
656 last2,
658 }
659
660 //============================================================================
661 // Method Description:
672 template<class InputIt1, class InputIt2, class OutputIt, class Compare>
676 {
677 return std::set_union(
679 std::execution::par_unseq,
680#endif
681 first1,
682 last1,
683 first2,
684 last2,
686 comp);
687 }
688
689 //============================================================================
690 // Method Description:
696 template<class RandomIt>
698 {
699 return std::sort(
701 std::execution::par_unseq,
702#endif
703 first,
704 last);
705 }
706
707 //============================================================================
708 // Method Description:
715 template<class RandomIt, class Compare>
717 {
718 return std::sort(
720 std::execution::par_unseq,
721#endif
722 first,
723 last,
724 comp);
725 }
726
727 //============================================================================
728 // Method Description:
734 template<class RandomIt>
736 {
737 std::stable_sort(
739 std::execution::par_unseq,
740#endif
741 first,
742 last);
743 }
744
745 //============================================================================
746 // Method Description:
753 template<class RandomIt, class Compare>
755 {
756 std::stable_sort(
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>
777 {
778 return std::transform(
780 std::execution::par_unseq,
781#endif
782 first,
783 last,
786 }
787
788 //============================================================================
789 // Method Description:
799 template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
802 {
803 return std::transform(
805 std::execution::par_unseq,
806#endif
807 first1,
808 last1,
809 first2,
812 }
813
814 //============================================================================
815 // Method Description:
824 template<class ForwardIt1, class ForwardIt2, class T>
826 {
827 return std::transform_reduce(
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>
850 {
851 return std::transform_reduce(
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>
874 {
875 return std::unique_copy(
877 std::execution::par_unseq,
878#endif
879 first,
880 last,
882 }
883
884 //============================================================================
885 // Method Description:
894 template<class InputIt, class OutputIt, class BinaryPredicate>
897 {
898 return std::unique_copy(
900 std::execution::par_unseq,
901#endif
902 first,
903 last,
906 }
907} // namespace nc::stl_algorithms
#define CONDITIONAL_NO_EXCEPT
Definition StlAlgorithms.hpp:41
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
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) noexcept
Definition StlAlgorithms.hpp:286
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition StlAlgorithms.hpp:735
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 all_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition StlAlgorithms.hpp:56
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
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)
Definition StlAlgorithms.hpp:531
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
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
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition StlAlgorithms.hpp:508
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition StlAlgorithms.hpp:98
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
NdArray< dtype > arange(dtype inStart, dtype inStop, dtype inStep=1)
Definition arange.hpp:59