source: trunk/yat/utility/WeightIterator.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_weight_iterator_
2#define _theplu_yat_utility_weight_iterator_
3
4// $Id: WeightIterator.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 <boost/iterator/iterator_adaptor.hpp>
28#include <boost/type_traits/remove_reference.hpp>
29
30namespace theplu {
31namespace yat {
32namespace utility {
33
34  /**
35     @brief DataIterator
36  */
37  template<typename Base>
38  class WeightIterator
39    : public boost::iterator_adaptor<
40    WeightIterator<Base>               // Derived
41    , Base                          // Base
42    , typename boost::remove_reference<typename iterator_traits<Base>::weight_reference>::type
43    , boost::forward_traversal_tag    // CategoryOrTraversal
44    , typename iterator_traits<Base>::weight_reference // Reference
45    >
46
47  {
48  public:
49    /**
50       \brief Constructor from \a Base iterator
51     */
52    explicit WeightIterator(Base b)
53      : WeightIterator::iterator_adaptor_(b) {}
54
55    /**
56       \brief Conversion constructor.
57
58       Create a WeightIterator<Base> from a
59       WeightIterator<B2>. Possible if B2 is convertible to a
60       Base. Constructor allows implicit conversions such as iterator
61       to const_iterator.
62     */
63    template<typename B2>
64    WeightIterator(WeightIterator<B2> other,
65                   typename boost::enable_if_convertible<B2, Base>::type* = 0 )
66      : WeightIterator::iterator_adaptor_(other.base()) {}
67
68    /**
69       using iterator_traits::data on Base iterator
70
71       \return data
72     */
73    typename iterator_traits<Base>::weight_reference operator*(void) const
74    { return iterator_traits<Base>().weight(this->base()); }
75
76  private:
77  };
78
79  /**
80     \brief convenient function to create DataIterator
81
82     Convenient function in same fashion as std::make_pair.
83   */
84  template<typename Base>
85  WeightIterator<Base> weight_iterator(Base base)
86  {
87    return WeightIterator<Base>(base);
88  }
89 
90
91
92}}} // of namespace utility, yat, and theplu
93
94#endif
Note: See TracBrowser for help on using the repository browser.