Changeset 1062 for trunk/yat/utility


Ignore:
Timestamp:
Feb 10, 2008, 7:41:06 PM (14 years ago)
Author:
Peter
Message:

redesigning Iterator class to hold a 2D container rather than 1D as before. refs #267

Location:
trunk/yat/utility
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/yat/utility/Iterator.h

    r1040 r1062  
    55
    66/*
    7   Copyright (C) 2007 Peter Johansson
     7  Copyright (C) 2007, 2008 Peter Johansson
    88
    99  This file is part of the yat library, http://trac.thep.lu.se/yat
     
    3636namespace utility {
    3737
    38   class VectorBase;
    39 
    4038  /**
    4139     @brief Iterator
    4240  */
    43   template<typename T>
     41  template<typename Container, typename T>
    4442  class Iterator
    4543  {
     
    4846    typedef double value_type;
    4947    typedef size_t difference_type;
     48    typedef T reference;
    5049    typedef double* pointer;
    51     typedef typename T::result_type reference;
    52     typedef typename T::container_type Container ;
    53 
     50   
    5451  private:
    55     typedef Iterator<T> self;
     52    typedef Iterator<Container, T> self;
    5653
    5754  public:
     
    6562
    6663       \param container iterator points to
    67        \param index telling which element iterator points to
     64       \param row telling which row iterator points to
     65       \param colun telling which column iterator points to
    6866    */
    69     Iterator(Container& container, difference_type index)
    70       : container_(&container), index_(index) {}
     67    Iterator(Container& container, difference_type row, difference_type column)
     68      : container_(&container), index_(row*container.columns()+column) {}
    7169
    7270    /**
     
    7573    reference operator*(void) const
    7674    {
    77       yat_assert<std::out_of_range>(index_<container_->size(),
     75      yat_assert<std::out_of_range>(index_ < this->size(),
    7876                                    "Iterator::operator*");
    79       return container_->operator()(index_);
     77      return container_->operator()(this->row(index_), this->column(index_));
    8078    }
    8179
     
    8583    reference operator[](difference_type n) const
    8684    {
    87       yat_assert<std::out_of_range>(index_+n < container_->size(),
     85      yat_assert<std::out_of_range>(index_+n < this->size(),
    8886                                    "Iterator::operator[]");
    89       return container_->operator()(index_+n);
     87      return container_->operator()(this->row(index_+n), this->column(index_+n));
    9088    }
    9189
     
    139137       \return copy of resulting iterator
    140138     */
    141     friend Iterator operator+(const Iterator& lhs, size_t n)
     139    friend Iterator operator+(const Iterator& lhs, difference_type n)
    142140    { return self(*lhs.container_, lhs.index_+n); }
    143141
     
    147145       \return copy of resulting iterator
    148146     */
    149     friend Iterator operator-(const Iterator& lhs, size_t n)
     147    friend Iterator operator-(const Iterator& lhs, difference_type n)
    150148    { return self(*lhs.container_, lhs.index_-n); }
    151149
     
    156154     */
    157155    friend difference_type operator-(const Iterator& lhs, const Iterator& rhs)
    158     { return lhs.index_-rhs.index_; }
     156    {
     157      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     158                                     "Iterator::operator-");
     159      return lhs.index_-rhs.index_;
     160    }
    159161
    160162   
     
    165167     */
    166168    friend bool operator==(const self& lhs, const self& rhs)
    167     { return lhs.container_==rhs.container_ && lhs.index_==rhs.index_; }
     169    {
     170      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     171                                     "Iterator::operator==");
     172      return lhs.index_==rhs.index_;
     173    }
    168174   
    169175    /**
     
    172178       \return False if \a lhs and \a rhs are pointing to same element
    173179     */
    174     friend bool operator!=(const Iterator& lhs,
    175                            const Iterator& rhs)
    176     { return !(lhs.container_==rhs.container_ && lhs.index_==rhs.index_); }
     180    friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
     181    {
     182      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     183                                     "Iterator::operator!=");
     184      return !(lhs==rhs);
     185    }
    177186   
    178187    /**
     
    180189     */
    181190    friend bool operator<(const self& lhs, const self& rhs)
    182     { return lhs.index_<rhs.index_; }
     191    {
     192      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     193                                     "Iterator::operator<");
     194      return lhs.index_<rhs.index_;
     195    }
    183196   
    184197    /**
     
    186199     */
    187200    friend bool operator<=(const self& lhs, const self& rhs)
    188     { return lhs.index_<=rhs.index_; }
     201    {
     202      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     203                                     "Iterator::operator<=");
     204      return lhs.index_<=rhs.index_;
     205    }
    189206   
    190207    /**
    191208       \brief Larger operator
    192209     */
    193     friend bool operator>(const self& lhs,
    194                            const self& rhs)
    195     { return lhs.index_>rhs.index_; }
     210    friend bool operator>(const self& lhs, const self& rhs)
     211    {
     212      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     213                                     "Iterator::operator>");
     214      return lhs.index_>rhs.index_;
     215    }
    196216   
    197217    /**
     
    199219     */
    200220    friend bool operator>=(const self& lhs, const self& rhs)
    201     { return lhs.index_>=rhs.index_; }
     221    {
     222      yat_assert<std::runtime_error>(lhs.container_==rhs.container_,
     223                                     "Iterator::operator>=");
     224      return lhs.index_>=rhs.index_;
     225    }
    202226   
    203227  private:
    204228    Container* container_;
    205     size_t index_;
     229    difference_type index_;
     230
     231    difference_type column(difference_type i) const
     232    { return i % container_->columns(); }
     233    difference_type row(difference_type i) const
     234    { return static_cast<difference_type>(i/container_->columns()); }
     235    difference_type size() const
     236    { return container_->columns()*container_->rows(); }
    206237
    207238    // Using compiler generated copy
  • trunk/yat/utility/Makefile.am

    r1044 r1062  
    3636include_utility_HEADERS = \
    3737  Alignment.h ColumnStream.h CommandLine.h \
    38   Exception.h FileUtil.h Functor.h Iterator.h iterator_traits.h \
     38  Exception.h FileUtil.h Iterator.h iterator_traits.h \
    3939  IteratorWeighted.h kNNI.h matrix.h NNI.h \
    4040  Option.h OptionArg.h OptionFile.h OptionInFile.h OptionOutFile.h \
Note: See TracChangeset for help on using the changeset viewer.