|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D.learn - code generalization
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 :( Jun 08 2009
"Christopher Wright" <dhasenan gmail.com> wrote in message news:h0kagg$13so$1 digitalmars.com...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 :( Jun 08 2009
"Christopher Wright" <dhasenan gmail.com> wrote in message news:h0kagg$13so$1 digitalmars.com...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 :( 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
|