NumCpp  1.0
A C++ implementation of the Python Numpy library
StlAlgorithms.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include <algorithm>
32 #include <iterator>
33 #include <numeric>
34 #include <utility>
35 
36 #if defined(__cpp_lib_execution) && defined(__cpp_lib_parallel_algorithm)
37 #define PARALLEL_ALGORITHMS_SUPPORTED
38 #define NO_EXCEPT
39 #include <execution>
40 #else
41 #define NO_EXCEPT noexcept
42 #endif
43 
44 namespace nc
45 {
46  namespace stl_algorithms
47  {
48  //============================================================================
49  // Method Description:
57  template<class InputIt, class UnaryPredicate>
58  bool all_of(InputIt first, InputIt last, UnaryPredicate p) NO_EXCEPT
59  {
60  return std::all_of(
61 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
62  std::execution::par_unseq,
63 #endif
64  first, last, p);
65  }
66 
67  //============================================================================
68  // Method Description:
76  template<class InputIt, class UnaryPredicate>
77  bool any_of(InputIt first, InputIt last, UnaryPredicate p) NO_EXCEPT
78  {
79  return std::any_of(
80 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
81  std::execution::par_unseq,
82 #endif
83  first, last, p);
84  }
85 
86  //============================================================================
87  // Method Description:
95  template<class InputIt, class OutputIt>
96  OutputIt copy(InputIt first, InputIt last, OutputIt destination) NO_EXCEPT
97  {
98  return std::copy(
99 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
100  std::execution::par_unseq,
101 #endif
102  first, last, destination);
103  }
104 
105  //============================================================================
106  // Method Description:
114  template<class InputIt, class T>
115  typename std::iterator_traits<InputIt>::difference_type
116  count(InputIt first, InputIt last, const T &value) NO_EXCEPT
117  {
118  return std::count(
119 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
120  std::execution::par_unseq,
121 #endif
122  first, last, value);
123 
124  }
125 
126  //============================================================================
127  // Method Description:
135  template<class InputIt1, class InputIt2>
136  bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) NO_EXCEPT
137  {
138  return std::equal(
139 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
140  std::execution::par_unseq,
141 #endif
142  first1, last1, first2);
143  }
144 
145  //============================================================================
146  // Method Description:
155  template<class InputIt1, class InputIt2, class BinaryPredicate>
156  bool equal(InputIt1 first1, InputIt1 last1,
157  InputIt2 first2, BinaryPredicate p) NO_EXCEPT
158  {
159  return std::equal(
160 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
161  std::execution::par_unseq,
162 #endif
163  first1, last1, first2, p);
164  }
165 
166  //============================================================================
167  // Method Description:
174  template<class ForwardIt, class T>
175  void fill(ForwardIt first, ForwardIt last, const T& value) NO_EXCEPT
176  {
177  return std::fill(
178 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
179  std::execution::par_unseq,
180 #endif
181  first, last, value);
182 
183  }
184 
185  //============================================================================
186  // Method Description:
195  template<class InputIt, class T>
196  InputIt find(InputIt first, InputIt last, const T& value) NO_EXCEPT
197  {
198  return std::find(
199 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
200  std::execution::par_unseq,
201 #endif
202  first, last, value);
203  }
204 
205  //============================================================================
206  // Method Description:
213  template<class InputIt, class UnaryFunction>
214  void for_each(InputIt first, InputIt last, UnaryFunction f) NO_EXCEPT
215  {
217 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
218  std::execution::par_unseq,
219 #endif
220  first, last, f);
221  }
222 
223  //============================================================================
224  // Method Description:
231  template<class ForwardIt>
232  bool is_sorted(ForwardIt first, ForwardIt last) NO_EXCEPT
233  {
234  return std::is_sorted(
235 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
236  std::execution::par_unseq,
237 #endif
238  first, last);
239  }
240 
241  //============================================================================
242  // Method Description:
250  template<class ForwardIt, class Compare>
251  bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) NO_EXCEPT
252  {
253  return std::is_sorted(
254 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
255  std::execution::par_unseq,
256 #endif
257  first, last, comp);
258  }
259 
260  //============================================================================
261  // Method Description:
268  template<class ForwardIt>
269  ForwardIt max_element(ForwardIt first, ForwardIt last) NO_EXCEPT
270  {
271  return std::max_element(
272 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
273  std::execution::par_unseq,
274 #endif
275  first, last);
276  }
277 
278  //============================================================================
279  // Method Description:
287  template<class ForwardIt, class Compare>
288  ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) NO_EXCEPT
289  {
290  return std::max_element(
291 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
292  std::execution::par_unseq,
293 #endif
294  first, last, comp);
295  }
296 
297  //============================================================================
298  // Method Description:
304  template<class ForwardIt>
305  ForwardIt min_element(ForwardIt first, ForwardIt last) NO_EXCEPT
306  {
307  return std::min_element(
308 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
309  std::execution::par_unseq,
310 #endif
311  first, last);
312  }
313 
314  //============================================================================
315  // Method Description:
323  template<class ForwardIt, class Compare>
324  ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) NO_EXCEPT
325  {
326  return std::min_element(
327 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
328  std::execution::par_unseq,
329 #endif
330  first, last, comp);
331  }
332 
333  //============================================================================
334  // Method Description:
341  template<class ForwardIt>
342  std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) NO_EXCEPT
343  {
344  return std::minmax_element(
345 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
346  std::execution::par_unseq,
347 #endif
348  first, last);
349  }
350 
351  //============================================================================
352  // Method Description:
360  template<class ForwardIt, class Compare>
361  std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last, Compare comp) NO_EXCEPT
362  {
363  return std::minmax_element(
364 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
365  std::execution::par_unseq,
366 #endif
367  first, last, comp);
368  }
369 
370  //============================================================================
371  // Method Description:
379  template<class InputIt, class UnaryPredicate>
380  bool none_of(InputIt first, InputIt last, UnaryPredicate p) NO_EXCEPT
381  {
382  return std::none_of(
383 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
384  std::execution::par_unseq,
385 #endif
386  first, last, p);
387  }
388 
389  //============================================================================
390  // Method Description:
397  template<class RandomIt>
398  void nth_element(RandomIt first, RandomIt nth, RandomIt last) NO_EXCEPT
399  {
401 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
402  std::execution::par_unseq,
403 #endif
404  first, nth, last);
405  }
406 
407  //============================================================================
408  // Method Description:
416  template<class RandomIt, class Compare>
417  void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) NO_EXCEPT
418  {
420 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
421  std::execution::par_unseq,
422 #endif
423  first, nth, last, comp);
424  }
425 
426  //============================================================================
427  // Method Description:
435  template<class ForwardIt, class T>
436  void replace(ForwardIt first, ForwardIt last,
437  const T& oldValue, const T& newValue) NO_EXCEPT
438  {
439  std::replace(
440 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
441  std::execution::par_unseq,
442 #endif
443  first, last, oldValue, newValue);
444  }
445 
446  //============================================================================
447  // Method Description:
453  template<class BidirIt>
454  void reverse(BidirIt first, BidirIt last) NO_EXCEPT
455  {
456  std::reverse(
457 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
458  std::execution::par_unseq,
459 #endif
460  first, last);
461  }
462 
463  //============================================================================
464  // Method Description:
471  template<class ForwardIt>
472  void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) NO_EXCEPT
473  {
474  std::rotate(
475 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
476  std::execution::par_unseq,
477 #endif
478  first, firstN, last);
479  }
480 
481  //============================================================================
482  // Method Description:
492  template<class InputIt1, class InputIt2, class OutputIt>
493  OutputIt set_difference(InputIt1 first1, InputIt1 last1,
494  InputIt2 first2, InputIt2 last2,
495  OutputIt destination)
496  {
497  return std::set_difference(
498 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
499  std::execution::par_unseq,
500 #endif
501  first1, last1, first2, last2, destination);
502  }
503 
504  //============================================================================
505  // Method Description:
516  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
517  OutputIt set_difference(InputIt1 first1, InputIt1 last1,
518  InputIt2 first2, InputIt2 last2,
519  OutputIt destination, Compare comp) NO_EXCEPT
520  {
521  return std::set_difference(
522 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
523  std::execution::par_unseq,
524 #endif
525  first1, last1, first2, last2, destination, comp);
526  }
527 
528  //============================================================================
529  // Method Description:
539  template<class InputIt1, class InputIt2, class OutputIt>
540  OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
541  InputIt2 first2, InputIt2 last2,
542  OutputIt destination) NO_EXCEPT
543  {
544  return std::set_intersection(
545 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
546  std::execution::par_unseq,
547 #endif
548  first1, last1, first2, last2, destination);
549  }
550 
551  //============================================================================
552  // Method Description:
563  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
564  OutputIt set_intersection(InputIt1 first1, InputIt1 last1,
565  InputIt2 first2, InputIt2 last2,
566  OutputIt destination, Compare comp) NO_EXCEPT
567  {
568  return std::set_intersection(
569 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
570  std::execution::par_unseq,
571 #endif
572  first1, last1, first2, last2, destination, comp);
573  }
574 
575  //============================================================================
576  // Method Description:
586  template<class InputIt1, class InputIt2, class OutputIt>
587  OutputIt set_union(InputIt1 first1, InputIt1 last1,
588  InputIt2 first2, InputIt2 last2,
589  OutputIt destination) NO_EXCEPT
590  {
591  return std::set_union(
592 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
593  std::execution::par_unseq,
594 #endif
595  first1, last1, first2, last2, destination);
596  }
597 
598  //============================================================================
599  // Method Description:
610  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
611  OutputIt set_union(InputIt1 first1, InputIt1 last1,
612  InputIt2 first2, InputIt2 last2,
613  OutputIt destination, Compare comp) NO_EXCEPT
614  {
615  return std::set_union(
616 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
617  std::execution::par_unseq,
618 #endif
619  first1, last1, first2, last2, destination, comp);
620  }
621 
622  //============================================================================
623  // Method Description:
629  template<class RandomIt>
630  void sort(RandomIt first, RandomIt last) NO_EXCEPT
631  {
632  return std::sort(
633 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
634  std::execution::par_unseq,
635 #endif
636  first, last);
637  }
638 
639  //============================================================================
640  // Method Description:
647  template<class RandomIt, class Compare>
648  void sort(RandomIt first, RandomIt last, Compare comp) NO_EXCEPT
649  {
650  return std::sort(
651 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
652  std::execution::par_unseq,
653 #endif
654  first, last, comp);
655  }
656 
657  //============================================================================
658  // Method Description:
664  template<class RandomIt>
665  void stable_sort(RandomIt first, RandomIt last) NO_EXCEPT
666  {
668 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
669  std::execution::par_unseq,
670 #endif
671  first, last);
672  }
673 
674  //============================================================================
675  // Method Description:
682  template<class RandomIt, class Compare>
683  void stable_sort(RandomIt first, RandomIt last, Compare comp) NO_EXCEPT
684  {
686 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
687  std::execution::par_unseq,
688 #endif
689  first, last, comp);
690  }
691 
692  //============================================================================
693  // Method Description:
702  template<class InputIt, class OutputIt, class UnaryOperation>
703  OutputIt transform(InputIt first, InputIt last, OutputIt destination,
704  UnaryOperation unaryFunction) NO_EXCEPT
705  {
706  return std::transform(
707 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
708  std::execution::par_unseq,
709 #endif
710  first, last, destination, unaryFunction);
711  }
712 
713  //============================================================================
714  // Method Description:
724  template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
725  OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2,
726  OutputIt destination, BinaryOperation unaryFunction) NO_EXCEPT
727  {
728  return std::transform(
729 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
730  std::execution::par_unseq,
731 #endif
732  first1, last1, first2, destination, unaryFunction);
733  }
734 
735  //============================================================================
736  // Method Description:
744  template<class InputIt, class OutputIt>
745  constexpr OutputIt unique_copy(InputIt first, InputIt last,
746  OutputIt destination) NO_EXCEPT
747  {
748  return std::unique_copy(
749 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
750  std::execution::par_unseq,
751 #endif
752  first, last, destination);
753  }
754 
755  //============================================================================
756  // Method Description:
765  template<class InputIt, class OutputIt, class BinaryPredicate>
766  constexpr OutputIt unique_copy(InputIt first, InputIt last,
767  OutputIt destination, BinaryPredicate binaryFunction) NO_EXCEPT
768  {
769  return std::unique_copy(
770 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
771  std::execution::par_unseq,
772 #endif
773  first, last, destination, binaryFunction);
774  }
775  }
776 }
nc::stl_algorithms::minmax_element
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:361
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f) noexcept
Definition: StlAlgorithms.hpp:214
nc::stl_algorithms::set_union
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:611
nc::stl_algorithms::equal
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:156
nc::stl_algorithms::max_element
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:288
nc::stl_algorithms::min_element
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:324
nc::stl_algorithms::is_sorted
bool is_sorted(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:232
nc::stl_algorithms::max_element
ForwardIt max_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:269
nc::stl_algorithms::stable_sort
void stable_sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:683
nc::stl_algorithms::all_of
bool all_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:58
nc::stl_algorithms::fill
void fill(ForwardIt first, ForwardIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:175
nc::stl_algorithms::set_intersection
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:564
nc::stl_algorithms::sort
void sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:648
nc::stl_algorithms::copy
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:96
nc::stl_algorithms::transform
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt destination, BinaryOperation unaryFunction) noexcept
Definition: StlAlgorithms.hpp:725
nc::stl_algorithms::reverse
void reverse(BidirIt first, BidirIt last) noexcept
Definition: StlAlgorithms.hpp:454
nc::stl_algorithms::find
InputIt find(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:196
nc::stl_algorithms::nth_element
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:398
nc::stl_algorithms::count
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:116
nc
Definition: Coordinate.hpp:45
nc::stl_algorithms::any_of
bool any_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:77
nc::stl_algorithms::set_difference
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
Definition: StlAlgorithms.hpp:493
nc::stl_algorithms::nth_element
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:417
nc::stl_algorithms::is_sorted
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:251
nc::stl_algorithms::replace
void replace(ForwardIt first, ForwardIt last, const T &oldValue, const T &newValue) noexcept
Definition: StlAlgorithms.hpp:436
nc::stl_algorithms::set_union
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:587
nc::stl_algorithms::unique_copy
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:745
nc::stl_algorithms::transform
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction) noexcept
Definition: StlAlgorithms.hpp:703
nc::stl_algorithms::sort
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:630
nc::stl_algorithms::rotate
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:472
nc::stl_algorithms::unique_copy
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination, BinaryPredicate binaryFunction) noexcept
Definition: StlAlgorithms.hpp:766
nc::stl_algorithms::min_element
ForwardIt min_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:305
nc::stl_algorithms::equal
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:136
nc::stl_algorithms::stable_sort
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:665
nc::stl_algorithms::minmax_element
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:342
NO_EXCEPT
#define NO_EXCEPT
Definition: StlAlgorithms.hpp:41
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58
nc::stl_algorithms::set_intersection
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:540
nc::stl_algorithms::none_of
bool none_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:380
nc::stl_algorithms::set_difference
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:517