phdMesh  Version of the Day
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups
PairIter.hpp
1 /*------------------------------------------------------------------------*/
2 /* phdMesh : Parallel Heterogneous Dynamic unstructured Mesh */
3 /* Copyright (2007) Sandia Corporation */
4 /* */
5 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
6 /* license for use of this work by or on behalf of the U.S. Government. */
7 /* */
8 /* This library is free software; you can redistribute it and/or modify */
9 /* it under the terms of the GNU Lesser General Public License as */
10 /* published by the Free Software Foundation; either version 2.1 of the */
11 /* License, or (at your option) any later version. */
12 /* */
13 /* This library is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */
16 /* Lesser General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU Lesser General Public */
19 /* License along with this library; if not, write to the Free Software */
20 /* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 */
21 /* USA */
22 /*------------------------------------------------------------------------*/
23 
24 #ifndef util_PairIter_hpp
25 #define util_PairIter_hpp
26 
27 #include <utility>
28 #include <iterator>
29 
30 namespace phdmesh {
31 
39 template< class IterType ,
40  class IterCategory =
41  typename std::iterator_traits< IterType >::iterator_category >
42 class PairIter {};
43 
44 //----------------------------------------------------------------------
45 // Specialized for random access iterators, others TBD.
46 
51 template< class IterType >
52 class PairIter< IterType , std::random_access_iterator_tag >
53  : public std::pair< IterType , IterType >
54 {
55 private:
56  typedef std::pair< IterType , IterType > Pair ;
58  typedef std::iterator_traits< IterType > Traits ;
59 public:
60 
61  //--------------------------------
62 
63  typedef IterType iterator ;
64  typedef typename Traits::value_type value_type ;
65  typedef typename Traits::pointer pointer ;
66  typedef typename Traits::reference reference ;
67  typedef typename Traits::difference_type difference_type ;
68  typedef size_t size_type ;
69 
70  //--------------------------------
71 
72  ~PairIter() {}
73 
74  PairIter() : Pair() { Pair::second = Pair::first ; }
75 
76  PairIter( const Self & rhs ) : Pair( rhs ) {}
77 
78  PairIter( const Pair & rhs ) : Pair( rhs ) {}
79 
80  Self & operator = ( const Self & rhs )
81  { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
82 
83  Self & operator = ( const Pair & rhs )
84  { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
85 
86  //--------------------------------
87 
88  bool operator == ( const Self & rhs ) const
89  { return Pair::first == rhs.first && Pair::second == rhs.second ; }
90 
91  bool operator != ( const Self & rhs ) const
92  { return Pair::first != rhs.first || Pair::second != rhs.second ; }
93 
94  bool operator == ( const Pair & rhs ) const
95  { return Pair::first == rhs.first && Pair::second == rhs.second ; }
96 
97  bool operator != ( const Pair & rhs ) const
98  { return Pair::first != rhs.first || Pair::second != rhs.second ; }
99 
100  //--------------------------------
101 
102  Self & operator ++ () { ++ Pair::first ; return *this ; }
103 
104  Self operator ++ (int) { Self tmp(*this); ++ Pair::first ; return tmp ; }
105 
106  reference operator * () const { return * Pair::first ; }
107  pointer operator -> () const { return & * Pair::first ; }
108 
109  //--------------------------------
110  // Container-like functionality for random access iterators.
111 
112  reference front() const { return * Pair::first ; }
113  reference back() const { return Pair::second[-1] ; }
114 
115  iterator begin() const { return Pair::first ; }
116  iterator end() const { return Pair::second ; }
117 
118  template<class Iterator>
119  PairIter( Iterator i , Iterator e ) : Pair(i,e) {}
120 
121  template<class Container>
122  explicit
123  PairIter( const Container & c ) : Pair( c.begin() , c.end() ) {}
124 
125  template<class Container>
126  explicit
127  PairIter( Container & c ) : Pair( c.begin() , c.end() ) {}
128 
129  bool empty () const { return ! ( Pair::first < Pair::second ) ; }
130 
131  operator bool () const { return Pair::first < Pair::second ; }
132 
133  reference operator [] ( size_t n ) const { return Pair::first[n] ; }
134 
135  size_t size() const
136  {
137  const difference_type d = std::distance( Pair::first , Pair::second );
138  return d < 0 ? 0 : (size_t) d ;
139  }
140 };
141 
142 } // namespace phdmesh
143 
144 #endif /* util_PairIter_hpp */
145 
Pair of begin and end iterators wrapped to provide a container-like view of the span.
Definition: PairIter.hpp:52
Pair of begin and end iterators wrapped to provide a container-like view of the span.
Definition: PairIter.hpp:42