Fast temporary dynamic arrays? (And slicing of them)

2010-09-05 Thread Tom Kazimiers
Hi all, so I have started to look at D and dug through the documentation, but could not get a good answer on the following: How can I have a (temporary) dynamic array on stack and make references to it (no copying)? I successively put integers in an array (but don't know how much there will be in

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-05 Thread bearophile
Tom Kazimiers: > How can I have a (temporary) dynamic array on stack and make references > to it (no copying)? I successively put integers in an array (but don't > know how much there will be in advance) with an appender!(int[]) and get > the date out with appender.data(). Later on I pass the resul

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-05 Thread Jonathan M Davis
On Sunday 05 September 2010 18:02:29 Tom Kazimiers wrote: > Hi all, > > so I have started to look at D and dug through the documentation, but > could not get a good answer on the following: > > How can I have a (temporary) dynamic array on stack and make references > to it (no copying)? I success

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-05 Thread bearophile
My first test shows that it may work. But I have to grow the array backwards, and push back the array start, because that's how my stack grows (using alloca to allocate geometrically bigger chunks). So unless you want to reverse the items once the array is built, you have to change the algorithm

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread Steven Schveighoffer
On Sun, 05 Sep 2010 22:41:50 -0400, bearophile wrote: Tom Kazimiers: How can I have a (temporary) dynamic array on stack and make references to it (no copying)? I successively put integers in an array (but don't know how much there will be in advance) with an appender!(int[]) and get the dat

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread Tom Kazimiers
Hi, On 09/06/2010 04:55 AM, Jonathan M Davis wrote: > Static arrays are value types, but dynamic arrays are reference types. > > [...] > > No array copying takes place anywhere in that program. If you want to > copy an array, you'd do one of the following > > [...] > > Passing dynamic arrays to fu

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread Tom Kazimiers
Hi, thanks for your tests. On 09/06/2010 04:59 AM, bearophile wrote: > My first test shows that it may work. But I have to grow the array > backwards, and push back the array start, because that's how my stack > grows (using alloca to allocate geometrically bigger chunks). So > unless you want to

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread bearophile
Steven Schveighoffer: > Note that the new appender uses heap data to store its implementation, so > it's not as quick as it could be. This is per Andrei's requirement that > it be a reference type. Thank you for your answers. But I don't fully understand your answer. Do you mean it uses the

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread Steven Schveighoffer
On Tue, 07 Sep 2010 12:54:52 -0400, bearophile wrote: Steven Schveighoffer: Note that the new appender uses heap data to store its implementation, so it's not as quick as it could be. This is per Andrei's requirement that it be a reference type. Thank you for your answers. But I don't f

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread bearophile
Steven Schveighoffer: > An appender is an ouput range, so passing it into a function so the > function can output to it is a requirement. I see, that's useful. I will write a Pimp-less version of it, then (when I don't need a range, but just a local accumulator). Thank you for your always gent

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread bearophile
> Beware of stack overflows. And you may add another runtime test to see in what direction the stack grows. Bye, bearophile

Re: Fast temporary dynamic arrays? (And slicing of them)

2010-09-07 Thread bearophile
Tom Kazimiers: > But good to know that it would work with backward growing of the > array - do have you an example of that? Just created: import std.stdio: writeln; import std.c.stdlib: alloca; void main() { int n = 30; alias int T; enum int initialCapacity = 4; static assert(in