Jpp
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 
49  int debug;
50 
51  try {
52 
53  JParser<> zap("Example program to test range operations.");
54 
55  zap['d'] = make_field(debug) = 3;
56 
57  zap(argc, argv);
58  }
59  catch(const exception &error) {
60  FATAL(error.what() << endl);
61  }
62 
63 
64  typedef JTOOLS::JRange<double> JRange_t;
65 
66  const double xmin = -1.0;
67  const double xmax = +1.0;
68  const double dx = (xmax - xmin) / 10;
69 
70  const int WIDTH = 12;
71 
72  {
73  JRange_t A(JRange_t::DEFAULT_RANGE);
74  JRange_t B(xmin, xmax);
75 
76  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
77 
78  DEBUG(setw(WIDTH) << left << "inside" << ' ' << B << ' ' << setw(5) << x << ' ' << (B(x) ? "Y" : "N") << endl);
79 
80  ASSERT(B(x) == (x >= xmin && x <= xmax));
81 
82  A.include(x);
83  }
84 
85  DEBUG(setw(WIDTH) << left << "range" << ' ' << A << endl);
86 
87  ASSERT(A == B);
88 
89 
90  for (double x = xmin - (xmax - xmin); x < xmax + (xmax - xmin) + 0.5 * dx; x += dx) {
91 
92  double x1 = x;
93 
94  while (x1 <= xmin) { x1 += (xmax - xmin); }
95  while (x1 > xmax) { x1 -= (xmax - xmin); }
96 
97  ASSERT(x1 == B.mod(x));
98  }
99  }
100 
101 
102  {
103  const double x1 = -1.0;
104  const double x2 = +0.5;
105  const double x3 = -0.5;
106  const double x4 = +1.0;
107 
108  JRange_t A(x1, x2);
109  JRange_t B(x3, x4);
110 
111  DEBUG(setw(WIDTH) << left << "join" << ' ' << A << ' ' << B << " = " << join(A,B) << endl);
112 
113  ASSERT(overlap(A,B));
114  ASSERT(join(A,B) == JRange_t(x3,x2));
115  }
116 
117  {
118  const double x1 = -1.0;
119  const double x2 = -0.5;
120  const double x3 = +0.5;
121  const double x4 = +1.0;
122 
123  JRange_t A(x1, x2);
124  JRange_t B(x3, x4);
125 
126  DEBUG(setw(WIDTH) << left << "combine" << ' ' << A << ' ' << B << " = " << combine(A,B) << endl);
127 
128  ASSERT(combine(A,B) == JRange_t(x1,x4));
129  }
130 
131  {
132  const double x1 = -1.0;
133  const double x2 = -0.5;
134  const double x3 = +0.5;
135  const double x4 = +1.0;
136 
137  JRange_t A(x1, x2);
138  JRange_t B(x3, x4);
139 
140  DEBUG(setw(WIDTH) << left << "add" << ' ' << A << ' ' << B << " = " << A + B << endl);
141 
142  ASSERT((A + B) == JRange_t(x1+x3,x2+x4));
143  }
144 
145  {
146  struct __A__ {
147  __A__() : value(0.0) {}
148  __A__(const double value) : value(value) {}
149 
150  double get() const { return value; }
151 
152  double value;
153  };
154 
155  vector<__A__> buffer;
156 
157  for (double x = xmin; x < xmax + 0.5 * dx; x += dx) {
158  buffer.push_back(x);
159  }
160 
161  JRange_t A;
162  JRange_t B;
163 
164  A.setRange(buffer.begin(), buffer.end(), &__A__::value);
165  B.setRange(buffer.begin(), buffer.end(), &__A__::get);
166 
167  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << A << endl);
168  DEBUG(setw(WIDTH) << left << "setRange" << ' ' << B << endl);
169 
170  ASSERT(A == JRange_t(xmin,xmax));
171  ASSERT(B == JRange_t(xmin,xmax));
172  }
173 
174  return 0;
175 }
176 
JMessage.hh
ASSERT
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
JTOOLS::JPair::first
JKey_t first
Definition: JPair.hh:128
main
int main(int argc, char **argv)
Definition: JRange.cc:45
std::vector
Definition: JSTDTypes.hh:12
JTOOLS::JRange< double >
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JTOOLS::join
JRange< T, JComparator_t > join(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Join ranges.
Definition: JRange.hh:671
JTOOLS::overlap
bool overlap(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Test overlap between ranges.
Definition: JRange.hh:653
JRange.hh
debug
int debug
debug level
Definition: JSirene.cc:59
WIDTH
Auxiliary data structure for alignment of data.
Definition: JPrint.hh:267
operator<<
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Definition: clb_common_header.hh:72
JTOOLS::combine
JRange< T, JComparator_t > combine(const JRange< T, JComparator_t > &first, const JRange< T, JComparator_t > &second)
Combine ranges.
Definition: JRange.hh:688
JParser.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
JAANET::get
T get(const JHead &header)
Get object from header.
Definition: JHeadToolkit.hh:295
JTOOLS::JPair::second
JValue_t second
Definition: JPair.hh:129
FATAL
#define FATAL(A)
Definition: JMessage.hh:67