VisionWorks Toolkit Reference

December 18, 2015 | 1.2 Release

 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Range.hpp
Go to the documentation of this file.
1 /*
2 # Copyright (c) 2014, NVIDIA CORPORATION. All rights reserved.
3 #
4 # Redistribution and use in source and binary forms, with or without
5 # modification, are permitted provided that the following conditions
6 # are met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of NVIDIA CORPORATION nor the names of its
13 # contributors may be used to endorse or promote products derived
14 # from this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef NVXIO_RANGE_HPP
30 #define NVXIO_RANGE_HPP
31 
32 #include <limits>
33 
39 namespace nvxio
40 {
41 
55 template <typename T>
56 struct Range
57 {
58  T low;
59  T high;
60  bool lowInclusive;
68  bool lowConstrained() const;
69 
75  bool highConstrained() const;
76 
81  bool contains(T x) const;
82 };
83 
84 template <typename T>
85 inline bool Range<T>::lowConstrained() const
86 {
87  return low != std::numeric_limits<T>::lowest() || !lowInclusive;
88 }
89 
90 template <typename T>
91 inline bool Range<T>::highConstrained() const
92 {
93  return high != std::numeric_limits<T>::max() || !highInclusive;
94 }
95 
96 template <typename T>
97 inline bool Range<T>::contains(T x) const
98 {
99  bool lowOk = lowInclusive ? x >= low : x > low;
100  bool highOk = highInclusive ? x <= high : x < high;
101  return lowOk && highOk;
102 }
103 
111 template <typename T>
112 inline Range<T> operator & (const Range<T> &r1, const Range<T> &r2)
113 {
114  Range<T> result;
115 
116  if (r1.low < r2.low)
117  {
118  result.low = r2.low;
119  result.lowInclusive = r2.lowInclusive;
120  }
121  else if (r1.low > r2.low)
122  {
123  result.low = r1.low;
124  result.lowInclusive = r1.lowInclusive;
125  }
126  else
127  {
128  result.low = r1.low;
129  result.lowInclusive = r1.lowInclusive && r2.lowInclusive;
130  }
131 
132  if (r1.high < r2.high)
133  {
134  result.high = r1.high;
135  result.highInclusive = r1.highInclusive;
136  }
137  else if (r1.high > r2.high)
138  {
139  result.high = r2.high;
140  result.highInclusive = r2.highInclusive;
141  }
142  else
143  {
144  result.high = r2.high;
145  result.highInclusive = r1.highInclusive && r2.highInclusive;
146  }
147 
148  return result;
149 }
150 
155 namespace ranges
156 {
162 template <typename T>
163 inline Range<T> all() {
164  return { std::numeric_limits<T>::lowest(), std::numeric_limits<T>::max(), true, true };
165 }
166 
173 template <typename T>
174 inline Range<T> lessThan(T x) {
175  return { std::numeric_limits<T>::lowest(), x, true, false };
176 }
177 
184 template <typename T>
185 inline Range<T> moreThan(T x) {
186  return { x, std::numeric_limits<T>::max(), false, true };
187 }
188 
195 template <typename T>
196 inline Range<T> atLeast(T x) {
197  return { x, std::numeric_limits<T>::max(), true, true };
198 }
199 
206 template <typename T>
207 inline Range<T> atMost(T x) {
208  return { std::numeric_limits<T>::lowest(), x, true, true };
209 }
210 
211 }
212 
213 }
214 
215 #endif // NVXIO_RANGE_HPP
Range< T > atMost(T x)
Creates a range that includes the values that are less than or equal to the particular number (values...
Definition: Range.hpp:207
bool highConstrained() const
Determines if the range is right-bounded.
Definition: Range.hpp:91
bool lowConstrained() const
Determines if the range is left-bounded.
Definition: Range.hpp:85
Range< T > all()
Creates a range that includes all points.
Definition: Range.hpp:163
Range< T > operator&(const Range< T > &r1, const Range< T > &r2)
Calculates intersection of the two ranges.
Definition: Range.hpp:112
Range< T > moreThan(T x)
Creates a range that includes the values that are greater than the particular number (values > x)...
Definition: Range.hpp:185
Range< T > lessThan(T x)
Creates a range that includes the values that are less than the particular number (values < x)...
Definition: Range.hpp:174
Range class.
Definition: Range.hpp:56
bool highInclusive
Holds the flag that determines if the range includes the right bound.
Definition: Range.hpp:61
Range< T > atLeast(T x)
Creates a range that includes the values that are greater than or equal to the particular number (val...
Definition: Range.hpp:196
bool contains(T x) const
Determines if the range includes the particular point.
Definition: Range.hpp:97
T low
Holds the left bound of the range.
Definition: Range.hpp:58
T high
Holds the right bound of the range.
Definition: Range.hpp:59
bool lowInclusive
Holds the flag that determines if the range includes the left bound.
Definition: Range.hpp:60