Thread overview | |||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 12, 2009 Should be easy | ||||
---|---|---|---|---|
| ||||
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 ! |
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | For 2 dim array I like "auto a=new char[][](40,25);" so that "a[39][24]='B';" 'B' is at the bottom right of the 2D array. |
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Joel Christensen | Thanks.
But my problem lays in supporting arrays of arbitrary depth.
> For 2 dim array I like "auto a=new char[][](40,25);" so that "a[39][24]='B';" 'B' is at the bottom right of the 2D array.
|
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | 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); } |
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | Thanks!
I thought about the idea of just creating the code and mix it in, but now I
can see why I failed at that: Your code is kind of read-only to me, for now,
because I need to change it a bit to accept an array instead of seperat
indices.
Wouldn't it be nice if it worked like this:
int[] index = (1,2,3);
array(index) = -1;
>
> 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);
> }
|
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
> Thanks!
> I thought about the idea of just creating the code and mix it in, but now I
> can see why I failed at that: Your code is kind of read-only to me, for now,
> because I need to change it a bit to accept an array instead of seperat
> indices.
>
> Wouldn't it be nice if it worked like this:
> int[] index = (1,2,3);
> array(index) = -1;
>
>> 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);
>> }
>
>
IndexArray should take an array of integers as well. The int[] foo... syntax is implicit "convert to array" anyway.
Also, please bottom-post. It's the convention.
:)
|
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | Saaa wrote:
> I can't figure out how to create the IndexArray function,
> it should work on arrays of any depth
BaseType!(TArray) index(TArray : TArray[])(TArray array, int[] indices...)
{
return index(array[indices[0]], indices[1..$]);
}
TElement index(TElement)(TElement element, int[] ignored...)
{
return element;
}
This uses template specialization to handle arrays differently than non-arrays (that's what the "TArray : TArray[]" business means). It uses the BaseType template worked out in one of your previous threads. It's close enough to being the simplest thing that could work that I wouldn't bother looking for anything simpler.
|
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to downs | > > IndexArray should take an array of integers as well. The int[] foo... syntax is implicit "convert to array" anyway. I think I'll go with Christophers code, hope you don't mind :) > > Also, please bottom-post. It's the convention. > > :) Why is that? scroll scroll I probably use a retarded newsreader :D |
June 12, 2009 Re: Should be easy | ||||
---|---|---|---|---|
| ||||
Posted in reply to Saaa | On Sat, 13 Jun 2009 03:37:59 +0400, Saaa <empty@needmail.com> wrote:
>>
>> IndexArray should take an array of integers as well. The int[] foo...
>> syntax is implicit "convert to array" anyway.
>
> I think I'll go with Christophers code, hope you don't mind :)
>
>>
>> Also, please bottom-post. It's the convention.
>>
>> :)
>
> Why is that?
> scroll
> scroll
> I probably use a retarded newsreader :D
>
>
Just don't quote. Why include quote, if there is nothing under it?
|
June 12, 2009 [OT] quoting text | ||||
---|---|---|---|---|
| ||||
Posted in reply to Denis Koroskin | On Fri, 12 Jun 2009 19:40:02 -0400, Denis Koroskin <2korden@gmail.com> wrote:
> Just don't quote. Why include quote, if there is nothing under it?
Why is it that some email clients start you out at the top of the quoted messages, and some clients go below?
I guess it depends on your style. If you respond to the entire message, then putting at the top makes sense, because then you can read the response quickly, and read the history below if you want.
But if you want to respond point-by-point, then going below makes sense. You can respond to each point, then have your main point at the bottomm of the message.
My email clients always put quoted text below. However, my news client always quotes above.
Someone should do a study...
-Steve
|
Copyright © 1999-2021 by the D Language Foundation