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

Last change on this file since 1404 was 1404, checked in by Peter, 13 years ago

refs #366 - weighted percentile
Added a class Percentile that can calculate the percentile for both
unweighted and weighted ranges. In order to make sense of the weighted
case, I had to modify the definition of percentile (slightly). The old
function percentile is using the old definition, but a new function,
percentile2, is using the new function and is simply calling the
Percentile class. The median is the same for the two definitions and
therefore it makes no difference which function to call, but to enable
calculation of weighted median percentile2 is called.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1#ifndef _theplu_yat_utility_data_iterator_
2#define _theplu_yat_utility_data_iterator_
3
4// $Id: DataIterator.h 1404 2008-08-07 22:47:18Z peter $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software
23  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  02111-1307, USA.
25*/
26
27#include "iterator_traits.h"
28
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  template<typename Base>
40  class DataIterator
41    : public boost::iterator_adaptor<
42    DataIterator<Base>               // Derived
43    , Base                          // Base
44    , typename boost::remove_reference<typename iterator_traits<Base>::data_reference>::type
45    , boost::forward_traversal_tag    // CategoryOrTraversal
46    , typename iterator_traits<Base>::data_reference // Reference
47    >
48
49  {
50  public:
51    /**
52       \brief Constructor from \a Base iterator
53     */
54    explicit DataIterator(Base b)
55      : DataIterator::iterator_adaptor_(b) {}
56
57    /**
58       \brief Conversion constructor.
59
60       Create a DataIterator<Base> from a DataIterator<B2>. Possible
61       if B2 is convertible to a Base. Constructor allows implicit
62       conversions such as iterator to const_iterator.
63     */
64    template<typename B2>
65    DataIterator(DataIterator<B2> other,
66                 typename boost::enable_if_convertible<B2, Base>::type* = 0 )
67      : DataIterator::iterator_adaptor_(other.base()) {}
68
69    /**
70       using iterator_traits::data on Base iterator
71
72       \return data
73     */
74    typename iterator_traits<Base>::data_reference operator*(void) const
75    { return iterator_traits<Base>().data(this->base()); }
76
77  private:
78  };
79
80  /**
81     \brief convenient function to create DataIterator
82
83     Convenient function in same fashion as std::make_pair.
84   */
85  template<typename Base>
86  DataIterator<Base> data_iterator(Base base)
87  {
88    return DataIterator<Base>(base);
89  }
90 
91
92
93}}} // of namespace utility, yat, and theplu
94
95#endif
Note: See TracBrowser for help on using the repository browser.