www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array operations + some strange things

reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
I know array operations aren't implemented yet but i can do this:

int[] arr = new int[10];

arr[] = 5; //set all elements of arr to 5
and i see this code as:
for(int i=0;i<arr.length;i++)
{
    arr[i]=5;
}

So i wanted to try this in two dimensions and wrote

 int [][] p4 = new int[][7];
 p4[] = new int[6];

expecting the last line to mean:
for(int i=0; i<p4.length; i++)
{
    p4[i]=new int[6];
}

but it DOESN'T:
It turned out to be something like:

int[] temp = new int[6];
for(int i=0; i<p4.length; i++)
{
    p4[i]=temp;
}

So i end up with an array where all rows point to the same
array instead of a new array being allocated foreach row!

Is this the expected behaviour? If so, what are the reasons?
May 05 2004
next sibling parent reply "Scott Egan" <scotte tpg.com.aux> writes:
It would appear that the rvalue is only evaluated once.

Issues like this show the risks with with some of semantic sugar people want
with arrays.


"Ivan Senji" <ivan.senji public.srce.hr> wrote in message
news:c7aob7$103l$1 digitaldaemon.com...
 I know array operations aren't implemented yet but i can do this:

 int[] arr = new int[10];

 arr[] = 5; //set all elements of arr to 5
 and i see this code as:
 for(int i=0;i<arr.length;i++)
 {
     arr[i]=5;
 }

 So i wanted to try this in two dimensions and wrote

  int [][] p4 = new int[][7];
  p4[] = new int[6];

 expecting the last line to mean:
 for(int i=0; i<p4.length; i++)
 {
     p4[i]=new int[6];
 }

 but it DOESN'T:
 It turned out to be something like:

 int[] temp = new int[6];
 for(int i=0; i<p4.length; i++)
 {
     p4[i]=temp;
 }

 So i end up with an array where all rows point to the same
 array instead of a new array being allocated foreach row!

 Is this the expected behaviour? If so, what are the reasons?
May 05 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Scott Egan" <scotte tpg.com.aux> wrote in message
news:c7arno$1685$1 digitaldaemon.com...
 It would appear that the rvalue is only evaluated once.

 Issues like this show the risks with with some of semantic sugar people
want
 with arrays.
This wouldn't be an issue if the semantics were consistently defined! I don't think anyone would like to define the array where every row points to the same one like in this example. int [][] p4 = new int[][7]; p4[] = new int[6]; Rrvalue should be evaluated every time, just like it is in the spec: a[] op b is for(int i=0; i<a.length; i++) { a[i] op b; }
 "Ivan Senji" <ivan.senji public.srce.hr> wrote in message
 news:c7aob7$103l$1 digitaldaemon.com...
 I know array operations aren't implemented yet but i can do this:

 int[] arr = new int[10];

 arr[] = 5; //set all elements of arr to 5
 and i see this code as:
 for(int i=0;i<arr.length;i++)
 {
     arr[i]=5;
 }

 So i wanted to try this in two dimensions and wrote

  int [][] p4 = new int[][7];
  p4[] = new int[6];

 expecting the last line to mean:
 for(int i=0; i<p4.length; i++)
 {
     p4[i]=new int[6];
 }

 but it DOESN'T:
 It turned out to be something like:

 int[] temp = new int[6];
 for(int i=0; i<p4.length; i++)
 {
     p4[i]=temp;
 }

 So i end up with an array where all rows point to the same
 array instead of a new array being allocated foreach row!

 Is this the expected behaviour? If so, what are the reasons?
May 05 2004
next sibling parent Norbert Nemec <Norbert.Nemec gmx.de> writes:
Ivan Senji wrote:

 "Scott Egan" <scotte tpg.com.aux> wrote in message
 news:c7arno$1685$1 digitaldaemon.com...
 It would appear that the rvalue is only evaluated once.

 Issues like this show the risks with with some of semantic sugar people
want
 with arrays.
