Boost logo

Boost :

From: David White (dave_at_[hidden])
Date: 2002-05-21 05:39:28


Noel,

I would suggest making iterator_n the same iterator category as the type
it's adapting. This is so you can do something like:

distance(iterator_n(cont.begin()),iterator_n(cont.end(),n));

and have it operate in constant time if cont is (say), a vector, and in
order n time if cont is (say) a list.

Also, I would make sure it can support input and output iterators as the
base type, since this would also be useful.

Oh and yes, the class template wouldn't be called iterator_n, that's the
name of a helper function template for easy generation of the iterators.
Well, actually "iterator_n" is just a name off the top of my head, a
better name could likely be thought of.

Note that this might have efficiency implications compared to
specialized algorithms, since an iterator_n object will be larger than a
normal iterator, and it's copying will be mildly expensive.

David.

On Tue, 2002-05-21 at 09:35, Noel Yap wrote:
> --- David White <dave_at_[hidden]> wrote:
> > On Mon, 2002-05-20 at 23:13, Noel Yap wrote:
> > > --- David White <dave_at_[hidden]> wrote:
> > > Currently, I've created a function copy_n that
> > copies
> > > at most 'n' elements.
> >
> > So do I. Unfortunately this function template has
> > the same name as a
> > function template in a common STL implementation
> > (STLPort) which has
> > slightly different functionality; this can cause
> > confusion.
> >
> > I can see other '_n' type
> > > functions. iterator_n would make such functions
> > moot
> > > so iterator_n would be a great addition to Boost.
> > Is
> > > there an effort to add this into Boost?
> >
> > not at this stage, I only just posted the idea;
> > naturally there could be
> > if enough people said they thought it was a good
> > idea...
>
> I thought it (iterator_n) is such a great idea that I
> got rid of my copy_n function in favor of a newly
> implemented iterator_n adapter.
>
> I have some minor differences than the one you
> described. Here's the source if you're interested:
>
> template < typename ForwardIterator, typename Size
> = std::size_t >
> class iterator_n
> : private boost::iterator<
> std::forward_iterator_tag, typename
> std::iterator_traits< ForwardIterator >::value_type,
> typename std::iterator_traits< ForwardIterator
> >::difference_type >,
> private boost::forward_iteratable< iterator_n<
> ForwardIterator >, typename std::iterator_traits<
> ForwardIterator >::pointer >
> {
> private: ForwardIterator iter;
> private: Size n;
>
> private: void constraints(void)
> {
> boost::function_requires< typename
> boost::ForwardIteratorConcept< ForwardIterator > >();
> }
>
> public: iterator_n()
> : n(0)
> {
> }
>
> public: iterator_n(ForwardIterator iter, Size
> n = 0)
> : iter(iter),
> n(n)
> {
> }
>
> public: typename std::iterator_traits<
> ForwardIterator >::value_type &operator *(void) const
> {
> return *iter;
> }
>
> public: iterator_n &operator ++(void)
> {
> ++iter;
> --n;
> }
>
> public: bool operator ==(const iterator_n
> &rhs) const
> {
> return iter == rhs.iter
> || n == rhs.n;
> }
> };
>
> As you can see, it has a default constructor so that
> it can be usable in situations where there is no "end"
> iterator.
>
> There's probably a lot of room for improvement so I
> welcome any comments.
>
> Noel
>
>
> __________________________________________________
> Do You Yahoo!?
> LAUNCH - Your Yahoo! Music Experience
> http://launch.yahoo.com
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


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