yat  0.8.3pre
WeightedIterator.h
00001 #ifndef _theplu_yat_utility_weighted_iterator_
00002 #define _theplu_yat_utility_weighted_iterator_
00003 
00004 // $Id: WeightedIterator.h 2375 2010-12-13 03:28:46Z peter $
00005 
00006 /*
00007   Copyright (C) 2008, 2009, 2010 Peter Johansson
00008 
00009   This file is part of the yat library, http://dev.thep.lu.se/yat
00010 
00011   The yat library is free software; you can redistribute it and/or
00012   modify it under the terms of the GNU General Public License as
00013   published by the Free Software Foundation; either version 3 of the
00014   License, or (at your option) any later version.
00015 
00016   The yat library is distributed in the hope that it will be useful,
00017   but WITHOUT ANY WARRANTY; without even the implied warranty of
00018   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019   General Public License for more details.
00020 
00021   You should have received a copy of the GNU General Public License
00022   along with yat. If not, see <http://www.gnu.org/licenses/>.
00023 */
00024 
00025 #include "DataWeight.h"
00026 #include "DataWeightProxy.h"
00027 
00028 #include <boost/iterator/iterator_facade.hpp>
00029 
00030 #include <iterator>
00031 
00032 namespace theplu {
00033 namespace yat {
00034 namespace utility {
00035 
00057   template<typename DataIterator, typename WeightIterator>
00058   class WeightedIterator
00059     : public boost::iterator_facade<
00060     WeightedIterator<DataIterator, WeightIterator>,
00061     DataWeight,
00062     typename std::iterator_traits<DataIterator>::iterator_category,
00063     DataWeightProxy<DataIterator, WeightIterator> >
00064     
00065   {
00066   public:
00070     typedef DataIterator data_iterator;
00071 
00075     typedef WeightIterator weight_iterator;
00076 
00084     WeightedIterator(void)
00085     {}
00086 
00090     WeightedIterator(DataIterator d, WeightIterator w)
00091       : d_iter_(d), w_iter_(w)
00092     {}
00093     
00097     const DataIterator& data_base(void) const { return d_iter_; }
00098 
00102     const WeightIterator& weight_base(void) const { return w_iter_; }
00103 
00107     DataWeightProxy<DataIterator, WeightIterator> operator[](int n) const
00108     {
00109       return DataWeightProxy<DataIterator, WeightIterator>(d_iter_+n, 
00110                                                            w_iter_+n); 
00111     }     
00112     
00113 
00122     template<typename D2, typename W2>
00123     WeightedIterator(WeightedIterator<D2, W2> other,
00124             typename boost::enable_if_convertible<D2,DataIterator>::type* = 0,
00125             typename boost::enable_if_convertible<W2,WeightIterator>::type* = 0)
00126       : d_iter_(other.data_base()), w_iter_(other.weight_base()) {}
00127 
00128 
00129   private:
00130     friend class boost::iterator_core_access;
00131 
00132     DataIterator d_iter_;
00133     WeightIterator w_iter_;
00134     
00135     void advance(size_t n)
00136     { std::advance(d_iter_, n); std::advance(w_iter_, n); }
00137 
00138     void decrement(void) { --d_iter_; --w_iter_; }
00139     
00140     typename std::iterator_traits<DataIterator>::difference_type 
00141     distance_to(const WeightedIterator& other) const
00142     { return std::distance(d_iter_, other.d_iter_); }
00143 
00144     utility::DataWeightProxy<DataIterator, WeightIterator> 
00145     dereference(void) const 
00146     { return DataWeightProxy<DataIterator, WeightIterator>(d_iter_, 
00147                                                            w_iter_); 
00148     }
00149 
00150     bool equal(const WeightedIterator& other) const
00151     { return d_iter_==other.d_iter_ && w_iter_==other.w_iter_; }
00152 
00153     void increment(void) { ++d_iter_; ++w_iter_; }
00154 
00155   };
00156 
00164   template<typename DataIterator, typename WeightIterator>
00165   WeightedIterator<DataIterator, WeightIterator> 
00166   weighted_iterator(DataIterator data, WeightIterator weight)
00167   {
00168     return WeightedIterator<DataIterator, WeightIterator>(data, weight);
00169   }
00170   
00171 }}} // of namespace utility, yat, and theplu
00172 
00173 #endif

Generated on Thu Dec 20 2012 03:12:58 for yat by  doxygen 1.8.0-20120409