Changeset 1062


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

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

Location:
trunk
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/iterator_test.cc

    r1038 r1062  
    4949  bool ok = true;
    5050
     51  *message << "testing utility::vector::iterator" << std::endl;
    5152  utility::vector vec(12);
    5253  classifier::DataLookup1D lookup(vec);
     
    6061  utility::vector::iterator end=vec.end();
    6162  std::sort(begin, end);
     63
     64  *message << "testing classifier::DataLookup1D::const_iterator" << std::endl;
    6265  classifier::DataLookup1D::const_iterator lbegin=lookup.begin();
    6366  classifier::DataLookup1D::const_iterator lend=lookup.end();
    6467 
     68  *message << "copy from DataLookup1D to Vector" << std::endl;
    6569  std::copy(lbegin, lend, begin);
     70  *message << "copy from Vector to Vector" << std::endl;
    6671  std::copy(begin, end, begin);
     72  *message << "sort Vector" << std::endl;
    6773  std::sort(begin, end);
    6874
  • trunk/yat/classifier/DataLookup1D.cc

    r1018 r1062  
    9595  DataLookup1D::const_iterator DataLookup1D::begin(void) const
    9696  {
    97     return DataLookup1D::const_iterator(*this, 0);
     97    typedef utility::Iterator<const DataLookup2D, const double> Iter;
     98    if (column_vector_)
     99      return const_iterator(Iter(*matrix_, 0, index_), matrix_->columns());
     100    return const_iterator(Iter(*matrix_, index_, 0), 1);
    98101  }
    99102
     
    101104  DataLookup1D::const_iterator DataLookup1D::end(void) const
    102105  {
    103     return DataLookup1D::const_iterator(*this, size());
     106    typedef utility::Iterator<const DataLookup2D, const double> Iter;
     107    if (column_vector_)
     108      return const_iterator(Iter(*matrix_, matrix_->rows(), index_),
     109                            matrix_->columns());
     110    return const_iterator(Iter(*matrix_, index_+1, 0), 1);
    104111  }
    105112
  • trunk/yat/classifier/DataLookup1D.h

    r1040 r1062  
    2727*/
    2828
    29 #include "yat/utility/Functor.h"
     29#include "DataLookup2D.h"
    3030#include "yat/utility/Iterator.h"
     31#include "yat/utility/StrideIterator.h"
    3132
    3233#include <iostream>
     
    4041namespace classifier { 
    4142
    42   class DataLookup2D;
    43 
    4443  ///
    4544  /// @brief Class for general vector view.
    4645  ///
    47 
    4846  class DataLookup1D
    4947  {
     
    5149  public:
    5250    /// 'Read Only' iterator
    53     typedef utility::Iterator<const utility::ContIdentity<const double,
    54                                                           const DataLookup1D> >
     51    typedef utility::StrideIterator<utility::Iterator<const DataLookup2D,
     52                                                      const double> >
    5553    const_iterator;
    5654
  • trunk/yat/classifier/MatrixLookup.h

    r1035 r1062  
    2828
    2929#include "DataLookup2D.h"
     30#include "yat/utility/Iterator.h"
    3031
    3132#include <iostream>
     
    6970  class MatrixLookup : public DataLookup2D
    7071  {
    71  
     72    typedef utility::Iterator<const MatrixLookup, const double&> const_iterator;
     73
    7274  public:
    73 
    74 
    7575    ///
    7676    /// Constructor creating a lookup into the entire @a matrix.
     
    217217
    218218    /**
     219     */
     220    const_iterator begin(void) const;
     221    const_iterator begin_row(size_t) const;
     222    const_iterator begin_column(size_t) const;
     223
     224    /**
    219225       the new MatrixLookup will consist of the rolumn vectors
    220226       defined by @a index. This means that the returned MatrixLookup
  • 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.