Jpp  master_rocky-43-ge265d140c
the software that should make you happy
JRange.cc
Go to the documentation of this file.
1 
2 #include <string>
3 #include <iostream>
4 #include <iomanip>
5 
6 #include "JTools/JRange.hh"
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 
11 
12 namespace {
13 
14  /**
15  * Write range to output.
16  *
17  * \param out output stream
18  * \param range range
19  * \return output stream
20  */
21  inline std::ostream& operator<<(std::ostream& out, const JTOOLS::JRange<double>& range)
22  {
23  using namespace std;
24 
25  out << fixed << right << showpos << setprecision(1)
26  << "["
27  << setw(5)
28  << range.first
29  << ","
30  << setw(5)
31  << range.second
32  << "]";
33 
34  return out;
35  }
36 }
37 
38 
39 /**
40  * \file
41  *
42  * Example program to test JTOOLS::JRange operations.
43  * \author mdejong
44  */
45 int main(int argc, char **argv)
46 {
47  using namespace std;
48  using namespace JPP;
49 
50  int debug;
51 
52  try {
53 
54  JParser<> zap("Example program to test range operations.");
55 
56  zap['d'] = make_field(debug) = 3;
57 
58  zap(argc, argv);
59  }
60  catch(const exception &error) {
61  FATAL(error.what() << endl);
62  }
63 
64 
65  const double xmin = -1.0;
66  const double xmax = +1.0;
67  const double dx = (xmax - xmin) / 10;
68 
69  const int WIDTH = 12;
70 
71  {
72  typedef JRange<double> JRange_t;
73 
74  JRange_t A(JRange_t::DEFAULT_RANGE());
75  JRange_t B(xmin, xmax);
76 
77  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
78 
79  DEBUG(setw(WIDTH) << left << "inside" << ' ' << B << ' ' << setw(5) << x << ' ' << (B(x) ? "Y" : "N") << endl);
80 
81  ASSERT(B(x) == (x >= xmin && x <= xmax));
82 
83  A.include(x);
84  }
85 
86  DEBUG(setw(WIDTH) << left << "range" << ' ' << A << endl);
87 
88  ASSERT(A == B);
89 
90 
91  for (double x = xmin - (xmax - xmin); x < xmax + (xmax - xmin) + 0.5 * dx; x += dx) {
92 
93  double x1 = x;
94 
95  while (x1 <= xmin) { x1 += (xmax - xmin); }
96  while (x1 > xmax) { x1 -= (xmax - xmin); }
97 
98  ASSERT(x1 == B.mod(x));
99  }
100  }
101 
102  {
103  typedef JRange<double> JRange_t;
104 
105  const double x1 = -1.0;
106  const double x2 = +0.5;
107  const double x3 = -0.5;
108  const double x4 = +1.0;
109 
110  JRange_t A(x1, x2);
111  JRange_t B(x3, x4);
112 
113  DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl);
114 
115  ASSERT(overlap(A,B));
116  ASSERT(join(A,B) == JRange_t(x3,x2));
117  }
118 
119  {
120  typedef JRange<double> JRange_t;
121 
122  const double x1 = -1.0;
123  const double x2 = -0.5;
124  const double x3 = +0.5;
125  const double x4 = +1.0;
126 
127  JRange_t A(x1, x2);
128  JRange_t B(x3, x4);
129 
130  DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl);
131 
132  ASSERT(combine(A,B) == JRange_t(x1,x4));
133  }
134 
135  {
136  typedef JRange<double> JRange_t;
137 
138  const double x1 = -1.0;
139  const double x2 = -0.5;
140  const double x3 = +0.5;
141  const double x4 = +1.0;
142 
143  JRange_t A(x1, x2);
144  JRange_t B(x3, x4);
145 
146  DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl);
147 
148  ASSERT((A + B) == JRange_t(x1+x3,x2+x4));
149  }
150 
151  {
152  typedef JRange<double> JRange_t;
153 
154  struct __A__ {
155  __A__() : value(0.0) {}
156  __A__(const double value) : value(value) {}
157 
158  double get() const { return value; }
159 
160  double value;
161  };
162 
163  vector<__A__> buffer;
164 
165  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
166  buffer.push_back(x);
167  }
168 
169  JRange_t A;
170  JRange_t B;
171 
172  A.setRange(make_array(buffer.begin(), buffer.end(), &__A__::value));
173  B.setRange(make_array(buffer.begin(), buffer.end(), &__A__::get));
174 
175  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl);
176  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl);
177 
178  ASSERT(A == JRange_t(xmin,xmax));
179  ASSERT(B == JRange_t(xmin,xmax));
180  }
181 
182  {
183  typedef JRange<int> JRange_t;
184 
185  JRange_t A(1,100);
188 
189  ASSERT(overlap(A,B));
190  ASSERT(overlap(A,C));
191  ASSERT(!overlap(B,C));
192  }
193 
194  return 0;
195 }
196 
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
int main(int argc, char **argv)
Definition: JRange.cc:45
Auxiliary class to define a range between two values.
Utility class to parse command line options.
Definition: JParser.hh:1698
JKey_t first
Definition: JPair.hh:128
JValue_t second
Definition: JPair.hh:129
range_type & include(argument_type x)
Include given value to range.
Definition: JRange.hh:397
void setRange(const range_type &range)
Set range.
Definition: JRange.hh:146
T getLowerLimit() const
Get lower limit.
Definition: JRange.hh:202
T mod(argument_type x) const
Modulo value with respect to range.
Definition: JRange.hh:365
T getUpperLimit() const
Get upper limit.
Definition: JRange.hh:213
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
const double xmax
Definition: JQuadrature.cc:24
const double xmin
Definition: JQuadrature.cc:23
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
static const double C
Physics constants.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
bool overlap(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Test overlap between ranges.
Definition: JRange.hh:641
JRange< T, JComparator_t > combine(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Combine ranges.
Definition: JRange.hh:676
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:659
Definition: JSTDTypes.hh:14
Type definition of range.
Definition: JHead.hh:43
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231