C++'s std::rotate

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 18 08:10:11 PDT 2014


On 8/18/14, 1:44 AM, Peter Alexander wrote:
> On Monday, 11 August 2014 at 03:29:56 UTC, Andrei Alexandrescu wrote:
>> [...] can be implemented generically for ranges that offer
>> front as a reference:
>>
>> bool sameFront(R1, R2)(R1 r1, R2 r2) {
>>   return &r1.front == &r2.front;
>> }
>
> This doesn't work for ranges that visit the same element twice, e.g.
>
> cycle(arr).take(arr.length + 1)
> [0, 0].map!(i => arr[i])

Correct. Though maybe that's a good thing - rotating a cycle is unlikely 
to produce something interesting.

> I suspect most ranges will have to implement the sameFront primitive
> manually, usually forwarding to the underlying range.

Only those that return rvalues from front.

> Related: most mutating algorithms won't work for these kinds of ranges,
> as we usually presume lvalue ranges never visit the same lvalue twice.
> Perhaps this needs to be mentioned on affected algorithms?

Looks like a benign effect to me.


Andrei



More information about the Digitalmars-d mailing list