Circular Buffer

Jonathan Dunlap jadit2 at gmail.com
Mon Feb 10 19:10:01 PST 2014


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]);
> }



More information about the Digitalmars-d-learn mailing list