Jpp test-rotations-new
the software that should make you happy
Loading...
Searching...
No Matches
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
13namespace {
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 */
63int main(int argc, char **argv)
64{
65 using namespace std;
66 using namespace JPP;
67
68 int debug;
69
70 try {
71
72 JParser<> zap("Example program to test converions of array of objects to array of data members.");
73
74 zap['d'] = make_field(debug) = 3;
75
76 zap(argc, argv);
77 }
78 catch(const exception &error) {
79 FATAL(error.what() << endl);
80 }
81
82
83 vector<__A__> buffer;
84
85 int numberOfElements = 5;
86
87 for (int i = 0; i != numberOfElements; ++i) {
88 buffer.push_back(__A__(i + 1));
89 }
90
91 if (debug >= debug_t) {
92 print(cout, numberOfElements, buffer.data());
93 print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::get).data());
94 print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::i) .data());
95 cout << "max " << getMaximum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::min()) << endl;
96 cout << "min " << getMinimum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::max()) << endl;
97 }
98
99 {
100 const int* p = make_array(buffer.begin(), buffer.end(), &__A__::get).data();
101
102 for (int i = 0; i != numberOfElements; ++i, ++p) {
103 ASSERT(buffer[i].get() == *p);
104 }
105 }
106
107 {
108 const int* p = make_array(buffer.begin(), buffer.end(), &__A__::i).data();
109
110 for (int i = 0; i != numberOfElements; ++i, ++p) {
111 ASSERT(buffer[i].i == *p);
112 }
113 }
114 {
115 ASSERT(getMaximum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::min()) == numberOfElements);
116 ASSERT(getMinimum(make_array(buffer.begin(), buffer.end(), &__A__::i), numeric_limits<int>::max()) == 1);
117 }
118 {
119 ASSERT(getCount(make_array(buffer.begin(), buffer.end(), &__A__::i), equal_to<int>()) == (size_t) numberOfElements);
120
121 buffer.push_back(__A__(1));
122
123 ASSERT(buffer.size() == (size_t) (numberOfElements + 1));
124 ASSERT(getCount(make_array(buffer.begin(), buffer.end(), &__A__::i), equal_to<int>()) == (size_t) (numberOfElements));
125 }
126
127 return 0;
128}
General purpose messaging.
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
void print(const TH1 &h1, std::ostream &out)
Print histogram parameters.
int main(int argc, char **argv)
Definition JVectorize.cc:63
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
Utility class to parse command line options.
Definition JParser.hh:1698
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).