Boost logo

Boost :

Subject: Re: [boost] [iterators] how about an identity_iterator or generate_iterator?
From: Neal Becker (ndbecker2_at_[hidden])
Date: 2008-12-12 08:40:07


#ifndef constant_iterator_H
#define constant_iterator_H

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

namespace boost {

  template<typename scalar_t>
  class constant_iterator : public boost::iterator_facade<
    constant_iterator<scalar_t>,
    scalar_t,
    boost::random_access_traversal_tag,
    scalar_t
>
  {
  public:
    typedef typename boost::iterator_facade<
    constant_iterator<scalar_t>,
    scalar_t,
    boost::random_access_traversal_tag,
    scalar_t> super_t;

    typedef typename super_t::difference_type difference_type;
    typedef typename super_t::reference reference;
    typedef typename super_t::value_type value_type;

    explicit constant_iterator (scalar_t const& _k, int _cnt=0) :
      k (_k), cnt (_cnt) {}

  private:
    friend class boost::iterator_core_access;
  

    void increment () {
      --cnt;
    }

    difference_type
    distance_to (constant_iterator<scalar_t> const& y) const {
      return -(cnt - y.cnt);
    }

    bool equal (constant_iterator<scalar_t> const& y) const {
      return distance_to (y) == 0;
    }

    reference dereference() const {
      return k;
    }

    void advance (difference_type d) {
      cnt -= d;
    }

  private:

    scalar_t k;
    int cnt;
  };

  template<typename scalar_t>
  constant_iterator<scalar_t> make_constant_iterator(scalar_t k, int cnt=0) {
    return constant_iterator<scalar_t> (k, cnt);
  }

} //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