1376 |
16 Jul 08 |
peter |
1 |
#ifndef _theplu_yat_utility_weight_iterator_ |
1376 |
16 Jul 08 |
peter |
2 |
#define _theplu_yat_utility_weight_iterator_ |
1363 |
08 Jul 08 |
peter |
3 |
|
1363 |
08 Jul 08 |
peter |
// $Id$ |
1363 |
08 Jul 08 |
peter |
5 |
|
1363 |
08 Jul 08 |
peter |
6 |
/* |
2119 |
12 Dec 09 |
peter |
Copyright (C) 2008 Jari Häkkinen, Peter Johansson |
4359 |
23 Aug 23 |
peter |
Copyright (C) 2009, 2010, 2014, 2016 Peter Johansson |
1363 |
08 Jul 08 |
peter |
9 |
|
1469 |
02 Sep 08 |
peter |
This file is part of the yat library, http://dev.thep.lu.se/yat |
1363 |
08 Jul 08 |
peter |
11 |
|
1363 |
08 Jul 08 |
peter |
The yat library is free software; you can redistribute it and/or |
1363 |
08 Jul 08 |
peter |
modify it under the terms of the GNU General Public License as |
1486 |
09 Sep 08 |
jari |
published by the Free Software Foundation; either version 3 of the |
1363 |
08 Jul 08 |
peter |
License, or (at your option) any later version. |
1363 |
08 Jul 08 |
peter |
16 |
|
1363 |
08 Jul 08 |
peter |
The yat library is distributed in the hope that it will be useful, |
1363 |
08 Jul 08 |
peter |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1363 |
08 Jul 08 |
peter |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
1363 |
08 Jul 08 |
peter |
General Public License for more details. |
1363 |
08 Jul 08 |
peter |
21 |
|
1363 |
08 Jul 08 |
peter |
You should have received a copy of the GNU General Public License |
1487 |
10 Sep 08 |
jari |
along with yat. If not, see <http://www.gnu.org/licenses/>. |
1363 |
08 Jul 08 |
peter |
24 |
*/ |
1363 |
08 Jul 08 |
peter |
25 |
|
3521 |
07 Oct 16 |
peter |
26 |
#include "concept_check.h" |
2282 |
26 Jun 10 |
peter |
27 |
#include "iterator_traits.h" |
2282 |
26 Jun 10 |
peter |
28 |
|
2282 |
26 Jun 10 |
peter |
29 |
#include <boost/concept_check.hpp> |
1365 |
10 Jul 08 |
peter |
30 |
#include <boost/iterator/iterator_adaptor.hpp> |
3506 |
19 Jul 16 |
peter |
31 |
#include <boost/iterator/iterator_concepts.hpp> |
1375 |
16 Jul 08 |
peter |
32 |
#include <boost/type_traits/remove_reference.hpp> |
1363 |
08 Jul 08 |
peter |
33 |
|
1363 |
08 Jul 08 |
peter |
34 |
namespace theplu { |
1363 |
08 Jul 08 |
peter |
35 |
namespace yat { |
1363 |
08 Jul 08 |
peter |
36 |
namespace utility { |
1363 |
08 Jul 08 |
peter |
37 |
|
1363 |
08 Jul 08 |
peter |
38 |
/** |
1461 |
31 Aug 08 |
peter |
@brief WeightIterator |
2202 |
21 Feb 10 |
peter |
40 |
|
2202 |
21 Feb 10 |
peter |
WeightIterator is an iterator adaptor that enables iterating over |
2202 |
21 Feb 10 |
peter |
a weighted range but accessing only the weight part of the |
2202 |
21 Feb 10 |
peter |
range. The iterator behaves just like it Base iterator except |
2202 |
21 Feb 10 |
peter |
that operator* returns iterator_traits<Base>.weight(base), i.e., |
2202 |
21 Feb 10 |
peter |
the weight part is returned. |
2202 |
21 Feb 10 |
peter |
46 |
|
2202 |
21 Feb 10 |
peter |
Here is a short example illustrating typical usage: |
2202 |
21 Feb 10 |
peter |
\code |
2202 |
21 Feb 10 |
peter |
49 |
|
2202 |
21 Feb 10 |
peter |
std::vector<DataWeight> weighted_vec; |
2202 |
21 Feb 10 |
peter |
51 |
... |
2202 |
21 Feb 10 |
peter |
std::vector<double> vec(weighted_vec.size()); |
2202 |
21 Feb 10 |
peter |
std::copy(weight_iterator(weighted_vec.begin()), |
2202 |
21 Feb 10 |
peter |
weight_iterator(weighted_vec.end()), |
2202 |
21 Feb 10 |
peter |
vec.begin()); |
2202 |
21 Feb 10 |
peter |
56 |
|
2202 |
21 Feb 10 |
peter |
57 |
|
2202 |
21 Feb 10 |
peter |
\endcode |
2282 |
26 Jun 10 |
peter |
59 |
|
3521 |
07 Oct 16 |
peter |
Type Requirement: |
3521 |
07 Oct 16 |
peter |
- \c BASE must model a \ref concept_data_iterator |
1363 |
08 Jul 08 |
peter |
62 |
*/ |
1365 |
10 Jul 08 |
peter |
63 |
template<typename Base> |
1376 |
16 Jul 08 |
peter |
64 |
class WeightIterator |
1365 |
10 Jul 08 |
peter |
65 |
: public boost::iterator_adaptor< |
1376 |
16 Jul 08 |
peter |
66 |
WeightIterator<Base> // Derived |
1365 |
10 Jul 08 |
peter |
67 |
, Base // Base |
3276 |
05 Jul 14 |
peter |
68 |
, typename boost::remove_reference<typename iterator_traits<Base>::weight_reference>::type |
1749 |
26 Jan 09 |
peter |
69 |
, boost::use_default // CategoryOrTraversal |
1376 |
16 Jul 08 |
peter |
70 |
, typename iterator_traits<Base>::weight_reference // Reference |
1365 |
10 Jul 08 |
peter |
71 |
> |
1365 |
10 Jul 08 |
peter |
72 |
|
1363 |
08 Jul 08 |
peter |
73 |
{ |
1363 |
08 Jul 08 |
peter |
74 |
public: |
1366 |
10 Jul 08 |
peter |
75 |
/** |
2202 |
21 Feb 10 |
peter |
\brief Default Constructor |
2202 |
21 Feb 10 |
peter |
77 |
|
2202 |
21 Feb 10 |
peter |
\since New in yat 0.6 |
2202 |
21 Feb 10 |
peter |
79 |
*/ |
2202 |
21 Feb 10 |
peter |
80 |
WeightIterator(void) |
4200 |
19 Aug 22 |
peter |
81 |
: WeightIterator::iterator_adaptor_() |
2282 |
26 Jun 10 |
peter |
82 |
{ |
3521 |
07 Oct 16 |
peter |
83 |
BOOST_CONCEPT_ASSERT((DataIteratorConcept<Base>)); |
2282 |
26 Jun 10 |
peter |
84 |
} |
2202 |
21 Feb 10 |
peter |
85 |
|
2202 |
21 Feb 10 |
peter |
86 |
/** |
1366 |
10 Jul 08 |
peter |
\brief Constructor from \a Base iterator |
1366 |
10 Jul 08 |
peter |
88 |
*/ |
1376 |
16 Jul 08 |
peter |
89 |
explicit WeightIterator(Base b) |
2282 |
26 Jun 10 |
peter |
90 |
: WeightIterator::iterator_adaptor_(b) |
2282 |
26 Jun 10 |
peter |
91 |
{ |
3521 |
07 Oct 16 |
peter |
92 |
BOOST_CONCEPT_ASSERT((DataIteratorConcept<Base>)); |
2282 |
26 Jun 10 |
peter |
93 |
} |
1363 |
08 Jul 08 |
peter |
94 |
|
1366 |
10 Jul 08 |
peter |
95 |
/** |
4200 |
19 Aug 22 |
peter |
\brief Conversion constructor. |
1377 |
16 Jul 08 |
peter |
97 |
|
1377 |
16 Jul 08 |
peter |
Create a WeightIterator<Base> from a |
1377 |
16 Jul 08 |
peter |
WeightIterator<B2>. Possible if B2 is convertible to a |
1377 |
16 Jul 08 |
peter |
Base. Constructor allows implicit conversions such as iterator |
1377 |
16 Jul 08 |
peter |
to const_iterator. |
1377 |
16 Jul 08 |
peter |
102 |
*/ |
1377 |
16 Jul 08 |
peter |
103 |
template<typename B2> |
1377 |
16 Jul 08 |
peter |
104 |
WeightIterator(WeightIterator<B2> other, |
1377 |
16 Jul 08 |
peter |
105 |
typename boost::enable_if_convertible<B2, Base>::type* = 0 ) |
3521 |
07 Oct 16 |
peter |
106 |
: WeightIterator::iterator_adaptor_(other.base()) |
3521 |
07 Oct 16 |
peter |
107 |
{ |
3521 |
07 Oct 16 |
peter |
108 |
BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<Base>)); |
3521 |
07 Oct 16 |
peter |
109 |
} |
1377 |
16 Jul 08 |
peter |
110 |
|
1377 |
16 Jul 08 |
peter |
111 |
/** |
2202 |
21 Feb 10 |
peter |
using iterator_traits::weight on Base iterator |
1366 |
10 Jul 08 |
peter |
113 |
|
2202 |
21 Feb 10 |
peter |
\return weight |
1366 |
10 Jul 08 |
peter |
115 |
*/ |
1376 |
16 Jul 08 |
peter |
116 |
typename iterator_traits<Base>::weight_reference operator*(void) const |
1376 |
16 Jul 08 |
peter |
117 |
{ return iterator_traits<Base>().weight(this->base()); } |
1365 |
10 Jul 08 |
peter |
118 |
|
1363 |
08 Jul 08 |
peter |
119 |
private: |
1363 |
08 Jul 08 |
peter |
120 |
}; |
1363 |
08 Jul 08 |
peter |
121 |
|
1365 |
10 Jul 08 |
peter |
122 |
/** |
1461 |
31 Aug 08 |
peter |
\brief convenient function to create WeightIterator |
1363 |
08 Jul 08 |
peter |
124 |
|
1365 |
10 Jul 08 |
peter |
Convenient function in same fashion as std::make_pair. |
1887 |
31 Mar 09 |
peter |
126 |
|
1887 |
31 Mar 09 |
peter |
\relates WeightIterator |
1365 |
10 Jul 08 |
peter |
128 |
*/ |
1365 |
10 Jul 08 |
peter |
129 |
template<typename Base> |
1404 |
07 Aug 08 |
peter |
130 |
WeightIterator<Base> weight_iterator(Base base) |
1365 |
10 Jul 08 |
peter |
131 |
{ |
1376 |
16 Jul 08 |
peter |
132 |
return WeightIterator<Base>(base); |
1365 |
10 Jul 08 |
peter |
133 |
} |
1363 |
08 Jul 08 |
peter |
134 |
|
1365 |
10 Jul 08 |
peter |
135 |
|
1363 |
08 Jul 08 |
peter |
136 |
}}} // of namespace utility, yat, and theplu |
1363 |
08 Jul 08 |
peter |
137 |
|
1363 |
08 Jul 08 |
peter |
138 |
#endif |