iterator_facade and distance()

Hi,
I am trying to implement an iterator using boost::iterator_facade. It
works fine but it is very slow, I think because the distance()
function takes lineair time as it is seems to be implemented in terms
of increment(). I would like to implement the distance() function
myself but I am not sure how. I tried to implement distance_to() like
this:
class const_data_stream_iterator :
public boost::iterator_facade

At Sat, 16 Apr 2011 09:55:31 +0900, Andrej van der Zee wrote:
Hi,
I am trying to implement an iterator using boost::iterator_facade. It works fine but it is very slow, I think because the distance() function takes lineair time as it is seems to be implemented in terms of increment(). I would like to implement the distance() function myself but I am not sure how. I tried to implement distance_to() like this:
class const_data_stream_iterator : public boost::iterator_facade
{ public: typedef boost::iterator_facade
facade_t; private:
friend class boost::iterator_core_access;
facade_t::difference_type difference_to(const_data_stream_iterator & to) const { ... } };
But this does not work. How can I do this?
You misspelled "distance_to." Fix the spelling and it should work better. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Hi Dave, Thank you for your email.
You misspelled "distance_to." Fix the spelling and it should work better.
Indeed a typo. Unfortunately, changing it to the method below does not change anything. The method is never called even though I explicitly call std::distance(), which, I assume, should call eventually distance_to(), facade_t::difference_type distance_to(const data_stream::const_iterator & to) const { abort(); } Cheers, Andrej

At Sat, 16 Apr 2011 21:35:29 +0900, Andrej van der Zee wrote:
Hi Dave,
Thank you for your email.
You misspelled "distance_to." Fix the spelling and it should work better.
Indeed a typo. Unfortunately, changing it to the method below does not change anything. The method is never called even though I explicitly call std::distance(), which, I assume, should call eventually distance_to(),
facade_t::difference_type distance_to(const data_stream::const_iterator & to) const { abort(); }
Not unless yours is a random access iterator. Is it? -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Hi Dave, Thanks for your email.
Not unless yours is a random access iterator. Is it?
Its a forward iterator. Changing it to a random access iterator solves it indeed. This is my first iterator that I am writing, but why is it not possible to implement a custom distance method for forward iterators? Am I doing something conceptually wrong? Or should I specialize the std::distance() function? Thank you, Andrej

At Sun, 17 Apr 2011 08:43:00 +0900, Andrej van der Zee wrote:
Hi Dave,
Thanks for your email.
Not unless yours is a random access iterator. Is it?
Its a forward iterator. Changing it to a random access iterator solves it indeed.
This is my first iterator that I am writing, but why is it not possible to implement a custom distance method for forward iterators?
The implementation of std::distance only has the iterator category to go on when deciding whether to move by many steps or by one jump. There isn't any other information it can use.
Am I doing something conceptually wrong?
Could be. If you can move your iterator by N steps in one jump, maybe it should be a random access iterator.
Or should I specialize the std::distance() function?
You could specialize that function for specific iterator types. There's no partial specialization of function templates, and you're not allowed to add an overload to namespace std. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

Hi Dave, Once again, thank you for your help, I have to rethink my iterators. Cheers, Andrej
participants (2)
-
Andrej van der Zee
-
Dave Abrahams