Should be easy
downs
default_357-line at yahoo.de
Fri Jun 12 03:12:59 PDT 2009
Saaa wrote:
> I can't figure out how to create the IndexArray function,
> it should work on arrays of any depth
>
> int[][][] array; //any depth
> array.length=10;
> array[1].length=3;
> array[1][2].length=4;
> array[1][2][1]=99;
>
> writefln(array);
> //[[],[[],[],[0,99,0,0]],[],[],[],[],[],[],[],[]]
>
> int[3] index; //same length as depth
> index[0]=1;
> index[1]=2;
> index[2]=3;
>
> IndexArray( array, index) = -1;
> //equal to :
> //array[ index[0] ][ index[1] ][ index[2] ] = -1;
>
> writefln(array);
> //[[],[[],[],[0,99,0,-1]],[],[],[],[],[],[],[],[]]
>
> All suggestions are greatly appreciated !
>
>
>
Here's one.
module test144;
import std.stdio, std.metastrings;
struct PointerAssign(T) {
T* ptr;
T opAssign(T t) { *ptr = t; return t; }
static PointerAssign opCall(T* p) { PointerAssign res; res.ptr = p; return res; }
}
template isArray(T: T[]) { const bool isArray = true; }
template isArray(T) { const bool isArray = false; }
template Init(T) { const T Init; }
template ElemType(T) { alias typeof(Init!(T)[0]) ElemType; }
template BaseType(T) {
static if (isArray!(T)) alias BaseType!(ElemType!(T)) BaseType;
else alias T BaseType;
}
template Depth(T) {
static if (isArray!(T)) const int Depth = 1 + Depth!(ElemType!(T));
else const int Depth = 0;
}
string ctToString(int i) {
if (!i) return "0";
string res;
while (i) {
res = "0123456789"[i%10] ~ res;
i /= 10;
}
return res;
}
string index(int len) {
string res;
for (int i = 0; i < len; ++i)
res ~= "[indices["~ctToString(i)~"]] ";
return res;
}
PointerAssign!(BaseType!(T)) IndexArray(T)(T array, int[] indices...) {
return PointerAssign!(BaseType!(T)) (mixin("&array"~index(Depth!(T))));
}
void main() {
int[][][] array;
array.length = 10;
array[1].length = 3;
array[1][2].length = 4;
array[1][2][1] = 99;
writefln(array);
IndexArray(array, 1, 2, 3) = -1;
writefln(array);
}
More information about the Digitalmars-d-learn
mailing list