Bayeux  3.4.1
Core Foundation library for SuperNEMO
real_range.h
Go to the documentation of this file.
1 /* Author(s): Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2011-09-25
4  * Last modified: 2014-08-31
5  *
6  * License:
7  *
8  * Copyright (C) 2011-2014 Francois Mauger <mauger@lpccaen.in2p3.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or (at
13  * your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
23  * Boston, MA 02110-1301, USA.
24  *
25  * Description:
26  *
27  * Real range/interval class.
28  *
29  */
30 
31 #ifndef DATATOOLS_REAL_RANGE_H
32 #define DATATOOLS_REAL_RANGE_H
33 
34 // Standard Library:
35 #include <iostream>
36 #include <string>
37 #include <set>
38 
39 // This Project:
40 #include <datatools/range_tools.h>
41 
42 namespace datatools {
43 
45  class real_range
46  {
47  public:
48 
50  real_range();
51 
53  real_range(double from_, double to_,
56 
58  bool is_valid() const;
59 
61  void invalidate();
62 
64  void reset_lower();
65 
67  void reset_upper();
68 
70  void reset();
71 
73  void set_lower(double from_, range_bound_info_type policy_ = range_bound_included);
74 
76  void set_upper(double to_, range_bound_info_type policy_ = range_bound_included);
77 
79  void set(double from_,
80  double to_,
83 
85  bool is_lower_bounded() const;
86 
88  bool is_upper_bounded() const;
89 
91  bool is_half_bounded() const;
92 
94  bool is_bounded() const;
95 
97  bool is_lower_included() const;
98 
100  bool is_upper_included() const;
101 
103  double get_lower() const;
104 
106  double get_upper() const;
107 
109  double width(double tolerance_ = -1.0) const;
110 
112  bool is_empty(double tolerance_ = -1.0) const;
113 
115  bool is_singleton(double tolerance_ = -1.0) const;
116 
118  double singleton(double tolerance_ = -1.0) const;
119 
120  // Collections of 'make' methods :
121 
123  // "[2;2]"
124  void make_singleton(double);
125 
127  // "(0;0)"
128  void make_empty();
129 
131  // "[0;)"
133 
135  // "(;0]"
137 
139  // "[lower;)" or "(lower;[
140  void make_upper_unbounded(double from_, bool inclusive_ = true);
141 
143  // "(;upper]" or "(;upper)"
144  void make_lower_unbounded(double to_, bool inclusive_ = true);
145 
147  // "(;)"
148  void make_unbounded();
149 
151  // "(lower;upper]" or "(lower;upper)"
152  // "[lower;upper]" or "[lower;upper)"
153  void make_bounded(double from_, double to_,
154  bool lower_included_ = true,
155  bool upper_included_ = true);
156 
158  bool has(double value_, double tolerance_ = -1.0) const;
159 
161  bool has(const real_range & rr_, double tolerance_ = -1.0) const;
162 
164  void dump(std::ostream & out_ = std::clog, double tolerance_ = -1.0) const;
165 
167  double first(double tolerance_ = -1.0) const;
168 
170  double last(double tolerance_ = -1.0) const;
171 
173  bool has_unit_label() const;
174 
176  void set_unit_label(const std::string &);
177 
179  void reset_unit_label();
180 
182  const std::string & get_unit_label() const;
183 
185  bool has_preferred_unit() const;
186 
188  void reset_preferred_unit();
189 
191  void set_preferred_unit(const std::string & pus_);
192 
194  const std::string & get_preferred_unit() const;
195 
197  friend std::ostream & operator<<(std::ostream & out_,
198  const real_range & range_);
199 
201  friend std::istream & operator>>(std::istream & in_,
202  real_range & range_);
203 
205  static double compute_tolerance(double lower_, double upper_,
206  double relative_tolerance_ = -1.0);
207 
209  int compare(const real_range & range_) const;
210 
212  bool operator<(const real_range & range_) const;
213 
215  bool operator>(const real_range & range_) const;
216 
218  bool operator==(const real_range & range_) const;
219 
220  protected:
221 
223  double _effective_tolerance(double tolerance_) const;
224 
226  void _set_defaults();
227 
228  private:
229 
230  std::string _unit_label_;
231  std::string _preferred_unit_;
232  range_bound_info_type _lower_flag_;
233  double _lower_;
234  range_bound_info_type _upper_flag_;
235  double _upper_;
236  };
237 
238 } // end of namespace datatools
239 
240 #endif // DATATOOLS_REAL_RANGE_H
241 
242 // Local Variables: --
243 // mode: c++ --
244 // c-file-style: "gnu" --
245 // tab-width: 2 --
246 // End: --
void make_negative_unbounded()
Build a fully bounded negative interval ending at 0.
double first(double tolerance_=-1.0) const
Return the first value belonging to the half lower bounded interval.
void set(double from_, double to_, range_bound_info_type from_policy_=range_bound_included, range_bound_info_type to_policy_=range_bound_included)
Set the bounds.
bool is_upper_included() const
Check if upper bound is included.
double get_lower() const
Return lower bound.
bool is_valid() const
Check the validity of the interval.
double last(double tolerance_=-1.0) const
Return the last value belonging to the half upper bounded interval.
void make_lower_unbounded(double to_, bool inclusive_=true)
Build a half bounded interval with no lower bound.
void dump(std::ostream &out_=std::clog, double tolerance_=-1.0) const
Basic print.
void _set_defaults()
Set default attribute values.
void set_preferred_unit(const std::string &pus_)
Set the preferred unit symbol.
bool has_preferred_unit() const
Check if the preferred unit label is set.
range_bound_info_type
Definition: range_tools.h:39
bool is_singleton(double tolerance_=-1.0) const
Check if the interval is degenerated (one unique value)
const std::string & get_preferred_unit() const
Reset the preferred unit symbol.
void set_upper(double to_, range_bound_info_type policy_=range_bound_included)
Set the upper bound.
bool is_upper_bounded() const
Check if upper bound is defined.
void reset_unit_label()
Reset the unit label.
void reset_lower()
Reset lower bound.
void reset_upper()
Reset upper bound.
void set_unit_label(const std::string &)
Set the unit label.
bool is_empty(double tolerance_=-1.0) const
Check if interval is empty.
void make_bounded(double from_, double to_, bool lower_included_=true, bool upper_included_=true)
Build a fully bounded interval with specific lower and upper bounds.
void make_singleton(double)
Build a singleton.
bool has_unit_label() const
Check if unit label is set.
bool operator>(const real_range &range_) const
Comparison operator.
real_range()
Default constructor.
bool has(double value_, double tolerance_=-1.0) const
Check if a value belongs to the interval.
double singleton(double tolerance_=-1.0) const
Return the singleton value.
void reset_preferred_unit()
Reset the preferred unit symbol.
friend std::ostream & operator<<(std::ostream &out_, const real_range &range_)
Print operator using the ISO_31-11 standard.
void make_empty()
Build an empty interval.
bool is_bounded() const
Check if both bounds are defined.
bool operator==(const real_range &range_) const
Comparison operator.
void make_positive_unbounded()
Build a fully bounded positive interval starting at 0.
void make_unbounded()
Build a full unbounded interval with no lower or upper bounds.
double width(double tolerance_=-1.0) const
Return the width of the interval.
bool operator<(const real_range &range_) const
Comparison operator.
void reset()
Reset the interval.
friend std::istream & operator>>(std::istream &in_, real_range &range_)
Input operator using the ISO_31-11 standard.
A class representing an interval of real (double precision) values with support of embedded units.
Definition: real_range.h:45
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
bool is_lower_included() const
Check if lower bound is included.
double _effective_tolerance(double tolerance_) const
Return an effective absolute tolerance.
Bound is set and included in the interval.
Definition: range_tools.h:42
double get_upper() const
Return upper bound.
int compare(const real_range &range_) const
Compare intervals.
const std::string & get_unit_label() const
Return the unit label.
void set_lower(double from_, range_bound_info_type policy_=range_bound_included)
Set the lower bound.
static double compute_tolerance(double lower_, double upper_, double relative_tolerance_=-1.0)
Given a relative tolerance, compute the absolute tolerance associated to bounds.
bool is_half_bounded() const
Check if the intervalis half-bounded.
void invalidate()
Invalidate the interval.
void make_upper_unbounded(double from_, bool inclusive_=true)
Build a half bounded interval with no upper bound.
bool is_lower_bounded() const
Check if lower bound is defined.