digitalmars.D.learn - code generalization
- Saaa (30/30) Jun 08 2009 I just finished my array parser but I can't support just any depth becau...
- Christopher Wright (5/7) Jun 08 2009 Recursion?
I just finished my array parser but I can't support just any depth because each depth needs its own code the way I am doing it now :( Also, as you can see, I am using a doubling strategy to minimize the amount of allocations (strating with 4). Is this still a good strategy with D having a GC? .. switch( depth ) { case 0: if( temp.length < index[depth] ) temp.length = temp.length * 2; break; static if( is(U A:A[])) { case 1: if( temp[ index[0] ].length < index[depth] ) temp[index[0]].length = temp[index[0]].length * 2; break; } static if( is(U A:A[][])) { case 2: if( temp[ index[0] ][ index[1] ].length < index[depth] ) temp[ index[0] ][ index[1] ].length = temp[ index[0] ][ index[1] ].length * 2; break; } default: assert(false); break; } ..
Jun 08 2009
Saaa wrote:I just finished my array parser but I can't support just any depth because each depth needs its own code the way I am doing it now :(Recursion? static if (is (U A : A[])) will give you int[] for int[][], so you can recursively call the parsing function with A as the type argument rather than U.
Jun 08 2009
"Christopher Wright" <dhasenan gmail.com> wrote in message news:h0kagg$13so$1 digitalmars.com...Saaa wrote:I'm sorry, it is still a bit difficult to see as it's all a bit new to me but, depth is the current depth not the depth of the array. With a[][][] you can be in 3 depth levels so there need to be as many cases as the depth of the array. I probably also need recursion to in stead of the switch create a function which can set the length of an array at a certain depth :)I just finished my array parser but I can't support just any depth because each depth needs its own code the way I am doing it now :(Recursion? static if (is (U A : A[])) will give you int[] for int[][], so you can recursively call the parsing function with A as the type argument rather than U.
Jun 08 2009
"Christopher Wright" <dhasenan gmail.com> wrote in message news:h0kagg$13so$1 digitalmars.com...Saaa wrote:My attempt to rewrite the switch (failed : ) ddata\ddata.d(94): Error: slice expression array[] is not a modifiable lvalue ddata\ddata.d(169): template instance ddata.ddata.setLength!(int[]) error instantiating //an array depth walker :) void setLength (T)( ref T array, int depth , int index) { if(depth > 0) { depth--; setLength (array[], depth, index); //94 } else { if(array.length < index) array.length = array.length * 2; } }I just finished my array parser but I can't support just any depth because each depth needs its own code the way I am doing it now :(Recursion? static if (is (U A : A[])) will give you int[] for int[][], so you can recursively call the parsing function with A as the type argument rather than U.
Jun 08 2009
meant this: which of course also fails, but I hope you get the jist void setLength (T)( ref T array, int depth , int index[]) { if(depth > 0) { depth--; setLength (&array[index[0]], depth, index[1..$]); } else { if(array.length < index[0]) array.length = array.length * 2; } }
Jun 08 2009