npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
isMonotonous.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ISMONOTONOUS_HH_
2 #define NPSTAT_ISMONOTONOUS_HH_
3 
4 /*!
5 // \file isMonotonous.hh
6 //
7 // \brief A few simple template functions for checking monotonicity of
8 // container values
9 //
10 // Author: I. Volobouev
11 //
12 // July 2012
13 */
14 
15 namespace npstat {
16  /** Check if the sequence of values is strictly increasing */
17  template<class Iter>
18  inline bool isStrictlyIncreasing(Iter begin, Iter const end)
19  {
20  if (begin == end)
21  return false;
22  Iter first(begin);
23  bool status = ++begin != end;
24  for (; begin != end && status; ++begin, ++first)
25  if (!(*first < *begin))
26  status = false;
27  return status;
28  }
29 
30  /** Check if the sequence of values is strictly decreasing */
31  template<class Iter>
32  inline bool isStrictlyDecreasing(Iter begin, Iter const end)
33  {
34  if (begin == end)
35  return false;
36  Iter first(begin);
37  bool status = ++begin != end;
38  for (; begin != end && status; ++begin, ++first)
39  if (!(*begin < *first))
40  status = false;
41  return status;
42  }
43 
44  /** Check if the sequence of values is strictly increasing or decreasing */
45  template<class Iter>
46  inline bool isStrictlyMonotonous(Iter const begin, Iter const end)
47  {
48  return isStrictlyIncreasing(begin, end) ||
49  isStrictlyDecreasing(begin, end);
50  }
51 
52  /** Check if the sequence of values is not decreasing */
53  template<class Iter>
54  inline bool isNonDecreasing(Iter begin, Iter const end)
55  {
56  if (begin == end)
57  return false;
58  Iter first(begin);
59  bool status = ++begin != end;
60  for (; begin != end && status; ++begin, ++first)
61  if (*begin < *first)
62  status = false;
63  return status;
64  }
65 
66  /** Check if the sequence of values is not increasing */
67  template<class Iter>
68  inline bool isNonIncreasing(Iter begin, Iter const end)
69  {
70  if (begin == end)
71  return false;
72  Iter first(begin);
73  bool status = ++begin != end;
74  for (; begin != end && status; ++begin, ++first)
75  if (*first < *begin)
76  status = false;
77  return status;
78  }
79 
80  /**
81  // Check if the sequence of values is either non-increasing
82  // or non-decreasing
83  */
84  template<class Iter>
85  inline bool isMonotonous(Iter const begin, Iter const end)
86  {
87  return isNonDecreasing(begin, end) || isNonIncreasing(begin, end);
88  }
89 
90 #ifdef SWIG
91  template<typename T>
92  inline bool isStrictlyIncreasing2(const T* arr, const unsigned len)
93  {return isStrictlyIncreasing(arr, arr+len);}
94 
95  template<typename T>
96  inline bool isStrictlyDecreasing2(const T* arr, const unsigned len)
97  {return isStrictlyDecreasing(arr, arr+len);}
98 
99  template<typename T>
100  inline bool isStrictlyMonotonous2(const T* arr, const unsigned len)
101  {return isStrictlyMonotonous(arr, arr+len);}
102 
103  template<typename T>
104  inline bool isNonDecreasing2(const T* arr, const unsigned len)
105  {return isNonDecreasing(arr, arr+len);}
106 
107  template<typename T>
108  inline bool isNonIncreasing2(const T* arr, const unsigned len)
109  {return isNonIncreasing(arr, arr+len);}
110 
111  template<typename T>
112  inline bool isMonotonous2(const T* arr, const unsigned len)
113  {return isMonotonous(arr, arr+len);}
114 #endif // SWIG
115 }
116 
117 #endif // NPSTAT_ISMONOTONOUS_HH_
Definition: AbsArrayProjector.hh:14
bool isNonIncreasing(Iter begin, Iter const end)
Definition: isMonotonous.hh:68
bool isMonotonous(Iter const begin, Iter const end)
Definition: isMonotonous.hh:85
bool isStrictlyMonotonous(Iter const begin, Iter const end)
Definition: isMonotonous.hh:46
bool isNonDecreasing(Iter begin, Iter const end)
Definition: isMonotonous.hh:54
bool isStrictlyDecreasing(Iter begin, Iter const end)
Definition: isMonotonous.hh:32
bool isStrictlyIncreasing(Iter begin, Iter const end)
Definition: isMonotonous.hh:18