Jpp  17.3.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JHashCollection.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <cmath>
5 #include <set>
6 
8 
9 #include "Jeep/JPrint.hh"
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 
13 
14 namespace {
15 
16  /**
17  * Example class to get value for hash key.
18  */
19  struct get_value {
20  /**
21  * Constructor.
22  *
23  * \param precision precision
24  */
25  get_value(const double precision)
26  {
27  this->precision = precision;
28  }
29 
30 
31  /*
32  * Function.
33  *
34  * \param key key
35  * \return hash value
36  */
37  inline int operator()(const double key) const
38  {
39  return (int) ((key + 0.5*precision) / precision);
40  }
41 
42 
43  double precision;
44  };
45 }
46 
47 
48 /**
49  * \file
50  *
51  * Example program to test JTOOLS::JHashCollection class.
52  * \author mdejong
53  */
54 int main(int argc, char **argv)
55 {
56  using namespace std;
57 
58  double precision;
59  int debug;
60 
61  try {
62 
63  JParser<> zap("Example program to test hash collection.");
64 
65  zap['e'] = make_field(precision) = 1.0e-3;
66  zap['d'] = make_field(debug) = 3;
67 
68  zap(argc, argv);
69  }
70  catch(const exception &error) {
71  FATAL(error.what() << endl);
72  }
73 
74 
75  using namespace JPP;
76 
77  typedef JHashCollection<double, get_value> hash_collection;
78 
79  hash_collection buffer(precision);
80 
81 
82  set<double> input = { 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0 };
83  set<double> rm = { 1.5, 1.1 };
84 
85  for (set<double>::const_reverse_iterator x = input.rbegin(); x != input.rend(); ++x) {
86  buffer.insert(*x);
87  buffer.insert(*x);
88  }
89 
90  for (hash_collection::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
91  DEBUG(FIXED(5,2) << *i << endl);
92  }
93 
94  ASSERT(buffer.size() == input.size(), "Test of buffer size with multiple inserts of same element.");
95 
96 
97  for (set<double>::const_iterator x = input.begin(); x != input.end(); ++x) {
98 
99  DEBUG(FIXED(5,2) << *x << ' ' << buffer.getIndex(*x) << endl);
100 
101  ASSERT(buffer.has(*x), "Test of buffer content.");
102  }
103 
104  {
105  hash_collection out(precision);
106 
107  out = buffer;
108 
109  for (set<double>::const_iterator x = input.begin(); x != input.end(); ++x) {
110 
111  DEBUG(FIXED(5,2) << *x << ' ' << out.getIndex(*x) << endl);
112 
113  ASSERT(out.has(*x), "Test of buffer content after assignment.");
114  }
115  }
116 
117  for (set<double>::const_iterator x = rm.begin(); x != rm.end(); ++x) {
118 
119  hash_collection::iterator p = buffer.find(*x);
120 
121  DEBUG("find " << FIXED(5,2) << *x << " at position " << distance(buffer.begin(),p) << ' ' << buffer.getIndex(*x) << endl);
122 
123  buffer.erase(p);
124  }
125 
126  for (set<double>::const_iterator x = input.begin(); x != input.end(); ++x) {
127 
128  ASSERT(buffer.has(*x) == (rm.count(*x) == 0), "Test of buffer content after erase.");
129  }
130 
131  return 0;
132 }
Utility class to parse command line options.
Definition: JParser.hh:1517
int main(int argc, char *argv[])
Definition: Main.cc:15
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
General purpose class for a hash collection of unique elements.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition: JResult.hh:998
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62