digitalmars.D.learn - Should be easy
- Saaa (19/19) Jun 12 2009 I can't figure out how to create the IndexArray function,
- Joel Christensen (2/2) Jun 12 2009 For 2 dim array I like "auto a=new char[][](40,25);" so that
- Saaa (2/4) Jun 12 2009
- downs (49/77) Jun 12 2009 Here's one.
- Saaa (8/57) Jun 12 2009 Thanks!
- downs (4/76) Jun 12 2009 IndexArray should take an array of integers as well. The int[] foo... sy...
- Saaa (5/9) Jun 12 2009 Why is that?
- Denis Koroskin (2/14) Jun 12 2009 Just don't quote. Why include quote, if there is nothing under it?
- Steven Schveighoffer (14/15) Jun 12 2009 Why is it that some email clients start you out at the top of the quoted...
- Saaa (4/13) Jun 12 2009 Who would do that!
- Stewart Gordon (17/26) Jun 13 2009 If I want to read the whole message you're replying to, I can open up
- Steven Schveighoffer (28/54) Jun 13 2009 Yes, but there are some issues there:
- Stewart Gordon (15/30) Jun 18 2009 Is it really happening with _my_ messages? (When a thread becomes
- Steven Schveighoffer (12/37) Jun 18 2009 I'm not sure. I see regulars all the time "start" new threads even thou...
- Jarrett Billingsley (8/28) Jun 18 2009 sage
- Stewart Gordon (9/21) Jun 22 2009 I didn't think you were. I think my point applies equally to anybody
- BCS (4/19) Jun 12 2009 I think that bit is the main reason. If you are not referencing somethin...
- Christopher Wright (14/16) Jun 12 2009 BaseType!(TArray) index(TArray : TArray[])(TArray array, int[] indices.....
- Saaa (16/32) Jun 12 2009 I really like templates :D
- Christopher Wright (17/22) Jun 13 2009 If you're using d2, add 'ref' to the return type.
- Saaa (21/34) Jun 16 2009 int[][][] a;
- grauzone (3/3) Jun 12 2009 Ever heard of recursion?
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 !
Jun 12 2009
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.
Jun 12 2009
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.
Jun 12 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); }
Jun 12 2009
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); }
Jun 12 2009
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;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. :)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); }
Jun 12 2009
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
Jun 12 2009
On Sat, 13 Jun 2009 03:37:59 +0400, Saaa <empty needmail.com> wrote:Just don't quote. Why include quote, if there is nothing under it?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
Jun 12 2009
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
Jun 12 2009
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.I also switch my way of reply on the type of the reply and often find it strange people quote the whole post only to reply something general.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.Who would do that!My email clients always put quoted text below. However, my news client always quotes above. Someone should do a study...Don't tell me nobody did that already . .
Jun 12 2009
Saaa wrote:If I want to read the whole message you're replying to, I can open up the mesasge you're replying to in my newsreader. <snip>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.Anybody who is well-educated on how to use newsgroups?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.Who would do that!<snip> Below/above what? - the cursor? - one or more blank lines? - your signature? - the message you typed, after you hit the send button? I for one would like to see newsreaders that will, at least as a pref, put the cursor above the quoted text and blank lines/sig below. This sets the user ready to work down the message, trimming it down and inserting reply text where it fits. See https://bugzilla.mozilla.org/show_bug.cgi?id=227376 Stewart.My email clients always put quoted text below. However, my news client always quotes above.
Jun 13 2009
On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:Saaa wrote:Yes, but there are some issues there: 1. the newsgroup/newsreader sometimes doesn't correctly put your message as a reply to the original. 2. You may not read messages threaded, so it might be tough to find the original message. 3. You almost ALWAYS want to read the immediately responded-to message for context (i.e. quote level 1), I am annoyed when I have to close the message I was reading to read the one responded to. Especially when I am following 5 threads at once. I can see arguments for both methods. I use both, but only really the second method for newsgroups. I'm not sure why, but it just feels more natural.If I want to read the whole message you're replying to, I can open up the mesasge you're replying to in my newsreader.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.<snip>Gee, I don't remember having newsgroups 101 in school :P In fact, I don't think I ever received education from anyone. I just do what feels natural, and what makes sense.Anybody who is well-educated on how to use newsgroups?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.Who would do that!Yes<snip> Below/above what? - the cursor?My email clients always put quoted text below. However, my news client always quotes above.- one or more blank lines?Yes- your signature?Yes- the message you typed, after you hit the send button?No, the quoted text appears as I type my message.I for one would like to see newsreaders that will, at least as a pref, put the cursor above the quoted text and blank lines/sig below. This sets the user ready to work down the message, trimming it down and inserting reply text where it fits. See https://bugzilla.mozilla.org/show_bug.cgi?id=227376That sounds like a feature I would use. I think another good feature would probably be to limit the quoted text to N levels (do you need 5 levels of context to make your point?). I do want to say that It doesn't bother me what people do, I just find it interesting how different social tools evolve in different directions, even when the interface is pretty much identical. -Steve
Jun 13 2009
Steven Schveighoffer wrote:On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:<snip>Is it really happening with _my_ messages? (When a thread becomes broken up, how often compared to not is it due to user error?)If I want to read the whole message you're replying to, I can open up the mesasge you're replying to in my newsreader.Yes, but there are some issues there: 1. the newsgroup/newsreader sometimes doesn't correctly put your message as a reply to the original.2. You may not read messages threaded, so it might be tough to find the original message.If I want to be able to see what a message is in reply to, why would I use a newsreader that doesn't offer a threaded view?3. You almost ALWAYS want to read the immediately responded-to message for context (i.e. quote level 1), I am annoyed when I have to close the message I was reading to read the one responded to. Especially when I am following 5 threads at once.<snip> Does Opera Mail make it as cumbersome as that? Most programs I've read newsgroups in offer two alternatives: - keeping two message windows open at once - a preview pane that can be quickly and easily changed to view a different message Maybe what would be better still is some kind of split-pane view of two messages at once. Stewart.
Jun 18 2009
On Thu, 18 Jun 2009 19:16:46 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:Steven Schveighoffer wrote:I'm not sure. I see regulars all the time "start" new threads even though they are replying to others. I'm not sure where the problem lies.On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:<snip>Is it really happening with _my_ messages? (When a thread becomes broken up, how often compared to not is it due to user error?)If I want to read the whole message you're replying to, I can open up the mesasge you're replying to in my newsreader.Yes, but there are some issues there: 1. the newsgroup/newsreader sometimes doesn't correctly put your message as a reply to the original.I'm not saying *you* personally, I meant someone who doesn't view a newsgroup in threaded mode. Someone who does that would enjoy context in the message.2. You may not read messages threaded, so it might be tough to find the original message.If I want to be able to see what a message is in reply to, why would I use a newsreader that doesn't offer a threaded view?I use the preview pane. Yes, I can open multiple messages, but I prefer having the context contained within the same message. I appreciate when the poster deletes unrelated context, but I don't think I've ever been annoyed at having too much context... -Steve3. You almost ALWAYS want to read the immediately responded-to message for context (i.e. quote level 1), I am annoyed when I have to close the message I was reading to read the one responded to. Especially when I am following 5 threads at once.<snip> Does Opera Mail make it as cumbersome as that? Most programs I've read newsgroups in offer two alternatives: - keeping two message windows open at once - a preview pane that can be quickly and easily changed to view a different message
Jun 18 2009
On Thu, Jun 18, 2009 at 11:15 PM, Steven Schveighoffer<schveiguy yahoo.com> wrote:On Thu, 18 Jun 2009 19:16:46 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:sageSteven Schveighoffer wrote:On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com=wrote:<snip>If I want to read the whole message you're replying to, I can open up the mesasge you're replying to in my newsreader.=A0Yes, but there are some issues there: =A01. the newsgroup/newsreader sometimes doesn't correctly put your mes=kenas a reply to the original.Is it really happening with _my_ messages? =A0(When a thread becomes bro=ughup, how often compared to not is it due to user error?)I'm not sure. =A0I see regulars all the time "start" new threads even tho=they are replying to others. =A0I'm not sure where the problem lies.It's most likely the web interface. It does that a lot. I've noticed the mailing list does it too, sometimes. But it's usually much better-behaved.
Jun 18 2009
Steven Schveighoffer wrote:On Thu, 18 Jun 2009 19:16:46 -0400, Stewart Gordon <smjg_1998 yahoo.com> wrote:<snip>I didn't think you were. I think my point applies equally to anybody who reads newsgroups. <snip>If I want to be able to see what a message is in reply to, why would I use a newsreader that doesn't offer a threaded view?I'm not saying *you* personally, I meant someone who doesn't view a newsgroup in threaded mode. Someone who does that would enjoy context in the message.I use the preview pane. Yes, I can open multiple messages, but I prefer having the context contained within the same message. I appreciate when the poster deletes unrelated context, but I don't think I've ever been annoyed at having too much context...Maybe ... but ISTM a long message that's been blindly quoted in its entirety and in a single contiguous block seldom does justice to the definition of "context". Stewart.
Jun 22 2009
Hello Steven,On Fri, 12 Jun 2009 19:40:02 -0400, Denis Koroskin <2korden gmail.com> wrote:I think that bit is the main reason. If you are not referencing something, then delete it from the quote. If you are referencing it, put your stuff below, sort of like a caption.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.
Jun 12 2009
Saaa wrote:I can't figure out how to create the IndexArray function, it should work on arrays of any depthBaseType!(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.
Jun 12 2009
Saaa wrote:I really like templates :D I actually tried something like this, but I couldn't get it to compile. My try misses quit a few things I see void IndexArray(T : T[], U)(T array, U index) { IndexArray( array[index[0]][], index[1..$]); } void IndexArray(T : int, U)(T array, U index) { array[index[0]] = -7;//test } I did get it to compile at one time, but didn't know how to use it, like your code.. index( array, index2); //compiles and all, but how do I set the value? index( array, index2) = -1; // doesn't work Also, why the ... ?I can't figure out how to create the IndexArray function, it should work on arrays of any depthBaseType!(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.
Jun 12 2009
Saaa wrote:I did get it to compile at one time, but didn't know how to use it, like your code.. index( array, index2); //compiles and all, but how do I set the value? index( array, index2) = -1; // doesn't workIf you're using d2, add 'ref' to the return type. Otherwise, you need indexAssign: void indexAssign(TArray : TArray[])(TArray array, BaseType!(TArray) value, int[] indices...) { static if (is (typeof (array[0]) == typeof(value)) { array[indices[0]] = value; } else { indexAssign(array[indices[0]], value, indices[1..$]); } }Also, why the ... ?In case you know the number of indices ahead of time. It costs nothing and lets you use a more natural syntax some of the time.
Jun 13 2009
template BaseType(T: T[]) { alias BaseType!(T) BaseType; } template BaseType(T) { alias T BaseType; }Otherwise, you need indexAssign: void indexAssign(TArray : TArray[])(TArray array, BaseType!(TArray) value, int[] indices...) { static if (is (typeof (array[0]) == typeof(value)) { array[indices[0]] = value; } else { indexAssign(array[indices[0]], value, indices[1..$]); } }int[][][] a; a.length = 3; a[1].length = 3; a[1][2].length = 3; int[3] index; index[0]=1; index[1]=2; index[2]=3; int value = 10; writefln(BaseType!(value).stringof); // template instance BaseType!(value) does not match any template declaration indexAssign(a,value,index); // template ddata.main.indexAssign(T : T[]) does not match any template declaration // template ddata.main.indexAssign(T : T[]) cannot deduce template function from argument types !()(int[][][],int,int[3u]) What am I doing wrong this time..? just when I though I understood the code a bit :)
Jun 16 2009
Ever heard of recursion? Why don't you simply handle all types recursively? Why do you need this "array depth" stuff?
Jun 12 2009
Ever heard of recursion? Why don't you simply handle all types recursively? Why do you need this "array depth" stuff?Most probably because I tackle my parsing problem sub-optimally. This is how I do it: load file into char[][] create: int[][][] arr; call: ddata.get( (in) file, (in) 'array1', (ref) arr); find row starting with 'int[][][] array1'. parse char by char from there every time the end of a value is found convert the slice to the correct type and put it in the corresponding arr index. return;
Jun 12 2009
Ever heard of recursion? Why don't you simply handle all types recursively?I'm still very interested in what exactly that means. Could you maybe give a small example?
Jun 14 2009
Hello Saaa,int Index(T)(T arr, int[] ind) { static if(is(T B == B[][])) // if array of array return Index!(B[])(arr[ind[0]], ind[1..$]); else { static assert(is(T B == B[])); // had better be an array; return arr[ind[0]]; } } void main() { int[][][] d; d.length = 3; d[1].length = 3; d[1][2].length = 3; d[1][2] = [0,1,1]; assert(0==Index!(int[][][])(d,[1,2,0])); }Ever heard of recursion? Why don't you simply handle all types recursively?I'm still very interested in what exactly that means. Could you maybe give a small example?
Jun 14 2009
Thanks, but how does this differ from what Christopher Wright suggested?int Index(T)(T arr, int[] ind) { static if(is(T B == B[][])) // if array of array return Index!(B[])(arr[ind[0]], ind[1..$]); else { static assert(is(T B == B[])); // had better be an array; return arr[ind[0]]; } } void main() { int[][][] d; d.length = 3; d[1].length = 3; d[1][2].length = 3; d[1][2] = [0,1,1]; assert(0==Index!(int[][][])(d,[1,2,0])); }Ever heard of recursion? Why don't you simply handle all types recursively?I'm still very interested in what exactly that means. Could you maybe give a small example?
Jun 15 2009
Hello Saaa,I don't know if it does, it is a direct answer to your question: "Could you maybe give a small example?" This code is an example of what handling all (array) types recursively means.Thanks, but how does this differ from what Christopher Wright suggested?int Index(T)(T arr, int[] ind) { static if(is(T B == B[][])) // if array of array return Index!(B[])(arr[ind[0]], ind[1..$]); else { static assert(is(T B == B[])); // had better be an array; return arr[ind[0]]; } } void main() { int[][][] d; d.length = 3; d[1].length = 3; d[1][2].length = 3; d[1][2] = [0,1,1]; assert(0==Index!(int[][][])(d,[1,2,0])); }Ever heard of recursion? Why don't you simply handle all types recursively?I'm still very interested in what exactly that means. Could you maybe give a small example?
Jun 15 2009