Boost logo

Boost :

Subject: [boost] [iterators] how about an identity_iterator or generate_iterator?
From: Dean Michael Berris (mikhailberis_at_[hidden])
Date: 2008-12-12 07:30:51


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
    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
    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.

Boost list run by bdawes at, gregod at, cpdaniel at, john at