This wouldn't be an issue if the semantics were consistently defined! I don't think anyone would like to define the array where every row points to the same one like in this example. int [][] p4 = new int[][7]; p4[] = new int[6]; Rrvalue should be evaluated every time, just like it is in the spec: a[] op b is for(int i=0; i<a.length; i++) { a[i] op b; }
I don't know which way it should be, but in any case, the spec should clearly define this. As far as I can see it, the specs for array operations are not very exact yet. I hope, much of this will be cleared up as soon as Walter or somebody tries to actually implement the stuff and finds the delicate cases in that course.
May 05 2004
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Ivan Senji wrote:

 "Scott Egan" <scotte tpg.com.aux> wrote in message
 news:c7arno$1685$1 digitaldaemon.com...
<snip>
 Rrvalue should be evaluated every time, just like it is in the spec:
  a[] op b
 is
 for(int i=0; i<a.length; i++)
 {
     a[i] op b;
 }
<snip> Actually, I don't think that's supposed to say anything about when or how many times b is evaluated. It's using b to mean the result, rather than the expression. Evaluating it once for each element would be analogous to the old C #define CUBE(x) ((x)*(x)*(x)) qwert = CUBE(++yuiop); i.e. it would lead to both confusion and potential undefined behaviour. The alternative would be to assign to each element a copy of the expression, i.e. (again using b to mean the result) a[] op b; is for (int i = 0; i < a.length; i++) { a[i] op b.dup; } But you could go on arguing over when this is desired and when it isn't, when you also consider AAs and class objects.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 05 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c7b661$1mqd$1 digitaldaemon.com...
 Ivan Senji wrote:

 "Scott Egan" <scotte tpg.com.aux> wrote in message
 news:c7arno$1685$1 digitaldaemon.com...
<snip>
 Rrvalue should be evaluated every time, just like it is in the spec:
  a[] op b
 is
 for(int i=0; i<a.length; i++)
 {
     a[i] op b;
 }
<snip> Actually, I don't think that's supposed to say anything about when or how many times b is evaluated. It's using b to mean the result, rather than the expression.
But when array operations get implemented it will mean the expression. For example with int[] a,b,c; c[] = a[] + b[]; means for(int i=0;i<c.length; i++) { c[i]=a[i]+b[i]; } And the expression is evaluated for every index i.
 Evaluating it once for each element would be analogous to the old C

 #define CUBE(x) ((x)*(x)*(x))
 qwert = CUBE(++yuiop);

 i.e. it would lead to both confusion and potential undefined behaviour.

 The alternative would be to assign to each element a copy of the
 expression, i.e. (again using b to mean the result)

 a[] op b;
 is
 for (int i = 0; i < a.length; i++) {
 a[i] op b.dup;
 }

 But you could go on arguing over when this is desired and when it isn't,
 when you also consider AAs and class objects....

 Stewart.

 --
 My e-mail is valid but not my primary mailbox, aside from its being the
 unfortunate victim of intensive mail-bombing at the moment.  Please keep
 replies on the 'group where everyone may benefit.
May 05 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Ivan Senji wrote:
<snip>
 But when array operations get implemented it will mean the expression.
 
 For example
 with int[] a,b,c;
 c[] = a[] + b[];
 
 means
 
 for(int i=0;i<c.length; i++)
 {
     c[i]=a[i]+b[i];
 }
 
 And the expression is evaluated for every index i.
<snip top of upside-down reply> Not _the_ expression, but _an_ expression. That expression is b[i], where b remains the _result_ of the expression in the code. Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 06 2004
parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c7d6dp$1o8d$1 digitaldaemon.com...
 Ivan Senji wrote:
 <snip>
 But when array operations get implemented it will mean the expression.

 For example
 with int[] a,b,c;
 c[] = a[] + b[];

 means

 for(int i=0;i<c.length; i++)
 {
     c[i]=a[i]+b[i];
 }

 And the expression is evaluated for every index i.
