Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:29:18

0001 #ifndef FWCore_Utilities_Algorithms_h
0002 #define FWCore_Utilities_Algorithms_h
0003 
0004 #include <algorithm>
0005 
0006 namespace edm {
0007 
0008   /// Function templates that provide wrappers for standard algorithms,
0009   /// avoiding some duplication
0010   /// and assuring that incommensurate iterators are not used.
0011 
0012   /// wrapper for std::for_each
0013   template <typename ForwardSequence, typename Func>
0014   inline Func for_all(ForwardSequence& s, Func f) {
0015     return std::for_each(s.begin(), s.end(), f);
0016   }
0017 
0018   /// wrappers for copy
0019   template <typename ForwardSequence, typename Func>
0020   inline Func copy_all(ForwardSequence& s, Func f) {
0021     return std::copy(s.begin(), s.end(), f);
0022   }
0023 
0024   /// wrappers for std::find
0025   template <typename ForwardSequence, typename Datum>
0026   inline typename ForwardSequence::const_iterator find_in_all(ForwardSequence const& s, Datum const& d) {
0027     return std::find(s.begin(), s.end(), d);
0028   }
0029 
0030   template <typename ForwardSequence, typename Datum>
0031   inline typename ForwardSequence::iterator find_in_all(ForwardSequence& s, Datum const& d) {
0032     return std::find(s.begin(), s.end(), d);
0033   }
0034 
0035   template <typename ForwardSequence, typename Datum>
0036   inline bool search_all(ForwardSequence const& s, Datum const& d) {
0037     return std::find(s.begin(), s.end(), d) != s.end();
0038   }
0039 
0040   /// wrappers for std::find
0041   template <typename ForwardSequence, typename Predicate>
0042   inline typename ForwardSequence::const_iterator find_if_in_all(ForwardSequence const& s, Predicate const& p) {
0043     return std::find_if(s.begin(), s.end(), p);
0044   }
0045 
0046   template <typename ForwardSequence, typename Predicate>
0047   inline typename ForwardSequence::iterator find_if_in_all(ForwardSequence& s, Predicate const& p) {
0048     return std::find_if(s.begin(), s.end(), p);
0049   }
0050 
0051   template <typename ForwardSequence, typename Predicate>
0052   inline bool search_if_in_all(ForwardSequence const& s, Predicate const& p) {
0053     return std::find_if(s.begin(), s.end(), p) != s.end();
0054   }
0055 
0056   /// wrappers for std::binary_search
0057   template <typename ForwardSequence, typename Datum>
0058   inline bool binary_search_all(ForwardSequence const& s, Datum const& d) {
0059     return std::binary_search(s.begin(), s.end(), d);
0060   }
0061 
0062   template <typename ForwardSequence, typename Datum, typename Predicate>
0063   inline bool binary_search_all(ForwardSequence const& s, Datum const& d, Predicate p) {
0064     return std::binary_search(s.begin(), s.end(), d, p);
0065   }
0066 
0067   /// wrappers for std::lower_bound
0068   template <typename ForwardSequence, typename Datum>
0069   inline typename ForwardSequence::const_iterator lower_bound_all(ForwardSequence const& s, Datum const& d) {
0070     return std::lower_bound(s.begin(), s.end(), d);
0071   }
0072 
0073   template <typename ForwardSequence, typename Datum>
0074   inline typename ForwardSequence::iterator lower_bound_all(ForwardSequence& s, Datum const& d) {
0075     return std::lower_bound(s.begin(), s.end(), d);
0076   }
0077 
0078   template <typename ForwardSequence, typename Datum, typename Predicate>
0079   inline typename ForwardSequence::const_iterator lower_bound_all(ForwardSequence const& s,
0080                                                                   Datum const& d,
0081                                                                   Predicate p) {
0082     return std::lower_bound(s.begin(), s.end(), d, p);
0083   }
0084 
0085   template <typename ForwardSequence, typename Datum, typename Predicate>
0086   inline typename ForwardSequence::iterator lower_bound_all(ForwardSequence& s, Datum const& d, Predicate p) {
0087     return std::lower_bound(s.begin(), s.end(), d, p);
0088   }
0089 
0090   /// wrappers for std::sort
0091   template <typename RandomAccessSequence>
0092   inline void sort_all(RandomAccessSequence& s) {
0093     std::sort(s.begin(), s.end());
0094   }
0095 
0096   template <typename RandomAccessSequence, typename Predicate>
0097   inline void sort_all(RandomAccessSequence& s, Predicate p) {
0098     std::sort(s.begin(), s.end(), p);
0099   }
0100 
0101   /// wrappers for std::stable_sort
0102   template <typename RandomAccessSequence>
0103   inline void stable_sort_all(RandomAccessSequence& s) {
0104     std::stable_sort(s.begin(), s.end());
0105   }
0106 
0107   template <typename RandomAccessSequence, typename Predicate>
0108   inline void stable_sort_all(RandomAccessSequence& s, Predicate p) {
0109     std::stable_sort(s.begin(), s.end(), p);
0110   }
0111 }  // namespace edm
0112 #endif