|
Archives
D Programming
digitalmars.D
digitalmars.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 - 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 !
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.
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.
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);
}
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);
}
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.
:)
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
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.
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.
My email clients always put quoted text below. However, my news client
always quotes above.
Someone should do a study...
Saaa wrote:
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.
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>
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.
Anybody who is well-educated on how to use newsgroups?
My email clients always put quoted text below. However, my news client
always quotes above.
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.
Steven Schveighoffer wrote:
On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com>
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.
Yes, but there are some issues there:
1. the newsgroup/newsreader sometimes doesn't correctly put your message
as a reply to the original.
Is it really happening with _my_ messages? (When a thread becomes
broken up, how often compared to not is it due to user error?)
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.
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.
Steven Schveighoffer wrote:
On Thu, 18 Jun 2009 19:16:46 -0400, Stewart Gordon <smjg_1998 yahoo.com>
wrote:
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 didn't think you were. I think my point applies equally to anybody
who reads newsgroups.
<snip>
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.
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:
Steven 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=
as a reply to the original.
Is it really happening with _my_ messages? =A0(When a thread becomes bro=
up, 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.
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?
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
Hello Steven,
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?
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.
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.
On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon <smjg_1998 yahoo.com>
wrote:
Saaa wrote:
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.
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.
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.
<snip>
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.
Anybody who is well-educated on how to use newsgroups?
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.
My email clients always put quoted text below. However, my news
client always quotes above.
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
That 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
On Thu, 18 Jun 2009 19:16:46 -0400, Stewart Gordon <smjg_1998 yahoo.com>
wrote:
Steven Schveighoffer wrote:
On Sat, 13 Jun 2009 10:51:56 -0400, Stewart Gordon
<smjg_1998 yahoo.com> 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.
1. the newsgroup/newsreader sometimes doesn't correctly put your
message as a reply to the original.
Is it really happening with _my_ messages? (When a thread becomes
broken up, how often compared to not is it due to user error?)
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.
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'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.
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.
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
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...
-Steve
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.
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.
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 ... ?
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 work
If 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.
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 :)
Ever heard of recursion?
Why don't you simply handle all types recursively? Why do you need this
"array depth" stuff?
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;
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?
Hello Saaa,
Ever heard of recursion?
Why don't you simply handle all types recursively?
give a small example?
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?
give a small example?
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]));
}
but how does this differ from what Christopher Wright suggested?
Hello Saaa,
Ever heard of recursion?
Why don't you simply handle all types recursively?
maybe give a small example?
{
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]));
}
but how does this differ from what Christopher Wright suggested?
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.
|
|