Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-06-03 12:41:32


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 ) I would
suggest that we introduce a mini-library simply for stride iterators (and
k-stride iterators, where the stride factor is a compile-time constant.) Any
interest in seeing the following stride iterators submitted to boost as a
mini-library?

// public domain by Christopher Diggins
#ifndef CDIGGINS_STRIDE_ITER_HPP
#define CDIGGINS_STRIDE_ITER_HPP

#include <iterator>

namespace cdiggins
{
  template<class Iter_T>
  class stride_iter
  {
  public:
    // public typedefs
    typedef typename std::iterator_traits<Iter_T>::value_type value_type;
    typedef typename std::iterator_traits<Iter_T>::reference reference;
    typedef typename std::iterator_traits<Iter_T>::difference_type
difference_type;
    typedef typename std::iterator_traits<Iter_T>::pointer pointer;
    typedef std::random_access_iterator_tag iterator_category;
    typedef stride_iter self;

    // constructors
    stride_iter() : m(null), step(0) { };
    explicit stride_iter(Iter_T x, difference_type n) : m(x), step(n) { }

    // operators
    self& operator++() { m += step; return *this; }
    self operator++(int) { self tmp = *this; m += step; return tmp; }
    self& operator+=(difference_type x) { m += x * step; return *this; }
    self& operator--() { m -= step; return *this; }
    self operator--(int) { self tmp = *this; m -= step; return tmp; }
    self& operator-=(difference_type x) { m -= x * step; return *this; }
    reference operator[](difference_type n) { return m[n * step]; }
    reference operator*() { return *m; }

    // friend operators
    friend bool operator==(const self& x, const self& y) { return x.m ==
y.m; }
    friend bool operator!=(const self& x, const self& y) { return x.m !=
y.m; }
    friend bool operator<(const self& x, const self& y) { return x.m <
y.m; }
    friend difference_type operator-(const self&x, const self& y) { return
x.m - y.m; }
    friend self operator+(const self&x, difference_type y) { return
stride_iter(x) += y; }
    friend self operator+(difference_type x, const self& y) { return
stride_iter(y) += x; }
  private:
    Iter_T m;
    difference_type step;
  };

  template<class Iter_T, int Step_N>
  class kstride_iter
  {
  public:
    // public typedefs
    typedef typename std::iterator_traits<Iter_T>::value_type value_type;
    typedef typename std::iterator_traits<Iter_T>::reference reference;
    typedef typename std::iterator_traits<Iter_T>::difference_type
difference_type;
    typedef typename std::iterator_traits<Iter_T>::pointer pointer;
    typedef std::random_access_iterator_tag iterator_category;
    typedef kstride_iter self;

    // constructors
    kstride_iter() : m(NULL) { }
    explicit kstride_iter(Iter_T x) : m(x) { }

    // operators
    self& operator++() { m += step(); return *this; }
    self operator++(int) { self tmp = *this; m += Step_N; return tmp; }
    self& operator+=(difference_type x) { m += x * Step_N; return *this; }
    self& operator--() { m -= step(); return *this; }
    self operator--(int) { self tmp = *this; m -= Step_N; return tmp; }
    self& operator-=(difference_type x) { m -= x * Step_N; return *this; }
    reference operator[](difference_type n) { return m[n * Step_N]; }
    reference operator*() { return *m; }

    // friend operators
    friend bool operator==(const self& x, const self& y) { return x.m ==
y.m; }
    friend bool operator!=(const self& x, const self& y) { return x.m !=
y.m; }
    friend bool operator<(const self& x, const self& y) { return x.m <
y.m; }
    friend difference_type operator-(const self&x, const self& y) { return
x.m - y.m; }
    friend self operator+(const self&x, difference_type y) { return
stride_iter(x) += y; }
    friend self operator+(difference_type x, const self& y) { return
stride_iter(y) += x; }
  private:
    Iter_T m;
  };
}

#endif

Christopher Diggins
http://www.cdiggins.com


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