Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-04-12 16:31:24


"Neal D. Becker" <ndbecker2_at_[hidden]> writes:

> David Abrahams wrote:
>
>> "Neal D. Becker" <ndbecker2_at_[hidden]> writes:
>>
>>> Is the preference for the former case over the latter intentional?
>>
>> Not particularly; it just worked out that way. I guess in the
>> interest of efficiency it'd be better to take the one that doesn't
>> involve a unary minus.
>>
>>> This
>>> caused some surprise when I tried to re-implement my
>>> cycle_iterator_adapator (a kind of circular buffer adaptor).
>>>
>>> std::copy (b, a, ...)
>>> will do:
>>> template<typename _RandomAccessIter, typename _OutputIter>
>>> inline _OutputIter
>>> __copy(_RandomAccessIter __first, _RandomAccessIter __last,
>>> _OutputIter __result,
>>> random_access_iterator_tag)
>>> {
>>> typedef typename
>>> iterator_traits<_RandomAccessIter>::difference_type
>>> _Distance;
>>> for (_Distance __n = __last - __first; __n > 0; --__n) {
>>> *__result = *__first;
>>> ++__first;
>>> ++__result;
>>> }
>>> return __result;
>>> }
>>>
>>> As I implemented cycle_iterator_adaptor distance_to, it was expecting
>>> that given iterators [a,b], you probably want a positive distance from a
>>> to b,
>>
>> Correct, if b is reachable by applying increments to a.
>>
>>> but the way iterator_adaptors is implemented, it wants a negative
>>> distance.
>>
>> Huh? What do you mean "it wants"?
>>
>
> Sorry for the confusion. I'm implementing a kind of circular buffer. You
> can reach from a->b or b->a with a positive increment.

Whoa there. A circular buffer must always have one invalid element.
Reaching a->b->a with positive increments should be impossible, or you
have a bug in your implementation. At least, that was true the last
time I thought about it. Otherwise, how can you tell the difference
between empty and full?

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

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