Re: Successively prepending to slices.

2014-04-10 Thread monarch_dodra
On Thursday, 10 April 2014 at 20:03:46 UTC, JR wrote: In my NewlineBufferThingy struct, I sometimes need to move a slice at an arbitrary point of an array to the very beginning of it, like a poor man's circular buffer. Just in case somebody is wondering, phobos has std.range.cycle for circula

Re: Successively prepending to slices.

2014-04-10 Thread dnspies
On Thursday, 10 April 2014 at 05:54:52 UTC, monarch_dodra wrote: On Wednesday, 9 April 2014 at 23:53:04 UTC, dnspies wrote: Does concatenation always create a new array? What if the type of the original is immutable (or even const)? In that case, wouldn't it be worthwhile to check first if rh

Re: Successively prepending to slices.

2014-04-10 Thread JR
It depends on your use-case, I think. How often will you be prepending? Will you be appending as well? On Thursday, 10 April 2014 at 16:10:04 UTC, Steven Schveighoffer wrote: Note, I create a deque class in dcollections which maintains 2 arrays, one for prepending (and is in reverse order), an

Re: Successively prepending to slices.

2014-04-10 Thread Steven Schveighoffer
On Wed, 09 Apr 2014 19:18:25 -0400, dnspies wrote: I know that D slices are set up so you can successively append to them and it only has to re-allocate infrequently, but what about with prepending? ie, does D have a way to handle int[] x; for(int i=0; i < 1000; i++) { x = [i] ~ x; }

Re: Successively prepending to slices.

2014-04-10 Thread John Colvin
On Wednesday, 9 April 2014 at 23:18:27 UTC, dnspies wrote: I know that D slices are set up so you can successively append to them and it only has to re-allocate infrequently, but what about with prepending? ie, does D have a way to handle int[] x; for(int i=0; i < 1000; i++) { x = [i] ~ x;

Re: Successively prepending to slices.

2014-04-09 Thread monarch_dodra
On Wednesday, 9 April 2014 at 23:18:27 UTC, dnspies wrote: I know that D slices are set up so you can successively append to them and it only has to re-allocate infrequently, but what about with prepending? ie, does D have a way to handle int[] x; for(int i=0; i < 1000; i++) { x = [i] ~ x;

Re: Successively prepending to slices.

2014-04-09 Thread monarch_dodra
On Wednesday, 9 April 2014 at 23:53:04 UTC, dnspies wrote: Does concatenation always create a new array? What if the type of the original is immutable (or even const)? In that case, wouldn't it be worthwhile to check first if rhs can be copied to the right of lhs? Yes, it *always* creates a

Re: Successively prepending to slices.

2014-04-09 Thread bearophile
dnspies: Does concatenation always create a new array? Concatenation allocated a new array (unless some compiler is able to optimize away something in some cases, but I think this is not yet happening). Bye, bearophile

Re: Successively prepending to slices.

2014-04-09 Thread dnspies
On Wednesday, 9 April 2014 at 23:43:27 UTC, bearophile wrote: dnspies: efficiently, or is it better to avoid this sort of thing? D arrays are dynamic on the right. So appending on their left is not efficient (also here you are not prepending, you are concatenating. Array concatenation creat

Re: Successively prepending to slices.

2014-04-09 Thread bearophile
dnspies: efficiently, or is it better to avoid this sort of thing? D arrays are dynamic on the right. So appending on their left is not efficient (also here you are not prepending, you are concatenating. Array concatenation creates a new array). In some cases you can append on the right man