1 | #ifndef _theplu_yat_utility_weighted_iterator_ |
---|
2 | #define _theplu_yat_utility_weighted_iterator_ |
---|
3 | |
---|
4 | // $Id: WeightedIterator.h 1531 2008-09-24 21:47:41Z peter $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2008 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 <boost/iterator/iterator_facade.hpp> |
---|
26 | |
---|
27 | namespace theplu { |
---|
28 | namespace yat { |
---|
29 | namespace utility { |
---|
30 | |
---|
31 | /** |
---|
32 | @brief WeightedIterator |
---|
33 | */ |
---|
34 | template<typename DataIterator, typename WeightIterator> |
---|
35 | class WeightedIterator |
---|
36 | : public boost::iterator_facade< |
---|
37 | WeightedIterator<DataIterator, WeightIterator>, |
---|
38 | DataWeight, |
---|
39 | std::random_access_iterator_tag, |
---|
40 | DataWeight> |
---|
41 | |
---|
42 | { |
---|
43 | public: |
---|
44 | /** |
---|
45 | \brief Constructor |
---|
46 | */ |
---|
47 | WeightedIterator(DataIterator d, WeightIterator w) |
---|
48 | : d_iter_(d), w_iter_(w) |
---|
49 | {} |
---|
50 | |
---|
51 | /** |
---|
52 | \brief Conversion constructor. |
---|
53 | |
---|
54 | Create a WeightIterator<Base> from a |
---|
55 | WeightIterator<B2>. Possible if B2 is convertible to a |
---|
56 | Base. Constructor allows implicit conversions such as iterator |
---|
57 | to const_iterator. |
---|
58 | */ |
---|
59 | /* |
---|
60 | template<typename D2, typename W2> |
---|
61 | WeightedIterator(WeightedIterator<D2, W2> other, |
---|
62 | typename boost::enable_if_convertible<D2,DataIterator>::type* = 0, |
---|
63 | typename boost::enable_if_convertible<W2,WeightIterator>::type* = 0) |
---|
64 | {} |
---|
65 | */ |
---|
66 | |
---|
67 | private: |
---|
68 | friend class boost::iterator_core_access; |
---|
69 | |
---|
70 | DataIterator d_iter_; |
---|
71 | WeightIterator w_iter_; |
---|
72 | |
---|
73 | DataWeight dereference(void) const |
---|
74 | { return DataWeight(*d_iter_, *w_iter_); } |
---|
75 | |
---|
76 | void increment(void) { ++d_iter_; ++w_iter_; } |
---|
77 | |
---|
78 | }; |
---|
79 | |
---|
80 | /** |
---|
81 | \brief convenient function to create WeightedIterator |
---|
82 | |
---|
83 | Convenient function in same fashion as std::make_pair. |
---|
84 | */ |
---|
85 | template<typename DataIterator, typename WeightIterator> |
---|
86 | WeightedIterator<DataIterator, WeightIterator> |
---|
87 | weighted_iterator(DataIterator data, WeightIterator weight) |
---|
88 | { |
---|
89 | return WeightedIterator<DataIterator, WeightIterator>(data, weight); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | }}} // of namespace utility, yat, and theplu |
---|
95 | |
---|
96 | #endif |
---|