phdMesh Version of the Day
PairIter.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*      phdMesh : Parallel Heterogneous Dynamic unstructured Mesh         */
00003 /*                Copyright (2007) Sandia Corporation                     */
00004 /*                                                                        */
00005 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00006 /*  license for use of this work by or on behalf of the U.S. Government.  */
00007 /*                                                                        */
00008 /*  This library is free software; you can redistribute it and/or modify  */
00009 /*  it under the terms of the GNU Lesser General Public License as        */
00010 /*  published by the Free Software Foundation; either version 2.1 of the  */
00011 /*  License, or (at your option) any later version.                       */
00012 /*                                                                        */
00013 /*  This library is distributed in the hope that it will be useful,       */
00014 /*  but WITHOUT ANY WARRANTY; without even the implied warranty of        */
00015 /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     */
00016 /*  Lesser General Public License for more details.                       */
00017 /*                                                                        */
00018 /*  You should have received a copy of the GNU Lesser General Public      */
00019 /*  License along with this library; if not, write to the Free Software   */
00020 /*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307   */
00021 /*  USA                                                                   */
00022 /*------------------------------------------------------------------------*/
00023 
00024 #ifndef util_PairIter_hpp
00025 #define util_PairIter_hpp
00026 
00027 #include <utility>
00028 #include <iterator>
00029 
00030 namespace phdmesh {
00031 
00039 template< class IterType ,
00040           class IterCategory =
00041              typename std::iterator_traits< IterType >::iterator_category >
00042 class PairIter {};
00043 
00044 //----------------------------------------------------------------------
00045 // Specialized for random access iterators, others TBD.
00046 
00051 template< class IterType >
00052 class PairIter< IterType , std::random_access_iterator_tag > 
00053   : public std::pair< IterType , IterType >
00054 {
00055 private:
00056   typedef std::pair< IterType , IterType > Pair ;
00057   typedef PairIter< IterType , std::random_access_iterator_tag > Self ;
00058   typedef std::iterator_traits< IterType > Traits ;
00059 public:
00060 
00061   //--------------------------------
00062 
00063   typedef          IterType                iterator ;
00064   typedef typename Traits::value_type      value_type ;
00065   typedef typename Traits::pointer         pointer ;
00066   typedef typename Traits::reference       reference ;
00067   typedef typename Traits::difference_type difference_type ;
00068   typedef          size_t                  size_type ;
00069 
00070   //--------------------------------
00071 
00072   ~PairIter() {}
00073 
00074   PairIter() : Pair() { Pair::second = Pair::first ; }
00075 
00076   PairIter( const Self & rhs ) : Pair( rhs ) {}
00077 
00078   PairIter( const Pair & rhs ) : Pair( rhs ) {}
00079 
00080   Self & operator = ( const Self & rhs )
00081     { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
00082 
00083   Self & operator = ( const Pair & rhs )
00084     { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
00085 
00086   //--------------------------------
00087 
00088   bool operator == ( const Self & rhs ) const
00089     { return Pair::first == rhs.first && Pair::second == rhs.second ; }
00090 
00091   bool operator != ( const Self & rhs ) const
00092     { return Pair::first != rhs.first || Pair::second != rhs.second ; }
00093 
00094   bool operator == ( const Pair & rhs ) const
00095     { return Pair::first == rhs.first && Pair::second == rhs.second ; }
00096 
00097   bool operator != ( const Pair & rhs ) const
00098     { return Pair::first != rhs.first || Pair::second != rhs.second ; }
00099 
00100   //--------------------------------
00101 
00102   Self & operator ++ () { ++ Pair::first ; return *this ; }
00103 
00104   Self operator ++ (int) { Self tmp(*this); ++ Pair::first ; return tmp ; }
00105 
00106   reference operator * ()  const { return * Pair::first ; }
00107   pointer   operator -> () const { return & * Pair::first ; }
00108 
00109   //--------------------------------
00110   // Container-like functionality for random access iterators.
00111 
00112   reference front() const { return * Pair::first ; }
00113   reference back()  const { return  Pair::second[-1] ; }
00114 
00115   iterator begin() const { return  Pair::first ; }
00116   iterator end()   const { return  Pair::second ; }
00117 
00118   template<class Iterator>
00119   PairIter( Iterator i , Iterator e ) : Pair(i,e) {}
00120 
00121   template<class Container>
00122   explicit
00123   PairIter( const Container & c ) : Pair( c.begin() , c.end() ) {}
00124 
00125   template<class Container>
00126   explicit
00127   PairIter( Container & c ) : Pair( c.begin() , c.end() ) {}
00128 
00129   bool empty () const { return ! ( Pair::first < Pair::second ) ; }
00130 
00131   operator bool () const { return Pair::first < Pair::second ; }
00132 
00133   reference operator [] ( size_t n ) const { return Pair::first[n] ; }
00134 
00135   size_t size() const
00136     {
00137       const difference_type d = std::distance( Pair::first , Pair::second );
00138       return d < 0 ? 0 : (size_t) d ;
00139     }
00140 };
00141 
00142 } // namespace phdmesh
00143 
00144 #endif /* util_PairIter_hpp */
00145 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator