Jpp  18.0.0-rc.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JArray.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <iterator>
5 #include <cmath>
6 
7 #include "JTools/JArray.hh"
8 #include "JTools/JMultiKey.hh"
9 
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 
13 
14 /**
15  * \file
16  *
17  * Example program to test JTOOLS::JArray class.
18  * \author mdejong
19  */
20 int main(int argc, char **argv)
21 {
22  using namespace std;
23  using namespace JPP;
24 
25  int debug;
26 
27  try {
28 
29  JParser<> zap("Example program to test fixed length array class.");
30 
31  zap['d'] = make_field(debug) = 3;
32 
33  zap(argc, argv);
34  }
35  catch(const exception &error) {
36  FATAL(error.what() << endl);
37  }
38 
39 
40  {
41  //JArray<3, int> array(1, 2); // generates compiler error
42  //JArray<3, int> array(1, 2, 3, 4); // generates compiler error
43  //JArray<3, int> array(1, 2, "abc"); // generates compiler error
44  }
45 
46  {
47  const int i0 = 1;
48 
49  JArray<1, int> array(i0);
50 
51  ASSERT(i0 == array[0]);
52  }
53 
54  {
55  const int i0 = 1;
56  const int i1 = 2;
57 
58  JArray<2, int> array(i0, i1);
59 
60  ASSERT(i0 == array[0]);
61  ASSERT(i1 == array[1]);
62  }
63 
64 
65  {
66  const int N = 9;
67 
68  typedef JArray<N, int> JArray1_t;
69  typedef JArray<N-1, int> JArray2_t;
70 
71  JArray1_t array;
72 
73  for (JArray1_t::iterator i = array.begin(); i != array.end(); ++i) {
74  *i = distance(array.begin(),i);
75  }
76 
77  if (debug >= debug_t) {
78  cout << "input A ";
79  copy(array.begin(), array.end(), ostream_iterator<int>(cout, " "));
80  cout << endl;
81  }
82 
83  {
84  JArray1_t buffer = array + array;
85 
86  for (int i = 0; i != N; ++i) {
87  ASSERT(2*array[i] == buffer[i]);
88  }
89  }
90 
91  {
92  JArray2_t buffer = array.pop_front();
93 
94  if (debug >= debug_t) {
95  cout << "pop_front ";
96  copy(buffer.begin(), buffer.end(), ostream_iterator<int>(cout, " "));
97  cout << endl;
98  }
99 
100  for (int i = 0; i != N-1; ++i) {
101  ASSERT(array[i+1] == buffer[i]);
102  }
103  }
104 
105  {
106  JArray1_t buffer(array.pop_back(), array[N-1]);
107 
108  ASSERT(array == buffer);
109  }
110 
111  {
112  JArray2_t buffer = array.pop_back();
113 
114  if (debug >= debug_t) {
115  cout << "pop_back ";
116  copy(buffer.begin(), buffer.end(), ostream_iterator<int>(cout, " "));
117  cout << endl;
118  }
119 
120  for (int i = 0; i != N-1; ++i) {
121  ASSERT(array[i] == buffer[i]);
122  }
123  }
124  }
125 
126  {
127  JArray<3, int> array(1, 2, 3);
128 
129  JArray<3, const int> p3(array);
130 
131  if (debug >= debug_t) {
132  copy(p3.begin(), p3.end(), ostream_iterator<int>(cout, " "));
133  cout << endl;
134  }
135 
136  ASSERT(array[0] == p3[0]);
137 
138  JArray<2, const int> p2(p3.pop_front());
139 
140  if (debug >= debug_t) {
141  copy(p2.begin(), p2.end(), ostream_iterator<int>(cout, " "));
142  cout << endl;
143  }
144 
145  ASSERT(array[1] == p2[0]);
146 
147  JArray<1, const int> p1(p2.pop_front());
148 
149  if (debug >= debug_t) {
150  copy(p1.begin(), p1.end(), ostream_iterator<int>(cout, " "));
151  cout << endl;
152  }
153 
154  ASSERT(array[2] == p1[0]);
155 
156  //JArray<0, const int> p0(p1.pop_front()); // compile error, as should be
157  }
158 
159  {
160  JMultiKey<2, int> key;
161 
162  key.first = 0;
163  key.second.first = 1;
164 
165  int i = 2;
166 
167  JArray<3, int> array(key, i);
168 
169  if (debug >= debug_t) {
170  copy(array.begin(), array.end(), ostream_iterator<int>(cout, " "));
171  cout << endl;
172  }
173 
174  ASSERT(array[0] == key.first);
175  ASSERT(array[1] == key.second.first);
176  ASSERT(array[2] == i);
177  }
178 
179  return 0;
180 }
Utility class to parse command line options.
Definition: JParser.hh:1514
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
TPaveText * p1
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
p2
Definition: module-Z:fit.sh:74
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:36
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
p3
Definition: module-Z:fit.sh:74
int debug
debug level