Bayeux  3.4.1
Core Foundation library for SuperNEMO
integer_range.h
Go to the documentation of this file.
1 /* Author(s): Francois Mauger <mauger@lpccaen.in2p3.fr>
3  * Creation date: 2011-09-22
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  * Integer range class.
28  *
29  */
30 
31 #ifndef DATATOOLS_INTEGER_RANGE_H
32 #define DATATOOLS_INTEGER_RANGE_H
33 
34 // Standard Library:
35 #include <iostream>
36 #include <string>
37 #include <set>
38 
39 // Third Party:
40 // - Boost:
41 #include <boost/cstdint.hpp>
42 
43 // This Project:
44 #include <datatools/range_tools.h>
45 
46 namespace datatools {
47 
50  {
51  public:
52 
53  // Intrinsic integer is 32 bits:
54  typedef int32_t value_type;
55 
57  integer_range();
58 
63 
65  bool is_valid() const;
66 
68  void invalidate();
69 
71  void reset_lower();
72 
74  void reset_upper();
75 
77  void reset();
78 
81 
84 
86  void set(value_type from, value_type to,
89 
91  bool is_lower_bounded() const;
92 
94  bool is_upper_bounded() const;
95 
97  bool is_half_bounded() const;
98 
100  bool is_bounded() const;
101 
103  bool is_singleton() const;
104 
106  bool is_lower_included() const;
107 
109  bool is_upper_included() const;
110 
112  value_type get_lower_bound() const;
113 
115  value_type get_upper_bound() const;
116 
118  uint64_t count() const;
119 
121  bool is_empty() const;
122 
123  // Collections of 'make-methods' :
124 
126  // "(0;0)"
127  void make_empty();
128 
130  // "[min_int;max_int]"
131  void make_full();
132 
134  // "[0;max_int]"
135  void make_full_positive();
136 
138  // "[min_int;0]"
139  void make_full_negative();
140 
142  // "[lower;)" or "(lower;)
143  void make_upper_unbounded(value_type from_, bool inclusive_ = true);
144 
146  // "(;upper]" or "(;upper)"
147  void make_lower_unbounded(value_type to_, bool inclusive_ = true);
148 
150  // "(lower;upper]" or "(lower;upper)"
151  // "[lower;upper]" or "[lower;upper)"
152  void make_bounded(value_type from_, value_type to_,
153  bool lower_included_ = true,
154  bool upper_included_ = true);
155 
157  bool has(value_type value) const;
158 
160  bool has(const integer_range & ir) const;
161 
163  void dump(std::ostream& out = std::clog) const;
164 
166  friend std::ostream& operator<<(std::ostream & out,
167  const integer_range & range_);
168 
170  friend std::istream& operator>>(std::istream & in,
171  integer_range & range_);
172 
174  value_type begin() const;
175 
177  value_type end() const;
178 
180  value_type first() const;
181 
183  value_type last() const;
184 
186  int compare(const integer_range& range_) const;
187 
189  bool operator<(const integer_range & range_) const;
190 
192  bool operator>(const integer_range & range_) const;
193 
195  bool operator==(const integer_range & range_) const;
196 
198  void make_canonical(integer_range & range_) const;
199 
201  bool intersect(const integer_range & range_, integer_range & inter_) const;
202 
204  bool intersect(const integer_range & range_) const;
205 
206  private:
207  range_bound_info_type _lower_flag_;
208  value_type _lower_;
209  range_bound_info_type _upper_flag_;
210  value_type _upper_;
211  };
212 
213 } // end of namespace datatools
214 
215 #endif // DATATOOLS_INTEGER_RANGE_H
216 
217 // Local Variables: --
218 // mode: c++ --
219 // c-file-style: "gnu" --
220 // tab-width: 2 --
221 // End: --
friend std::ostream & operator<<(std::ostream &out, const integer_range &range_)
Print operator.
value_type first() const
Return the first value belonging to the half lower bounded interval.
bool intersect(const integer_range &range_, integer_range &inter_) const
Check intersect with another interval and build the resulting intersection interval.
bool is_upper_included() const
Check if upper bound is included.
int32_t value_type
Definition: integer_range.h:54
bool is_bounded() const
Check if both bounds are defined.
bool operator>(const integer_range &range_) const
Comparison operator.
void reset_upper()
Reset upper bound.
bool has(value_type value) const
Check if a value belongs to the interval.
value_type end() const
Return the past-the-end value (not belonging) from the fully bounded interval.
void make_lower_unbounded(value_type to_, bool inclusive_=true)
Build a half bounded interval with no lower bound.
value_type get_upper_bound() const
Return upper bound.
void make_empty()
Build an empty interval.
bool is_upper_bounded() const
Check if upper bound is defined.
range_bound_info_type
Definition: range_tools.h:39
bool is_empty() const
Check if interval is empty.
bool is_singleton() const
Check if the interval is degenerated (one unique value)
void make_full()
Build a fully bounded interval.
void set(value_type from, value_type to, range_bound_info_type from_policy=range_bound_included, range_bound_info_type to_policy=range_bound_included)
Set the bounds.
value_type get_lower_bound() const
Return lower bound.
void set_upper(value_type to_, range_bound_info_type policy_=range_bound_included)
Set the upper bound.
bool operator==(const integer_range &range_) const
Comparison operator.
void make_full_positive()
Build a fully bounded positive interval starting at 0.
bool operator<(const integer_range &range_) const
Comparison operator.
uint64_t count() const
Return the number of values in the interval.
A class representing an interval of integer values.
Definition: integer_range.h:49
void make_full_negative()
Build a fully bounded negative interval ending at 0.
void reset_lower()
Reset lower bound.
value_type last() const
Return the last value belonging to the half upper bounded interval.
bool is_half_bounded() const
Check if the intervalis half-bounded.
bool is_lower_included() const
Check if lower bound is included.
bool is_valid() const
Check the validity of the interval.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
bool is_lower_bounded() const
Check if lower bound is defined.
Bound is set and included in the interval.
Definition: range_tools.h:42
void reset()
Reset the interval.
friend std::istream & operator>>(std::istream &in, integer_range &range_)
Input operator.
void make_bounded(value_type from_, value_type to_, bool lower_included_=true, bool upper_included_=true)
Build a fully bounded interval with specific lower and upper bounds.
value_type begin() const
Return the first value from the fully bounded interval.
void set_lower(value_type from_, range_bound_info_type policy_=range_bound_included)
Set the lower bound.
void make_upper_unbounded(value_type from_, bool inclusive_=true)
Build a half bounded interval with no upper bound.
void dump(std::ostream &out=std::clog) const
Basic print.
void make_canonical(integer_range &range_) const
Build the associated canonical interval (with bounds flagged as included)
integer_range()
Default constructor.
void invalidate()
Invalidate the interval.
int compare(const integer_range &range_) const
Compare intervals.