Changeset 1537


Ignore:
Timestamp:
Sep 27, 2008, 12:37:33 AM (15 years ago)
Author:
Peter
Message:

refs #368 - redesign to solve some issues regarding mutable functionality in WeightedIterator?

Location:
trunk
Files:
1 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/data_weight_proxy_test.cc

    r1535 r1537  
    3131  {
    3232  public:
    33     ProxyHolder(utility::DataWeightProxy& proxy)
     33    typedef utility::DataWeightProxy<double*, double*> Proxy;
     34    ProxyHolder(Proxy& proxy)
    3435      : proxy_(proxy) {}
    3536
    36     inline utility::DataWeightProxy& operator()(void) { return proxy_; }
    37     inline const utility::DataWeightProxy& operator()(void) const
     37    inline Proxy& operator()(void) { return proxy_; }
     38    inline const Proxy& operator()(void) const
    3839    { return proxy_; }
    3940  private:
    40     utility::DataWeightProxy& proxy_;
     41    Proxy& proxy_;
    4142  };
    4243}}}
     
    4950  suite.err() << "testing DataWeightProxy" << std::endl;
    5051
    51   using utility::DataWeightProxy;
    5252  using utility::DataWeight;
     53
     54  typedef utility::DataWeightProxy<double*, double*> Proxy;
    5355
    5456  double x=1.2;
    5557  double w=3.14;
    5658  DataWeight xw(x, w);
    57   DataWeightProxy proxy(x,w);
     59  Proxy proxy(&x,&w);
    5860  if (xw != proxy) {
    5961    suite.add(false);
  • trunk/test/iterator_test.cc

    r1532 r1537  
    2727#include "yat/classifier/MatrixLookupWeighted.h"
    2828#include "yat/utility/Container2DIterator.h"
     29#include "yat/utility/DataWeight.h"
    2930#include "yat/utility/DataIterator.h"
    3031#include "yat/utility/Matrix.h"
     
    377378  suite.add(! (iter7<iter));
    378379
     380  typename RandomAccessIterator::value_type tmp = iter[0];
     381  typename RandomAccessIterator::value_type tmp2 = *iter;
     382  tmp = tmp; // avoid compiler warning
     383  if (!suite.add(tmp == tmp2))
     384    suite.err() << "operator[] failed" << std::endl;
    379385  if (!suite.add(iter[0] == *iter))
    380386    suite.err() << "operator[] failed" << std::endl;
     
    410416  std::vector<double> vec(3,1);
    411417  typedef std::vector<double>::iterator Iter;
    412   utility::WeightedIterator<Iter, Iter> iter(vec.begin(), vec.begin());
    413 
     418  typedef utility::WeightedIterator<Iter, Iter> WIter;
     419  WIter iter(vec.begin(), vec.begin());
     420
     421  utility::DataWeight tmp = *iter;
     422
     423  test_bidirectional_iterator(iter, suite);
    414424  test_random_access_iterator(iter, suite);
    415 
    416 }
     425  //std::vector<double> data(vec.size());
     426  //std::vector<double> weight(vec.size());
     427  //WIter iter2(data.begin(), weight.begin());
     428  //  std::copy(iter, iter+3, iter2);
     429  //suite.add(suite.equal(data.front(), 1));
     430}
  • trunk/yat/utility/DataWeightProxy.h

    r1535 r1537  
    3333
    3434     \brief Proxy class for DataWeight
     35
     36     DataIterator and WeightIterator should be a TrivialIterator
    3537  */
     38  template<typename DataIterator, typename WeightIterator>
    3639  class DataWeightProxy
    3740  {
     
    4043       \brief Default constructor
    4144
    42        \param data data value to hold
    43        \param weight weight value to hold
     45       \param data iterator pointing to data to hold
     46       \param weight iterator pointing to weight to hold
    4447     */
    45     DataWeightProxy(double& data, double& weight);
     48    DataWeightProxy(DataIterator data, WeightIterator weight)
     49      : data_(data), weight_(weight) {}
    4650
    4751    /**
    4852       \return reference to data
    4953     */
    50     double& data(void);
     54    double& data(void) { return *data_; }
    5155
    5256    /**
    5357       \return const reference to data
    5458     */
    55     const double& data(void) const;
     59    const double& data(void) const { return *data_ ; }
    5660
    5761    /**
    5862       \return reference to weight
    5963     */
    60     double& weight(void);
     64    double& weight(void) { return *weight_; }
    6165
    6266    /**
    6367       \return const reference to weight
    6468     */
    65     const double& weight(void) const;
     69    const double& weight(void) const { return *weight_; }
    6670
    6771    /**
    6872       \brief assignment operator
    6973     */
    70     DataWeightProxy& operator=(const DataWeight& rhs);
     74    DataWeightProxy& operator=(const DataWeight& rhs)
     75    {
     76      data() = rhs.data();
     77      weight() = rhs.weight();
     78      return *this;
     79    }
    7180
    7281    /**
    7382       \brief Conversion to DataWeight
    7483     */
    75     operator DataWeight() const;
     84    operator DataWeight() const { return DataWeight(data(), weight()); }
    7685
    7786  private:
    78     double& data_;
    79     double& weight_;
     87    DataIterator data_;
     88    WeightIterator weight_;
    8089
    81     // no copying
    82     DataWeightProxy(const DataWeightProxy&);
    83     DataWeightProxy& operator=(const DataWeightProxy&);
     90    // using compiler generated copy and assignment
     91    //DataWeightProxy& operator=(const DataWeightProxy&);
     92    //DataWeightProxy(const DataWeightProxy&);
    8493  };
     94
     95 
     96  /**
     97     \brief equality operator
     98   */
     99  template<typename DataIterator, typename WeightIterator>
     100  bool operator==(const DataWeightProxy<DataIterator, WeightIterator>& lhs,
     101                  const DataWeightProxy<DataIterator, WeightIterator>& rhs)
     102  {
     103    return lhs.data()==rhs.data() && lhs.weight()==rhs.weight();
     104  }
    85105
    86106}}} // of namespace utility, yat, and theplu
  • trunk/yat/utility/Makefile.am

    r1533 r1537  
    2323noinst_LTLIBRARIES = libutility.la
    2424libutility_la_SOURCES = \
    25   Alignment.cc ColumnStream.cc CommandLine.cc DataWeight.cc DataWeightProxy.cc \
     25  Alignment.cc ColumnStream.cc CommandLine.cc DataWeight.cc \
    2626  FileUtil.cc Index.cc kNNI.cc \
    2727  Matrix.cc MatrixWeighted.cc NNI.cc Option.cc \
  • trunk/yat/utility/WeightedIterator.h

    r1532 r1537  
    2323*/
    2424
     25#include <DataWeight.h>
     26#include <DataWeightProxy.h>
     27
    2528#include <boost/iterator/iterator_facade.hpp>
    2629
     
    3235
    3336  /**
    34      @brief WeightedIterator
     37     \brief WeightedIterator
    3538  */
    3639  template<typename DataIterator, typename WeightIterator>
     
    3942    WeightedIterator<DataIterator, WeightIterator>,
    4043    DataWeight,
    41     std::random_access_iterator_tag,
    42     DataWeight>
     44    typename std::iterator_traits<DataIterator>::iterator_category,
     45    DataWeightProxy<DataIterator, WeightIterator> >
    4346   
    4447  {
     
    5053      : d_iter_(d), w_iter_(w)
    5154    {}
     55   
     56
     57    /**
     58       \brief element operator
     59     */
     60    DataWeightProxy<DataIterator, WeightIterator> operator[](int n) const
     61    {
     62      return DataWeightProxy<DataIterator, WeightIterator>(d_iter_+n,
     63                                                           w_iter_+n);
     64    }     
     65   
    5266
    5367    /**
    5468       \brief Conversion constructor.
    55 
     69       
    5670       Create a WeightIterator<Base> from a
    5771       WeightIterator<B2>. Possible if B2 is convertible to a
     
    7286    DataIterator d_iter_;
    7387    WeightIterator w_iter_;
    74 
     88   
    7589    void advance(size_t n)
    7690    { std::advance(d_iter_, n); std::advance(w_iter_, n); }
    7791
    7892    void decrement(void) { --d_iter_; --w_iter_; }
    79 
     93   
    8094    typename std::iterator_traits<DataIterator>::difference_type
    8195    distance_to(const WeightedIterator& other) const
    8296    { return std::distance(d_iter_, other.d_iter_); }
    8397
    84     DataWeight dereference(void) const
    85     { return DataWeight(*d_iter_, *w_iter_); }
     98    utility::DataWeightProxy<DataIterator, WeightIterator>
     99    dereference(void) const
     100    {
     101      return DataWeightProxy<DataIterator, WeightIterator>(d_iter_,
     102                                                           w_iter_);
     103    }
    86104
    87105    bool equal(const WeightedIterator& other) const
Note: See TracChangeset for help on using the changeset viewer.