<snip top of upside-down reply> Not _the_ expression, but _an_ expression. That expression is b[i], where b remains the _result_ of the expression in the code.
I'm sorry but my English is not that great and i didn't undesrtand what areyou trying to say. It would be nice if there was a syntax to ensure that the expression on the right is evaluated every time. For example int[] x = new int[30]; int i=0; x[!] = i++; This would fill x with numbers form 0 to 29 what isn't currently possible this easy. I was also thinking (when we get array operations one day) that it would be great to have access to the index used in for. i could then write x[] = index(0) * 3; filling x with 0, 3,6... another thing i tried to do today is: int[][][] x; x = new int[][][10]; x[] = new int[][30]; //this doesn't work the expected way // maybe x[!] = new int[][30]; but this isn't even possible: x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice this would mean for(int i=0; i<x.length;i++) for(int j=0;j<x[i].length; j++) { x[i][j] = new int[15]; } But if we could do even: x[!][!][!] = index(0)+index(1)+index(2); that would be a very easy way to fill arrays with some pattern. All this i have said are only my daydreams :) about future D-arrays. But right now they are allready much better then (for example) C/C++ arrays. There are also many things that could be done to make them even better and more powerfull tool :)
 Stewart.

 --
 My e-mail is valid but not my primary mailbox, aside from its being the
 unfortunate victim of intensive mail-bombing at the moment.  Please keep
 replies on the 'group where everyone may benefit.
May 06 2004
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Ivan Senji wrote:

 "Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
 news:c7d6dp$1o8d$1 digitaldaemon.com...
<snip>
Not _the_ expression, but _an_ expression.  That expression is b[i],
where b remains the _result_ of the expression in the code.
I'm sorry but my English is not that great and i didn't undesrtand what areyou trying to say.
Maybe that wasn't the best way of putting it. Suppose you have a function that returns an array. Then you could do qwert[] = yuiop(); or equivalently qwert[] = yuiop()[]; The function yuiop is evaluated once. Each element of the result is then taken and put into the corresponding element of qwert. Similarly, with arithmetic c[] = a[] + b[]; the addition is done once across the array, and then the array elements are put into c. In fact, these are all equivalent: c[] = a + b; c[] = a[] + b; c[] = a + b[]; c[] = a[] + b[]; c[] = (a + b)[];
 It would be nice if there was a syntax to ensure that the expression on the
 right is
 evaluated every time.
 
 For example
 int[] x = new int[30];
 int i=0;
 x[!] = i++;
 
 This would fill x with numbers form 0 to 29 what isn't currently possible
 this easy.
I'm not sure about this. One might look at this and think "this isn't in a loop, so i will be incremented once". Especially before they've discovered what [!] means. Rewriting the left-hand side to change the behaviour of the right-hand side seems a little odd. Moreover, if the assignment is in the middle of an expression, or you have [!]s chained together, it could get really confusing.
 I was also thinking (when we get array operations one day) that it would be
 great to have access to the index used in for.
 
 i could then write
 x[] = index(0) * 3;
 filling x with 0, 3,6...
Hmm ... I don't know how tricky such a feature would be.
 another thing i tried to do today is:
 int[][][] x;
 x = new int[][][10];
 x[] = new int[][30];  //this doesn't work the expected way
 // maybe x[!] = new int[][30];
 
 but this isn't even possible:
 
 x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice
I don't know if something like x = new int[15][20][30]; would work. Maybe I'll try it when I get home.... <snip>
 But if we could do even:
 x[!][!][!] = index(0)+index(1)+index(2);
 
 that would be a very easy way to fill arrays with some pattern.
<snip> Maybe there are some APL hackers around, who'd like to be able to just about anything with a one-liner.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 06 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
news:c7e1kp$5d5$1 digitaldaemon.com...
 Ivan Senji wrote:

 "Stewart Gordon" <smjg_1998 yahoo.com> wrote in message
 news:c7d6dp$1o8d$1 digitaldaemon.com...
<snip>
Not _the_ expression, but _an_ expression.  That expression is b[i],
where b remains the _result_ of the expression in the code.
I'm sorry but my English is not that great and i didn't undesrtand what areyou trying to say.
Maybe that wasn't the best way of putting it. Suppose you have a function that returns an array. Then you could do qwert[] = yuiop(); or equivalently qwert[] = yuiop()[]; The function yuiop is evaluated once. Each element of the result is then taken and put into the corresponding element of qwert. Similarly, with arithmetic c[] = a[] + b[]; the addition is done once across the array, and then the array elements are put into c. In fact, these are all equivalent: c[] = a + b; c[] = a[] + b; c[] = a + b[]; c[] = a[] + b[]; c[] = (a + b)[];
 It would be nice if there was a syntax to ensure that the expression on
