yat/utility/DataWeightProxy.h

Code
Comments
Other
Rev Date Author Line
1533 25 Sep 08 peter 1 #ifndef _theplu_yat_utility_data_weight_proxy_
3460 21 Jan 16 peter 2 #define _theplu_yat_utility_data_weight_proxy_
1379 16 Jul 08 peter 3
1379 16 Jul 08 peter 4 // $Id$
1379 16 Jul 08 peter 5
1379 16 Jul 08 peter 6 /*
2119 12 Dec 09 peter 7   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4254 21 Nov 22 peter 8   Copyright (C) 2009, 2010, 2016, 2017, 2022 Peter Johansson
1379 16 Jul 08 peter 9
1469 02 Sep 08 peter 10   This file is part of the yat library, http://dev.thep.lu.se/yat
1379 16 Jul 08 peter 11
1379 16 Jul 08 peter 12   The yat library is free software; you can redistribute it and/or
1379 16 Jul 08 peter 13   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 14   published by the Free Software Foundation; either version 3 of the
1379 16 Jul 08 peter 15   License, or (at your option) any later version.
1379 16 Jul 08 peter 16
1379 16 Jul 08 peter 17   The yat library is distributed in the hope that it will be useful,
1379 16 Jul 08 peter 18   but WITHOUT ANY WARRANTY; without even the implied warranty of
1379 16 Jul 08 peter 19   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1379 16 Jul 08 peter 20   General Public License for more details.
1379 16 Jul 08 peter 21
1379 16 Jul 08 peter 22   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 23   along with yat. If not, see <http://www.gnu.org/licenses/>.
1379 16 Jul 08 peter 24 */
1379 16 Jul 08 peter 25
1535 25 Sep 08 peter 26 #include "DataWeight.h"
1535 25 Sep 08 peter 27
2375 13 Dec 10 peter 28 #include <boost/concept/assert.hpp>
3548 01 Jan 17 peter 29 #include <boost/iterator/iterator_concepts.hpp>
3460 21 Jan 16 peter 30 #include <boost/iterator/iterator_traits.hpp>
2375 13 Dec 10 peter 31
2375 13 Dec 10 peter 32 #include <iterator>
2375 13 Dec 10 peter 33
1379 16 Jul 08 peter 34 namespace theplu {
1379 16 Jul 08 peter 35 namespace yat {
1379 16 Jul 08 peter 36 namespace utility {
1379 16 Jul 08 peter 37
1379 16 Jul 08 peter 38   /**
1533 25 Sep 08 peter 39      \internal
1533 25 Sep 08 peter 40
1533 25 Sep 08 peter 41      \brief Proxy class for DataWeight
1537 26 Sep 08 peter 42
2375 13 Dec 10 peter 43      Class is used in WeightedIterator
2375 13 Dec 10 peter 44
3460 21 Jan 16 peter 45      Type Requirements (for both DataIterator and WeightIterator):
3460 21 Jan 16 peter 46      - underlying iterators are \readable_iterator
2375 13 Dec 10 peter 47      - value_type must be convertible to \c const \c double.
2375 13 Dec 10 peter 48      - reference must be convertible to \c const \c double&.
2375 13 Dec 10 peter 49      - If the DataWeightProxy is going to be used in a non-const way
2375 13 Dec 10 peter 50        iterators must be mutable and \c reference must be exactly \c
2375 13 Dec 10 peter 51        double&. See data(void) and weight(void).
1379 16 Jul 08 peter 52   */
1537 26 Sep 08 peter 53   template<typename DataIterator, typename WeightIterator>
1533 25 Sep 08 peter 54   class DataWeightProxy
1379 16 Jul 08 peter 55   {
1379 16 Jul 08 peter 56   public:
1379 16 Jul 08 peter 57     /**
2375 13 Dec 10 peter 58        \brief Constructor
1379 16 Jul 08 peter 59
1537 26 Sep 08 peter 60        \param data iterator pointing to data to hold
1537 26 Sep 08 peter 61        \param weight iterator pointing to weight to hold
1379 16 Jul 08 peter 62      */
1537 26 Sep 08 peter 63     DataWeightProxy(DataIterator data, WeightIterator weight)
3460 21 Jan 16 peter 64       : data_(data), weight_(weight)
2375 13 Dec 10 peter 65     {
3460 21 Jan 16 peter 66       BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<DataIterator>));
3460 21 Jan 16 peter 67       BOOST_CONCEPT_ASSERT((boost_concepts::ReadableIterator<WeightIterator>));
3460 21 Jan 16 peter 68
2375 13 Dec 10 peter 69       using boost::Convertible;
2375 13 Dec 10 peter 70       // DataIterator::value must be convertible to double
2375 13 Dec 10 peter 71       BOOST_CONCEPT_ASSERT((Convertible<data_type, double>));
1379 16 Jul 08 peter 72
2375 13 Dec 10 peter 73       // same check for WeightIterator
2375 13 Dec 10 peter 74       BOOST_CONCEPT_ASSERT((Convertible<weight_type, double>));
2375 13 Dec 10 peter 75     }
2375 13 Dec 10 peter 76
1379 16 Jul 08 peter 77     /**
4254 21 Nov 22 peter 78        Shallow copy underlying iterators.
4254 21 Nov 22 peter 79      */
4254 21 Nov 22 peter 80     DataWeightProxy(const DataWeightProxy& other) = default;
4254 21 Nov 22 peter 81
4254 21 Nov 22 peter 82     /**
1379 16 Jul 08 peter 83        \return reference to data
1379 16 Jul 08 peter 84
2375 13 Dec 10 peter 85        This function requires that reference type of data_iterator
2375 13 Dec 10 peter 86        is convertible to \c double. This function is typically used as
2375 13 Dec 10 peter 87        left-hand side in an assignment and thus expected to being able
2375 13 Dec 10 peter 88        to change the underlying data such as a double& can change the
2375 13 Dec 10 peter 89        underlying double. But it is not required that return type is a
2375 13 Dec 10 peter 90        double& as a Proxy class can do the job too.
2375 13 Dec 10 peter 91     */
3460 21 Jan 16 peter 92     typename boost::iterator_reference<DataIterator>::type data(void)
3460 21 Jan 16 peter 93     {
3460 21 Jan 16 peter 94       return *data_;
2375 13 Dec 10 peter 95     }
2375 13 Dec 10 peter 96
1379 16 Jul 08 peter 97     /**
1379 16 Jul 08 peter 98        \return const reference to data
1379 16 Jul 08 peter 99      */
1537 26 Sep 08 peter 100     const double& data(void) const { return *data_ ; }
1379 16 Jul 08 peter 101
1379 16 Jul 08 peter 102     /**
1379 16 Jul 08 peter 103        \return reference to weight
2375 13 Dec 10 peter 104
2375 13 Dec 10 peter 105        This function requires that reference type of weight_iterator
2375 13 Dec 10 peter 106        is convertible to \c double. This function is typically used as
2375 13 Dec 10 peter 107        left-hand side in an assignment and thus expected to being able
2375 13 Dec 10 peter 108        to change the underlying data such as a double& can change the
2375 13 Dec 10 peter 109        underlying double. But it is not required that return type is a
2375 13 Dec 10 peter 110        double& as a Proxy class can do the job too.
1379 16 Jul 08 peter 111      */
3460 21 Jan 16 peter 112     typename boost::iterator_reference<WeightIterator>::type weight(void)
3460 21 Jan 16 peter 113     {
3460 21 Jan 16 peter 114       return *weight_;
2375 13 Dec 10 peter 115     }
1379 16 Jul 08 peter 116
1379 16 Jul 08 peter 117     /**
1379 16 Jul 08 peter 118        \return const reference to weight
1379 16 Jul 08 peter 119      */
1537 26 Sep 08 peter 120     const double& weight(void) const { return *weight_; }
1535 25 Sep 08 peter 121
1535 25 Sep 08 peter 122     /**
1535 25 Sep 08 peter 123        \brief assignment operator
2375 13 Dec 10 peter 124
2375 13 Dec 10 peter 125        This function uses non-const function data(void) and
2375 13 Dec 10 peter 126        weight(void) and consequently requires that reference type of
2375 13 Dec 10 peter 127        both data_iterator and weight_iterator are convertible to \c
2375 13 Dec 10 peter 128        double.
1535 25 Sep 08 peter 129      */
1538 27 Sep 08 peter 130     DataWeightProxy& operator=(const DataWeightProxy& rhs)
1538 27 Sep 08 peter 131     {
1538 27 Sep 08 peter 132       data() = rhs.data();
1538 27 Sep 08 peter 133       weight() = rhs.weight();
1538 27 Sep 08 peter 134       return *this;
1538 27 Sep 08 peter 135     }
1538 27 Sep 08 peter 136
1538 27 Sep 08 peter 137     /**
1538 27 Sep 08 peter 138        \brief assignment operator
1538 27 Sep 08 peter 139      */
1537 26 Sep 08 peter 140     DataWeightProxy& operator=(const DataWeight& rhs)
1537 26 Sep 08 peter 141     {
1537 26 Sep 08 peter 142       data() = rhs.data();
1537 26 Sep 08 peter 143       weight() = rhs.weight();
1537 26 Sep 08 peter 144       return *this;
1537 26 Sep 08 peter 145     }
1535 25 Sep 08 peter 146
1535 25 Sep 08 peter 147     /**
1535 25 Sep 08 peter 148        \brief Conversion to DataWeight
1535 25 Sep 08 peter 149      */
3460 21 Jan 16 peter 150     operator DataWeight() const
1538 27 Sep 08 peter 151     { return DataWeight(this->data(), this->weight()); }
1535 25 Sep 08 peter 152
1379 16 Jul 08 peter 153   private:
1537 26 Sep 08 peter 154     DataIterator data_;
1537 26 Sep 08 peter 155     WeightIterator weight_;
1533 25 Sep 08 peter 156
3460 21 Jan 16 peter 157     typedef typename boost::iterator_value<DataIterator>::type data_type;
3460 21 Jan 16 peter 158     typedef typename boost::iterator_reference<DataIterator>::type data_reference;
2375 13 Dec 10 peter 159
3460 21 Jan 16 peter 160     typedef typename boost::iterator_value<WeightIterator>::type weight_type;
3460 21 Jan 16 peter 161     typedef
3460 21 Jan 16 peter 162     typename boost::iterator_reference<WeightIterator>::type weight_reference;
2375 13 Dec 10 peter 163
2278 25 Jun 10 peter 164     // using compiler generated copy
1537 26 Sep 08 peter 165     //DataWeightProxy(const DataWeightProxy&);
1379 16 Jul 08 peter 166   };
1379 16 Jul 08 peter 167
1379 16 Jul 08 peter 168 }}} // of namespace utility, yat, and theplu
1379 16 Jul 08 peter 169
1379 16 Jul 08 peter 170 #endif