LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
RealComparisons.h
Go to the documentation of this file.
1 
11 #ifndef LARCORE_COREUTILS_REALCOMPARISONS_H
12 #define LARCORE_COREUTILS_REALCOMPARISONS_H
13 
14 // C/C++ standard libraries
15 #include <cmath> // std::abs()
16 #include <utility> // std::move()
17 
18 namespace lar::util {
19 
63  template <typename RealType>
64  struct RealComparisons {
65  using Value_t = RealType;
66 
68  constexpr RealComparisons(Value_t threshold) : threshold(threshold) {}
69 
71  constexpr bool zero(Value_t value) const { return std::abs(value) <= threshold; }
72 
74  constexpr bool nonZero(Value_t value) const { return !zero(value); }
75 
77  constexpr bool equal(Value_t a, Value_t b) const { return zero(a - b); }
78 
80  constexpr bool nonEqual(Value_t a, Value_t b) const { return !equal(a, b); }
81 
83  constexpr bool strictlyNegative(Value_t value) const { return value < -threshold; }
84 
86  constexpr bool strictlyPositive(Value_t value) const { return value > threshold; }
87 
89  constexpr bool nonNegative(Value_t value) const { return value >= -threshold; }
90 
92  constexpr bool nonPositive(Value_t value) const { return value <= threshold; }
93 
95  constexpr bool strictlySmaller(Value_t a, Value_t b) const { return strictlyNegative(a - b); }
96 
98  constexpr bool nonSmaller(Value_t a, Value_t b) const { return nonNegative(a - b); }
99 
101  constexpr bool strictlyGreater(Value_t a, Value_t b) const { return strictlyPositive(a - b); }
102 
104  constexpr bool nonGreater(Value_t a, Value_t b) const { return nonPositive(a - b); }
105 
107  constexpr bool within(Value_t value, Value_t lower, Value_t upper) const
108  {
109  return nonNegative(value - lower) && nonPositive(value - upper);
110  }
111 
113  constexpr bool withinSorted(Value_t value, Value_t lower, Value_t upper) const
114  {
115  return (lower < upper) ? within(value, lower, upper) : within(value, upper, lower);
116  } // sortedWithin
117 
119 
120  }; // struct RealComparisons<>
121 
122  //--------------------------------------------------------------------------
124  template <typename RealType>
126 
128 
130  constexpr Vector2DComparison(Comp_t const& comparer) : comparer(comparer) {}
131 
133  Vector2DComparison(Comp_t&& comparer) : comparer(std::move(comparer)) {}
134 
136  constexpr Vector2DComparison(RealType threshold) : comparer(threshold) {}
137 
139  constexpr Comp_t comp() const { return comparer; }
140 
142  template <typename Vect>
143  constexpr bool zero(Vect const& v) const
144  {
145  return comp().zero(v.X()) && comp().zero(v.Y());
146  }
147 
149  template <typename Vect>
150  constexpr bool nonZero(Vect const& v) const
151  {
152  return !zero(v);
153  }
154 
156  template <typename VectA, typename VectB>
157  constexpr bool equal(VectA const& a, VectB const& b) const
158  {
159  return comp().equal(a.X(), b.X()) && comp().equal(a.Y(), b.Y());
160  }
161 
163  template <typename VectA, typename VectB>
164  constexpr bool nonEqual(VectA const& a, VectB const& b) const
165  {
166  return !equal(a, b);
167  }
168 
169  private:
170  Comp_t const comparer;
171 
172  }; // struct Vector2DComparison
173 
174  //--------------------------------------------------------------------------
176  template <typename RealType>
178  {
180  }
181 
183  template <typename RealType>
185  {
186  return Vector2DComparison<RealType>(comp);
187  }
188 
189  //--------------------------------------------------------------------------
191  template <typename RealType>
193 
196 
199 
201  constexpr Vector3DComparison(Comp_t const& comparer) : comparer(comparer) {}
202 
204  Vector3DComparison(Comp_t&& comparer) : comparer(std::move(comparer)) {}
205 
207  constexpr Vector3DComparison(RealType threshold) : comparer(threshold) {}
208 
210  constexpr Comp_t comp() const { return comp2D().comp(); }
211 
213  constexpr Comp2D_t comp2D() const { return comparer; }
214 
216  template <typename Vect>
217  constexpr bool zero(Vect const& v) const
218  {
219  return comp2D().zero(v) && comp().zero(v.Z());
220  }
221 
223  template <typename Vect>
224  constexpr bool nonZero(Vect const& v) const
225  {
226  return !zero(v);
227  }
228 
230  template <typename VectA, typename VectB>
231  constexpr bool equal(VectA const& a, VectB const& b) const
232  {
233  return comp2D().equal(a, b) && comp().equal(a.Z(), b.Z());
234  }
235 
237  template <typename VectA, typename VectB>
238  constexpr bool nonEqual(VectA const& a, VectB const& b) const
239  {
240  return !equal(a, b);
241  }
242 
243  private:
245 
246  }; // struct Vector3DComparison
247 
248  //--------------------------------------------------------------------------
250  template <typename RealType>
252  {
254  }
255 
257  template <typename RealType>
259  {
260  return Vector3DComparison<RealType>(comp);
261  }
262 
263  //--------------------------------------------------------------------------
264 
265 } // namespace lar::util
266 
267 #endif // LARCORE_COREUTILS_REALCOMPARISONS_H
constexpr Vector3DComparison(Comp_t const &comparer)
Copy the specified comparison.
constexpr bool nonEqual(Value_t a, Value_t b) const
Returns whether a and b are farther than the threshold.
constexpr bool zero(Vect const &v) const
Returns whether the specified vector is null (within tolerance).
Class comparing 2D vectors.
Provides simple real number checks.
constexpr Comp_t comp() const
Returns the basic value comparer.
constexpr bool zero(Value_t value) const
Returns whether the value is no farther from 0 than the threshold.
auto makeVector2DComparison(RealType threshold)
Creates a Vector2DComparison from a RealComparisons object.
constexpr auto abs(T v)
Returns the absolute value of the argument.
STL namespace.
Comp_t const comparer
Comparison object.
constexpr Vector3DComparison(RealType threshold)
Use the specified threshold.
constexpr bool equal(VectA const &a, VectB const &b) const
Returns whether the specified vectors match (within tolerance).
constexpr bool withinSorted(Value_t value, Value_t lower, Value_t upper) const
Returns whether value is between bounds (included); bounds are sorted.
constexpr bool nonEqual(VectA const &a, VectB const &b) const
Returns whether the specified vectors do not match (within tolerance).
constexpr RealComparisons(Value_t threshold)
type of values being compered
constexpr bool strictlySmaller(Value_t a, Value_t b) const
Returns whether a is strictly smaller than b.
constexpr bool strictlyGreater(Value_t a, Value_t b) const
Returns whether a is strictly greater than b.
auto makeVector3DComparison(RealType threshold)
Creates a Vector3DComparison from a RealComparisons object.
constexpr Vector2DComparison(Comp_t const &comparer)
Copy the specified comparison.
Vector2DComparison(Comp_t &&comparer)
Steal the specified comparison.
Class comparing 2D vectors.
LArSoft utility namespace.
Definition: NumericUtils.cxx:6
constexpr bool strictlyNegative(Value_t value) const
Returns whether value is larger than zero beyond tolerance.
constexpr bool nonGreater(Value_t a, Value_t b) const
Returns whether a is smaller than (or equal to) b.
double value
Definition: spectrum.C:18
constexpr Comp_t comp() const
Returns the base value comparer.
constexpr bool strictlyPositive(Value_t value) const
Returns whether value is smaller than zero beyond tolerance.
Vector3DComparison(Comp_t &&comparer)
Steal the specified comparison.
constexpr Vector2DComparison(RealType threshold)
Use the specified threshold.
constexpr bool nonEqual(VectA const &a, VectB const &b) const
Returns whether the specified vectors do not match (within tolerance).
constexpr bool nonNegative(Value_t value) const
Returns whether value is larger than or equal() to zero.
constexpr bool nonZero(Value_t value) const
Returns whether the value is farther from 0 than the threshold.
constexpr bool zero(Vect const &v) const
Returns whether the specified vector is null (within tolerance).
constexpr bool nonZero(Vect const &v) const
Returns whether the specified vector is not null (within tolerance).
constexpr bool nonSmaller(Value_t a, Value_t b) const
Returns whether a is greater than (or equal to) b.
constexpr Comp2D_t comp2D() const
Returns the 2D vector comparer.
constexpr bool equal(Value_t a, Value_t b) const
Returns whether a and b are no farther than the threshold.
constexpr bool nonPositive(Value_t value) const
Returns whether value is smaller than or equal() to zero.
constexpr bool within(Value_t value, Value_t lower, Value_t upper) const
Returns whether value is between the bounds (included)
constexpr bool equal(VectA const &a, VectB const &b) const
Returns whether the specified vectors match (within tolerance).
constexpr bool nonZero(Vect const &v) const
Returns whether the specified vector is not null (within tolerance).