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