Changeset 1490 for trunk


Ignore:
Timestamp:
Sep 12, 2008, 12:10:26 AM (13 years ago)
Author:
Peter
Message:

fixes #440

The bug was introduced in trunk after 0.4 release, so it has never
seen daylight.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1487 r1490  
    3232#include "yat/utility/stl_utility.h"
    3333#include "yat/utility/Vector.h"
     34#include "yat/utility/VectorView.h"
     35#include "yat/utility/VectorConstView.h"
    3436#include "yat/utility/WeightIterator.h"
    3537
     
    7274void test_random_access_iterator(RandomAccessIterator, test::Suite&);
    7375
     76void test_stride_iterator(test::Suite& suite);
     77
    7478int main(int argc, char* argv[])
    7579
     
    7781  suite.err() << "testing iterator" << std::endl;
    7882
     83  test_stride_iterator(suite);
    7984  old_main(suite);
    8085  test_boost_util(suite);
     
    111116    suite.add(false);
    112117
     118  suite.err() << "sorting...";
    113119  utility::Vector::iterator end=vec.end();
    114120  std::sort(begin, end);
     121  suite.err() << " sorting done\n";
    115122
    116123  suite.err() << "testing classifier::DataLookup1D::const_iterator" << std::endl;
     
    371378  if (ok_cached && !suite.ok())
    372379    suite.err() << "failed" << std::endl;
    373  
    374 
    375 }
     380}
     381
     382void test_stride_iterator(test::Suite& suite)
     383{
     384  suite.err() << "testing StrideIterator" << std::endl;
     385  using utility::Vector;
     386  Vector a(10);
     387  // stride 2
     388  utility::VectorConstView b(a, 0, 5, 2);
     389  if (!suite.add(b.end()-b.begin()==5))
     390    suite.err() << "ERROR: StrideIterator::operator- returned: "
     391                << b.end()-b.begin()
     392                << " expected 5\n";
     393  utility::VectorView c(a, 0, 3, 2);
     394  Vector::iterator begin = c.begin();
     395  Vector::iterator end = c.end();
     396  Vector::const_iterator cbegin = begin;
     397  Vector::const_iterator cend = end;
     398
     399  if (!suite.add(c.size()==3))
     400    suite.err() << "c.size() incorrect" << std::endl;
     401  if (!suite.add(cend-cbegin == 3))
     402    suite.err() << "cend-cbegin failed\n";
     403}
  • trunk/yat/utility/StrideIterator.h

    r1487 r1490  
    2323  along with yat. If not, see <http://www.gnu.org/licenses/>.
    2424*/
     25
     26// debug
     27#include <iostream>
    2528
    2629#include "iterator_traits.h"
     
    8891    : public boost::iterator_adaptor<StrideIterator<Iter>, Iter>
    8992  {
     93    typedef boost::iterator_adaptor<StrideIterator<Iter>,Iter> super_t;
     94
    9095  public:
    9196    /// type of underlying iterator
    9297    typedef Iter iterator_type;
     98
     99    /*
    93100    /// value type
    94101    typedef typename std::iterator_traits<Iter>::value_type value_type;
     
    101108    /// weighted_iterator_tag if iterator is weighted
    102109    typedef typename yat::utility::weighted_iterator_traits<Iter>::type w_type;
     110    */
    103111
    104112    /**
     
    107115       Using default constructor of BASE iterator.
    108116     */
    109     StrideIterator(size_t stride=1)
     117    explicit StrideIterator(size_t stride=1)
    110118      : StrideIterator::iterator_adaptor_(), stride_(stride) {}
    111119
     
    117125
    118126   
     127    /**
     128       \brief Copy constructor
     129     */
     130    StrideIterator(const StrideIterator& other)
     131      : StrideIterator::iterator_adaptor_(other.base()), stride_(other.stride())
     132    {}
     133
    119134    /**
    120135       \brief Conversion constructor.
     
    130145        stride_(other.stride()) {}
    131146
     147    /**
     148        \brief Assignment operator
     149     */
     150    StrideIterator& operator=(const StrideIterator& rhs)
     151    {
     152      stride_ = rhs.stride();
     153      this->base_reference() = rhs.base();
     154      return *this;
     155    }
     156
    132157    /**
    133158       \return stride
     
    141166    size_t stride_;
    142167
     168    //typedef typename StrideIterator::iterator_adaptor_::difference_type
    143169    typedef typename StrideIterator::iterator_adaptor_::difference_type
    144170    difference_t;
    145171
    146     void advance(difference_t n){ this->base_reference() += stride_*n; }
     172    void advance(typename super_t::difference_type n)
     173    { this->base_reference() += stride_*n; }
     174
    147175    void decrement(void) { this->base_reference()-=stride_; }
    148176
    149177    template <class OtherIterator>
    150     difference_t distance_type(const StrideIterator<OtherIterator>& other) const
    151     { return (other.base() - this->base())/stride_; }
     178    typename super_t::difference_type
     179    distance_to(const StrideIterator<OtherIterator>& other) const
     180    {
     181      // casting to int to avoid loss of sign in numerator
     182      return (other.base() - this->base() )/static_cast<int>(stride_);
     183    }
    152184
    153185    void increment(void) { this->base_reference()+=stride_; }
    154186
    155     // Using compiler generated copy
    156     //StrideIterator(const StrideIterator&);
    157     //StrideIterator& operator=(const StrideIterator&);
    158187  };
    159188
Note: See TracChangeset for help on using the changeset viewer.