Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JVectorize.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <limits>
6 
7 #include "JLang/JVectorize.hh"
8 
9 #include "Jeep/JParser.hh"
10 #include "Jeep/JMessage.hh"
11 
12 
13 namespace {
14 
15  struct __A__
16  {
17  __A__() :
18  i(0)
19  {}
20 
21  __A__(const int __i) :
22  i(__i)
23  {}
24 
25  int get() const { return i; }
26 
27  friend inline std::ostream& operator<<(std::ostream& out, const __A__& object)
28  {
29  return out << object.i;
30  }
31 
32  int i;
33  };
34 
35 
36  /**
37  * Print data.
38  *
39  * \param out output stream
40  * \param N number of elements
41  * \param p pointer to data
42  */
43  template<class T>
44  inline void print(std::ostream& out, int N, T* p)
45  {
46  using namespace std;
47 
48  for (int i = 0; i != N; ++i) {
49  out << ' ' << setw(2) << p[i];
50  }
51 
52  cout << endl;
53  }
54 }
55 
56 
57 /**
58  * \file
59  *
60  * Example program to test JLANG::make_array method.
61  * \author mdejong
62  */
63 int main(int argc, char **argv)
64 {
65  using namespace std;
66 
67  int debug;
68 
69  try {
70 
71  JParser<> zap("Example program to test converions of array of objects to array of data members.");
72 
73  zap['d'] = make_field(debug) = 3;
74 
75  zap(argc, argv);
76  }
77  catch(const exception &error) {
78  FATAL(error.what() << endl);
79  }
80 
81 
82  using namespace JPP;
83 
84  vector<__A__> buffer;
85 
86  int numberOfElements = 5;
87 
88  for (int i = 0; i != numberOfElements; ++i) {
89  buffer.push_back(__A__(i + 1));
90  }
91 
92  if (debug >= debug_t) {
93  print(cout, numberOfElements, buffer.data());
94  print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::get).data());
95  print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::i) .data());
96  cout << "max " << getMaximum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::min()) << endl;
97  cout << "min " << getMinimum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::max()) << endl;
98  }
99 
100  {
101  const int* p = make_array(buffer.begin(), buffer.end(), &__A__::get).data();
102 
103  for (int i = 0; i != numberOfElements; ++i, ++p) {
104  ASSERT(buffer[i].get() == *p);
105  }
106  }
107 
108  {
109  const int* p = make_array(buffer.begin(), buffer.end(), &__A__::i).data();
110 
111  for (int i = 0; i != numberOfElements; ++i, ++p) {
112  ASSERT(buffer[i].i == *p);
113  }
114  }
115  {
116  ASSERT(getMaximum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::min()) == numberOfElements);
117  ASSERT(getMinimum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::max()) == 1);
118  }
119 
120  return 0;
121 }
Utility class to parse command line options.
Definition: JParser.hh:1711
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
T getMinimum(const array_type< T > &buffer, const T value)
Get minimum of values.
Definition: JVectorize.hh:240
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
do set_variable OUTPUT_DIRECTORY $WORKDIR T
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
print
Definition: JConvertDusj.sh:44
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
Utility class to parse command line options.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
T getMaximum(const array_type< T > &buffer, const T value)
Get maximum of values.
Definition: JVectorize.hh:218
int debug
debug level