LArSoft  v09_90_00
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 {
19  namespace util {
20 
64  template <typename RealType>
65  struct RealComparisons {
66  using Value_t = RealType;
67 
69  constexpr RealComparisons(Value_t threshold) : threshold(threshold) {}
70 
72  constexpr bool zero(Value_t value) const { return std::abs(value) <= threshold; }
73 
75  constexpr bool nonZero(Value_t value) const { return !zero(value); }
76 
78  constexpr bool equal(Value_t a, Value_t b) const { return zero(a - b); }
79 
81  constexpr bool nonEqual(Value_t a, Value_t b) const { return !equal(a, b); }
82 
84  constexpr bool strictlyNegative(Value_t value) const { return value < -threshold; }
85 
87  constexpr bool strictlyPositive(Value_t value) const { return value > threshold; }
88 
90  constexpr bool nonNegative(Value_t value) const { return value >= -threshold; }
91 
93  constexpr bool nonPositive(Value_t value) const { return value <= threshold; }
94 
96  constexpr bool strictlySmaller(Value_t a, Value_t b) const { return strictlyNegative(a - b); }
97 
99  constexpr bool nonSmaller(Value_t a, Value_t b) const { return nonNegative(a - b); }
100 
102  constexpr bool strictlyGreater(Value_t a, Value_t b) const { return strictlyPositive(a - b); }
103 
105  constexpr bool nonGreater(Value_t a, Value_t b) const { return nonPositive(a - b); }
106 
108  constexpr bool within(Value_t value, Value_t lower, Value_t upper) const
109  {
110  return nonNegative(value - lower) && nonPositive(value - upper);
111  }
112 
114  constexpr bool withinSorted(Value_t value, Value_t lower, Value_t upper) const
115  {
116  return (lower < upper) ? within(value, lower, upper) : within(value, upper, lower);
117  } // sortedWithin
118 
120 
121  }; // struct RealComparisons<>
122 
123  //--------------------------------------------------------------------------
125  template <typename RealType>
127 
129 
131  constexpr Vector2DComparison(Comp_t const& comparer) : comparer(comparer) {}
132 
134  Vector2DComparison(Comp_t&& comparer) : comparer(std::move(comparer)) {}
135 
137  constexpr Vector2DComparison(RealType threshold) : comparer(threshold) {}
138 
140  constexpr Comp_t comp() const { return comparer; }
141 
143  template <typename Vect>
144  constexpr bool zero(Vect const& v) const
145  {
146  return comp().zero(v.X()) && comp().zero(v.Y());
147  }
148 
150  template <typename Vect>
151  constexpr bool nonZero(Vect const& v) const
152  {
153  return !zero(v);
154  }
155 
157  template <typename VectA, typename VectB>
158  constexpr bool equal(VectA const& a, VectB const& b) const
159  {
160  return comp().equal(a.X(), b.X()) && comp().equal(a.Y(), b.Y());
161  }
162 
164  template <typename VectA, typename VectB>
165  constexpr bool nonEqual(VectA const& a, VectB const& b) const
166  {
167  return !equal(a, b);
168  }
169 
170  private:
171  Comp_t const comparer;
172 
173  }; // struct Vector2DComparison
174 
175  //--------------------------------------------------------------------------
177  template <typename RealType>
179  {
181  }
182 
184  template <typename RealType>
186  {
187  return Vector2DComparison<RealType>(comp);
188  }
189 
190  //--------------------------------------------------------------------------
192  template <typename RealType>
194 
197 
200 
202  constexpr Vector3DComparison(Comp_t const& comparer) : comparer(comparer) {}
203 
205  Vector3DComparison(Comp_t&& comparer) : comparer(std::move(comparer)) {}
206 
208  constexpr Vector3DComparison(RealType threshold) : comparer(threshold) {}
209 
211  constexpr Comp_t comp() const { return comp2D().comp(); }
212 
214  constexpr Comp2D_t comp2D() const { return comparer; }
215 
217  template <typename Vect>
218  constexpr bool zero(Vect const& v) const
219  {
220  return comp2D().zero(v) && comp().zero(v.Z());
221  }
222 
224  template <typename Vect>
225  constexpr bool nonZero(Vect const& v) const
226  {
227  return !zero(v);
228  }
229 
231  template <typename VectA, typename VectB>
232  constexpr bool equal(VectA const& a, VectB const& b) const
233  {
234  return comp2D().equal(a, b) && comp().equal(a.Z(), b.Z());
235  }
236 
238  template <typename VectA, typename VectB>
239  constexpr bool nonEqual(VectA const& a, VectB const& b) const
240  {
241  return !equal(a, b);
242  }
243 
244  private:
246 
247  }; // struct Vector3DComparison
248 
249  //--------------------------------------------------------------------------
251  template <typename RealType>
253  {
255  }
256 
258  template <typename RealType>
260  {
261  return Vector3DComparison<RealType>(comp);
262  }
263 
264  //--------------------------------------------------------------------------
265 
266  } // namespace util
267 } // namespace lar
268 
269 #endif // LARCORE_COREUTILS_REALCOMPARISONS_H
constexpr Vector3DComparison(Comp_t const &comparer)
Copy the specified comparison.
Namespace for general, non-LArSoft-specific utilities.
Definition: PIDAAlg.h:26
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.
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.
LArSoft-specific namespace.
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).