www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - painting arrays over arrays

reply BCS <BCS_member pathlink.com> writes:
Quite some time ago, I posted in one of these forums looking for a fast way to
“paint” a dynamic array of one type (int[5][] for example) over a dynamic array
of another type (int[]) several allocate-and-copy solutions were suggested, all
to slow, to clunky, etc. I found a good solution (see below) what I’m wondering

being or I did we all manage to not notice this one?

// take an input of type F[] and return an output of type T[] that overlays the
input

template paint(T, F)
{
T[] paint(F[] data)
{
return (cast(T*)data.ptr)[0 .. data.length*F.sizeof/T.sizeof];
}
}
Aug 17 2005
parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 17 Aug 2005 20:05:43 +0000 (UTC), BCS wrote:

 Quite some time ago, I posted in one of these forums looking for a fast way to
 “paint” a dynamic array of one type (int[5][] for example) over a dynamic array
 of another type (int[]) several allocate-and-copy solutions were suggested, all
 to slow, to clunky, etc. I found a good solution (see below) what I’m wondering

 being or I did we all manage to not notice this one?
 
 // take an input of type F[] and return an output of type T[] that overlays the
 input
 
 template paint(T, F)
 {
 T[] paint(F[] data)
 {
 return (cast(T*)data.ptr)[0 .. data.length*F.sizeof/T.sizeof];
 }
 }
No. That's basically what a few people said you needed to do. -- Derek Parnell Melbourne, Australia 18/08/2005 7:08:39 AM
Aug 17 2005
parent Derek Parnell <derek psych.ward> writes:
On Thu, 18 Aug 2005 07:09:30 +1000, Derek Parnell wrote:

 On Wed, 17 Aug 2005 20:05:43 +0000 (UTC), BCS wrote:
 
 Quite some time ago, I posted in one of these forums looking for a fast way to
 “paint” a dynamic array of one type (int[5][] for example) over a dynamic array
 of another type (int[]) several allocate-and-copy solutions were suggested, all
 to slow, to clunky, etc. I found a good solution (see below) what I’m wondering

 being or I did we all manage to not notice this one?
 
 // take an input of type F[] and return an output of type T[] that overlays the
 input
 
 template paint(T, F)
 {
 T[] paint(F[] data)
 {
 return (cast(T*)data.ptr)[0 .. data.length*F.sizeof/T.sizeof];
 }
 }
No. That's basically what a few people said you needed to do.
Sorry, I tell a lie. Your solution is much better than the ones provided earlier. -- Derek Parnell Melbourne, Australia 18/08/2005 7:21:23 AM
Aug 17 2005