Jpp  17.3.0-rc.2
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JComparator.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <algorithm>
6 #include <iterator>
7 
8 #include "JLang/JComparator.hh"
9 #include "JLang/JComparison.hh"
10 
11 #include "Jeep/JParser.hh"
12 #include "Jeep/JMessage.hh"
13 
14 
15 namespace {
16 
17  class __A__ {
18  public:
19  __A__(const double a)
20  {
21  this->a = a;
22  }
23 
24  double get() const
25  {
26  return -a;
27  }
28 
29  friend inline std::ostream& operator<<(std::ostream& out, const __A__& object) { return out << object.a; }
30 
31  double a;
32  };
33 
34 
35  template<class T>
36  void print(std::ostream& out, int debug, const char* title, T __begin, T __end)
37  {
38  using namespace std;
39  using namespace JPP;
40 
41  if (debug >= debug_t) {
42  out << title << endl;
43  copy(__begin, __end, ostream_iterator<typename T::value_type>(out, " "));
44  out << endl;
45  }
46  }
47 }
48 
49 
50 /**
51  * \file
52  *
53  * Auxiliary program to test JLANG::JComparator class and helper methods.
54  * \author mdejong
55  */
56 int main(int argc, char **argv)
57 {
58  using namespace std;
59  using namespace JPP;
60 
61  int debug;
62 
63  try {
64 
65  JParser<> zap("Auxiliary program to test object comparison methods.");
66 
67  zap['d'] = make_field(debug) = 3;
68 
69  zap(argc, argv);
70  }
71  catch(const exception &error) {
72  FATAL(error.what() << endl);
73  }
74 
75  typedef vector<__A__> buffer_type;
76 
77  buffer_type buffer;
78 
79  buffer.push_back(__A__(3.0));
80  buffer.push_back(__A__(4.0));
81  buffer.push_back(__A__(2.0));
82  buffer.push_back(__A__(1.0));
83  buffer.push_back(__A__(5.0));
84 
85  ASSERT(buffer.size() >= 2);
86 
87  print(cout, debug,
88  "data",
89  buffer.begin(), buffer.end());
90 
91 
92  sort(buffer.begin(), buffer.end(), make_comparator(&__A__::get));
93 
94  print(cout, debug,
95  "sort(.., make_comparator(&__A__::get))",
96  buffer.begin(), buffer.end());
97 
98  for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
99  ASSERT(p->get() < q->get());
100  }
101 
102 
103  sort(buffer.begin(), buffer.end(), make_comparator(&__A__::get, JComparison::gt()));
104 
105  print(cout, debug,
106  "sort(.., make_comparator(&__A__::get, JComparison::gt()))",
107  buffer.begin(), buffer.end());
108 
109  for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
110  ASSERT(p->get() > q->get());
111  }
112 
113 
114  sort(buffer.begin(), buffer.end(), make_comparator(&__A__::a));
115 
116  print(cout, debug,
117  "sort(.., make_comparator(&__A__::a))",
118  buffer.begin(), buffer.end());
119 
120  for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
121  ASSERT(p->a < q->a);
122  }
123 
124  sort(buffer.begin(), buffer.end(), make_comparator(&__A__::a, JComparison::gt()));
125 
126  print(cout, debug,
127  "sort(.., make_comparator(&__A__::a), JComparison::gt())",
128  buffer.begin(), buffer.end());
129 
130  for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
131  ASSERT(p->a > q->a);
132  }
133 
134  return 0;
135 }
Utility class to parse command line options.
Definition: JParser.hh:1517
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
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:1993
then JCalibrateToT a
Definition: JTuneHV.sh:116
do set_variable OUTPUT_DIRECTORY $WORKDIR T
print
Definition: JConvertDusj.sh:44
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
int debug
debug level