Sierra Toolkit  Version of the Day
PairIter.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef stk_util_util_PairIter_hpp
10 #define stk_util_util_PairIter_hpp
11 
12 #include <utility>
13 #include <iterator>
14 
15 namespace stk_classic {
16 
17 
18 template< class IterType ,
19  class IterCategory =
20  typename std::iterator_traits< IterType >::iterator_category >
21 class PairIter ;
22 
23 //----------------------------------------------------------------------
24 // Specialized for random access iterators, others TBD.
25 
29 
34 template< class IterType >
35 class PairIter< IterType , std::random_access_iterator_tag >
36  : public std::pair< IterType , IterType >
37 {
38 private:
39  typedef std::pair< IterType , IterType > Pair ;
41  typedef std::iterator_traits< IterType > Traits ;
42 public:
43 
44  //--------------------------------
45 
46  typedef IterType iterator ;
47  typedef typename Traits::value_type value_type ;
48  typedef typename Traits::pointer pointer ;
49  typedef typename Traits::reference reference ;
50  typedef typename Traits::difference_type difference_type ;
51  typedef size_t size_type ;
52 
53  //--------------------------------
54 
57  ~PairIter() {}
58 
60  PairIter() : Pair() { Pair::second = Pair::first ; }
61 
62  PairIter( const Self & rhs ) : Pair( rhs ) {}
63 
64  PairIter( const Pair & rhs ) : Pair( rhs ) {}
65 
66  Self & operator = ( const Self & rhs )
67  { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
68 
69  Self & operator = ( const Pair & rhs )
70  { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
71 
72  //--------------------------------
73 
74  bool operator == ( const Self & rhs ) const
75  { return Pair::first == rhs.first && Pair::second == rhs.second ; }
76 
77  bool operator != ( const Self & rhs ) const
78  { return Pair::first != rhs.first || Pair::second != rhs.second ; }
79 
80  bool operator == ( const Pair & rhs ) const
81  { return Pair::first == rhs.first && Pair::second == rhs.second ; }
82 
83  bool operator != ( const Pair & rhs ) const
84  { return Pair::first != rhs.first || Pair::second != rhs.second ; }
85 
86  //--------------------------------
87 
88  Self & operator ++ () { ++ Pair::first ; return *this ; }
89 
90  Self operator ++ (int) { Self tmp(*this); ++ Pair::first ; return tmp ; }
91 
92  reference operator * () const { return * Pair::first ; }
93  pointer operator -> () const { return & * Pair::first ; }
94 
95  //--------------------------------
96  // Container-like functionality for random access iterators.
97 
98  reference front() const { return * Pair::first ; }
99  reference back() const { return Pair::second[-1] ; }
100 
101  iterator begin() const { return Pair::first ; }
102  iterator end() const { return Pair::second ; }
103 
104  template<class Iterator>
105  PairIter( Iterator i , Iterator e ) : Pair(i,e) {}
106 
107  template<class Container>
108  explicit
109  PairIter( const Container & c ) : Pair( c.begin() , c.end() ) {}
110 
111  template<class Container>
112  explicit
113  PairIter( Container & c ) : Pair( c.begin() , c.end() ) {}
114 
115  bool empty () const { return ! ( Pair::first < Pair::second ) ; }
116 
117  reference operator [] ( size_t n ) const { return Pair::first[n] ; }
118 
119  size_t size() const
120  {
121  const difference_type d = std::distance( Pair::first , Pair::second );
122  return d < 0 ? 0 : (size_t) d ;
123  }
124 };
125 
129 
130 } // namespace stk_classic
131 
132 #endif
133 
Sierra Toolkit.