yat  0.14.5pre
WeightedIterator.h
1 #ifndef _theplu_yat_utility_weighted_iterator_
2 #define _theplu_yat_utility_weighted_iterator_
3 
4 // $Id: WeightedIterator.h 3461 2016-01-22 01:29:13Z peter $
5 
6 /*
7  Copyright (C) 2008, 2009, 2010, 2016 Peter Johansson
8 
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10 
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15 
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "DataWeight.h"
26 #include "DataWeightProxy.h"
27 
28 #include <boost/iterator/is_readable_iterator.hpp>
29 #include <boost/iterator/iterator_facade.hpp>
30 #include <boost/mpl/and.hpp>
31 #include <boost/mpl/bool.hpp>
32 #include <boost/mpl/eval_if.hpp>
33 #include <boost/type_traits/is_convertible.hpp>
34 #include <boost/utility/enable_if.hpp>
35 
36 #include <iterator>
37 
38 namespace theplu {
39 namespace yat {
40 namespace utility {
41 
42  namespace detail {
43  namespace weighted_iterator {
44 
51  template<typename DataIterator, typename WeightIterator>
52  class Traversal
53  {
54  typedef typename boost::iterator_traversal<DataIterator>::type T1;
55  typedef typename boost::iterator_traversal<WeightIterator>::type T2;
56  public:
59  typedef typename boost::mpl::if_<
60  boost::is_convertible<T1, T2>, T2, T1>::type type;
61  };
62 
63  } // end of weighted_iterator namespace
64  } // end of detail namespace
65 
93  template<typename DataIterator, typename WeightIterator>
95  : public boost::iterator_facade<
96  WeightedIterator<DataIterator, WeightIterator>,
97  DataWeight,
98  typename
99  detail::weighted_iterator::Traversal<DataIterator, WeightIterator>::type,
100  DataWeightProxy<DataIterator, WeightIterator> >
101 
102  {
103  public:
108 
113 
122  {}
123 
128  : d_iter_(d), w_iter_(w)
129  {}
130 
134  const DataIterator& data_base(void) const { return d_iter_; }
135 
139  const WeightIterator& weight_base(void) const { return w_iter_; }
140 
145  {
147  w_iter_+n);
148  }
149 
150 
159  template<typename D2, typename W2>
161  typename boost::enable_if_convertible<D2,DataIterator>::type* = 0,
162  typename boost::enable_if_convertible<W2,WeightIterator>::type* = 0)
163  : d_iter_(other.data_base()), w_iter_(other.weight_base()) {}
164 
165 
166  private:
167  friend class boost::iterator_core_access;
168 
169  DataIterator d_iter_;
170  WeightIterator w_iter_;
171 
172  void advance(size_t n)
173  { std::advance(d_iter_, n); std::advance(w_iter_, n); }
174 
175  void decrement(void) { --d_iter_; --w_iter_; }
176 
177  typename std::iterator_traits<DataIterator>::difference_type
178  distance_to(const WeightedIterator& other) const
179  { return std::distance(d_iter_, other.d_iter_); }
180 
181  utility::DataWeightProxy<DataIterator, WeightIterator>
182  dereference(void) const
183  { return DataWeightProxy<DataIterator, WeightIterator>(d_iter_,
184  w_iter_);
185  }
186 
187  bool equal(const WeightedIterator& other) const
188  { return d_iter_==other.d_iter_ && w_iter_==other.w_iter_; }
189 
190  void increment(void) { ++d_iter_; ++w_iter_; }
191 
192  };
193 
201  template<typename DataIterator, typename WeightIterator>
202  WeightedIterator<DataIterator, WeightIterator>
204  {
206  }
207 
208 }}} // of namespace utility, yat, and theplu
209 
210 #endif
DataIterator data_iterator
DataIterator.
Definition: WeightedIterator.h:107
WeightedIterator.
Definition: WeightedIterator.h:94
DataWeightProxy< DataIterator, WeightIterator > operator[](int n) const
element operator
Definition: WeightedIterator.h:144
WeightedIterator< DataIterator, WeightIterator > weighted_iterator(DataIterator data, WeightIterator weight)
convenient function to create WeightedIterator
Definition: WeightedIterator.h:203
Proxy class for DataWeight.
Definition: DataWeightProxy.h:54
const DataIterator & data_base(void) const
Definition: WeightedIterator.h:134
DataIterator.
Definition: DataIterator.h:63
boost::mpl::if_< boost::is_convertible< T1, T2 >, T2, T1 >::type type
Definition: WeightedIterator.h:60
WeightedIterator(DataIterator d, WeightIterator w)
Constructor.
Definition: WeightedIterator.h:127
WeightIterator weight_iterator
DataIterator.
Definition: WeightedIterator.h:112
WeightIterator.
Definition: WeightIterator.h:64
const WeightIterator & weight_base(void) const
Definition: WeightedIterator.h:139
WeightedIterator(void)
Default Constructor.
Definition: WeightedIterator.h:121
WeightedIterator(WeightedIterator< D2, W2 > other, typename boost::enable_if_convertible< D2, DataIterator >::type *=0, typename boost::enable_if_convertible< W2, WeightIterator >::type *=0)
Conversion constructor.
Definition: WeightedIterator.h:160

Generated on Tue Sep 26 2017 02:33:29 for yat by  doxygen 1.8.5