Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JTuple.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JTUPLE__
2#define __JTOOLS__JTUPLE__
3
4#include <istream>
5#include <ostream>
6
7#include "JLang/JTypeList.hh"
8#include "JLang/JNullType.hh"
9#include "JLang/JClass.hh"
10#include "JLang/JComparable.hh"
11#include "JLang/JType.hh"
12#include "JLang/JNumber.hh"
13#include "JMath/JMath.hh"
14#include "JIO/JSerialisable.hh"
15
16
17/**
18 * \file
19 * Data structure based on type list.
20 * \author mdejong
21 */
22namespace JTOOLS {}
23namespace JPP { using namespace JTOOLS; }
24
25namespace JTOOLS {
26
27 using JLANG::JTypeList;
28 using JLANG::JTYPELIST;
29 using JLANG::JNullType;
30 using JLANG::JClass;
31 using JLANG::JType;
32 using JLANG::JNumber;
33 using JLANG::for_each;
34 using JMATH::JMath;
36 using JIO::JReader;
37 using JIO::JWriter;
38
39
40 /**
41 * Template data structure.
42 */
43 template<class T>
44 struct JTuple :
45 public JMath < JTuple<T> >,
46 public JComparable< JTuple<T> >
47 {
48
50
51 /**
52 * Default constructor.
53 */
55 {}
56
57
58 /**
59 * Constructor.
60 *
61 * \param __first first
62 */
63 JTuple(typename JClass<T>::argument_type __first) :
64 first(__first)
65 {}
66
67
68 /**
69 * Negate tuple.
70 *
71 * \return this tuple
72 */
74 {
75 first = -first;
76
77 return *this;
78 }
79
80
81 /**
82 * Add tuple.
83 *
84 * \param tuple tuple
85 * \return this tuple
86 */
87 JTuple& add(const JTuple& tuple)
88 {
89 first += tuple.first;
90
91 return *this;
92 }
93
94
95 /**
96 * Subtract tuple.
97 *
98 * \param tuple tuple
99 * \return this tuple
100 */
101 JTuple& sub(const JTuple& tuple)
102 {
103 first -= tuple.first;
104
105 return *this;
106 }
107
108
109 /**
110 * Scale tuple.
111 *
112 * \param factor multiplication factor
113 * \return this tuple
114 */
115 JTuple& mul(const double factor)
116 {
117 first *= factor;
118
119 return *this;
120 }
121
122
123 /**
124 * Scale tuple.
125 *
126 * \param factor division factor
127 * \return this tuple
128 */
129 JTuple& div(const double factor)
130 {
131 first /= factor;
132
133 return *this;
134 }
135
136
137 /**
138 * Compare tuple.
139 *
140 * \param tuple tuple
141 * \return true if this tuple less than given tuple; else false
142 */
143 bool less(const JTuple& tuple) const
144 {
145 return first < tuple.first;
146 }
147
148
149 /**
150 * Read tuple from input.
151 *
152 * \param in input stream
153 * \param tuple tuple
154 * \return input stream
155 */
156 friend inline std::istream& operator>>(std::istream& in, JTuple& tuple)
157 {
158 return in >> tuple.first;
159 }
160
161
162 /**
163 * Write tuple to output.
164 *
165 * \param out output stream
166 * \param tuple tuple
167 * \return output stream
168 */
169 friend inline std::ostream& operator<<(std::ostream& out, const JTuple& tuple)
170 {
171 return out << tuple.first;
172 }
173
174
175 /**
176 * Read tuple from input.
177 *
178 * \param in reader
179 * \param tuple tuple
180 * \return reader
181 */
182 friend inline JReader& operator>>(JReader& in, JTuple& tuple)
183 {
184 return in >> tuple.first;
185 }
186
187
188 /**
189 * Write tuple to output.
190 *
191 * \param out writer
192 * \param tuple tuple
193 * \return writer
194 */
195 friend inline JWriter& operator<<(JWriter& out, const JTuple& tuple)
196 {
197 return out << tuple.first;
198 }
199
200
202 };
203
204
205
206 /**
207 * Template specialisation of JTuple for multiple data types.
208 */
209 template<class JHead_t, class JTail_t>
210 struct JTuple< JTypeList<JHead_t, JTail_t> > :
211 public JMath < JTuple< JTypeList<JHead_t, JTail_t> > >,
212 public JComparable< JTuple< JTypeList<JHead_t, JTail_t> > >
213 {
214
216
217
218 /**
219 * Default constructor.
220 */
222 {}
223
224
225 /**
226 * Constructor.
227 *
228 * \param __first first
229 * \param __second second
230 */
231 JTuple(typename JClass<JHead_t>::argument_type __first, const JTuple<JTail_t>& __second) :
232 first (__first),
233 second(__second)
234 {}
235
236
237 /**
238 * Initialise constructor.
239 *
240 * \param value first value
241 * \param args remaining values
242 */
243 template<class ...Args>
244 JTuple(typename JClass<JHead_t>::argument_type value, const Args& ...args) :
245 first (value),
246 second(args...)
247 {}
248
249
250 /**
251 * Negate tuple.
252 *
253 * \return this tuple
254 */
256 {
257 first = -first;
258 second.negate();
259
260 return *this;
261 }
262
263
264 /**
265 * Add tuple.
266 *
267 * \param tuple tuple
268 * \return this tuple
269 */
270 JTuple& add(const JTuple& tuple)
271 {
272 first += tuple.first;
273 second.add(tuple.second);
274
275 return *this;
276 }
277
278
279 /**
280 * Subtract tuple.
281 *
282 * \param tuple tuple
283 * \return this tuple
284 */
285 JTuple& sub(const JTuple& tuple)
286 {
287 first -= tuple.first;
288 second.sub(tuple.second);
289
290 return *this;
291 }
292
293
294 /**
295 * Scale tuple.
296 *
297 * \param factor multiplication factor
298 * \return this tuple
299 */
300 JTuple& mul(const double factor)
301 {
302 first *= factor;
303 second.mul(factor);
304
305 return *this;
306 }
307
308
309 /**
310 * Scale tuple.
311 *
312 * \param factor division factor
313 * \return this tuple
314 */
315 JTuple& div(const double factor)
316 {
317 first /= factor;
318 second.div(factor);
319
320 return *this;
321 }
322
323
324 /**
325 * Compare tuple.
326 *
327 * \param tuple tuple
328 * \return true if this tuple less than given tuple; else false
329 */
330 bool less(const JTuple& tuple) const
331 {
332 if (first == tuple.first)
333 return second < tuple.second;
334 else
335 return first < tuple.first;
336 }
337
338
339 /**
340 * Read tuple from input.
341 *
342 * \param in input stream
343 * \param tuple tuple
344 * \return input stream
345 */
346 friend inline std::istream& operator>>(std::istream& in, JTuple& tuple)
347 {
348 in >> tuple.first;
349 in >> tuple.second;
350
351 return in;
352 }
353
354
355 /**
356 * Write tuple to output.
357 *
358 * \param out output stream
359 * \param tuple tuple
360 * \return output stream
361 */
362 friend inline std::ostream& operator<<(std::ostream& out, const JTuple& tuple)
363 {
364 out << tuple.first;
365 out << ' ';
366 out << tuple.second;
367
368 return out;
369 }
370
371
372 /**
373 * Read tuple from input.
374 *
375 * \param in reader
376 * \param tuple tuple
377 * \return reader
378 */
379 friend inline JReader& operator>>(JReader& in, JTuple& tuple)
380 {
381 in >> tuple.first;
382 in >> tuple.second;
383
384 return in;
385 }
386
387
388 /**
389 * Write tuple to output.
390 *
391 * \param out writer
392 * \param tuple tuple
393 * \return writer
394 */
395 friend inline JWriter& operator<<(JWriter& out, const JTuple& tuple)
396 {
397 out << tuple.first;
398 out << tuple.second;
399
400 return out;
401 }
402
403
404 JHead_t first;
406 };
407
408
409 /**
410 * Template specialisation of JTuple for two data types.
411 */
412 template<class JHead_t, class JTail_t>
413 struct JTuple< JTypeList<JHead_t, JTypeList<JTail_t, JNullType> > > :
414 public JMath < JTuple<JTypeList<JHead_t, JTypeList<JTail_t, JNullType> > > >,
415 public JComparable< JTuple<JTypeList<JHead_t, JTypeList<JTail_t, JNullType> > > >
416 {
417
419
420 /**
421 * Default constructor.
422 */
424 {}
425
426
427 /**
428 * Constructor.
429 *
430 * \param __first first
431 * \param __second second
432 */
434 typename JClass<JTail_t>::argument_type __second) :
435 first (__first),
436 second(__second)
437 {}
438
439
440 /**
441 * Constructor.
442 *
443 * \param __first first
444 * \param __second second
445 */
446 JTuple(typename JClass<JHead_t>::argument_type __first, const JTuple<JTail_t>& __second) :
447 first (__first),
448 second(__second.first)
449 {}
450
451
452 /**
453 * Negate tuple.
454 *
455 * \return this tuple
456 */
458 {
459 first = -first;
460 second = -second;
461
462 return *this;
463 }
464
465
466 /**
467 * Add tuple.
468 *
469 * \param tuple tuple
470 * \return this tuple
471 */
472 JTuple& add(const JTuple& tuple)
473 {
474 first += tuple.first;
475 second += tuple.second;
476
477 return *this;
478 }
479
480
481 /**
482 * Subtract tuple.
483 *
484 * \param tuple tuple
485 * \return this tuple
486 */
487 JTuple& sub(const JTuple& tuple)
488 {
489 first -= tuple.first;
490 second -= tuple.second;
491
492 return *this;
493 }
494
495
496 /**
497 * Scale tuple.
498 *
499 * \param factor multiplication factor
500 * \return this tuple
501 */
502 JTuple& mul(const double factor)
503 {
504 first *= factor;
505 second *= factor;
506
507 return *this;
508 }
509
510
511 /**
512 * Scale tuple.
513 *
514 * \param factor division factor
515 * \return this tuple
516 */
517 JTuple& div(const double factor)
518 {
519 first /= factor;
520 second /= factor;
521
522 return *this;
523 }
524
525
526 /**
527 * Compare tuple.
528 *
529 * \param tuple tuple
530 * \return true if this tuple less than given tuple; else false
531 */
532 bool less(const JTuple& tuple) const
533 {
534 if (first == tuple.first)
535 return second < tuple.second;
536 else
537 return first < tuple.first;
538 }
539
540
541 /**
542 * Read tuple from input.
543 *
544 * \param in input stream
545 * \param tuple tuple
546 * \return input stream
547 */
548 friend inline std::istream& operator>>(std::istream& in, JTuple& tuple)
549 {
550 in >> tuple.first;
551 in >> tuple.second;
552
553 return in;
554 }
555
556
557 /**
558 * Write tuple to output.
559 *
560 * \param out output stream
561 * \param tuple tuple
562 * \return output stream
563 */
564 friend inline std::ostream& operator<<(std::ostream& out, const JTuple& tuple)
565 {
566 out << tuple.first;
567 out << ' ';
568 out << tuple.second;
569
570 return out;
571 }
572
573
574 /**
575 * Read tuple from input.
576 *
577 * \param in reader
578 * \param tuple tuple
579 * \return reader
580 */
581 friend inline JReader& operator>>(JReader& in, JTuple& tuple)
582 {
583 in >> tuple.first;
584 in >> tuple.second;
585
586 return in;
587 }
588
589
590 /**
591 * Write tuple to output.
592 *
593 * \param out writer
594 * \param tuple tuple
595 * \return writer
596 */
597 friend inline JWriter& operator<<(JWriter& out, const JTuple& tuple)
598 {
599 out << tuple.first;
600 out << tuple.second;
601
602 return out;
603 }
604
605
606 JHead_t first;
607 JTail_t second;
608 };
609
610
611 /**
612 * Terminator class of recursive JTuple class.
613 */
614 template<class JHead_t>
615 struct JTuple< JTypeList<JHead_t, JNullType> > :
616 public JTuple<JHead_t>
617 {
618
620
621 /**
622 * Default constructor.
623 */
625 {}
626
627
628 /**
629 * Constructor.
630 *
631 * \param __first first
632 */
634 JTuple<JHead_t>(__first)
635 {}
636
637
638 /**
639 * Constructor.
640 *
641 * \param __first first
642 * \param __second second
643 */
644 JTuple(typename JClass<JHead_t>::argument_type __first, const JTuple<JNullType>& __second) :
645 JTuple<JHead_t>(__first)
646 {}
647 };
648
649
650
651 /**
652 * For each data type method.
653 *
654 * The given object should provide for the function object operator
655 * <pre>
656 * template<class T>
657 * void object()(JType<T> type, tuple);
658 * </pre>
659 *
660 * \param object object
661 * \param typelist type list
662 * \param tuple tuple
663 * \return object
664 */
665 template<class JObject_t, class JHead_t, class JTail_t, class T>
666 JObject_t& for_each(JObject_t& object, JType< JTypeList<JHead_t, JTail_t> > typelist, const JTuple<T>& tuple)
667 {
668 for_each(object, JType<JHead_t>(), tuple);
669 for_each(object, JType<JTail_t>(), tuple);
670
671 return object;
672 }
673
674
675 /**
676 * For each data type method.
677 *
678 * The given object should provide for the function object operator
679 * <pre>
680 * template<class T>
681 * void object()(JType<T> type, tuple);
682 * </pre>
683 *
684 * \param object object
685 * \param type type
686 * \param tuple tuple
687 * \return object
688 */
689 template<class JObject_t, class JTypelist_t, class T>
690 JObject_t& for_each(JObject_t& object, JType<JTypelist_t> type, const JTuple<T>& tuple)
691 {
692 object(type, tuple);
693
694 return object;
695 }
696
697
698 /**
699 * Helper method for tuple.
700 *
701 * \param args values
702 * \return tuple
703 */
704 template<class ...Args>
705 inline JTuple<typename JTYPELIST<Args...>::typelist> make_tuple(const Args& ...args)
706 {
707 return JTuple<typename JTYPELIST<Args...>::typelist>(args...);
708 }
709
710
711 /**
712 * Termination method of for each data type method.
713 *
714 * \param object object
715 * \param type null type
716 * \param tuple tuple
717 * \return object
718 */
719 template<class JObject_t, class T>
720 JObject_t& for_each(JObject_t& object, JType<JNullType> type, const JTuple<T>& tuple)
721 {
722 return object;
723 }
724}
725
726#endif
Base class for data structures with artithmetic capabilities.
Interface for binary input.
Interface for binary output.
void for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist)
For each data type method.
Definition JTypeList.hh:414
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
JTuple< typename JTYPELIST< Args... >::typelist > make_tuple(const Args &...args)
Helper method for tuple.
Definition JTuple.hh:705
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist, const JTuple< T > &tuple)
For each data type method.
Definition JTuple.hh:666
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.
Auxiliary class for no type definition.
Definition JNullType.hh:19
Wrapper class for integer value.
Auxiliary class for recursive type list generation.
Definition JTypeList.hh:351
Type list.
Definition JTypeList.hh:23
Auxiliary class for a type holder.
Definition JType.hh:19
Auxiliary base class for aritmetic operations of derived class types.
Definition JMath.hh:347
JTuple(typename JClass< JHead_t >::argument_type __first, const JTuple< JNullType > &__second)
Constructor.
Definition JTuple.hh:644
JTuple(typename JClass< JHead_t >::argument_type __first)
Constructor.
Definition JTuple.hh:633
JTuple & div(const double factor)
Scale tuple.
Definition JTuple.hh:315
JTuple(typename JClass< JHead_t >::argument_type value, const Args &...args)
Initialise constructor.
Definition JTuple.hh:244
JTypeList< JHead_t, JTail_t > typelist
Definition JTuple.hh:215
friend JWriter & operator<<(JWriter &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:395
JTuple(typename JClass< JHead_t >::argument_type __first, const JTuple< JTail_t > &__second)
Constructor.
Definition JTuple.hh:231
bool less(const JTuple &tuple) const
Compare tuple.
Definition JTuple.hh:330
friend JReader & operator>>(JReader &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:379
friend std::istream & operator>>(std::istream &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:346
friend std::ostream & operator<<(std::ostream &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:362
JTuple & sub(const JTuple &tuple)
Subtract tuple.
Definition JTuple.hh:285
JTuple & mul(const double factor)
Scale tuple.
Definition JTuple.hh:300
JTuple & add(const JTuple &tuple)
Add tuple.
Definition JTuple.hh:270
JTuple(typename JClass< JHead_t >::argument_type __first, typename JClass< JTail_t >::argument_type __second)
Constructor.
Definition JTuple.hh:433
bool less(const JTuple &tuple) const
Compare tuple.
Definition JTuple.hh:532
friend JWriter & operator<<(JWriter &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:597
JTuple(typename JClass< JHead_t >::argument_type __first, const JTuple< JTail_t > &__second)
Constructor.
Definition JTuple.hh:446
friend JReader & operator>>(JReader &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:581
friend std::istream & operator>>(std::istream &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:548
friend std::ostream & operator<<(std::ostream &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:564
Template data structure.
Definition JTuple.hh:47
JTuple & mul(const double factor)
Scale tuple.
Definition JTuple.hh:115
JTuple & add(const JTuple &tuple)
Add tuple.
Definition JTuple.hh:87
bool less(const JTuple &tuple) const
Compare tuple.
Definition JTuple.hh:143
JTuple()
Default constructor.
Definition JTuple.hh:54
JTuple(typename JClass< T >::argument_type __first)
Constructor.
Definition JTuple.hh:63
friend JWriter & operator<<(JWriter &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:195
JTypeList< T > typelist
Definition JTuple.hh:49
friend JReader & operator>>(JReader &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:182
JTuple & div(const double factor)
Scale tuple.
Definition JTuple.hh:129
friend std::istream & operator>>(std::istream &in, JTuple &tuple)
Read tuple from input.
Definition JTuple.hh:156
friend std::ostream & operator<<(std::ostream &out, const JTuple &tuple)
Write tuple to output.
Definition JTuple.hh:169
JTuple & negate()
Negate tuple.
Definition JTuple.hh:73
JTuple & sub(const JTuple &tuple)
Subtract tuple.
Definition JTuple.hh:101