Boost logo

Boost :

From: Alex Chovanec (achovane_at_[hidden])
Date: 2004-08-06 02:27:54


I realize that this isn't the best time for proposals, what with everyone
trying to get the next release of boost out, but I have an idea for a new
iterator adaptor which I'd like to propose.

Consider an adapted iterator type Iter with base iterator type Base that has
the following traits:

    typedef Base value_type;
    typedef const Base & reference;

When you dereference an instance of the adapted iterator type Iter, you get
a const reference to its base iterator of type Base. So instead of
delegating the dereference operation to its base and returning the result,
the iterator adaptor simply returns its base. Since it doesn't dereference
its base, I would probably name it "lazy_iterator", or something like that.

There are some interesting things that you can do with this. For example:

        typedef std::vector< CString > vector;
        typedef std::vector< vector::iterator > iter_vector;
        typedef lazy_iterator< vector::iterator > lazy_iter;

        vector my_vector;
        iter_vector my_iter_vector;
        ...
        std::unique_copy(
            lazy_iter( my_vector.begin() ),
            lazy_iter( my_vector.end() ),
            std::back_inserter( my_iter_vector ),
            indirect_equal_to());

where indirect_equal_to is a special binary predicate which is defined like
this:

        struct indirect_equal_to
        {
            template <typename Iter>
            bool operator==(Iter a, Iter b)
            { return *a == *b; }
        };

After this call to std::unique_copy(), my_iter_vector is a sequence of
iterators that point to the unique elements in my_vector. Typically, you
would just copy the unique elements into another vector of strings, but the
approach described here is advantageous if you need to access the original
unique elements in my_vector (for instance, if you need to change them), or
if you want to avoid the overhead of copying strings.

You could also have an iterator adaptor type Iter whose dereferencing
operator returns &(*b), where b is its base iterator of type Base. This
could be useful for generating ranges of pointers from ranges of objects.

I think that this would be a useful iterator adaptor that would complement
indirect_iterator very nicely. Whereas indirect iterator is useful for
performing operations on ranges of iterators, this adaptor is ideal for
generating ranges of iterators.

Is anyone else interested in this?

Thanks,

Alex Chovanec


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