Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:28:23

0001 //  Copyright (c) 2010 Nuovation System Designs, LLC
0002 //    Grant Erickson <gerickson@nuovations.com>
0003 //
0004 //  Reworked somewhat by Marshall Clow; August 2010
0005 //  
0006 //  Distributed under the Boost Software License, Version 1.0. (See
0007 //  accompanying file LICENSE_1_0.txt or copy at
0008 //  http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 //  See http://www.boost.org/ for latest version.
0011 //
0012 
0013 #ifndef BOOST_ALGORITHM_ORDERED_HPP
0014 #define BOOST_ALGORITHM_ORDERED_HPP
0015 
0016 #include <functional>
0017 #include <iterator>
0018 
0019 #include <boost/config.hpp>
0020 #include <boost/range/begin.hpp>
0021 #include <boost/range/end.hpp>
0022 
0023 #include <boost/core/enable_if.hpp>
0024 #include <boost/type_traits/is_same.hpp>
0025 #include <boost/type_traits/type_identity.hpp> // for boost::type_identity
0026 
0027 namespace boost { namespace algorithm {
0028 
0029 /// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
0030 /// \return the point in the sequence [first, last) where the elements are unordered
0031 ///     (according to the comparison predicate 'p').
0032 /// 
0033 /// \param first The start of the sequence to be tested.
0034 /// \param last  One past the end of the sequence
0035 /// \param p     A binary predicate that returns true if two elements are ordered.
0036 ///
0037     template <typename ForwardIterator, typename Pred>
0038     BOOST_CXX14_CONSTEXPR ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last, Pred p )
0039     {
0040         if ( first == last ) return last;  // the empty sequence is ordered
0041         ForwardIterator next = first;
0042         while ( ++next != last )
0043         {
0044             if ( p ( *next, *first ))
0045                 return next;
0046             first = next;
0047         }
0048         return last;    
0049     }
0050 
0051 /// \fn is_sorted_until ( ForwardIterator first, ForwardIterator last )
0052 /// \return the point in the sequence [first, last) where the elements are unordered
0053 /// 
0054 /// \param first The start of the sequence to be tested.
0055 /// \param last  One past the end of the sequence
0056 ///
0057     template <typename ForwardIterator>
0058     BOOST_CXX14_CONSTEXPR ForwardIterator is_sorted_until ( ForwardIterator first, ForwardIterator last )
0059     {
0060         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
0061         return boost::algorithm::is_sorted_until ( first, last, std::less<value_type>());
0062     }
0063 
0064 
0065 /// \fn is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
0066 /// \return whether or not the entire sequence is sorted
0067 /// 
0068 /// \param first The start of the sequence to be tested.
0069 /// \param last  One past the end of the sequence
0070 /// \param p     A binary predicate that returns true if two elements are ordered.
0071 ///
0072     template <typename ForwardIterator, typename Pred>
0073     BOOST_CXX14_CONSTEXPR bool is_sorted ( ForwardIterator first, ForwardIterator last, Pred p )
0074     {
0075         return boost::algorithm::is_sorted_until (first, last, p) == last;
0076     }
0077 
0078 /// \fn is_sorted ( ForwardIterator first, ForwardIterator last )
0079 /// \return whether or not the entire sequence is sorted
0080 /// 
0081 /// \param first The start of the sequence to be tested.
0082 /// \param last  One past the end of the sequence
0083 ///
0084     template <typename ForwardIterator>
0085     BOOST_CXX14_CONSTEXPR bool is_sorted ( ForwardIterator first, ForwardIterator last )
0086     {
0087         return boost::algorithm::is_sorted_until (first, last) == last;
0088     }
0089 
0090 ///
0091 /// -- Range based versions of the C++11 functions
0092 ///
0093 
0094 /// \fn is_sorted_until ( const R &range, Pred p )
0095 /// \return the point in the range R where the elements are unordered
0096 ///     (according to the comparison predicate 'p').
0097 /// 
0098 /// \param range The range to be tested.
0099 /// \param p     A binary predicate that returns true if two elements are ordered.
0100 ///
0101     template <typename R, typename Pred>
0102     BOOST_CXX14_CONSTEXPR typename boost::lazy_disable_if_c<
0103         boost::is_same<R, Pred>::value, 
0104         typename boost::range_iterator<const R> 
0105     >::type is_sorted_until ( const R &range, Pred p )
0106     {
0107         return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range ), p );
0108     }
0109 
0110 
0111 /// \fn is_sorted_until ( const R &range )
0112 /// \return the point in the range R where the elements are unordered
0113 /// 
0114 /// \param range The range to be tested.
0115 ///
0116     template <typename R>
0117     BOOST_CXX14_CONSTEXPR typename boost::range_iterator<const R>::type is_sorted_until ( const R &range )
0118     {
0119         return boost::algorithm::is_sorted_until ( boost::begin ( range ), boost::end ( range ));
0120     }
0121 
0122 /// \fn is_sorted ( const R &range, Pred p )
0123 /// \return whether or not the entire range R is sorted
0124 ///     (according to the comparison predicate 'p').
0125 /// 
0126 /// \param range The range to be tested.
0127 /// \param p     A binary predicate that returns true if two elements are ordered.
0128 ///
0129     template <typename R, typename Pred>
0130     BOOST_CXX14_CONSTEXPR typename boost::lazy_disable_if_c< boost::is_same<R, Pred>::value, boost::type_identity<bool> >::type
0131     is_sorted ( const R &range, Pred p )
0132     {
0133         return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ), p );
0134     }
0135 
0136 
0137 /// \fn is_sorted ( const R &range )
0138 /// \return whether or not the entire range R is sorted
0139 /// 
0140 /// \param range The range to be tested.
0141 ///
0142     template <typename R>
0143     BOOST_CXX14_CONSTEXPR bool is_sorted ( const R &range )
0144     {
0145         return boost::algorithm::is_sorted ( boost::begin ( range ), boost::end ( range ));
0146     }
0147 
0148 
0149 ///
0150 /// -- Range based versions of the C++11 functions
0151 ///
0152 
0153 /// \fn is_increasing ( ForwardIterator first, ForwardIterator last )
0154 /// \return true if the entire sequence is increasing; i.e, each item is greater than or  
0155 ///     equal to the previous one.
0156 /// 
0157 /// \param first The start of the sequence to be tested.
0158 /// \param last  One past the end of the sequence
0159 ///
0160 /// \note This function will return true for sequences that contain items that compare
0161 ///     equal. If that is not what you intended, you should use is_strictly_increasing instead.
0162     template <typename ForwardIterator>
0163     BOOST_CXX14_CONSTEXPR bool is_increasing ( ForwardIterator first, ForwardIterator last )
0164     {
0165         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
0166         return boost::algorithm::is_sorted (first, last, std::less<value_type>());
0167     }
0168 
0169 
0170 /// \fn is_increasing ( const R &range )
0171 /// \return true if the entire sequence is increasing; i.e, each item is greater than or  
0172 ///     equal to the previous one.
0173 /// 
0174 /// \param range The range to be tested.
0175 ///
0176 /// \note This function will return true for sequences that contain items that compare
0177 ///     equal. If that is not what you intended, you should use is_strictly_increasing instead.
0178     template <typename R>
0179     BOOST_CXX14_CONSTEXPR bool is_increasing ( const R &range )
0180     {
0181         return is_increasing ( boost::begin ( range ), boost::end ( range ));
0182     }
0183 
0184 
0185 
0186 /// \fn is_decreasing ( ForwardIterator first, ForwardIterator last )
0187 /// \return true if the entire sequence is decreasing; i.e, each item is less than 
0188 ///     or equal to the previous one.
0189 /// 
0190 /// \param first The start of the sequence to be tested.
0191 /// \param last  One past the end of the sequence
0192 ///
0193 /// \note This function will return true for sequences that contain items that compare
0194 ///     equal. If that is not what you intended, you should use is_strictly_decreasing instead.
0195     template <typename ForwardIterator>
0196     BOOST_CXX14_CONSTEXPR bool is_decreasing ( ForwardIterator first, ForwardIterator last )
0197     {
0198         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
0199         return boost::algorithm::is_sorted (first, last, std::greater<value_type>());
0200     }
0201 
0202 /// \fn is_decreasing ( const R &range )
0203 /// \return true if the entire sequence is decreasing; i.e, each item is less than 
0204 ///     or equal to the previous one.
0205 /// 
0206 /// \param range The range to be tested.
0207 ///
0208 /// \note This function will return true for sequences that contain items that compare
0209 ///     equal. If that is not what you intended, you should use is_strictly_decreasing instead.
0210     template <typename R>
0211     BOOST_CXX14_CONSTEXPR bool is_decreasing ( const R &range )
0212     {
0213         return is_decreasing ( boost::begin ( range ), boost::end ( range ));
0214     }
0215 
0216 
0217 
0218 /// \fn is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
0219 /// \return true if the entire sequence is strictly increasing; i.e, each item is greater
0220 ///     than the previous one
0221 /// 
0222 /// \param first The start of the sequence to be tested.
0223 /// \param last  One past the end of the sequence
0224 ///
0225 /// \note This function will return false for sequences that contain items that compare
0226 ///     equal. If that is not what you intended, you should use is_increasing instead.
0227     template <typename ForwardIterator>
0228     BOOST_CXX14_CONSTEXPR bool is_strictly_increasing ( ForwardIterator first, ForwardIterator last )
0229     {
0230         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
0231         return boost::algorithm::is_sorted (first, last, std::less_equal<value_type>());
0232     }
0233 
0234 /// \fn is_strictly_increasing ( const R &range )
0235 /// \return true if the entire sequence is strictly increasing; i.e, each item is greater
0236 ///     than the previous one
0237 /// 
0238 /// \param range The range to be tested.
0239 ///
0240 /// \note This function will return false for sequences that contain items that compare
0241 ///     equal. If that is not what you intended, you should use is_increasing instead.
0242     template <typename R>
0243     BOOST_CXX14_CONSTEXPR bool is_strictly_increasing ( const R &range )
0244     {
0245         return is_strictly_increasing ( boost::begin ( range ), boost::end ( range ));
0246     }
0247 
0248 
0249 /// \fn is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
0250 /// \return true if the entire sequence is strictly decreasing; i.e, each item is less than
0251 ///     the previous one
0252 /// 
0253 /// \param first The start of the sequence to be tested.
0254 /// \param last  One past the end of the sequence
0255 ///
0256 /// \note This function will return false for sequences that contain items that compare
0257 ///     equal. If that is not what you intended, you should use is_decreasing instead.
0258     template <typename ForwardIterator>
0259     BOOST_CXX14_CONSTEXPR bool is_strictly_decreasing ( ForwardIterator first, ForwardIterator last )
0260     {
0261         typedef typename std::iterator_traits<ForwardIterator>::value_type value_type;
0262         return boost::algorithm::is_sorted (first, last, std::greater_equal<value_type>());
0263     }
0264 
0265 /// \fn is_strictly_decreasing ( const R &range )
0266 /// \return true if the entire sequence is strictly decreasing; i.e, each item is less than
0267 ///     the previous one
0268 /// 
0269 /// \param range The range to be tested.
0270 ///
0271 /// \note This function will return false for sequences that contain items that compare
0272 ///     equal. If that is not what you intended, you should use is_decreasing instead.
0273     template <typename R>
0274     BOOST_CXX14_CONSTEXPR bool is_strictly_decreasing ( const R &range )
0275     {
0276         return is_strictly_decreasing ( boost::begin ( range ), boost::end ( range ));
0277     }
0278 
0279 }} // namespace boost
0280 
0281 #endif  // BOOST_ALGORITHM_ORDERED_HPP