1365 |
10 Jul 08 |
peter |
1 |
#ifndef _theplu_yat_utility_data_iterator_ |
1365 |
10 Jul 08 |
peter |
2 |
#define _theplu_yat_utility_data_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" |
1376 |
16 Jul 08 |
peter |
27 |
#include "iterator_traits.h" |
1376 |
16 Jul 08 |
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> |
1375 |
16 Jul 08 |
peter |
31 |
#include <boost/type_traits/remove_reference.hpp> |
1363 |
08 Jul 08 |
peter |
32 |
|
1363 |
08 Jul 08 |
peter |
33 |
namespace theplu { |
1363 |
08 Jul 08 |
peter |
34 |
namespace yat { |
1363 |
08 Jul 08 |
peter |
35 |
namespace utility { |
1363 |
08 Jul 08 |
peter |
36 |
|
1363 |
08 Jul 08 |
peter |
37 |
/** |
1365 |
10 Jul 08 |
peter |
@brief DataIterator |
2202 |
21 Feb 10 |
peter |
39 |
|
2202 |
21 Feb 10 |
peter |
DataIterator is an iterator adaptor that enables iterating over a |
2202 |
21 Feb 10 |
peter |
weighted range but accessing only the data part. The iterator |
2202 |
21 Feb 10 |
peter |
behaves just like it Base iterator except that operator* returns |
2202 |
21 Feb 10 |
peter |
iterator_traits<Base>.data(base), i.e., the data part is |
2202 |
21 Feb 10 |
peter |
returned. |
2202 |
21 Feb 10 |
peter |
45 |
|
2202 |
21 Feb 10 |
peter |
Here is a short example illustrating typical usage: |
2202 |
21 Feb 10 |
peter |
\code |
2202 |
21 Feb 10 |
peter |
48 |
|
2202 |
21 Feb 10 |
peter |
std::vector<DataWeight> weighted_vec; |
2202 |
21 Feb 10 |
peter |
50 |
... |
2202 |
21 Feb 10 |
peter |
std::vector<double> vec(weighted_vec.size()); |
2202 |
21 Feb 10 |
peter |
std::copy(data_iterator(weighted_vec.begin()), |
2202 |
21 Feb 10 |
peter |
data_iterator(weighted_vec.end()), |
2202 |
21 Feb 10 |
peter |
vec.begin()); |
2202 |
21 Feb 10 |
peter |
55 |
|
2202 |
21 Feb 10 |
peter |
56 |
|
2202 |
21 Feb 10 |
peter |
\endcode |
2282 |
26 Jun 10 |
peter |
58 |
|
3322 |
06 Oct 14 |
peter |
Type Requirement: |
3322 |
06 Oct 14 |
peter |
- \c BASE must model a \ref concept_data_iterator |
1363 |
08 Jul 08 |
peter |
61 |
*/ |
1365 |
10 Jul 08 |
peter |
62 |
template<typename Base> |
1365 |
10 Jul 08 |
peter |
63 |
class DataIterator |
1365 |
10 Jul 08 |
peter |
64 |
: public boost::iterator_adaptor< |
1365 |
10 Jul 08 |
peter |
65 |
DataIterator<Base> // Derived |
1365 |
10 Jul 08 |
peter |
66 |
, Base // Base |
3276 |
05 Jul 14 |
peter |
67 |
, typename boost::remove_reference<typename iterator_traits<Base>::data_reference>::type |
1749 |
26 Jan 09 |
peter |
68 |
, boost::use_default // CategoryOrTraversal |
1375 |
16 Jul 08 |
peter |
69 |
, typename iterator_traits<Base>::data_reference // Reference |
1365 |
10 Jul 08 |
peter |
70 |
> |
1365 |
10 Jul 08 |
peter |
71 |
|
1363 |
08 Jul 08 |
peter |
72 |
{ |
1363 |
08 Jul 08 |
peter |
73 |
public: |
1366 |
10 Jul 08 |
peter |
74 |
/** |
1366 |
10 Jul 08 |
peter |
\brief Constructor from \a Base iterator |
1366 |
10 Jul 08 |
peter |
76 |
*/ |
1365 |
10 Jul 08 |
peter |
77 |
explicit DataIterator(Base b) |
3386 |
13 Mar 15 |
peter |
78 |
: DataIterator::iterator_adaptor_(b) |
2282 |
26 Jun 10 |
peter |
79 |
{ |
3521 |
07 Oct 16 |
peter |
80 |
BOOST_CONCEPT_ASSERT((DataIteratorConcept<Base>)); |
2282 |
26 Jun 10 |
peter |
81 |
} |
1363 |
08 Jul 08 |
peter |
82 |
|
1366 |
10 Jul 08 |
peter |
83 |
/** |
2202 |
21 Feb 10 |
peter |
\brief Default Constructor |
2202 |
21 Feb 10 |
peter |
85 |
|
2202 |
21 Feb 10 |
peter |
\since New in yat 0.6 |
2202 |
21 Feb 10 |
peter |
87 |
*/ |
2202 |
21 Feb 10 |
peter |
88 |
DataIterator(void) |
2282 |
26 Jun 10 |
peter |
89 |
: DataIterator::iterator_adaptor_() |
2282 |
26 Jun 10 |
peter |
90 |
{ |
3521 |
07 Oct 16 |
peter |
91 |
BOOST_CONCEPT_ASSERT((DataIteratorConcept<Base>)); |
2282 |
26 Jun 10 |
peter |
92 |
} |
2202 |
21 Feb 10 |
peter |
93 |
|
2202 |
21 Feb 10 |
peter |
94 |
/** |
3386 |
13 Mar 15 |
peter |
\brief Conversion constructor. |
1377 |
16 Jul 08 |
peter |
96 |
|
1377 |
16 Jul 08 |
peter |
Create a DataIterator<Base> from a DataIterator<B2>. Possible |
1377 |
16 Jul 08 |
peter |
if B2 is convertible to a Base. Constructor allows implicit |
1377 |
16 Jul 08 |
peter |
conversions such as iterator to const_iterator. |
1377 |
16 Jul 08 |
peter |
100 |
*/ |
1377 |
16 Jul 08 |
peter |
101 |
template<typename B2> |
1377 |
16 Jul 08 |
peter |
102 |
DataIterator(DataIterator<B2> other, |
1377 |
16 Jul 08 |
peter |
103 |
typename boost::enable_if_convertible<B2, Base>::type* = 0 ) |
3521 |
07 Oct 16 |
peter |
104 |
: DataIterator::iterator_adaptor_(other.base()) |
3521 |
07 Oct 16 |
peter |
105 |
{ |
3521 |
07 Oct 16 |
peter |
106 |
BOOST_CONCEPT_ASSERT((DataIteratorConcept<Base>)); |
3521 |
07 Oct 16 |
peter |
107 |
} |
1377 |
16 Jul 08 |
peter |
108 |
|
1377 |
16 Jul 08 |
peter |
109 |
/** |
1366 |
10 Jul 08 |
peter |
using iterator_traits::data on Base iterator |
1366 |
10 Jul 08 |
peter |
111 |
|
1366 |
10 Jul 08 |
peter |
\return data |
1366 |
10 Jul 08 |
peter |
113 |
*/ |
1375 |
16 Jul 08 |
peter |
114 |
typename iterator_traits<Base>::data_reference operator*(void) const |
1365 |
10 Jul 08 |
peter |
115 |
{ return iterator_traits<Base>().data(this->base()); } |
1365 |
10 Jul 08 |
peter |
116 |
|
1363 |
08 Jul 08 |
peter |
117 |
private: |
1363 |
08 Jul 08 |
peter |
118 |
}; |
1363 |
08 Jul 08 |
peter |
119 |
|
1365 |
10 Jul 08 |
peter |
120 |
/** |
1365 |
10 Jul 08 |
peter |
\brief convenient function to create DataIterator |
1363 |
08 Jul 08 |
peter |
122 |
|
1365 |
10 Jul 08 |
peter |
Convenient function in same fashion as std::make_pair. |
1887 |
31 Mar 09 |
peter |
124 |
|
1887 |
31 Mar 09 |
peter |
\relates DataIterator |
1365 |
10 Jul 08 |
peter |
126 |
*/ |
1365 |
10 Jul 08 |
peter |
127 |
template<typename Base> |
1404 |
07 Aug 08 |
peter |
128 |
DataIterator<Base> data_iterator(Base base) |
1365 |
10 Jul 08 |
peter |
129 |
{ |
1365 |
10 Jul 08 |
peter |
130 |
return DataIterator<Base>(base); |
1365 |
10 Jul 08 |
peter |
131 |
} |
1363 |
08 Jul 08 |
peter |
132 |
|
1365 |
10 Jul 08 |
peter |
133 |
|
1363 |
08 Jul 08 |
peter |
134 |
}}} // of namespace utility, yat, and theplu |
1363 |
08 Jul 08 |
peter |
135 |
|
1363 |
08 Jul 08 |
peter |
136 |
#endif |