Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:21

0001 #ifndef NPSTAT_ISMONOTONOUS_HH_
0002 #define NPSTAT_ISMONOTONOUS_HH_
0003 
0004 /*!
0005 // \file isMonotonous.h
0006 //
0007 // \brief A few simple template functions for checking monotonicity of
0008 //        container values
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // July 2012
0013 */
0014 
0015 namespace npstat {
0016   /** Check if the sequence of values is strictly increasing */
0017   template <class Iter>
0018   inline bool isStrictlyIncreasing(Iter begin, Iter const end) {
0019     if (begin == end)
0020       return false;
0021     Iter first(begin);
0022     bool status = ++begin != end;
0023     for (; begin != end && status; ++begin, ++first)
0024       if (!(*first < *begin))
0025         status = false;
0026     return status;
0027   }
0028 
0029   /** Check if the sequence of values is strictly decreasing */
0030   template <class Iter>
0031   inline bool isStrictlyDecreasing(Iter begin, Iter const end) {
0032     if (begin == end)
0033       return false;
0034     Iter first(begin);
0035     bool status = ++begin != end;
0036     for (; begin != end && status; ++begin, ++first)
0037       if (!(*begin < *first))
0038         status = false;
0039     return status;
0040   }
0041 
0042   /** Check if the sequence of values is strictly increasing or decreasing */
0043   template <class Iter>
0044   inline bool isStrictlyMonotonous(Iter const begin, Iter const end) {
0045     return isStrictlyIncreasing(begin, end) || isStrictlyDecreasing(begin, end);
0046   }
0047 
0048   /** Check if the sequence of values is not decreasing */
0049   template <class Iter>
0050   inline bool isNonDecreasing(Iter begin, Iter const end) {
0051     if (begin == end)
0052       return false;
0053     Iter first(begin);
0054     bool status = ++begin != end;
0055     for (; begin != end && status; ++begin, ++first)
0056       if (*begin < *first)
0057         status = false;
0058     return status;
0059   }
0060 
0061   /** Check if the sequence of values is not increasing */
0062   template <class Iter>
0063   inline bool isNonIncreasing(Iter begin, Iter const end) {
0064     if (begin == end)
0065       return false;
0066     Iter first(begin);
0067     bool status = ++begin != end;
0068     for (; begin != end && status; ++begin, ++first)
0069       if (*begin > *first)
0070         status = false;
0071     return status;
0072   }
0073 
0074   /** 
0075     // Check if the sequence of values is either non-increasing
0076     // or non-decreasing
0077     */
0078   template <class Iter>
0079   inline bool isMonotonous(Iter const begin, Iter const end) {
0080     return isNonDecreasing(begin, end) || isNonIncreasing(begin, end);
0081   }
0082 }  // namespace npstat
0083 
0084 #endif  // NPSTAT_ISMONOTONOUS_HH_