source: trunk/yat/utility/DataIterator.h @ 3417

Last change on this file since 3417 was 3417, checked in by Peter, 4 years ago

updating copyright statements

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#ifndef _theplu_yat_utility_data_iterator_
2#define _theplu_yat_utility_data_iterator_
3
4// $Id: DataIterator.h 3417 2015-05-25 01:35:59Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2014, 2015 Peter Johansson
9
10  This file is part of the yat library, http://dev.thep.lu.se/yat
11
12  The yat library is free software; you can redistribute it and/or
13  modify it under the terms of the GNU General Public License as
14  published by the Free Software Foundation; either version 3 of the
15  License, or (at your option) any later version.
16
17  The yat library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with yat. If not, see <http://www.gnu.org/licenses/>.
24*/
25
26#include "iterator_traits.h"
27
28#include <boost/concept_check.hpp>
29#include <boost/iterator/iterator_adaptor.hpp>
30#include <boost/type_traits/remove_reference.hpp>
31
32namespace theplu {
33namespace yat {
34namespace utility {
35
36  /**
37     @brief DataIterator
38
39     DataIterator is an iterator adaptor that enables iterating over a
40     weighted range but accessing only the data part. The iterator
41     behaves just like it Base iterator except that operator* returns
42     iterator_traits<Base>.data(base), i.e., the data part is
43     returned.
44
45     Here is a short example illustrating typical usage:
46     \code
47
48     std::vector<DataWeight> weighted_vec;
49     ...
50     std::vector<double> vec(weighted_vec.size());
51     std::copy(data_iterator(weighted_vec.begin()),
52               data_iterator(weighted_vec.end()),
53               vec.begin());
54
55
56     \endcode
57
58    Type Requirement:
59    - \c BASE must model a \ref concept_data_iterator
60  */
61  template<typename Base>
62  class DataIterator
63    : public boost::iterator_adaptor<
64    DataIterator<Base>               // Derived
65    , Base                          // Base
66    , typename boost::remove_reference<typename iterator_traits<Base>::data_reference>::type
67    , boost::use_default    // CategoryOrTraversal
68    , typename iterator_traits<Base>::data_reference // Reference
69    >
70
71  {
72  public:
73    /**
74       \brief Constructor from \a Base iterator
75     */
76    explicit DataIterator(Base b)
77      : DataIterator::iterator_adaptor_(b)
78    {
79      BOOST_CONCEPT_ASSERT((boost::InputIterator<Base>));
80    }
81
82    /**
83       \brief Default Constructor
84
85       \since New in yat 0.6
86     */
87    DataIterator(void)
88      : DataIterator::iterator_adaptor_()
89    {
90      BOOST_CONCEPT_ASSERT((boost::InputIterator<Base>));
91    }
92
93    /**
94       \brief Conversion constructor.
95
96       Create a DataIterator<Base> from a DataIterator<B2>. Possible
97       if B2 is convertible to a Base. Constructor allows implicit
98       conversions such as iterator to const_iterator.
99     */
100    template<typename B2>
101    DataIterator(DataIterator<B2> other,
102                 typename boost::enable_if_convertible<B2, Base>::type* = 0 )
103      : DataIterator::iterator_adaptor_(other.base()) {}
104
105    /**
106       using iterator_traits::data on Base iterator
107
108       \return data
109     */
110    typename iterator_traits<Base>::data_reference operator*(void) const
111    { return iterator_traits<Base>().data(this->base()); }
112
113  private:
114  };
115
116  /**
117     \brief convenient function to create DataIterator
118
119     Convenient function in same fashion as std::make_pair.
120
121     \relates DataIterator
122   */
123  template<typename Base>
124  DataIterator<Base> data_iterator(Base base)
125  {
126    return DataIterator<Base>(base);
127  }
128
129
130}}} // of namespace utility, yat, and theplu
131
132#endif
Note: See TracBrowser for help on using the repository browser.