Changeset 1038


Ignore:
Timestamp:
Feb 5, 2008, 9:46:37 PM (16 years ago)
Author:
Peter
Message:

StrideIterator? - fixes ticket:303

Location:
trunk
Files:
6 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1028 r1038  
    5353  utility::vector::iterator begin=vec.begin();
    5454  // test iterator to const_iterator conversion
    55   ///////////////////////////////////////////////////////
    56   /* const conversion doesn't work for new vector structure
    57      see ticket 303
    5855  utility::vector::const_iterator ci = vec.begin();
    5956  ci = begin;
    6057  if (begin!=ci)
    6158    ok = false;
    62   */ 
    63   ///////////////////////////////////////////////////////
     59
    6460  utility::vector::iterator end=vec.end();
    6561  std::sort(begin, end);
  • trunk/yat/utility/Makefile.am

    r1027 r1038  
    4040  Option.h OptionArg.h OptionFile.h OptionInFile.h OptionOutFile.h \
    4141  OptionHelp.h OptionSwitch.h \
    42   PCA.h stl_utility.h SVD.h TypeInfo.h utility.h vector.h \
     42  PCA.h stl_utility.h StrideIterator.h SVD.h TypeInfo.h utility.h vector.h \
    4343  VectorBase.h VectorConstView.h VectorMutable.h VectorView.h \
    4444  WeNNI.h yat_assert.h
  • trunk/yat/utility/StrideIterator.h

    r1037 r1038  
    1 #ifndef _theplu_yat_utility_iterator_
    2 #define _theplu_yat_utility_iterator_
     1#ifndef _theplu_yat_utility_stride_iterator_
     2#define _theplu_yat_utility_stride_iterator_
    33
    44// $Id$
    55
    66/*
    7   Copyright (C) 2007 Peter Johansson
     7  Copyright (C) 2008 Peter Johansson
    88
    99  This file is part of the yat library, http://trac.thep.lu.se/yat
     
    2828
    2929#include <iterator>
    30 #include <stddef.h>
    31 #include <stdexcept>
    32 #include <utility>
    3330
    3431namespace theplu {
     
    3633namespace utility {
    3734
    38   class VectorBase;
    39 
    4035  /**
    41      @brief Iterator
     36     @brief Stride Iterator
     37
     38     Works as a pointer except that all arithmetic is using the stride value
    4239  */
    43   template<typename T, typename Container>
    44   class Iterator
     40  template<typename T>
     41  class StrideIterator
     42    : public std::iterator<std::random_access_iterator_tag, T>
    4543  {
    4644  public:
    47     typedef std::random_access_iterator_tag iterator_category;
    48     typedef double value_type;
    49     typedef size_t difference_type;
    50     typedef double* pointer;
    51     typedef T reference;
    52 
    53   private:
    54     typedef Iterator<reference, Container> self;
    55 
    56   public:
    57     /**
    58        \brief Default Constructor
     45    typedef typename std::iterator_traits<T*>::difference_type difference_type;
     46    typedef typename std::iterator_traits<T*>::reference reference;
     47    typedef typename std::iterator_traits<T*>::pointer pointer;
     48
     49    /**
     50       \brief Constructor
    5951    */
    60     Iterator(void) {};
    61 
    62     /**
    63        \brief Constructor
    64 
    65        \param container iterator points to
    66        \param index telling which element iterator points to
    67     */
    68     Iterator(Container& container, difference_type index)
    69       : container_(&container), index_(index) {}
    70 
    71     operator Iterator<const reference, const Container>()
    72     { return Iterator<const reference, const Container>(*container_, index_); }
     52    explicit StrideIterator(pointer p, difference_type stride=1)
     53      : pointer_(p), stride_(stride) {}
     54
     55    operator StrideIterator<const T>()
     56    { return StrideIterator<const T>(pointer_, stride_); }
    7357
    7458    /**
     
    7660     */
    7761    reference operator*(void) const
    78     {
    79       yat_assert<std::out_of_range>(index_<container_->size(),
    80                                     "Iterator::operator*");
    81       return container_->operator()(index_);
    82     }
     62    { return *pointer_; }
     63
     64    /**
     65     */
     66    pointer operator->() const { return &(operator*()); }
    8367
    8468    /**
     
    8771    reference operator[](difference_type n) const
    8872    {
    89       yat_assert<std::out_of_range>(index_+n < container_->size(),
    90                                     "Iterator::operator[]");
    91       return container_->operator()(index_+n);
     73      return pointer_[stride_*n];
    9274    }
    9375
     
    9779       \return reference to *this
    9880     */
    99     Iterator& operator++(void) { ++index_; return *this; }
     81    StrideIterator& operator++(void) { pointer_+=stride_; return *this; }
    10082
    10183    /**
     
    10486       \return copy of iterator prior increment
    10587     */
    106     Iterator operator++(int)
    107     { self tmp(*this); ++index_; return tmp;}
     88    StrideIterator operator++(int)
     89    { StrideIterator tmp(*this); pointer_+=stride_; return tmp;}
    10890
    10991    /**
     
    11294       \return reference to resulting iterator
    11395     */
    114     Iterator& operator+=(int n) { index_+=n; return *this; }
     96    StrideIterator& operator+=(int n) { pointer_+=stride_*n; return *this; }
    11597
    11698    /**
     
    119101       \return copy of iterator prior decrement
    120102     */
    121     Iterator operator--(int)
    122     { self tmp(*this); --index_; return tmp;}
     103    StrideIterator operator--(int)
     104    { StrideIterator tmp(*this); pointer_-=stride_; return tmp;}
    123105
    124106    /**
     
    127109       \return reference to resulting iterator
    128110     */
    129     Iterator& operator--(void) { --index_; return *this; }
     111    StrideIterator& operator--(void) { pointer_-=stride_; return *this; }
    130112
    131113    /**
     
    134116       \return reference to resulting iterator
    135117     */
    136     Iterator& operator-=(int n) { index_-=n; return *this; }
     118    StrideIterator& operator-=(int n) { pointer_-=n*stride_; return *this; }
    137119
    138120    /**
     
    141123       \return copy of resulting iterator
    142124     */
    143     friend Iterator operator+(const Iterator& lhs, size_t n)
    144     { return self(*lhs.container_, lhs.index_+n); }
     125    friend StrideIterator operator+(const StrideIterator& lhs,difference_type n)
     126    { StrideIterator tmp(lhs); tmp+=n; return tmp; }
    145127
    146128    /**
     
    149131       \return copy of resulting iterator
    150132     */
    151     friend Iterator operator-(const Iterator& lhs, size_t n)
    152     { return self(*lhs.container_, lhs.index_-n); }
     133    friend StrideIterator operator-(const StrideIterator& lhs,
     134                                    difference_type n)
     135    { StrideIterator tmp(lhs); tmp-=n; return tmp; }
    153136
    154137    /**
     
    157140       \return distance between \a lhs and \a rhs
    158141     */
    159     friend difference_type operator-(const Iterator& lhs, const Iterator& rhs)
    160     { return lhs.index_-rhs.index_; }
     142    friend difference_type operator-(const StrideIterator& lhs,
     143                                     const StrideIterator& rhs)
     144    {
     145      yat_assert<std::runtime_error>(lhs.stride_==rhs.stride_,
     146        "StrideIterator::operator- different stride in lhs and rhs.");
     147      return static_cast<difference_type>( (lhs.pointer_-rhs.pointer_)/
     148                                           lhs.stride_);
     149    }
    161150
    162151   
     
    166155       \return True if \a lhs and \a rhs are pointing to same element
    167156     */
    168     friend bool operator==(const self& lhs, const self& rhs)
    169     { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
     157    friend bool operator==(const StrideIterator& lhs, const StrideIterator& rhs)
     158    { return lhs.pointer_==rhs.pointer_; }
    170159   
    171160    /**
     
    174163       \return False if \a lhs and \a rhs are pointing to same element
    175164     */
    176     friend bool operator!=(const Iterator& lhs,
    177                            const Iterator& rhs)
    178     { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
     165    friend bool operator!=(const StrideIterator& lhs, const StrideIterator& rhs)
     166    { return !(lhs==rhs); }
    179167   
    180168    /**
    181169       \brief Less operator
    182170     */
    183     friend bool operator<(const self& lhs, const self& rhs)
    184     { return lhs.index_<rhs.index_; }
     171    friend bool operator<(const StrideIterator& lhs, const StrideIterator& rhs)
     172    { return lhs.pointer_<rhs.pointer_; }
    185173   
    186174    /**
    187175       \brief Less equal operator
    188176     */
    189     friend bool operator<=(const self& lhs, const self& rhs)
    190     { return lhs.index_<=rhs.index_; }
     177    friend bool operator<=(const StrideIterator& lhs, const StrideIterator& rhs)
     178    { return !(rhs<lhs); }
    191179   
    192180    /**
    193181       \brief Larger operator
    194182     */
    195     friend bool operator>(const self& lhs,
    196                            const self& rhs)
    197     { return lhs.index_>rhs.index_; }
     183    friend bool operator>(const StrideIterator& lhs, const StrideIterator& rhs)
     184    { return rhs<lhs; }
    198185   
    199186    /**
    200187       \brief Larger equal operator
    201188     */
    202     friend bool operator>=(const self& lhs, const self& rhs)
    203     { return lhs.index_>=rhs.index_; }
     189    friend bool operator>=(const StrideIterator& lhs, const StrideIterator& rhs)
     190    { return !(lhs<rhs); }
    204191   
    205192  private:
    206     Container* container_;
    207     size_t index_;
     193    T* pointer_;
     194    size_t stride_;
    208195
    209196    // Using compiler generated copy
    210     //Iterator(const Iterator&);
    211     //Iterator& operator=(const Iterator&);
     197    //StrideIterator(const StrideIterator&);
     198    //StrideIterator& operator=(const StrideIterator&);
    212199  };
    213200}}} // of namespace utility, yat, and theplu
  • trunk/yat/utility/VectorBase.cc

    r1027 r1038  
    5656  VectorBase::const_iterator VectorBase::begin(void) const
    5757  {
    58     return const_iterator(*this, 0);
     58    if (const_vec_)
     59      return const_iterator(&(this->operator()(0)), const_vec_->stride);
     60    return const_iterator(NULL, 1);
    5961  }
    6062
     
    6264  VectorBase::const_iterator VectorBase::end(void) const
    6365  {
    64     return const_iterator(*this, size());
     66    if (const_vec_)
     67      return const_iterator(&(this->operator()(0))+const_vec_->stride*size(),
     68                            const_vec_->stride);
     69    return const_iterator(NULL, 1);
    6570  }
    6671
  • trunk/yat/utility/VectorBase.h

    r1027 r1038  
    3131
    3232#include "Exception.h"
    33 #include "Iterator.h"
     33#include "StrideIterator.h"
    3434
    3535#include <iostream>
     
    9090  public:
    9191    /// \brief VectorBase::const_iterator
    92     typedef Iterator<const double&, const VectorBase> const_iterator;
     92    typedef StrideIterator<const double> const_iterator;
    9393
    9494    /**
  • trunk/yat/utility/VectorMutable.cc

    r1027 r1038  
    7575  VectorMutable::iterator VectorMutable::begin(void)
    7676  {
    77     return iterator(*this, 0);
     77    if (vec_)
     78      return iterator(&(this->operator()(0)), vec_->stride);
     79    return iterator(NULL, 1);
    7880  }
    7981
     
    9092  VectorMutable::iterator VectorMutable::end(void)
    9193  {
    92     return iterator(*this, size());
     94    if (vec_)
     95      return iterator(&(this->operator()(0))+vec_->stride*size(),
     96                            const_vec_->stride);
     97    return iterator(NULL, 1);
    9398  }
    9499
  • trunk/yat/utility/VectorMutable.h

    r1027 r1038  
    3232#include "VectorBase.h"
    3333#include "Exception.h"
    34 #include "Iterator.h"
     34#include "StrideIterator.h"
    3535
    3636#include <iostream>
     
    9393       \brief mutable iterator
    9494    */
    95     typedef Iterator<double&, VectorMutable> iterator;
     95    typedef StrideIterator<double> iterator;
    9696
    9797    /**
Note: See TracChangeset for help on using the changeset viewer.