Sierra Toolkit Version of the Day
PairIter.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef stk_util_util_PairIter_hpp
00010 #define stk_util_util_PairIter_hpp
00011 
00012 #include <utility>
00013 #include <iterator>
00014 
00015 namespace stk {
00016 
00017 
00018 template< class IterType ,
00019           class IterCategory =
00020              typename std::iterator_traits< IterType >::iterator_category >
00021 class PairIter ;
00022 
00023 //----------------------------------------------------------------------
00024 // Specialized for random access iterators, others TBD.
00025 
00029 
00034 template< class IterType >
00035 class PairIter< IterType , std::random_access_iterator_tag > 
00036   : public std::pair< IterType , IterType >
00037 {
00038 private:
00039   typedef std::pair< IterType , IterType > Pair ;
00040   typedef PairIter< IterType , std::random_access_iterator_tag > Self ;
00041   typedef std::iterator_traits< IterType > Traits ;
00042 public:
00043 
00044   //--------------------------------
00045 
00046   typedef          IterType                iterator ;
00047   typedef typename Traits::value_type      value_type ;
00048   typedef typename Traits::pointer         pointer ;
00049   typedef typename Traits::reference       reference ;
00050   typedef typename Traits::difference_type difference_type ;
00051   typedef          size_t                  size_type ;
00052 
00053   //--------------------------------
00054 
00057   ~PairIter() {}
00058 
00060   PairIter() : Pair() { Pair::second = Pair::first ; }
00061 
00062   PairIter( const Self & rhs ) : Pair( rhs ) {}
00063 
00064   PairIter( const Pair & rhs ) : Pair( rhs ) {}
00065 
00066   Self & operator = ( const Self & rhs )
00067     { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
00068 
00069   Self & operator = ( const Pair & rhs )
00070     { Pair::first = rhs.first ; Pair::second = rhs.second ; return *this ; }
00071 
00072   //--------------------------------
00073 
00074   bool operator == ( const Self & rhs ) const
00075     { return Pair::first == rhs.first && Pair::second == rhs.second ; }
00076 
00077   bool operator != ( const Self & rhs ) const
00078     { return Pair::first != rhs.first || Pair::second != rhs.second ; }
00079 
00080   bool operator == ( const Pair & rhs ) const
00081     { return Pair::first == rhs.first && Pair::second == rhs.second ; }
00082 
00083   bool operator != ( const Pair & rhs ) const
00084     { return Pair::first != rhs.first || Pair::second != rhs.second ; }
00085 
00086   //--------------------------------
00087 
00088   Self & operator ++ () { ++ Pair::first ; return *this ; }
00089 
00090   Self operator ++ (int) { Self tmp(*this); ++ Pair::first ; return tmp ; }
00091 
00092   reference operator * ()  const { return * Pair::first ; }
00093   pointer   operator -> () const { return & * Pair::first ; }
00094 
00095   //--------------------------------
00096   // Container-like functionality for random access iterators.
00097 
00098   reference front() const { return * Pair::first ; }
00099   reference back()  const { return  Pair::second[-1] ; }
00100 
00101   iterator begin() const { return  Pair::first ; }
00102   iterator end()   const { return  Pair::second ; }
00103 
00104   template<class Iterator>
00105   PairIter( Iterator i , Iterator e ) : Pair(i,e) {}
00106 
00107   template<class Container>
00108   explicit
00109   PairIter( const Container & c ) : Pair( c.begin() , c.end() ) {}
00110 
00111   template<class Container>
00112   explicit
00113   PairIter( Container & c ) : Pair( c.begin() , c.end() ) {}
00114 
00115   bool empty () const { return ! ( Pair::first < Pair::second ) ; }
00116 
00117   reference operator [] ( size_t n ) const { return Pair::first[n] ; }
00118 
00119   size_t size() const
00120     {
00121       const difference_type d = std::distance( Pair::first , Pair::second );
00122       return d < 0 ? 0 : (size_t) d ;
00123     }
00124 };
00125 
00129 
00130 } // namespace stk
00131 
00132 #endif
00133 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines