source: trunk/yat/utility/DataWeightProxy.h

Last change on this file was 3550, checked in by Peter, 5 years ago

Update copyright years. Happy New Year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svndigest:ignore set to 1532
File size: 4.7 KB
Line 
1#ifndef _theplu_yat_utility_data_weight_proxy_
2#define _theplu_yat_utility_data_weight_proxy_
3
4// $Id: DataWeightProxy.h 3550 2017-01-03 05:41:02Z peter $
5
6/*
7  Copyright (C) 2008 Jari Häkkinen, Peter Johansson
8  Copyright (C) 2009, 2010, 2016, 2017 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 "DataWeight.h"
27
28#include <boost/concept/assert.hpp>
29#include <boost/iterator/iterator_concepts.hpp>
30#include <boost/iterator/iterator_traits.hpp>
31
32#include <iterator>
33
34namespace theplu {
35namespace yat {
36namespace utility {
37
38  /**
39     \internal
40
41     \brief Proxy class for DataWeight
42
43     Class is used in WeightedIterator
44
45     Type Requirements (for both DataIterator and WeightIterator):
46     - underlying iterators are \readable_iterator
47     - value_type must be convertible to \c const \c double.
48     - reference must be convertible to \c const \c double&.
49     - If the DataWeightProxy is going to be used in a non-const way
50       iterators must be mutable and \c reference must be exactly \c
51       double&. See data(void) and weight(void).
52  */
53  template<typename DataIterator, typename WeightIterator>
54  class DataWeightProxy
55  {
56  public:
57    /**
58       \brief Constructor
59
60       \param data iterator pointing to data to hold
61       \param weight iterator pointing to weight to hold
62     */
63    DataWeightProxy(DataIterator data, WeightIterator weight)
64      : data_(data), weight_(weight)
65    {
66      BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<DataIterator>));
67      BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<WeightIterator>));
68
69      using boost::Convertible;
70      // DataIterator::value must be convertible to double
71      BOOST_CONCEPT_ASSERT((Convertible<data_type, double>));
72
73      // same check for WeightIterator
74      BOOST_CONCEPT_ASSERT((Convertible<weight_type, double>));
75    }
76
77    /**
78       \return reference to data
79
80       This function requires that reference type of data_iterator
81       is convertible to \c double. This function is typically used as
82       left-hand side in an assignment and thus expected to being able
83       to change the underlying data such as a double& can change the
84       underlying double. But it is not required that return type is a
85       double& as a Proxy class can do the job too.
86    */
87    typename boost::iterator_reference<DataIterator>::type data(void)
88    {
89      return *data_;
90    }
91
92    /**
93       \return const reference to data
94     */
95    const double& data(void) const { return *data_ ; }
96
97    /**
98       \return reference to weight
99
100       This function requires that reference type of weight_iterator
101       is convertible to \c double. This function is typically used as
102       left-hand side in an assignment and thus expected to being able
103       to change the underlying data such as a double& can change the
104       underlying double. But it is not required that return type is a
105       double& as a Proxy class can do the job too.
106     */
107    typename boost::iterator_reference<WeightIterator>::type weight(void)
108    {
109      return *weight_;
110    }
111
112    /**
113       \return const reference to weight
114     */
115    const double& weight(void) const { return *weight_; }
116
117    /**
118       \brief assignment operator
119
120       This function uses non-const function data(void) and
121       weight(void) and consequently requires that reference type of
122       both data_iterator and weight_iterator are convertible to \c
123       double.
124     */
125    DataWeightProxy& operator=(const DataWeightProxy& rhs)
126    {
127      data() = rhs.data();
128      weight() = rhs.weight();
129      return *this;
130    }
131
132    /**
133       \brief assignment operator
134     */
135    DataWeightProxy& operator=(const DataWeight& rhs)
136    {
137      data() = rhs.data();
138      weight() = rhs.weight();
139      return *this;
140    }
141
142    /**
143       \brief Conversion to DataWeight
144     */
145    operator DataWeight() const
146    { return DataWeight(this->data(), this->weight()); }
147
148  private:
149    DataIterator data_;
150    WeightIterator weight_;
151
152    typedef typename boost::iterator_value<DataIterator>::type data_type;
153    typedef typename boost::iterator_reference<DataIterator>::type data_reference;
154
155    typedef typename boost::iterator_value<WeightIterator>::type weight_type;
156    typedef
157    typename boost::iterator_reference<WeightIterator>::type weight_reference;
158
159    // using compiler generated copy
160    //DataWeightProxy(const DataWeightProxy&);
161  };
162
163}}} // of namespace utility, yat, and theplu
164
165#endif
Note: See TracBrowser for help on using the repository browser.