Jpp test-rotations-old
the software that should make you happy
Loading...
Searching...
No Matches
JMultiKey.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JMULTIKEY__
2#define __JTOOLS__JMULTIKEY__
3
4#include <istream>
5#include <ostream>
6#include <utility>
7#include <cmath>
8
9#include "JLang/JClass.hh"
10#include "JLang/JException.hh"
11#include "JLang/JComparable.hh"
12#include "JLang/JManip.hh"
13
14
15/**
16 * \author mdejong
17 */
18
19namespace JTOOLS {}
20namespace JPP { using namespace JTOOLS; }
21
22namespace JTOOLS {
23
25 using JLANG::JClass;
26
27
28 /**
29 * \cond NEVER
30 * Forward declaration of template JMultiKey class.
31 * \endcond
32 */
33 template<unsigned int N, class JKey_t>
34 class JMultiKey;
35
36
37 namespace {
38 /**
39 * Auxiliary class for copying between const and non-const key types.
40 */
41 template<unsigned int N, class JKey_t>
42 struct JArgument {
43 typedef const JMultiKey<N, const JKey_t>& argument_type;
44 };
45
46
47 template<unsigned int N, class JKey_t>
48 struct JArgument<N, const JKey_t> {
49 typedef const JMultiKey<N, JKey_t>& argument_type;
50 };
51 }
52
53
54 /**
55 * Multidimensional key.
56 *
57 * This class reproduces the key of a multidimensional map.
58 * The individual data members can be accessed as, e.g:
59 * <pre>
60 * JMultiKey<3, key_type> key;
61 *
62 * key[[.second].second].first;
63 * </pre>
64 */
65 template<unsigned int N, class JKey_t>
66 class JMultiKey :
67 public std::pair<JKey_t, JMultiKey<N-1, JKey_t> >,
68 public JComparable< JMultiKey<N, JKey_t> >
69 {
70 public:
71
72 typedef JKey_t key_type;
73 typedef JMultiKey<N-1, JKey_t> mapped_type;
75
76
77 /**
78 * Default constructor.
79 */
81 pair_type()
82 {}
83
84
85 /**
86 * Constructor.
87 * The secondary key is appended to the end of the primary keys.
88 *
89 * \param __first primary keys
90 * \param __second secondary key
91 */
93 typename JClass<key_type> ::argument_type __second) :
94 pair_type(__first.first, mapped_type(__first.second, __second))
95 {}
96
97
98 /**
99 * Constructor.
100 * The primary key is inserted at the start of the secondary keys.
101 *
102 * \param __first primary key
103 * \param __second secondary keys
104 */
106 typename JClass<mapped_type>::argument_type __second) :
107 pair_type(__first, __second)
108 {}
109
110
111 /**
112 * Copy constructor.
113 *
114 * \param key key
115 */
117 pair_type(key.first, key.second)
118 {}
119
120
121 /**
122 * Less than method.
123 *
124 * \param key key
125 * \return true if this key less than given key; else false
126 */
127 bool less(const JMultiKey<N, JKey_t>& key) const
128 {
129 if (this->first == key.first)
130 return this->second.less(key.second);
131 else
132 return this->first < key.first;
133 }
134
135
136 /**
137 * Get length squared.
138 *
139 * \return square of length
140 */
141 double getLengthSquared() const
142 {
143 return this->first*this->first + this->second.getLengthSquared();
144 }
145
146
147 /**
148 * Get length.
149 *
150 * \return length
151 */
152 double getLength() const
153 {
154 return sqrt(this->getLengthSquared());
155 }
156
157
158 /**
159 * Get frontend key.
160 *
161 * \return frontend key
162 */
163 JMultiKey<N-1, JKey_t> front() const
164 {
165 return JMultiKey<N-1, JKey_t>(this->first, this->second.front());
166 }
167
168
169 /**
170 * Get backend key.
171 *
172 * \return backend key
173 */
175 {
176 return this->second.back();
177 }
178
179
180 /**
181 * Read key from input.
182 *
183 * \param in input stream
184 * \param key key
185 * \return input stream
186 */
187 friend inline std::istream& operator>>(std::istream& in, JMultiKey<N, JKey_t>& key)
188 {
189 in >> key.first;
190 in >> key.second;
191
192 return in;
193 }
194
195
196 /**
197 * Write key to output.
198 *
199 * \param out output stream
200 * \param key key
201 * \return output stream
202 */
203 friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<N, JKey_t>& key)
204 {
205 const JFormat format(out, getFormat< JMultiKey<N, JKey_t> >(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos)));
206
207 out << format << key.first;
208 out << ' ';
209 out << key.second;
210
211 return out;
212 }
213 };
214
215
216 /**
217 * Two-dimensional key.
218 */
219 template<class JKey_t>
220 class JMultiKey<2, JKey_t> :
221 public std::pair<JKey_t, JMultiKey<1, JKey_t> >,
222 public JComparable< JMultiKey<2, JKey_t> >
223 {
224 public:
225
226 typedef JKey_t key_type;
229
230
231 /**
232 * Default constructor.
233 */
235 pair_type()
236 {}
237
238
239 /**
240 * Constructor.
241 * The secondary key is appended to the end of the primary key.
242 *
243 * \param __first primary key
244 * \param __second secondary key
245 */
247 typename JClass<key_type> ::argument_type __second) :
248 pair_type(__first.first, __second)
249 {}
250
251
252 /**
253 * Constructor.
254 * The primary key is inserted at the start of the secondary key.
255 *
256 * \param __first primary key
257 * \param __second secondary key
258 */
260 typename JClass<mapped_type>::argument_type __second) :
261 pair_type(__first, __second.first)
262 {}
263
264
265 /**
266 * Copy constructor.
267 *
268 * \param key key
269 */
271 pair_type(key.first, key.second)
272 {}
273
274
275 /**
276 * Less than method.
277 *
278 * \param key key
279 * \return true if this key less than given key; else false
280 */
281 bool less(const JMultiKey<2, JKey_t>& key) const
282 {
283 if (this->first == key.first)
284 return this->second.less(key.second);
285 else
286 return this->first < key.first;
287 }
288
289
290 /**
291 * Get length squared.
292 *
293 * \return square of length
294 */
295 double getLengthSquared() const
296 {
297 return this->first*this->first + this->second.getLengthSquared();
298 }
299
300
301 /**
302 * Get length.
303 *
304 * \return length
305 */
306 double getLength() const
307 {
308 return sqrt(this->getLengthSquared());
309 }
310
311
312 /**
313 * Get frontend key.
314 *
315 * \return frontend key
316 */
318 {
319 return JMultiKey<1, JKey_t>(this->first);
320 }
321
322
323 /**
324 * Get backend key.
325 *
326 * \return backend key
327 */
329 {
330 return this->second.back();
331 }
332
333
334 /**
335 * Read key from input.
336 *
337 * \param in input stream
338 * \param key key
339 * \return input stream
340 */
341 friend inline std::istream& operator>>(std::istream& in, JMultiKey<2, JKey_t>& key)
342 {
343 in >> key.first;
344 in >> key.second;
345
346 return in;
347 }
348
349
350 /**
351 * Write key to output.
352 *
353 * \param out output stream
354 * \param key key
355 * \return output stream
356 */
357 friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<2, JKey_t>& key)
358 {
359 const JFormat format(out, getFormat< JMultiKey<2, JKey_t> >(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos)));
360
361 out << format << key.first;
362 out << ' ';
363 out << key.second;
364
365 return out;
366 }
367 };
368
369
370 /**
371 * One-dimensional key.
372 */
373 template<class JKey_t>
374 class JMultiKey<1, JKey_t> :
375 public JComparable< JMultiKey<1, JKey_t> >
376 {
377 public:
378
379 typedef JKey_t key_type;
381
382
383 /**
384 * Default constructor.
385 */
387 first()
388 {}
389
390
391 /**
392 * Constructor.
393 * The secondary key is appended to the end of the primary key.
394 *
395 * \param __first primary key
396 * \param __second secondary key
397 */
399 typename JClass<key_type> ::argument_type __second) :
400 first(__second)
401 {}
402
403
404 /**
405 * Constructor.
406 * The primary key is inserted at the start of the secondary key.
407 *
408 * \param __first primary key
409 * \param __second secondary key
410 */
412 typename JClass<mapped_type>::argument_type __second) :
413 first(__first)
414 {}
415
416
417 /**
418 * Constructor.
419 *
420 * \param __first key
421 */
423 first(__first)
424 {}
425
426
427 /**
428 * Copy constructor.
429 *
430 * \param key key
431 */
433 first(key.first)
434 {}
435
436
437 /**
438 * Less than method.
439 *
440 * \param key key
441 * \return true if this key less than given key; else false
442 */
443 bool less(const JMultiKey<1, JKey_t>& key) const
444 {
445 return this->first < key.first;
446 }
447
448
449 /**
450 * Get length squared.
451 *
452 * \return square of length
453 */
454 double getLengthSquared() const
455 {
456 return this->first*this->first;
457 }
458
459
460 /**
461 * Get length.
462 *
463 * \return length
464 */
465 double getLength() const
466 {
467 return sqrt(this->getLengthSquared());
468 }
469
470
471 /**
472 * Get backend key.
473 *
474 * \return backend key
475 */
476 key_type back() const
477 {
478 return this->first;
479 }
480
481
482 /**
483 * Read key from input.
484 *
485 * \param in input stream
486 * \param key key
487 * \return input stream
488 */
489 friend inline std::istream& operator>>(std::istream& in, JMultiKey<1, JKey_t>& key)
490 {
491 in >> key.first;
492
493 return in;
494 }
495
496
497 /**
498 * Write key to output.
499 *
500 * \param out output stream
501 * \param key key
502 * \return output stream
503 */
504 friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<1, JKey_t>& key)
505 {
506 const JFormat format(out, getFormat< JMultiKey<1, JKey_t> >(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos)));
507
508 out << format << key.first;
509
510 return out;
511 }
512
513
515 };
516
517
518 /**
519 * Empty key.
520 */
521 template<class JKey_t>
522 class JMultiKey<0, JKey_t>
523 {
524 public:
525
526 typedef JKey_t key_type;
527
528
529 /**
530 * Default constructor.
531 */
533 {}
534 };
535}
536
537#endif
Exceptions.
I/O manipulators.
JFormat_t & getFormat()
Get format for given type.
Definition JManip.hh:682
JMultiKey()
Default constructor.
Definition JMultiKey.hh:532
key_type back() const
Get backend key.
Definition JMultiKey.hh:476
friend std::istream & operator>>(std::istream &in, JMultiKey< 1, JKey_t > &key)
Read key from input.
Definition JMultiKey.hh:489
double getLengthSquared() const
Get length squared.
Definition JMultiKey.hh:454
bool less(const JMultiKey< 1, JKey_t > &key) const
Less than method.
Definition JMultiKey.hh:443
JMultiKey(typename JArgument< 1, JKey_t >::argument_type key)
Copy constructor.
Definition JMultiKey.hh:432
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type > ::argument_type __second)
Constructor.
Definition JMultiKey.hh:398
JMultiKey(typename JClass< key_type > ::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition JMultiKey.hh:411
JMultiKey(typename JClass< key_type >::argument_type __first)
Constructor.
Definition JMultiKey.hh:422
double getLength() const
Get length.
Definition JMultiKey.hh:465
friend std::ostream & operator<<(std::ostream &out, const JMultiKey< 1, JKey_t > &key)
Write key to output.
Definition JMultiKey.hh:504
JMultiKey()
Default constructor.
Definition JMultiKey.hh:386
JMultiKey< 0, JKey_t > mapped_type
Definition JMultiKey.hh:380
JMultiKey(typename JClass< key_type > ::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition JMultiKey.hh:259
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type > ::argument_type __second)
Constructor.
Definition JMultiKey.hh:246
double getLengthSquared() const
Get length squared.
Definition JMultiKey.hh:295
bool less(const JMultiKey< 2, JKey_t > &key) const
Less than method.
Definition JMultiKey.hh:281
friend std::ostream & operator<<(std::ostream &out, const JMultiKey< 2, JKey_t > &key)
Write key to output.
Definition JMultiKey.hh:357
JMultiKey< 1, JKey_t > mapped_type
Definition JMultiKey.hh:227
std::pair< key_type, mapped_type > pair_type
Definition JMultiKey.hh:228
JMultiKey< 1, JKey_t > front() const
Get frontend key.
Definition JMultiKey.hh:317
friend std::istream & operator>>(std::istream &in, JMultiKey< 2, JKey_t > &key)
Read key from input.
Definition JMultiKey.hh:341
JMultiKey()
Default constructor.
Definition JMultiKey.hh:234
JMultiKey(typename JArgument< 2, JKey_t >::argument_type key)
Copy constructor.
Definition JMultiKey.hh:270
key_type back() const
Get backend key.
Definition JMultiKey.hh:328
double getLength() const
Get length.
Definition JMultiKey.hh:306
Multidimensional key.
Definition JMultiKey.hh:69
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type > ::argument_type __second)
Constructor.
Definition JMultiKey.hh:92
double getLengthSquared() const
Get length squared.
Definition JMultiKey.hh:141
friend std::istream & operator>>(std::istream &in, JMultiKey< N, JKey_t > &key)
Read key from input.
Definition JMultiKey.hh:187
key_type back() const
Get backend key.
Definition JMultiKey.hh:174
friend std::ostream & operator<<(std::ostream &out, const JMultiKey< N, JKey_t > &key)
Write key to output.
Definition JMultiKey.hh:203
JMultiKey(typename JClass< key_type > ::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition JMultiKey.hh:105
JMultiKey< N-1, JKey_t > mapped_type
Definition JMultiKey.hh:73
std::pair< key_type, mapped_type > pair_type
Definition JMultiKey.hh:74
JMultiKey(typename JArgument< N, JKey_t >::argument_type key)
Copy constructor.
Definition JMultiKey.hh:116
JMultiKey< N-1, JKey_t > front() const
Get frontend key.
Definition JMultiKey.hh:163
bool less(const JMultiKey< N, JKey_t > &key) const
Less than method.
Definition JMultiKey.hh:127
double getLength() const
Get length.
Definition JMultiKey.hh:152
JMultiKey()
Default constructor.
Definition JMultiKey.hh:80
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Data structure for format specifications.
Definition JManip.hh:524
Auxiliary class to temporarily define format specifications.
Definition JManip.hh:636
Template for generic class types.
Definition JClass.hh:80
JArgument< T >::argument_type argument_type
Definition JClass.hh:82
Template definition of auxiliary base class for comparison of data structures.