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