Jpp  15.0.4
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  typedef JRange<double> JRange_t;
66 
67  const double xmin = -1.0;
68  const double xmax = +1.0;
69  const double dx = (xmax - xmin) / 10;
70 
71  const int WIDTH = 12;
72 
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  {
104  const double x1 = -1.0;
105  const double x2 = +0.5;
106  const double x3 = -0.5;
107  const double x4 = +1.0;
108 
109  JRange_t A(x1, x2);
110  JRange_t B(x3, x4);
111 
112  DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl);
113 
114  ASSERT(overlap(A,B));
115  ASSERT(join(A,B) == JRange_t(x3,x2));
116  }
117 
118  {
119  const double x1 = -1.0;
120  const double x2 = -0.5;
121  const double x3 = +0.5;
122  const double x4 = +1.0;
123 
124  JRange_t A(x1, x2);
125  JRange_t B(x3, x4);
126 
127  DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl);
128 
129  ASSERT(combine(A,B) == JRange_t(x1,x4));
130  }
131 
132  {
133  const double x1 = -1.0;
134  const double x2 = -0.5;
135  const double x3 = +0.5;
136  const double x4 = +1.0;
137 
138  JRange_t A(x1, x2);
139  JRange_t B(x3, x4);
140 
141  DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl);
142 
143  ASSERT((A + B) == JRange_t(x1+x3,x2+x4));
144  }
145 
146  {
147  struct __A__ {
148  __A__() : value(0.0) {}
149  __A__(const double value) : value(value) {}
150 
151  double get() const { return value; }
152 
153  double value;
154  };
155 
156  vector<__A__> buffer;
157 
158  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
159  buffer.push_back(x);
160  }
161 
162  JRange_t A;
163  JRange_t B;
164 
165  A.setRange(make_array(buffer.begin(), buffer.end(), &__A__::value));
166  B.setRange(make_array(buffer.begin(), buffer.end(), &__A__::get));
167 
168  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl);
169  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl);
170 
171  ASSERT(A == JRange_t(xmin,xmax));
172  ASSERT(B == JRange_t(xmin,xmax));
173  }
174 
175  return 0;
176 }
177 
Auxiliary data structure for alignment of data.
Definition: JManip.hh:231
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:681
T get(const JHead &header)
Get object from header.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
JRange< T, JComparator_t > combine(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Combine ranges.
Definition: JRange.hh:698
#define FATAL(A)
Definition: JMessage.hh:67
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
Auxiliary class to define a range between two values.
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
bool overlap(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Test overlap between ranges.
Definition: JRange.hh:663
source $JPP_DIR setenv csh $JPP_DIR eval JShellParser o a A