Jpp
JMultiMap.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "JTools/JMultiMap.hh"
6 #include "JTools/JMap.hh"
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 
11 namespace {
12 
13  using namespace JPP;
14 
15  /**
16  * Format
17  */
18  struct JFormat :
19  public std::ostream
20  {
21  /**
22  * Constructor.
23  *
24  * \param out output stream
25  * \param width width
26  * \param precision precision
27  */
28  JFormat(std::ostream& out, int width, int precision) :
29  std::ostream(out.rdbuf())
30  {
31  this->width = width;
32  this->precision = precision;
33  }
34 
35 
36  /**
37  * Type definition of I/O operator.
38  */
39  typedef std::ostream& (*io_manip) (std::ostream&);
40 
41 
42  /**
43  * Append I/O manipulator.
44  *
45  * \param manip I/O manipulator
46  * \return this format
47  */
48  JFormat& operator<<(io_manip manip)
49  {
50  static_cast<std::ostream&>(*this) << manip;
51 
52  return *this;
53  }
54 
55 
56  /**
57  * Output value
58  *
59  * \param value value
60  * \return this format
61  */
62  template<class T>
63  JFormat& operator<<(const T& value)
64  {
65  static_cast<std::ostream&>(*this) << value;
66 
67  return *this;
68  }
69 
70 
71  /**
72  * Output value
73  *
74  * \param value value
75  * \return this format
76  */
77  JFormat& operator<<(const double value)
78  {
79  static_cast<std::ostream&>(*this) << std::fixed << std::setw(width) << std::setprecision(precision) << value;
80 
81  return *this;
82  }
83 
84 
85  /**
86  * Output value
87  *
88  * \param value value
89  * \return this format
90  */
91  template<unsigned int N>
92  JFormat& operator<<(const JMultiKey<N, const double>& value)
93  {
94  return *this << value.first << ' ' << value.second;
95  }
96 
97 
98  /**
99  * Output value
100  *
101  * \param value value
102  * \return this format
103  */
104  JFormat& operator<<(const JMultiKey<1, const double>& value)
105  {
106  return *this << value.first;
107  }
108 
109  int width;
110  int precision;
111  };
112 }
113 
114 
115 /**
116  * \file
117  *
118  * Example program to test JTOOLS::JMultiMap.
119  * \author mdejong
120  */
121 int main(int argc, char **argv)
122 {
123  using namespace std;
124 
125  int debug;
126 
127  try {
128 
129  JParser<> zap("Example program to test multi-dimensional map.");
130 
131  zap['d'] = make_field(debug) = 3;
132 
133  zap(argc, argv);
134  }
135  catch(const exception &error) {
136  FATAL(error.what() << endl);
137  }
138 
139 
140  using namespace JPP;
141 
142  const double xmin = -1.0;
143  const double xmax = +1.0;
144  const double nx = 3;
145 
146  typedef JMAPLIST<JMap,
147  JMap,
148  JMap>::maplist JMaplist_t;
149 
150  typedef JMultiMap<double, double, JMaplist_t> JMultimap_t;
151 
152  JMultimap_t buffer;
153 
154  for (double x = xmin; x <= xmax; x += (xmax - xmin)/(nx - 1)) {
155  for (double y = xmin; y <= xmax; y += (xmax - xmin)/(nx - 1)) {
156  for (double z = xmin; z <= xmax; z += (xmax - xmin)/(nx - 1)) {
157  buffer[x][y][z] = x*100 + y*10 + z;
158  }
159  }
160  }
161 
162  JFormat out(cout, 6, 1);
163 
164  cout << "i->[second]*->(first|second)" << endl;
165 
166  for (JMultimap_t::super_const_iterator i = buffer.super_begin(); i != buffer.super_end(); ++i) {
167  out << i->first << ' '
168  << i->second->first << ' '
169  << i->second->second->first << ' '
170  << i->second->second->second << endl;
171  }
172 
173  cout << "i.getKey() i.getValue()" << endl;
174 
175  for (JMultimap_t::super_const_iterator i = buffer.super_begin(); i != buffer.super_end(); ++i) {
176  out << i.getKey() << ' ' << i.getValue() << endl;
177  }
178 }
JTOOLS::JMultiMap
Multidimensional map.
Definition: JMultiMap.hh:46
JMessage.hh
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JTOOLS::JMAPLIST
Auxiliary class for recursive map list generation.
Definition: JMapList.hh:108
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
debug
int debug
debug level
Definition: JSirene.cc:59
JMap.hh
operator<<
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Definition: clb_common_header.hh:72
JParser.hh
main
int main(int argc, char **argv)
Definition: JMultiMap.cc:121
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JMultiMap.hh
std
Definition: jaanetDictionary.h:36
JTOOLS::JMultiKey
Forward declaration of template JMultiKey class.
Definition: JMultiKey.hh:29
JTOOLS::JMap
Map of pair-wise elements.
Definition: JMap.hh:27
FATAL
#define FATAL(A)
Definition: JMessage.hh:67