the
 right is
 evaluated every time.

 For example
 int[] x = new int[30];
 int i=0;
 x[!] = i++;

 This would fill x with numbers form 0 to 29 what isn't currently
possible
 this easy.
I'm not sure about this. One might look at this and think "this isn't in a loop, so i will be incremented once". Especially before they've discovered what [!] means. Rewriting the left-hand side to change the behaviour of the right-hand side seems a little odd. Moreover, if the assignment is in the middle of an expression, or you have [!]s chained together, it could get really confusing.
You are probbably right. It is good to ask stupid question because that way you can get good answers :)
 I was also thinking (when we get array operations one day) that it would
be
 great to have access to the index used in for.

 i could then write
 x[] = index(0) * 3;
 filling x with 0, 3,6...
Hmm ... I don't know how tricky such a feature would be.
 another thing i tried to do today is:
 int[][][] x;
 x = new int[][][10];
 x[] = new int[][30];  //this doesn't work the expected way
 // maybe x[!] = new int[][30];

 but this isn't even possible:

 x[][] = new int[15]; // x[!][!] = new int[15]; would also be nice
I don't know if something like x = new int[15][20][30]; would work. Maybe I'll try it when I get home....
This won't work. The most you can do is x = new int[][][30]; but is possible for rectangular arrays.
 <snip>
 But if we could do even:
 x[!][!][!] = index(0)+index(1)+index(2);

 that would be a very easy way to fill arrays with some pattern.
<snip> Maybe there are some APL hackers around, who'd like to be able to just about anything with a one-liner.... Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 06 2004
prev sibling parent reply "Ivan Senji" <ivan.senji public.srce.hr> writes:
Here goes another strange thing.
int[][][] x = new int[][][10];
x[] = new int[][20]; //but not with the expected meaning.
x[][] = new int[][21]; //possible
x[][][] = new int[][22]; //also possible
x[][][][] = new int[][23]; //still possible...

type of x is int[][][]
type of x[]is int[][]
type of x[][] is int[][]
type of x[][][] is int[][]
May 06 2004
parent reply vathixSpamFix dprogramming.com (Vathix) writes:
In article <c7dp1g$2pv9$1 digitaldaemon.com>, ivan.senji public.srce.hr says...
Here goes another strange thing.
int[][][] x = new int[][][10];
x[] = new int[][20]; //but not with the expected meaning.
x[][] = new int[][21]; //possible
x[][][] = new int[][22]; //also possible
x[][][][] = new int[][23]; //still possible...

type of x is int[][][]
type of x[]is int[][]
type of x[][] is int[][]
type of x[][][] is int[][]
x[] is shorthand for x[0 .. x.length]. So there's nothing wrong with x[0 .. x.length][0 .. x.length][0 .. x.length] nor with x[][][]. It's just re-slicing the same memory. -- Christopher E. Miller
May 06 2004
parent "Ivan Senji" <ivan.senji public.srce.hr> writes:
"Vathix" <vathixSpamFix dprogramming.com> wrote in message
news:c7dq7l$2qt9$1 digitaldaemon.com...
 In article <c7dp1g$2pv9$1 digitaldaemon.com>, ivan.senji public.srce.hr
says...
Here goes another strange thing.
int[][][] x = new int[][][10];
x[] = new int[][20]; //but not with the expected meaning.
x[][] = new int[][21]; //possible
x[][][] = new int[][22]; //also possible
x[][][][] = new int[][23]; //still possible...

type of x is int[][][]
type of x[]is int[][]
type of x[][] is int[][]
type of x[][][] is int[][]
x[] is shorthand for x[0 .. x.length]. So there's nothing wrong with x[0 .. x.length][0 .. x.length][0 ..
x.length]
 nor with x[][][]. It's just re-slicing the same memory.
Makes sence! Thanks. But it would be nice to have moredimensional slices.
 --
 Christopher E. Miller
May 06 2004