STXXL  1.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tuple.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/tuple.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2003 Roman Dementiev <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_TUPLE_HEADER
14 #define STXXL_COMMON_TUPLE_HEADER
15 
16 #include <stxxl/bits/namespace.h>
18 #include <limits>
19 #include <ostream>
20 
22 
23 struct Plug { };
24 
25 
26 template <class T1,
27  class T2,
28  class T3,
29  class T4,
30  class T5,
31  class T6
32  >
33 struct tuple_base
34 {
35  typedef T1 first_type;
36  typedef T2 second_type;
37  typedef T3 third_type;
38  typedef T4 fourth_type;
39  typedef T5 fifth_type;
40  typedef T6 sixth_type;
41 
42  template <int I>
43  struct item_type
44  {
45 /*
46  typedef typename SWITCH<I, CASE<1,first_type,
47  CASE<2,second_type,
48  CASE<3,third_type,
49  CASE<4,fourth_type,
50  CASE<5,fifth_type,
51  CASE<6,sixth_type,
52  CASE<DEFAULT,void
53  > > > > > > > >::result result;
54 */
55  };
56 };
57 
58 
59 //! k-Tuple data type
60 //!
61 //! (defined for k < 7)
62 template <class T1,
63  class T2 = Plug,
64  class T3 = Plug,
65  class T4 = Plug,
66  class T5 = Plug,
67  class T6 = Plug
68  >
69 struct tuple
70 {
71  //! First tuple component type
72  typedef T1 first_type;
73  //! Second tuple component type
74  typedef T2 second_type;
75  //! Third tuple component type
76  typedef T3 third_type;
77  //! Fourth tuple component type
78  typedef T4 fourth_type;
79  //! Fifth tuple component type
80  typedef T5 fifth_type;
81  //! Sixth tuple component type
82  typedef T6 sixth_type;
83 
84  template <int I>
85  struct item_type
86  {
87  typedef typename SWITCH<I, CASE<1, first_type,
88  CASE<2, second_type,
89  CASE<3, third_type,
90  CASE<4, fourth_type,
91  CASE<5, fifth_type,
92  CASE<6, sixth_type,
93  CASE<DEFAULT, void
94  > > > > > > > >::result result;
95  };
96 
97  //! First tuple component
99  //! Second tuple component
101  //! Third tuple component
103  //! Fourth tuple component
105  //! Fifth tuple component
107  //! Sixth tuple component
109 
110  //! Empty constructor
111  tuple() { }
112 
113  //! Construct tuple from components
115  second_type _second,
116  third_type _third,
117  fourth_type _fourth,
118  fifth_type _fifth,
119  sixth_type _sixth
120  )
121  : first(_first),
122  second(_second),
123  third(_third),
124  fourth(_fourth),
125  fifth(_fifth),
126  sixth(_sixth)
127  { }
128 
129  //! Equality comparison
130  bool operator == (const tuple& t) const
131  {
132  return first == t.first && second == t.second && third == t.third
133  && fourth == t.fourth && fifth == t.fifth && sixth == t.sixth;
134  }
135 
136  //! Inequality comparison
137  bool operator != (const tuple& t) const
138  {
139  return !(first == t.first && second == t.second && third == t.third
140  && fourth == t.fourth && fifth == t.fifth && sixth == t.sixth);
141  }
142 
143  //! Make tuple ostream-able
144  friend std::ostream& operator << (std::ostream& os, const tuple& t)
145  {
146  return os << '(' << t.first << ',' << t.second << ',' << t.third
147  << ',' << t.fourth << ',' << t.fifth << ',' << t.sixth
148  << ')';
149  }
150 
151  //! Return minimum value of tuple using numeric_limits
152  static tuple min_value()
153  {
160  }
161 
162  //! Return maximum value of tuple using numeric_limits
163  static tuple max_value()
164  {
171  }
172 };
173 
174 //! Partial specialization for 1- \c tuple
175 template <class T1>
176 struct tuple<T1, Plug, Plug, Plug, Plug>
177 {
178  //! First tuple component type
179  typedef T1 first_type;
180 
181  //! First tuple component
183 
184  template <int I>
185  struct item_type
186  {
188  };
189 
190  //! Empty constructor
191  tuple() { }
192 
193  //! Initializing constructor
195  : first(first_)
196  { }
197 
198  //! Equality comparison
199  bool operator == (const tuple& t) const
200  {
201  return first == t.first;
202  }
203 
204  //! Inequality comparison
205  bool operator != (const tuple& t) const
206  {
207  return !(first == t.first);
208  }
209 
210  //! Make tuple ostream-able
211  friend std::ostream& operator << (std::ostream& os, const tuple& t)
212  {
213  return os << '(' << t.first << ')';
214  }
215 
216  //! Return minimum value of tuple using numeric_limits
217  static tuple min_value()
218  {
220  }
221 
222  //! Return maximum value of tuple using numeric_limits
223  static tuple max_value()
224  {
226  }
227 };
228 
229 //! Partial specialization for 2- \c tuple (equivalent to std::pair)
230 template <class T1, class T2>
231 struct tuple<T1, T2, Plug, Plug, Plug, Plug>
232 {
233  //! First tuple component type
234  typedef T1 first_type;
235  //! Second tuple component type
236  typedef T2 second_type;
237 
238  template <int I>
239  struct item_type
240  {
241  typedef typename SWITCH<I, CASE<1, first_type,
242  CASE<2, second_type,
245  };
246 
247  //! First tuple component
249  //! Second tuple component
251 
252  //! Empty constructor
253  tuple() { }
254 
255  //! Initializing constructor
257  second_type second_)
258  : first(first_),
259  second(second_)
260  { }
261 
262  //! Equality comparison
263  bool operator == (const tuple& t) const
264  {
265  return first == t.first && second == t.second;
266  }
267 
268  //! Inequality comparison
269  bool operator != (const tuple& t) const
270  {
271  return !(first == t.first && second == t.second);
272  }
273 
274  //! Make tuple ostream-able
275  friend std::ostream& operator << (std::ostream& os, const tuple& t)
276  {
277  return os << '(' << t.first << ',' << t.second << ')';
278  }
279 
280  //! Return minimum value of tuple using numeric_limits
281  static tuple min_value()
282  {
285  }
286 
287  //! Return maximum value of tuple using numeric_limits
288  static tuple max_value()
289  {
292  }
293 };
294 
295 
296 //! Partial specialization for 3- \c tuple (triple)
297 template <class T1,
298  class T2,
299  class T3
300  >
301 struct tuple<T1, T2, T3, Plug, Plug, Plug>
302 {
303  //! First tuple component type
304  typedef T1 first_type;
305  //! Second tuple component type
306  typedef T2 second_type;
307  //! Third tuple component type
308  typedef T3 third_type;
309 
310  template <int I>
311  struct item_type
312  {
313  typedef typename SWITCH<I, CASE<1, first_type,
314  CASE<2, second_type,
315  CASE<3, second_type,
317  > > > >::result result;
318  };
319 
320  //! First tuple component
322  //! Second tuple component
324  //! Third tuple component
326 
327  //! Empty constructor
328  tuple() { }
329 
330  //! Construct tuple from components
332  second_type _second,
333  third_type _third)
334  : first(_first),
335  second(_second),
336  third(_third)
337  { }
338 
339  //! Equality comparison
340  bool operator == (const tuple& t) const
341  {
342  return first == t.first && second == t.second && third == t.third;
343  }
344 
345  //! Inequality comparison
346  bool operator != (const tuple& t) const
347  {
348  return !(first == t.first && second == t.second && third == t.third);
349  }
350 
351  //! Make tuple ostream-able
352  friend std::ostream& operator << (std::ostream& os, const tuple& t)
353  {
354  return os << '(' << t.first << ',' << t.second << ',' << t.third
355  << ')';
356  }
357 
358  //! Return minimum value of tuple using numeric_limits
359  static tuple min_value()
360  {
364  }
365 
366  //! Return maximum value of tuple using numeric_limits
367  static tuple max_value()
368  {
372  }
373 };
374 
375 //! Partial specialization for 4- \c tuple
376 template <class T1,
377  class T2,
378  class T3,
379  class T4
380  >
381 struct tuple<T1, T2, T3, T4, Plug, Plug>
382 {
383  //! First tuple component type
384  typedef T1 first_type;
385  //! Second tuple component type
386  typedef T2 second_type;
387  //! Third tuple component type
388  typedef T3 third_type;
389  //! Fourth tuple component type
390  typedef T4 fourth_type;
391 
392  template <int I>
393  struct item_type
394  {
395  typedef typename SWITCH<I, CASE<1, first_type,
396  CASE<2, second_type,
397  CASE<3, third_type,
398  CASE<4, fourth_type,
399  CASE<DEFAULT, void
400  > > > > > >::result result;
401  };
402 
403  //! First tuple component
405  //! Second tuple component
407  //! Third tuple component
409  //! Fourth tuple component
411 
412  //! Empty constructor
413  tuple() { }
414 
415  //! Construct tuple from components
417  second_type _second,
418  third_type _third,
419  fourth_type _fourth)
420  : first(_first),
421  second(_second),
422  third(_third),
423  fourth(_fourth)
424  { }
425 
426  //! Equality comparison
427  bool operator == (const tuple& t) const
428  {
429  return first == t.first && second == t.second && third == t.third
430  && fourth == t.fourth;
431  }
432 
433  //! Inequality comparison
434  bool operator != (const tuple& t) const
435  {
436  return !(first == t.first && second == t.second && third == t.third
437  && fourth == t.fourth);
438  }
439 
440  //! Make tuple ostream-able
441  friend std::ostream& operator << (std::ostream& os, const tuple& t)
442  {
443  return os << '(' << t.first << ',' << t.second << ',' << t.third
444  << ',' << t.fourth << ')';
445  }
446 
447  //! Return minimum value of tuple using numeric_limits
448  static tuple min_value()
449  {
454  }
455 
456  //! Return maximum value of tuple using numeric_limits
457  static tuple max_value()
458  {
463  }
464 };
465 
466 //! Partial specialization for 5- \c tuple
467 template <class T1,
468  class T2,
469  class T3,
470  class T4,
471  class T5
472  >
473 struct tuple<T1, T2, T3, T4, T5, Plug>
474 {
475  //! First tuple component type
476  typedef T1 first_type;
477  //! Second tuple component type
478  typedef T2 second_type;
479  //! Third tuple component type
480  typedef T3 third_type;
481  //! Fourth tuple component type
482  typedef T4 fourth_type;
483  //! Fifth tuple component type
484  typedef T5 fifth_type;
485 
486 
487  template <int I>
488  struct item_type
489  {
490  typedef typename SWITCH<I, CASE<1, first_type,
491  CASE<2, second_type,
492  CASE<3, third_type,
493  CASE<4, fourth_type,
494  CASE<5, fifth_type,
495  CASE<DEFAULT, void
496  > > > > > > >::result result;
497  };
498 
499  //! First tuple component
501  //! Second tuple component
503  //! Third tuple component
505  //! Fourth tuple component
507  //! Fifth tuple component
509 
510  //! Empty constructor
511  tuple() { }
512 
513  //! Construct tuple from components
515  second_type _second,
516  third_type _third,
517  fourth_type _fourth,
518  fifth_type _fifth)
519  : first(_first),
520  second(_second),
521  third(_third),
522  fourth(_fourth),
523  fifth(_fifth)
524  { }
525 
526  //! Equality comparison
527  bool operator == (const tuple& t) const
528  {
529  return first == t.first && second == t.second && third == t.third
530  && fourth == t.fourth && fifth == t.fifth;
531  }
532 
533  //! Inequality comparison
534  bool operator != (const tuple& t) const
535  {
536  return !(first == t.first && second == t.second && third == t.third
537  && fourth == t.fourth && fifth == t.fifth);
538  }
539 
540  //! Make tuple ostream-able
541  friend std::ostream& operator << (std::ostream& os, const tuple& t)
542  {
543  return os << '(' << t.first << ',' << t.second << ',' << t.third
544  << ',' << t.fourth << ',' << t.fifth << ')';
545  }
546 
547  //! Return minimum value of tuple using numeric_limits
548  static tuple min_value()
549  {
555  }
556 
557  //! Return maximum value of tuple using numeric_limits
558  static tuple max_value()
559  {
565  }
566 };
567 
568 /*
569  template <class tuple_type,int I>
570  typename tuple_type::item_type<I>::result get(const tuple_type & t)
571  {
572  return NULL;
573  }
574 */
575 
576 template <typename TupleType>
578 {
579  typedef TupleType value_type;
580 
581  bool operator () (const value_type& a, const value_type& b) const
582  {
583  return (a.first < b.first);
584  }
585 
586  static value_type min_value() { return value_type::min_value(); }
587  static value_type max_value() { return value_type::max_value(); }
588 };
589 
590 template <typename TupleType>
592 {
593  typedef TupleType value_type;
594 
595  bool operator () (const value_type& a, const value_type& b) const
596  {
597  return (a.first > b.first);
598  }
599 
600  static value_type min_value() { return value_type::max_value(); }
601  static value_type max_value() { return value_type::min_value(); }
602 };
603 
604 template <typename TupleType>
606 {
607  typedef TupleType value_type;
608 
609  bool operator () (const value_type& a, const value_type& b) const
610  {
611  if (a.first == b.first)
612  return (a.second < b.second);
613  return (a.first < b.first);
614  }
615 
616  static value_type min_value() { return value_type::min_value(); }
617  static value_type max_value() { return value_type::max_value(); }
618 };
619 
620 template <typename TupleType>
622 {
623  typedef TupleType value_type;
624 
625  bool operator () (const value_type& a, const value_type& b) const
626  {
627  return (a.second < b.second);
628  }
629 
630  static value_type min_value() { return value_type::min_value(); }
631  static value_type max_value() { return value_type::max_value(); }
632 };
633 
634 namespace stream {
635 
636 /**
637  * Counter for creating tuple indexes for example.
638  */
639 template <class ValueType>
640 struct counter
641 {
642 public:
643  typedef ValueType value_type;
644 
645 protected:
647 
648 public:
649  counter(const value_type& start = 0)
650  : m_count(start)
651  { }
652 
653  const value_type& operator * () const
654  {
655  return m_count;
656  }
657 
659  {
660  ++m_count;
661  return *this;
662  }
663 
664  bool empty() const
665  {
666  return false;
667  }
668 };
669 
670 
671 /**
672  * Concatenates two tuple streams as streamA . streamB
673  */
674 template <class StreamA, class StreamB>
676 {
677 public:
678  typedef typename StreamA::value_type value_type;
679 
680 private:
681  StreamA& A;
682  StreamB& B;
683 
684 public:
685  concatenate(StreamA& A_, StreamB& B_) : A(A_), B(B_)
686  {
687  assert(!A.empty());
688  assert(!B.empty());
689  }
690 
691  const value_type& operator * () const
692  {
693  assert(!empty());
694 
695  if (!A.empty()) {
696  return *A;
697  }
698  else {
699  return *B;
700  }
701  }
702 
704  {
705  assert(!empty());
706 
707  if (!A.empty()) {
708  ++A;
709  }
710  else if (!B.empty()) {
711  ++B;
712  }
713 
714  return *this;
715  }
716 
717  bool empty() const
718  {
719  return (A.empty() && B.empty());
720  }
721 };
722 
723 } // namespace stream
724 
726 
727 #endif // !STXXL_COMMON_TUPLE_HEADER
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:288
T2 second_type
Second tuple component type.
Definition: tuple.h:306
const int DEFAULT
Definition: tmeta.h:63
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:558
first_type first
First tuple component.
Definition: tuple.h:404
TupleType value_type
Definition: tuple.h:593
static value_type min_value()
Definition: tuple.h:630
T5 fifth_type
Fifth tuple component type.
Definition: tuple.h:80
friend std::ostream & operator<<(std::ostream &os, const uint_pair &a)
make a uint_pair outputtable via iostreams, using unsigned long long.
Definition: uint_types.h:231
bool empty() const
Definition: tuple.h:664
counter(const value_type &start=0)
Definition: tuple.h:649
second_type second
Second tuple component.
Definition: tuple.h:100
second_type second
Second tuple component.
Definition: tuple.h:502
bool empty() const
Definition: tuple.h:717
T6 sixth_type
Sixth tuple component type.
Definition: tuple.h:82
T4 fourth_type
Fourth tuple component type.
Definition: tuple.h:482
second_type second
Second tuple component.
Definition: tuple.h:250
StreamA::value_type value_type
Definition: tuple.h:678
static value_type max_value()
Definition: tuple.h:617
TupleType value_type
Definition: tuple.h:623
IF< I==1, first_type, void >::result result
Definition: tuple.h:187
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:223
T2 second_type
Second tuple component type.
Definition: tuple.h:236
T1 first_type
First tuple component type.
Definition: tuple.h:179
tuple(first_type _first, second_type _second, third_type _third, fourth_type _fourth)
Construct tuple from components.
Definition: tuple.h:416
static uint_pair min()
return an uint_pair instance containing the smallest value possible
Definition: uint_types.h:237
third_type third
Third tuple component.
Definition: tuple.h:102
tuple()
Empty constructor.
Definition: tuple.h:111
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:217
tuple(first_type first_, second_type second_)
Initializing constructor.
Definition: tuple.h:256
first_type first
First tuple component.
Definition: tuple.h:98
ValueType value_type
Definition: tuple.h:643
T4 fourth_type
Fourth tuple component type.
Definition: tuple.h:78
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:166
T1 first_type
First tuple component type.
Definition: tuple.h:72
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:152
fourth_type fourth
Fourth tuple component.
Definition: tuple.h:104
T3 third_type
Third tuple component type.
Definition: tuple.h:388
static value_type max_value()
Definition: tuple.h:587
second_type second
Second tuple component.
Definition: tuple.h:323
T1 first_type
First tuple component type.
Definition: tuple.h:234
fifth_type fifth
Fifth tuple component.
Definition: tuple.h:106
bool operator!=(const uint_pair &b) const
inequality checking operator
Definition: uint_types.h:201
third_type third
Third tuple component.
Definition: tuple.h:408
third_type third
Third tuple component.
Definition: tuple.h:325
value_type m_count
Definition: tuple.h:646
first_type first
First tuple component.
Definition: tuple.h:500
static value_type min_value()
Definition: tuple.h:600
static value_type min_value()
Definition: tuple.h:616
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:548
T2 second_type
Second tuple component type.
Definition: tuple.h:386
tuple(first_type first_)
Initializing constructor.
Definition: tuple.h:194
second_type second
Second tuple component.
Definition: tuple.h:406
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:359
SWITCH< I, CASE< 1, first_type, CASE< 2, second_type, CASE< 3, third_type, CASE< 4, fourth_type, CASE< 5, fifth_type, CASE< DEFAULT, void > > > > > > >::result result
Definition: tuple.h:496
SWITCH< I, CASE< 1, first_type, CASE< 2, second_type, CASE< 3, second_type, CASE< DEFAULT, void > > > > >::result result
Definition: tuple.h:317
T1 first_type
First tuple component type.
Definition: tuple.h:384
T3 third_type
Third tuple component type.
Definition: tuple.h:76
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:244
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:367
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:163
first_type first
First tuple component.
Definition: tuple.h:182
third_type third
Third tuple component.
Definition: tuple.h:504
static value_type min_value()
Definition: tuple.h:586
fifth_type fifth
Fifth tuple component.
Definition: tuple.h:508
tuple(first_type _first, second_type _second, third_type _third, fourth_type _fourth, fifth_type _fifth, sixth_type _sixth)
Construct tuple from components.
Definition: tuple.h:114
T1 first_type
First tuple component type.
Definition: tuple.h:476
TupleType value_type
Definition: tuple.h:579
T3 third_type
Third tuple component type.
Definition: tuple.h:308
T4 fourth_type
Fourth tuple component type.
Definition: tuple.h:390
sixth_type sixth
Sixth tuple component.
Definition: tuple.h:108
T2 second_type
Second tuple component type.
Definition: tuple.h:478
T1 first_type
First tuple component type.
Definition: tuple.h:304
static value_type max_value()
Definition: tuple.h:601
tuple(first_type _first, second_type _second, third_type _third, fourth_type _fourth, fifth_type _fifth)
Construct tuple from components.
Definition: tuple.h:514
Type1 result
Definition: tmeta.h:33
static value_type max_value()
Definition: tuple.h:631
SWITCH< I, CASE< 1, first_type, CASE< 2, second_type, CASE< 3, third_type, CASE< 4, fourth_type, CASE< 5, fifth_type, CASE< 6, sixth_type, CASE< DEFAULT, void > > > > > > > >::result result
Definition: tuple.h:94
T2 second_type
Second tuple component type.
Definition: tuple.h:74
T3 third_type
Third tuple component type.
Definition: tuple.h:480
first_type first
First tuple component.
Definition: tuple.h:321
static tuple max_value()
Return maximum value of tuple using numeric_limits.
Definition: tuple.h:457
fourth_type fourth
Fourth tuple component.
Definition: tuple.h:506
T5 fifth_type
Fifth tuple component type.
Definition: tuple.h:484
first_type first
First tuple component.
Definition: tuple.h:248
SWITCH< I, CASE< 1, first_type, CASE< 2, second_type, CASE< DEFAULT, void > > > >::result result
Definition: tuple.h:244
fourth_type fourth
Fourth tuple component.
Definition: tuple.h:410
SWITCH< I, CASE< 1, first_type, CASE< 2, second_type, CASE< 3, third_type, CASE< 4, fourth_type, CASE< DEFAULT, void > > > > > >::result result
Definition: tuple.h:400
tuple(first_type _first, second_type _second, third_type _third)
Construct tuple from components.
Definition: tuple.h:331
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:281
k-Tuple data type
Definition: tuple.h:69
bool operator==(const uint_pair &b) const
equality checking operator
Definition: uint_types.h:195
#define STXXL_END_NAMESPACE
Definition: namespace.h:17
concatenate(StreamA &A_, StreamB &B_)
Definition: tuple.h:685
static tuple min_value()
Return minimum value of tuple using numeric_limits.
Definition: tuple.h:448