Circular Buffer

Andrea Fontana nospam at example.com
Tue Feb 11 01:15:01 PST 2014


On Tuesday, 11 February 2014 at 09:10:16 UTC, Andrea Fontana 
wrote:
> On Tuesday, 11 February 2014 at 03:10:02 UTC, Jonathan Dunlap 
> wrote:
>> Wow! This is GREAT stuff. My use-case is slightly more 
>> complex, and I'm not sure how to best apply this knowledge. 
>> The retro reverses the array which is problematic in itself as 
>> well as losing the starting index location. I have an array 
>> that I'd like to elegantly "rotate". Best way I can show this 
>> is by example of an imaginary rotate function:
>>
>> auto data = [1,2,3];
>> assert( data.cycle.rotate(2) == [3,1,2] );
>> assert( data.cycle.rotate(-2) == [2,3,1] );
>>
>> Perhaps what I'm doing is too complex requires me making my 
>> own iterator or something. In my quest of writing readable 
>> efficient code, I'm wondering what's the best route here. 
>> Thanks :)
>>
>> On Monday, 10 February 2014 at 09:16:31 UTC, Gary Willoughby 
>> wrote:
>>> void main(string[] args)
>>> {
>>> 	auto data = [1,2,3];
>>>
>>> 	assert(data.cycle.take(5).array       == [1,2,3,1,2]);
>>> 	assert(data.retro.cycle.take(5).array == [3,2,1,3,2]);
>>> }
>
> data.cycle.rotate(-2) == data.cycle(data.length + (-2 % 
> data.length))
> I guess you can implement your rotate function with this in 
> mind.

I missed a .rotate after data.cycle, of course.


More information about the Digitalmars-d-learn mailing list