NumCpp  2.6.2
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 <iterator>
32 #include <numeric>
33 #include <utility>
34 
35 #if defined(__cpp_lib_execution) && 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 
43 namespace nc
44 {
45  namespace stl_algorithms
46  {
47  //============================================================================
48  // Method Description:
56  template<class InputIt, class UnaryPredicate>
57  bool all_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
58  {
59  return std::all_of(
60 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
61  std::execution::par_unseq,
62 #endif
63  first, last, 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, last, p);
83  }
84 
85  //============================================================================
86  // Method Description:
94  template<class InputIt, class OutputIt>
95  OutputIt copy(InputIt first, InputIt last, OutputIt destination) CONDITIONAL_NO_EXCEPT
96  {
97  return std::copy(
98 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
99  std::execution::par_unseq,
100 #endif
101  first, last, destination);
102  }
103 
104  //============================================================================
105  // Method Description:
113  template<class InputIt, class T>
114  typename std::iterator_traits<InputIt>::difference_type
115  count(InputIt first, InputIt last, const T &value) CONDITIONAL_NO_EXCEPT
116  {
117  return std::count(
118 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
119  std::execution::par_unseq,
120 #endif
121  first, last, value);
122 
123  }
124 
125  //============================================================================
126  // Method Description:
134  template<class InputIt1, class InputIt2>
135  bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) CONDITIONAL_NO_EXCEPT
136  {
137  return std::equal(
138 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
139  std::execution::par_unseq,
140 #endif
141  first1, last1, first2);
142  }
143 
144  //============================================================================
145  // Method Description:
154  template<class InputIt1, class InputIt2, class BinaryPredicate>
155  bool equal(InputIt1 first1, InputIt1 last1,
156  InputIt2 first2, BinaryPredicate p) CONDITIONAL_NO_EXCEPT
157  {
158  return std::equal(
159 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
160  std::execution::par_unseq,
161 #endif
162  first1, last1, first2, p);
163  }
164 
165  //============================================================================
166  // Method Description:
173  template<class ForwardIt, class T>
174  void fill(ForwardIt first, ForwardIt last, const T& value) CONDITIONAL_NO_EXCEPT
175  {
176  return std::fill(
177 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
178  std::execution::par_unseq,
179 #endif
180  first, last, value);
181 
182  }
183 
184  //============================================================================
185  // Method Description:
194  template<class InputIt, class T>
195  InputIt find(InputIt first, InputIt last, const T& value) CONDITIONAL_NO_EXCEPT
196  {
197  return std::find(
198 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
199  std::execution::par_unseq,
200 #endif
201  first, last, value);
202  }
203 
204  //============================================================================
205  // Method Description:
212  template<class InputIt, class UnaryFunction>
213  void for_each(InputIt first, InputIt last, UnaryFunction f)
214  {
216 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
217  std::execution::par_unseq,
218 #endif
219  first, last, f);
220  }
221 
222  //============================================================================
223  // Method Description:
230  template<class ForwardIt>
231  bool is_sorted(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
232  {
233  return std::is_sorted(
234 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
235  std::execution::par_unseq,
236 #endif
237  first, last);
238  }
239 
240  //============================================================================
241  // Method Description:
249  template<class ForwardIt, class Compare>
250  bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
251  {
252  return std::is_sorted(
253 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
254  std::execution::par_unseq,
255 #endif
256  first, last, comp);
257  }
258 
259  //============================================================================
260  // Method Description:
267  template<class ForwardIt>
268  ForwardIt max_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
269  {
270  return std::max_element(
271 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
272  std::execution::par_unseq,
273 #endif
274  first, last);
275  }
276 
277  //============================================================================
278  // Method Description:
286  template<class ForwardIt, class Compare>
287  ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
288  {
289  return std::max_element(
290 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
291  std::execution::par_unseq,
292 #endif
293  first, last, comp);
294  }
295 
296  //============================================================================
297  // Method Description:
303  template<class ForwardIt>
304  ForwardIt min_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
305  {
306  return std::min_element(
307 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
308  std::execution::par_unseq,
309 #endif
310  first, last);
311  }
312 
313  //============================================================================
314  // Method Description:
322  template<class ForwardIt, class Compare>
323  ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
324  {
325  return std::min_element(
326 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
327  std::execution::par_unseq,
328 #endif
329  first, last, comp);
330  }
331 
332  //============================================================================
333  // Method Description:
340  template<class ForwardIt>
341  std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
342  {
343  return std::minmax_element(
344 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
345  std::execution::par_unseq,
346 #endif
347  first, last);
348  }
349 
350  //============================================================================
351  // Method Description:
359  template<class ForwardIt, class Compare>
360  std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
361  {
362  return std::minmax_element(
363 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
364  std::execution::par_unseq,
365 #endif
366  first, last, comp);
367  }
368 
369  //============================================================================
370  // Method Description:
378  template<class InputIt, class UnaryPredicate>
379  bool none_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
380  {
381  return std::none_of(
382 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
383  std::execution::par_unseq,
384 #endif
385  first, last, p);
386  }
387 
388  //============================================================================
389  // Method Description:
396  template<class RandomIt>
397  void nth_element(RandomIt first, RandomIt nth, RandomIt last) CONDITIONAL_NO_EXCEPT
398  {
400 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
401  std::execution::par_unseq,
402 #endif
403  first, nth, last);
404  }
405 
406  //============================================================================
407  // Method Description:
415  template<class RandomIt, class Compare>
416  void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
417  {
419 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
420  std::execution::par_unseq,
421 #endif
422  first, nth, last, comp);
423  }
424 
425  //============================================================================
426  // Method Description:
434  template<class ForwardIt, class T>
435  void replace(ForwardIt first, ForwardIt last,
436  const T& oldValue, const T& newValue) CONDITIONAL_NO_EXCEPT
437  {
438  std::replace(
439 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
440  std::execution::par_unseq,
441 #endif
442  first, last, oldValue, newValue);
443  }
444 
445  //============================================================================
446  // Method Description:
452  template<class BidirIt>
453  void reverse(BidirIt first, BidirIt last) CONDITIONAL_NO_EXCEPT
454  {
455  std::reverse(
456 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
457  std::execution::par_unseq,
458 #endif
459  first, last);
460  }
461 
462  //============================================================================
463  // Method Description:
470  template<class ForwardIt>
471  void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) CONDITIONAL_NO_EXCEPT
472  {
473  std::rotate(
474 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
475  std::execution::par_unseq,
476 #endif
477  first, firstN, last);
478  }
479 
480  //============================================================================
481  // Method Description:
491  template<class InputIt1, class InputIt2, class OutputIt>
492  OutputIt set_difference(InputIt1 first1, InputIt1 last1,
493  InputIt2 first2, InputIt2 last2,
494  OutputIt destination)
495  {
496  return std::set_difference(
497 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
498  std::execution::par_unseq,
499 #endif
500  first1, last1, first2, last2, destination);
501  }
502 
503  //============================================================================
504  // Method Description:
515  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
516  OutputIt set_difference(InputIt1 first1, InputIt1 last1,
517  InputIt2 first2, InputIt2 last2,
518  OutputIt destination, Compare comp) CONDITIONAL_NO_EXCEPT
519  {
520  return std::set_difference(
521 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
522  std::execution::par_unseq,
523 #endif
524  first1, last1, first2, last2, destination, comp);
525  }
526 
527  //============================================================================
528  // Method Description:
538  template<class InputIt1, class InputIt2, class OutputIt>
539  OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
540  InputIt2 first2, InputIt2 last2,
541  OutputIt destination) CONDITIONAL_NO_EXCEPT
542  {
543  return std::set_intersection(
544 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
545  std::execution::par_unseq,
546 #endif
547  first1, last1, first2, last2, destination);
548  }
549 
550  //============================================================================
551  // Method Description:
562  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
563  OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
564  InputIt2 first2, InputIt2 last2,
565  OutputIt destination, Compare comp) CONDITIONAL_NO_EXCEPT
566  {
567  return std::set_intersection(
568 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
569  std::execution::par_unseq,
570 #endif
571  first1, last1, first2, last2, destination, comp);
572  }
573 
574  //============================================================================
575  // Method Description:
585  template<class InputIt1, class InputIt2, class OutputIt>
586  OutputIt set_union(InputIt1 first1, InputIt1 last1,
587  InputIt2 first2, InputIt2 last2,
588  OutputIt destination) CONDITIONAL_NO_EXCEPT
589  {
590  return std::set_union(
591 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
592  std::execution::par_unseq,
593 #endif
594  first1, last1, first2, last2, destination);
595  }
596 
597  //============================================================================
598  // Method Description:
609  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
610  OutputIt set_union(InputIt1 first1, InputIt1 last1,
611  InputIt2 first2, InputIt2 last2,
612  OutputIt destination, Compare comp) CONDITIONAL_NO_EXCEPT
613  {
614  return std::set_union(
615 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
616  std::execution::par_unseq,
617 #endif
618  first1, last1, first2, last2, destination, comp);
619  }
620 
621  //============================================================================
622  // Method Description:
628  template<class RandomIt>
629  void sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
630  {
631  return std::sort(
632 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
633  std::execution::par_unseq,
634 #endif
635  first, last);
636  }
637 
638  //============================================================================
639  // Method Description:
646  template<class RandomIt, class Compare>
647  void sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
648  {
649  return std::sort(
650 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
651  std::execution::par_unseq,
652 #endif
653  first, last, comp);
654  }
655 
656  //============================================================================
657  // Method Description:
663  template<class RandomIt>
664  void stable_sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
665  {
667 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
668  std::execution::par_unseq,
669 #endif
670  first, last);
671  }
672 
673  //============================================================================
674  // Method Description:
681  template<class RandomIt, class Compare>
682  void stable_sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
683  {
685 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
686  std::execution::par_unseq,
687 #endif
688  first, last, comp);
689  }
690 
691  //============================================================================
692  // Method Description:
701  template<class InputIt, class OutputIt, class UnaryOperation>
702  OutputIt transform(InputIt first, InputIt last, OutputIt destination,
703  UnaryOperation unaryFunction)
704  {
705  return std::transform(
706 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
707  std::execution::par_unseq,
708 #endif
709  first, last, destination, unaryFunction);
710  }
711 
712  //============================================================================
713  // Method Description:
723  template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
724  OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
725  OutputIt destination, BinaryOperation unaryFunction)
726  {
727  return std::transform(
728 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
729  std::execution::par_unseq,
730 #endif
731  first1, last1, first2, destination, unaryFunction);
732  }
733 
734  //============================================================================
735  // Method Description:
743  template<class InputIt, class OutputIt>
744  constexpr OutputIt unique_copy(InputIt first, InputIt last,
745  OutputIt destination) CONDITIONAL_NO_EXCEPT
746  {
747  return std::unique_copy(
748 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
749  std::execution::par_unseq,
750 #endif
751  first, last, destination);
752  }
753 
754  //============================================================================
755  // Method Description:
764  template<class InputIt, class OutputIt, class BinaryPredicate>
765  constexpr OutputIt unique_copy(InputIt first, InputIt last,
766  OutputIt destination, BinaryPredicate binaryFunction) CONDITIONAL_NO_EXCEPT
767  {
768  return std::unique_copy(
769 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
770  std::execution::par_unseq,
771 #endif
772  first, last, destination, binaryFunction);
773  }
774  } // namespace stl_algorithms
775 } // namespace nc
#define CONDITIONAL_NO_EXCEPT
Definition: StlAlgorithms.hpp:40
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:56
bool any_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:76
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:629
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:250
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:115
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:586
bool none_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:379
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:287
ForwardIt max_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:268
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:610
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:664
void sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:647
void reverse(BidirIt first, BidirIt last) noexcept
Definition: StlAlgorithms.hpp:453
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:702
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:155
bool all_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:57
void for_each(InputIt first, InputIt last, UnaryFunction f)
Definition: StlAlgorithms.hpp:213
InputIt find(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:195
void stable_sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:682
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:744
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:516
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
Definition: StlAlgorithms.hpp:492
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:341
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:416
void replace(ForwardIt first, ForwardIt last, const T &oldValue, const T &newValue) noexcept
Definition: StlAlgorithms.hpp:435
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:539
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:135
bool is_sorted(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:231
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:323
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:471
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:563
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:360
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:95
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination, BinaryPredicate binaryFunction) noexcept
Definition: StlAlgorithms.hpp:765
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt destination, BinaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:724
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:397
ForwardIt min_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:304
void fill(ForwardIt first, ForwardIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:174
Definition: Coordinate.hpp:45