<br><br><div class="gmail_quote">On Thu, Aug 26, 2010 at 03:50, bearophile <span dir="ltr"><<a href="mailto:bearophileHUGS@lycos.com">bearophileHUGS@lycos.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Jason Spencer:<br>
<div class="im">> Knowing just the # of dimensions won't tell me<br>
> the total size or how to index. I need the size of each dimension.<br>
<br>
</div>If you create such structs, you do what you want, so it represents a nD rectangular array. The total size is computed by the product of n runtime values stored inside the struct itself. And the indexing is not a problem, but you need n-1 run-time multiplications to find your index, unless you impose the constraint your sizes are a power of two, so you can replace the multiplications with shifts, that sometimes may give a small performance gain.<br>
</blockquote><div><br>What bearophile means is something like this:<br><br>struct Tensor(T, size_t nDim)<br>{<br> T[nDim] dimensions;<br> T[] values;<br><br> this(...) etc.<br>}<br><br>so Tensor!(int,3) is a 3D perfectly cubic 'matrix' if ints, with for example dimensions 3*5*8.<br>
The only thing CT-defined is the rank (scalar: 0, vector: 1, etc), the number of dimensions. The dimensions themselves are RT values. The total size is the product of these values. It can be calculated during construction.<br>
<br>What you lose is the CT checking that can be done for multiplication or additions if all dimensions are exposed in the type.<br>I don't know, would that be an possible addition to Phobos? I remember many people here creating matrix libraries, so someone probably made this already.<br>
<br><br>Concerning arrays of arrays of arrays (or ranges), a useful template is one that gives the number of []'s, the rank:<br><br>template rank(T)<br>{<br> static if (!isArray!T) // or (isInputRange!T)<br> enum size_t rank = 0;<br>
else<br> enum size_t rank = 1 + rank!(ElementType!T); // continue recursion<br>}<br><br>As you can see, it considers a simple scalar type to have rank 0. Maybe that's not what you want.<br>This allows you to put simple template constraints, like if(rank!T == 2) ...<br>
<br>I played with ranges of ranges a few month ago. Maybe some functions there could interest you:<br><br><a href="http://svn.dsource.org/projects/dranges/trunk/dranges/docs/rangeofranges.html">http://svn.dsource.org/projects/dranges/trunk/dranges/docs/rangeofranges.html</a><br>
<a href="http://www.dsource.org/projects/dranges/browser/trunk/dranges/rangeofranges.d">http://www.dsource.org/projects/dranges/browser/trunk/dranges/rangeofranges.d</a><br><br>Mapping a range of ranges, creating them by tensorial product, etc. But the docs are still incomplete, and the global module is due a clean-up, I'm afraid.<br>
<br><br>Philippe<br><br></div></div>