Boost logo

Boost :

From: Neal Becker (ndbecker2_at_[hidden])
Date: 2005-06-03 13:41:11


christopher diggins wrote:

> ----- Original Message -----
> From: "Jonathan Turkanis" <technews_at_[hidden]>
> To: <boost_at_[hidden]>
> Sent: Friday, June 03, 2005 2:00 PM
> Subject: [boost] Re: stride iterators and matricies
>
>
>> christopher diggins wrote:
>>> Well all of this talk of matricies, and what not got me thinking that
>>> perhaps we should get back to basics and introduce stride iterators
>>> into Boost which properly model the Random Access Iterator concept
>>> (unless they are already existant somewhere and I simplty overlooked
>>> them :-p )
>>
>> You did look at the iterators library, didn't you?
>>
>> http://www.boost.org/libs/iterator/doc/index.html
>
> Yes. The only thing I saw resembling a stride iterator was permutation
> iterator. However I did not understand how to make a stride iterator using
> it. Perhaps it is trivial to create a stride iterator using the iterator
> library and you could show me how?
>

Here's my version:
// arch-tag: dc0a48cf-c241-4480-b722-6e181cbd9fca
/*
 * (C) Copyright Neal D. Becker (2004)
 * Permission to copy, use, modify, sell and distribute this software
 * is granted provided this copyright notice appears in all copies.
 * This software is provided "as is" without express or implied
 * warranty, and with no claim as to its suitability for any purpose.
*/

#ifndef strided_iterator_hpp
#define strided_iterator_hpp

#include <boost/iterator/iterator_adaptor.hpp>
#include <iterator>

namespace boost {

  template<typename BaseIterator>
  class strided_iterator : public boost::iterator_adaptor<
    strided_iterator<BaseIterator>,
    BaseIterator> {

    friend class iterator_core_access;

  public:

    typedef typename
boost::iterator_adaptor<strided_iterator<BaseIterator>,BaseIterator>
super_t;
    typedef typename std::iterator_traits<BaseIterator>::difference_type
difference_type;

    strided_iterator() {}

    explicit strided_iterator (BaseIterator _base, size_t _stride) :
      super_t (_base), stride (_stride) {}

    void increment()
    { this->base_reference() += stride; }

    void decrement()
    { this->base_reference() -= stride; }

    void advance(difference_type n)
    { this->base_reference() += n*stride; }

    difference_type
    distance_to(strided_iterator<BaseIterator> const& y) const
    { return (y.base_reference()-this->base_reference())/stride; }

      
  private:

    const int stride;
  };

  template<typename BaseIterator>
  strided_iterator<BaseIterator> make_strided_iterator(BaseIterator const&
begin, int stride) {
    return strided_iterator<BaseIterator> (begin, stride);
  }

    

} // namespace boost

#endif


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk