NumCpp  2.8.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 
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,
64  last,
65  p);
66  }
67 
68  //============================================================================
69  // Method Description:
77  template<class InputIt, class UnaryPredicate>
78  bool any_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
79  {
80  return std::any_of(
81 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
82  std::execution::par_unseq,
83 #endif
84  first,
85  last,
86  p);
87  }
88 
89  //============================================================================
90  // Method Description:
98  template<class InputIt, class OutputIt>
99  OutputIt copy(InputIt first, InputIt last, OutputIt destination) CONDITIONAL_NO_EXCEPT
100  {
101  return std::copy(
102 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
103  std::execution::par_unseq,
104 #endif
105  first,
106  last,
107  destination);
108  }
109 
110  //============================================================================
111  // Method Description:
119  template<class InputIt, class T>
120  typename std::iterator_traits<InputIt>::difference_type
121  count(InputIt first, InputIt last, const T& value) CONDITIONAL_NO_EXCEPT
122  {
123  return std::count(
124 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
125  std::execution::par_unseq,
126 #endif
127  first,
128  last,
129  value);
130  }
131 
132  //============================================================================
133  // Method Description:
141  template<class InputIt1, class InputIt2>
142  bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) CONDITIONAL_NO_EXCEPT
143  {
144  return std::equal(
145 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
146  std::execution::par_unseq,
147 #endif
148  first1,
149  last1,
150  first2);
151  }
152 
153  //============================================================================
154  // Method Description:
163  template<class InputIt1, class InputIt2, class BinaryPredicate>
164  bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) CONDITIONAL_NO_EXCEPT
165  {
166  return std::equal(
167 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
168  std::execution::par_unseq,
169 #endif
170  first1,
171  last1,
172  first2,
173  p);
174  }
175 
176  //============================================================================
177  // Method Description:
184  template<class ForwardIt, class T>
185  void fill(ForwardIt first, ForwardIt last, const T& value) CONDITIONAL_NO_EXCEPT
186  {
187  return std::fill(
188 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
189  std::execution::par_unseq,
190 #endif
191  first,
192  last,
193  value);
194  }
195 
196  //============================================================================
197  // Method Description:
206  template<class InputIt, class T>
207  InputIt find(InputIt first, InputIt last, const T& value) CONDITIONAL_NO_EXCEPT
208  {
209  return std::find(
210 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
211  std::execution::par_unseq,
212 #endif
213  first,
214  last,
215  value);
216  }
217 
218  //============================================================================
219  // Method Description:
226  template<class InputIt, class UnaryFunction>
227  void for_each(InputIt first, InputIt last, UnaryFunction f)
228  {
230 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
231  std::execution::par_unseq,
232 #endif
233  first,
234  last,
235  f);
236  }
237 
238  //============================================================================
239  // Method Description:
246  template<class ForwardIt>
247  bool is_sorted(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
248  {
249  return std::is_sorted(
250 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
251  std::execution::par_unseq,
252 #endif
253  first,
254  last);
255  }
256 
257  //============================================================================
258  // Method Description:
266  template<class ForwardIt, class Compare>
267  bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
268  {
269  return std::is_sorted(
270 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
271  std::execution::par_unseq,
272 #endif
273  first,
274  last,
275  comp);
276  }
277 
278  //============================================================================
279  // Method Description:
286  template<class ForwardIt>
287  ForwardIt max_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
288  {
289  return std::max_element(
290 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
291  std::execution::par_unseq,
292 #endif
293  first,
294  last);
295  }
296 
297  //============================================================================
298  // Method Description:
306  template<class ForwardIt, class Compare>
307  ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
308  {
309  return std::max_element(
310 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
311  std::execution::par_unseq,
312 #endif
313  first,
314  last,
315  comp);
316  }
317 
318  //============================================================================
319  // Method Description:
325  template<class ForwardIt>
326  ForwardIt min_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
327  {
328  return std::min_element(
329 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
330  std::execution::par_unseq,
331 #endif
332  first,
333  last);
334  }
335 
336  //============================================================================
337  // Method Description:
345  template<class ForwardIt, class Compare>
346  ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
347  {
348  return std::min_element(
349 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
350  std::execution::par_unseq,
351 #endif
352  first,
353  last,
354  comp);
355  }
356 
357  //============================================================================
358  // Method Description:
365  template<class ForwardIt>
366  std::pair<ForwardIt, ForwardIt> minmax_element(ForwardIt first, ForwardIt last) CONDITIONAL_NO_EXCEPT
367  {
368  return std::minmax_element(
369 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
370  std::execution::par_unseq,
371 #endif
372  first,
373  last);
374  }
375 
376  //============================================================================
377  // Method Description:
385  template<class ForwardIt, class Compare>
386  std::pair<ForwardIt, ForwardIt>
387  minmax_element(ForwardIt first, ForwardIt last, Compare comp) CONDITIONAL_NO_EXCEPT
388  {
389  return std::minmax_element(
390 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
391  std::execution::par_unseq,
392 #endif
393  first,
394  last,
395  comp);
396  }
397 
398  //============================================================================
399  // Method Description:
407  template<class InputIt, class UnaryPredicate>
408  bool none_of(InputIt first, InputIt last, UnaryPredicate p) CONDITIONAL_NO_EXCEPT
409  {
410  return std::none_of(
411 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
412  std::execution::par_unseq,
413 #endif
414  first,
415  last,
416  p);
417  }
418 
419  //============================================================================
420  // Method Description:
427  template<class RandomIt>
428  void nth_element(RandomIt first, RandomIt nth, RandomIt last) CONDITIONAL_NO_EXCEPT
429  {
431 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
432  std::execution::par_unseq,
433 #endif
434  first,
435  nth,
436  last);
437  }
438 
439  //============================================================================
440  // Method Description:
448  template<class RandomIt, class Compare>
449  void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
450  {
452 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
453  std::execution::par_unseq,
454 #endif
455  first,
456  nth,
457  last,
458  comp);
459  }
460 
461  //============================================================================
462  // Method Description:
470  template<class ForwardIt, class T>
471  void replace(ForwardIt first, ForwardIt last, const T& oldValue, const T& newValue) CONDITIONAL_NO_EXCEPT
472  {
473  std::replace(
474 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
475  std::execution::par_unseq,
476 #endif
477  first,
478  last,
479  oldValue,
480  newValue);
481  }
482 
483  //============================================================================
484  // Method Description:
490  template<class BidirIt>
491  void reverse(BidirIt first, BidirIt last) CONDITIONAL_NO_EXCEPT
492  {
493  std::reverse(
494 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
495  std::execution::par_unseq,
496 #endif
497  first,
498  last);
499  }
500 
501  //============================================================================
502  // Method Description:
509  template<class ForwardIt>
510  void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) CONDITIONAL_NO_EXCEPT
511  {
512  std::rotate(
513 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
514  std::execution::par_unseq,
515 #endif
516  first,
517  firstN,
518  last);
519  }
520 
521  //============================================================================
522  // Method Description:
532  template<class InputIt1, class InputIt2, class OutputIt>
533  OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
534  {
535  return std::set_difference(
536 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
537  std::execution::par_unseq,
538 #endif
539  first1,
540  last1,
541  first2,
542  last2,
543  destination);
544  }
545 
546  //============================================================================
547  // Method Description:
558  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
559  OutputIt set_difference(InputIt1 first1,
560  InputIt1 last1,
561  InputIt2 first2,
562  InputIt2 last2,
563  OutputIt destination,
564  Compare comp) CONDITIONAL_NO_EXCEPT
565  {
566  return std::set_difference(
567 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
568  std::execution::par_unseq,
569 #endif
570  first1,
571  last1,
572  first2,
573  last2,
574  destination,
575  comp);
576  }
577 
578  //============================================================================
579  // Method Description:
589  template<class InputIt1, class InputIt2, class OutputIt>
590  OutputIt set_intersection(InputIt1 first1,
591  InputIt1 last1,
592  InputIt2 first2,
593  InputIt2 last2,
594  OutputIt destination) CONDITIONAL_NO_EXCEPT
595  {
596  return std::set_intersection(
597 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
598  std::execution::par_unseq,
599 #endif
600  first1,
601  last1,
602  first2,
603  last2,
604  destination);
605  }
606 
607  //============================================================================
608  // Method Description:
619  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
620  OutputIt set_intersection(InputIt1 first1,
621  InputIt1 last1,
622  InputIt2 first2,
623  InputIt2 last2,
624  OutputIt destination,
625  Compare comp) CONDITIONAL_NO_EXCEPT
626  {
627  return std::set_intersection(
628 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
629  std::execution::par_unseq,
630 #endif
631  first1,
632  last1,
633  first2,
634  last2,
635  destination,
636  comp);
637  }
638 
639  //============================================================================
640  // Method Description:
650  template<class InputIt1, class InputIt2, class OutputIt>
651  OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
653  {
654  return std::set_union(
655 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
656  std::execution::par_unseq,
657 #endif
658  first1,
659  last1,
660  first2,
661  last2,
662  destination);
663  }
664 
665  //============================================================================
666  // Method Description:
677  template<class InputIt1, class InputIt2, class OutputIt, class Compare>
678  OutputIt set_union(InputIt1 first1,
679  InputIt1 last1,
680  InputIt2 first2,
681  InputIt2 last2,
682  OutputIt destination,
683  Compare comp) CONDITIONAL_NO_EXCEPT
684  {
685  return std::set_union(
686 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
687  std::execution::par_unseq,
688 #endif
689  first1,
690  last1,
691  first2,
692  last2,
693  destination,
694  comp);
695  }
696 
697  //============================================================================
698  // Method Description:
704  template<class RandomIt>
705  void sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
706  {
707  return std::sort(
708 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
709  std::execution::par_unseq,
710 #endif
711  first,
712  last);
713  }
714 
715  //============================================================================
716  // Method Description:
723  template<class RandomIt, class Compare>
724  void sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
725  {
726  return std::sort(
727 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
728  std::execution::par_unseq,
729 #endif
730  first,
731  last,
732  comp);
733  }
734 
735  //============================================================================
736  // Method Description:
742  template<class RandomIt>
743  void stable_sort(RandomIt first, RandomIt last) CONDITIONAL_NO_EXCEPT
744  {
746 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
747  std::execution::par_unseq,
748 #endif
749  first,
750  last);
751  }
752 
753  //============================================================================
754  // Method Description:
761  template<class RandomIt, class Compare>
762  void stable_sort(RandomIt first, RandomIt last, Compare comp) CONDITIONAL_NO_EXCEPT
763  {
765 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
766  std::execution::par_unseq,
767 #endif
768  first,
769  last,
770  comp);
771  }
772 
773  //============================================================================
774  // Method Description:
783  template<class InputIt, class OutputIt, class UnaryOperation>
784  OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
785  {
786  return std::transform(
787 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
788  std::execution::par_unseq,
789 #endif
790  first,
791  last,
792  destination,
793  unaryFunction);
794  }
795 
796  //============================================================================
797  // Method Description:
807  template<class InputIt1, class InputIt2, class OutputIt, class BinaryOperation>
808  OutputIt transform(InputIt1 first1,
809  InputIt1 last1,
810  InputIt2 first2,
811  OutputIt destination,
812  BinaryOperation unaryFunction)
813  {
814  return std::transform(
815 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
816  std::execution::par_unseq,
817 #endif
818  first1,
819  last1,
820  first2,
821  destination,
822  unaryFunction);
823  }
824 
825  //============================================================================
826  // Method Description:
834  template<class InputIt, class OutputIt>
835  constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) CONDITIONAL_NO_EXCEPT
836  {
837  return std::unique_copy(
838 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
839  std::execution::par_unseq,
840 #endif
841  first,
842  last,
843  destination);
844  }
845 
846  //============================================================================
847  // Method Description:
856  template<class InputIt, class OutputIt, class BinaryPredicate>
857  constexpr OutputIt unique_copy(InputIt first,
858  InputIt last,
859  OutputIt destination,
860  BinaryPredicate binaryFunction) CONDITIONAL_NO_EXCEPT
861  {
862  return std::unique_copy(
863 #ifdef PARALLEL_ALGORITHMS_SUPPORTED
864  std::execution::par_unseq,
865 #endif
866  first,
867  last,
868  destination,
869  binaryFunction);
870  }
871  } // namespace stl_algorithms
872 } // namespace nc
#define CONDITIONAL_NO_EXCEPT
Definition: StlAlgorithms.hpp:40
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
bool any_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:78
void sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:705
bool is_sorted(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:267
std::iterator_traits< InputIt >::difference_type count(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:121
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:651
bool none_of(InputIt first, InputIt last, UnaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:408
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:307
ForwardIt max_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:287
OutputIt set_union(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:678
void stable_sort(RandomIt first, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:743
void sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:724
void reverse(BidirIt first, BidirIt last) noexcept
Definition: StlAlgorithms.hpp:491
OutputIt transform(InputIt first, InputIt last, OutputIt destination, UnaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:784
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p) noexcept
Definition: StlAlgorithms.hpp:164
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:227
InputIt find(InputIt first, InputIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:207
void stable_sort(RandomIt first, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:762
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:835
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:559
OutputIt set_difference(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination)
Definition: StlAlgorithms.hpp:533
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:366
void nth_element(RandomIt first, RandomIt nth, RandomIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:449
void replace(ForwardIt first, ForwardIt last, const T &oldValue, const T &newValue) noexcept
Definition: StlAlgorithms.hpp:471
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:590
bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) noexcept
Definition: StlAlgorithms.hpp:142
bool is_sorted(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:247
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:346
void rotate(ForwardIt first, ForwardIt firstN, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:510
OutputIt set_intersection(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, OutputIt destination, Compare comp) noexcept
Definition: StlAlgorithms.hpp:620
std::pair< ForwardIt, ForwardIt > minmax_element(ForwardIt first, ForwardIt last, Compare comp) noexcept
Definition: StlAlgorithms.hpp:387
OutputIt copy(InputIt first, InputIt last, OutputIt destination) noexcept
Definition: StlAlgorithms.hpp:99
constexpr OutputIt unique_copy(InputIt first, InputIt last, OutputIt destination, BinaryPredicate binaryFunction) noexcept
Definition: StlAlgorithms.hpp:857
OutputIt transform(InputIt1 first1, InputIt1 last1, InputIt2 first2, OutputIt destination, BinaryOperation unaryFunction)
Definition: StlAlgorithms.hpp:808
void nth_element(RandomIt first, RandomIt nth, RandomIt last) noexcept
Definition: StlAlgorithms.hpp:428
ForwardIt min_element(ForwardIt first, ForwardIt last) noexcept
Definition: StlAlgorithms.hpp:326
void fill(ForwardIt first, ForwardIt last, const T &value) noexcept
Definition: StlAlgorithms.hpp:185
Definition: Coordinate.hpp:45