[iterators] how about an identity_iterator or generate_iterator?

Hi, Has anybody found a need for an "identity_iterator"? Something that you can initialize to a value and returns that value every time it's referenced? It could model a random access iterator, and a default constructed version will mark an "end" of the identity_iterator. Something to the effect of: template <class Type> struct identity_iterator { identity_iterator(size_t count = 0) : v_(), count_(count) {} identity_iterator(const identity_iterator & o) : v_(o.v_), count_(o.count_) {} Type operator*() { return v_; } identity_iterator & operator++() { ++count_; *this; } identity_iterator & operator++(int) { ++count_; *this; } bool operator==(identity_iterator const & rhs) { return (this->count_ == rhs.count_); } bool operator<(identity_iterator const & rhs) { return (this->count_ < rhs.count_); } // ... and other required iterator nested types private: Type v_; size_t count_; }; Or even a "generate_iterator" which takes a nullary function object of signature "tuple<bool, T>()" where bool indicates whether it's at the end? It can be modeled as an input iterator. Something like: template <class T> struct generate_iterator { generate_iterator() : f_(), at_end(true) { } generate_iterator(function<tuple<bool, T>() > f) : f_(f), at_end(false) { } // copy constructor, swap, assignment, etc. T operator* () { T return_; tie(at_end, return_) = f_(); return return_; } // other required nested types private: function<tuple<bool, T>()> f_; bool at_end; }; Or is there already something out there which allows these iterator semantics? -- Dean Michael C. Berris Software Engineer, Friendster, Inc.

Dean Michael Berris wrote:
How about: template< typename T > class identity_iterator : public boost::iterator_facade< identity_iterator<T>, T , boost::forward_traversal_tag, const T&> { public: identity_iterator(const T& value, int index) : m_value(value), m_index(index) {} private: // virtual overrides friend class boost::iterator_core_access; void increment() { ++m_index; } const T& dereference() const { return m_value; } bool equal(literal_iterator<T> const& rhs) const { return m_index == rhs.m_index; } private: T m_value; int m_index; }; The index allows use with algorithms where and end iterator is req'd.
See: http://www.boost.org/doc/libs/1_37_0/libs/iterator/doc/function_output_itera... Jeff

On Fri, Dec 12, 2008 at 9:01 PM, Jeff Flinn <TriumphSprint2000@hotmail.com> wrote:
Looks good to me. :)
But that's an output iterator -- I'm looking for something that behaves like an _input_ iterator. Perhaps something like the above, but instead of returning a value calls an encapsulated function? Thanks for the quick response Jeff! -- Dean Michael C. Berris Software Engineer, Friendster, Inc.

#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
participants (3)
-
Dean Michael Berris
-
Jeff Flinn
-
Neal Becker