www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Template

reply Mael <mael.primet gmail.com> writes:
Hello,

another question,

I wanted to generate an image having an arbitrary dimension (each pixel is a 
n-tuple).

The option

class Image(vType, int vDim)
{
...
  vType[vDim][] data ;
}

isn't really good, because the array isn't "unfolded" resulting in some
overhead (or is there a way to build a vType[dim][n] array that would be
represented as a vType[n*dim] array, and the compiler would optimize arr[p][k]
in arr[p*dim+k] ?)

so I wanted to have a template generate a struct that would look like
struct tuple {
vType elem1 ;
vType elem2 ;
...
}
and define the data as
{
...
tuple[] data ;
}

(I guess this would give what I expect).
I couldn't get to do this with template metaprogramming, could anyone give a
hint on how to recurse to build such a struct ? 


Thanks 

 
Jun 06 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Mael" <mael.primet gmail.com> wrote in message 
news:g2bbpi$10n4$1 digitalmars.com...
 Hello,

 another question,

 I wanted to generate an image having an arbitrary dimension (each pixel is 
 a  n-tuple).

 The option

 class Image(vType, int vDim)
 {
 ...
  vType[vDim][] data ;
 }

 isn't really good, because the array isn't "unfolded" resulting in some 
 overhead (or is there a way to build a vType[dim][n] array that would be 
 represented as a vType[n*dim] array, and the compiler would optimize 
 arr[p][k] in arr[p*dim+k] ?)
If you declare a two-dimensional (not sure about more dimensions..) fixed-size array of fixed-size arrays, like "int[4][4]", it will be treated as a rectangular array and not as an array of arrays. They are mentioned here: (http://www.digitalmars.com/d/1.0/arrays.html) in the "Rectangular Arrays" section. :)
Jun 06 2008
parent reply Mael <mael.primet gmail.com> writes:
 If you declare a two-dimensional (not sure about more dimensions..) 
 fixed-size array of fixed-size arrays, like "int[4][4]", it will be treated 
And what prevents the compiler to represent int[3][] arrays as "single-dimensional" 3*length array, since this is what it would do in case the declaration was struct triple { int a, b, c ; } triple[] my_array ; ?
Jun 06 2008
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Mael" <mael.primet gmail.com> wrote in message 
news:g2biok$1epu$1 digitalmars.com...
 If you declare a two-dimensional (not sure about more dimensions..)
 fixed-size array of fixed-size arrays, like "int[4][4]", it will be 
 treated
And what prevents the compiler to represent int[3][] arrays as "single-dimensional" 3*length array, since this is what it would do in case the declaration was struct triple { int a, b, c ; } triple[] my_array ;
Ah, it seems that both are treated the same. If you make such an array: int[3][] a; a.length = 5; You'll find that the data for all the elements is contiguous, as with structs. I wouldn't be surprised if the compiler would access the values in it using one pointer dereference instead of 2. Fixed size arrays are weird, sometimes they're reference types and sometimes they're value types. It's hard to remember when :S
Jun 06 2008
parent Mael <mael.primet gmail.com> writes:
 structs.  I wouldn't be surprised if the compiler would access the values in 
 it using one pointer dereference instead of 2.
Well, using dmd 1.30 with -O -release gives a two-dereference code, but maybe there's a compiler option to change this behavior ?
Jun 06 2008