digitalmars.D.learn - question with compile-time reflection
- Zhenya (38/38) Mar 09 2013 Hi!
- Rob T (4/4) Mar 09 2013 template NDimensionalArrayType(T,alias size)
- Ivan Kazmenko (73/79) Mar 09 2013 I'm no D expert, but here's what I got.
- Zhenya (3/84) Mar 09 2013 Thank you very much for your time)
- Zhenya (5/5) Mar 09 2013 Although of course I would want to know what's wrong with
- Zhenya (2/7) Mar 09 2013 sorry for my english(
- Rob T (5/5) Mar 10 2013 template NDimensionalArrayType(T,alias size)
- Zhenya (6/11) Mar 10 2013 It's just a little hack for known bug - only string and integer
- Zhenya (2/2) Mar 10 2013 And since alias can represent any symbol,i think that it's
- anonymous (2/5) Mar 10 2013 http://dlang.org/template.html#TemplateAliasParameter
- Rob T (6/12) Mar 10 2013 I actually read that page, but somehow missed this description or
Hi! this code fails with message:NDimensionalArray.doIndex(Array) if (is(Array _ : NDimensionalArrayType!(T,s),const(int[]) s)) cannot deduce template function from argument types !()(int[2u][2u],const(int[])) Tell me please,am I wrong? And if yes,what should I change to make it work? template NDimensionalArrayType(T,alias size) if(is(typeof(size) _ : const int[]) && size.length > 0) { static if(size.length > 1) alias NDimensionalArrayType!(T,size[1..$])[size[0]] NDimensionalArrayType; else alias T[size[0]] NDimensionalArrayType; } struct NDimensionalArray(T,alias size) { private NDimensionalArrayType!(T,size) m_array; static ref T doIndex(Array)(Array array,const int[] index) //HERE if(is(Array _ : NDimensionalArrayType!(T,s),const int[] s)) { assert(index.length == s.length); if(index.length == 1) return array[index[0]]; else return doIndex(array[index[0]],index[1..$]); } ref T opIndex(const int[] index) { return doIndex(m_array,index); } alias m_array this; } void main() { NDimensionalArray!(int,[2,2]) array; }
Mar 09 2013
template NDimensionalArrayType(T,alias size) I think your use of "alias size" is incorrect. See http://dlang.org/declaration.html --rt
Mar 09 2013
this code fails with message:NDimensionalArray.doIndex(Array) if (is(Array _ : NDimensionalArrayType!(T,s),const(int[]) s)) cannot deduce template function from argument types !()(int[2u][2u],const(int[])) Tell me please,am I wrong? And if yes,what should I change to make it work?I'm no D expert, but here's what I got. 1. One problem is returning the wrong type from doIndex. When the condition "if(index.length == 1)" is false, the subsequent line is still compiled but fails to return a "ref T" since "array[index[0]]" is an array of T, not a T. The correct way (and better for performance, too) would be a compile-time "static if" there, like in the code below. (I don't quite get the syntax of the template constraint for doIndex, so I've just commented that out for now.) 2. The next problem is that fixed-size arrays obey value semantics, so they should be passed by reference and not by value. Here's what works for me (DMD 2.062): ----- import std.stdio; template NDimensionalArrayType(T,alias size) if(is(typeof(size) _ : const int[]) && size.length > 0) { static if(size.length > 1) alias NDimensionalArrayType!(T,size[1..$])[size[0]] NDimensionalArrayType; else alias T[size[0]] NDimensionalArrayType; } struct NDimensionalArray(T,alias size) { private NDimensionalArrayType!(T,size) m_array; static ref T doIndex(Array)(ref Array array,const int[] index) //HERE // if(is(Array _ : NDimensionalArrayType!(T,s),const int[] s)) { // assert(index.length == s.length); assert(index.length <= size.length); debug {writefln("doIndex (%s, %s = %s)", typeof(array).stringof, typeof(index).stringof, index);} static if(is(Array : T[])) { assert(index.length == 1); return array[index[0]]; } else { assert(index.length > 1); return doIndex(array[index[0]],index[1..$]); } } ref T opIndex(const int[] index) { return doIndex(m_array,index); } alias m_array this; } void main() { NDimensionalArray!(int,[5,2]) array; array[[4,1]] = 1; assert(array[[4,1]] == 1); } ----- With the debug mode on, it successfully prints the following: ----- doIndex (int[2u][5u], const(int[]) = [4, 1]) doIndex (int[2u], const(int[]) = [1]) doIndex (int[2u][5u], const(int[]) = [4, 1]) doIndex (int[2u], const(int[]) = [1]) ----- On a side note, this still looks like too much effort to do one opIndex in terms of performance. I doubt that any current compiler would optimize all that into a simple multiply + bounds-check + add loop. ----- Ivan Kazmenko.
Mar 09 2013
On Saturday, 9 March 2013 at 23:05:56 UTC, Ivan Kazmenko wrote:Thank you very much for your time) Your answer is very useful.this code fails with message:NDimensionalArray.doIndex(Array) if (is(Array _ : NDimensionalArrayType!(T,s),const(int[]) s)) cannot deduce template function from argument types !()(int[2u][2u],const(int[])) Tell me please,am I wrong? And if yes,what should I change to make it work?I'm no D expert, but here's what I got. 1. One problem is returning the wrong type from doIndex. When the condition "if(index.length == 1)" is false, the subsequent line is still compiled but fails to return a "ref T" since "array[index[0]]" is an array of T, not a T. The correct way (and better for performance, too) would be a compile-time "static if" there, like in the code below. (I don't quite get the syntax of the template constraint for doIndex, so I've just commented that out for now.) 2. The next problem is that fixed-size arrays obey value semantics, so they should be passed by reference and not by value. Here's what works for me (DMD 2.062): ----- import std.stdio; template NDimensionalArrayType(T,alias size) if(is(typeof(size) _ : const int[]) && size.length > 0) { static if(size.length > 1) alias NDimensionalArrayType!(T,size[1..$])[size[0]] NDimensionalArrayType; else alias T[size[0]] NDimensionalArrayType; } struct NDimensionalArray(T,alias size) { private NDimensionalArrayType!(T,size) m_array; static ref T doIndex(Array)(ref Array array,const int[] index) //HERE // if(is(Array _ : NDimensionalArrayType!(T,s),const int[] s)) { // assert(index.length == s.length); assert(index.length <= size.length); debug {writefln("doIndex (%s, %s = %s)", typeof(array).stringof, typeof(index).stringof, index);} static if(is(Array : T[])) { assert(index.length == 1); return array[index[0]]; } else { assert(index.length > 1); return doIndex(array[index[0]],index[1..$]); } } ref T opIndex(const int[] index) { return doIndex(m_array,index); } alias m_array this; } void main() { NDimensionalArray!(int,[5,2]) array; array[[4,1]] = 1; assert(array[[4,1]] == 1); } ----- With the debug mode on, it successfully prints the following: ----- doIndex (int[2u][5u], const(int[]) = [4, 1]) doIndex (int[2u], const(int[]) = [1]) doIndex (int[2u][5u], const(int[]) = [4, 1]) doIndex (int[2u], const(int[]) = [1]) ----- On a side note, this still looks like too much effort to do one opIndex in terms of performance. I doubt that any current compiler would optimize all that into a simple multiply + bounds-check + add loop. ----- Ivan Kazmenko.
Mar 09 2013
Although of course I would want to know what's wrong with constrait,because it protect doArray against incorrect using. I don't worry about effiency very much,I just wanted to right n-dimensional tree of intervals.And for doing it I needed n-dimensional array with opIndex like it.
Mar 09 2013
On Saturday, 9 March 2013 at 23:40:25 UTC, Zhenya wrote:Although of course I would want to know what's wrong with constrait,because it protect doArray against incorrect using. I don't worry about effiency very much,I just wanted to right n-dimensional tree of intervals.And for doing it I needed n-dimensional array with opIndex like it.sorry for my english(
Mar 09 2013
template NDimensionalArrayType(T,alias size) I'm wondering what "alias size" does? I can't find that form documented anywhere, but it seems to be valid. Thanks. --rt
Mar 10 2013
On Monday, 11 March 2013 at 00:10:46 UTC, Rob T wrote:template NDimensionalArrayType(T,alias size) I'm wondering what "alias size" does? I can't find that form documented anywhere, but it seems to be valid. Thanks. --rtIt's just a little hack for known bug - only string and integer value can be passed into template. size is array,and it's really size of n-dimensional array,every element matches its dimension.
Mar 10 2013
And since alias can represent any symbol,i think that it's correct usage.
Mar 10 2013
On Monday, 11 March 2013 at 00:10:46 UTC, Rob T wrote:template NDimensionalArrayType(T,alias size) I'm wondering what "alias size" does? I can't find that form documented anywhere, but it seems to be valid.http://dlang.org/template.html#TemplateAliasParameter
Mar 10 2013
On Monday, 11 March 2013 at 00:26:25 UTC, anonymous wrote:On Monday, 11 March 2013 at 00:10:46 UTC, Rob T wrote:I actually read that page, but somehow missed this description or more likely did not understand what it meant. I think I could spend 10 years reading up on D and still not fully know everything about it. --rttemplate NDimensionalArrayType(T,alias size) I'm wondering what "alias size" does? I can't find that form documented anywhere, but it seems to be valid.http://dlang.org/template.html#TemplateAliasParameter
Mar 10 2013