Go to Overview over all GrAL packages.
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Container/mapped-value-iterator.h

Go to the documentation of this file.
00001 #ifndef NMWR_GB_MAPPED_VALUE_ITERATOR_H
00002 #define NMWR_GB_MAPPED_VALUE_ITERATOR_H
00003 
00004 
00005 // $LICENSE
00006 
00007 
00008 //----------------------------------------------------------------
00015 //----------------------------------------------------------------
00016 
00017 
00018 
00019 
00033 template<class P>
00034 struct get_first {
00035   typedef P                       argument_type;
00036   typedef typename P::first_type  result_type;
00037   typedef typename P::first_type  value_type;
00038 
00039   // this does not work if pair<const T1, T2> (used in hash_map)
00040   //  static       value_type& value(      P& p) { return p.first;}
00041   static 
00042   value_type const& value(const P& p)            { return p.first;}
00043   value_type const& operator()(P const& p) const { return p.first;}
00044 };
00045 
00051 template<class P>
00052 struct get_second {
00053   typedef P                       argument_type;
00054   typedef typename P::second_type result_type;
00055   typedef typename P::second_type value_type;
00056 
00057   static 
00058   value_type const& value(const P& p)            { return p.second;}
00059   value_type const& operator()(P const& p) const { return p.second;}
00060 };
00061 
00062 
00063 
00076 template<class It, class F>
00077 class mapped_value_const_iterator {
00078 public:
00079   typedef typename F::value_type value_type;
00080 private:
00081   It it;
00082   typedef mapped_value_const_iterator<It,F> self;
00083 public:
00084   mapped_value_const_iterator(const It& i) : it(i) {}
00085 
00086   self& operator++() { ++it; return *this;}
00087   const value_type&  operator*() const { return F::value(*it);}
00088   //  friend
00089   bool operator==(const self& rs) const
00090     { return (it == rs.it);}
00091   bool operator!=(const self& rs) const
00092     { return !(it == rs.it);}
00093 };
00094 
00095 
00096 template<class It, class F>
00097 inline mapped_value_const_iterator<It,F>
00098 map_iter_c(It i, const F& f) { return mapped_value_const_iterator<It,F>(i);}
00099 
00100 template<class It>
00101 inline mapped_value_const_iterator<It,get_first<typename It::value_type> >
00102 get1st_c(It i) {
00103   typedef typename It::value_type v_type;
00104   return mapped_value_const_iterator<It,get_first<v_type> >(i);
00105 }
00106 
00107 template<class It>
00108 inline mapped_value_const_iterator<It,get_second<typename It::value_type> >
00109 get2nd_c(It i) { 
00110   typedef typename It::value_type v_type;
00111   return mapped_value_const_iterator<It,get_second<v_type> >(i);
00112 }
00113 
00127 template<class It, class F>
00128 class mapped_value_iterator {
00129 private:
00130   It it;
00131   typedef mapped_value_iterator<It,F>  self;
00132 public:
00133   typedef typename F::value_type value_type;
00134 
00135   mapped_value_iterator(const It& i) : it(i) {}
00136 
00137   self& operator++() { ++it; return *this;}
00138   value_type&  operator*() const { return F::value(*it);}
00139   //friend 
00140   bool operator==(const self& rs) const
00141     { return (it == rs.it);}
00142   bool operator!=(const self& rs) const
00143     { return !(it == rs.it);}
00144 };
00145 
00146 
00147 
00148 template<class It, class F>
00149 inline mapped_value_iterator<It,F>
00150 map_iter(It i, const F& f) { return mapped_value_iterator<It,F>(i);}
00151 
00152 template<class It>
00153 inline mapped_value_iterator<It,get_first<typename It::value_type> >
00154 get1st(It i) { 
00155   typedef typename It::value_type v_type;
00156   return mapped_value_iterator<It,get_first<v_type> >(i);
00157 }
00158 
00159 template<class It>
00160 inline mapped_value_iterator<It,get_second<typename It::value_type> >
00161 get2nd(It i) { 
00162   typedef typename It::value_type v_type;
00163   return mapped_value_iterator<It,get_second<v_type> >(i);
00164 }
00165 
00166 //----------------------------------------------------------------
00167 //        iterator traits for the above templates
00168 //----------------------------------------------------------------
00169 
00170 // requires partial specialization
00171 
00172 namespace std {
00173   template<typename It> struct iterator_traits;
00174   
00175   template<class It, class F>
00176     struct iterator_traits<mapped_value_const_iterator<It,F> >
00177     {
00178       typedef iterator_traits<It> bt;
00179       typedef typename bt::iterator_category iterator_category;
00180       typedef typename bt::difference_type   difference_type;
00181       
00182       typedef typename F::result_type        value_type;
00183       typedef const value_type&              reference;
00184       typedef const value_type*              pointer;
00185     };
00186 
00187 
00188   template<class It, class F>
00189     struct iterator_traits<mapped_value_iterator<It,F> >
00190     {
00191       typedef iterator_traits<It> bt;
00192       typedef typename bt::iterator_category iterator_category;
00193       typedef typename bt::difference_type   difference_type;
00194       
00195       typedef typename F::result_type        value_type;
00196       typedef value_type&                    reference;
00197       typedef value_type*                    pointer;
00198     };
00199   
00200 } // namespace std
00201 
00202 #endif
00203 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:14 2002 for Sequence by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000