www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - .dup property

reply "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
I cannot understand the behaviour of .dup property...
I was expecting the following code print 0 twice... but it only prints once.

Can anyone explain me why...

 double[][] a;
 a.length = 1;
 a[0] ~= 0;

 void vcopy(double[][] vector) {
  double[][] vcopy = vector.dup;
  vcopy[0][0] = 1;
 }

 vcopy(a);
 writefln("a[0]: ", a[0][0]); //prints: 1

 double[] b;
 b ~= 0;

 void vcopy2(double[] vector) {
  double[] vcopy = vector.dup;
  vcopy[0] = 1;
 }

 vcopy2(b);
 writefln("b[0]: ", b[0]); //prints: 0

-- 
Miguel Ferreira Simoes 
Nov 14 2004
parent reply "Miguel Ferreira Simões" <Kobold netcabo.pt> writes:
I found this solution... but it seems too obscure.
I think there is a cleaner one.

 void vcopy(double[][] vector) {
    double[][] vcopy = vector.dup;
    for(uint i = 0; i < vector.length; i++)
        vcopy[i] = vector[i].dup;
    vcopy[0][0] = 1;
 }



Miguel Ferreira Simoes 
Nov 14 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira Simões 
<Kobold netcabo.pt> wrote:
 I found this solution... but it seems too obscure.
 I think there is a cleaner one.

Nope.. a double[][] is a dymanic array of dynamic arrays, further "vector.dup" only dups the "dynamic array", not the "of dynamic arrays", you have to do it manually as you have below. This is a good example of where a standard template for dup'ing [][]'s is a good idea.
  void vcopy(double[][] vector) {
     double[][] vcopy = vector.dup;
     for(uint i = 0; i < vector.length; i++)
         vcopy[i] = vector[i].dup;
     vcopy[0][0] = 1;
  }

Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 14 2004
parent reply h3r3tic <foo bar.baz> writes:
Regan Heath wrote:

 On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira Simões 
 <Kobold netcabo.pt> wrote:
 
 I found this solution... but it seems too obscure.
 I think there is a cleaner one.

Nope.. a double[][] is a dymanic array of dynamic arrays, further "vector.dup" only dups the "dynamic array", not the "of dynamic arrays", you have to do it manually as you have below. This is a good example of where a standard template for dup'ing [][]'s is a good idea.

yup.. and as far as we don't have implicit tempate instantiation, maybe adding dup2, dup3, ... dup9? would be a good idea code-length-wise ;] one point of view would be that it's good to write the full form to realize the performance cost that one pays for copying multidimensional (or should i say - nested) arrays. on the other hand, though... sometimes you just don't care and want it written quickly. other idea: "dupN" ("dupn") since the compiler will know how far the arrays are nested / what is their dimension it can call "dup" for each of them yet another: make that recursive dup'ing default and rename the single-depth dup to dup1... heh, any other ideas ? or maybe there's somebody who want's to format /q my ideas ;)
Nov 14 2004
parent reply "Garett Bass" <gtbass studiotekne.com> writes:
how about .dup and .deep?  I would have preferred .copy & 
.deepCopy, since that is the terminology I usually see in 
programming texts, however, there are difficulties creating 
a true, general, deep copy method.



"h3r3tic" <foo bar.baz> wrote in message 
news:cn8t4g$8nb$1 digitaldaemon.com...
 Regan Heath wrote:

 On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira 
 Simões <Kobold netcabo.pt> wrote:

 I found this solution... but it seems too obscure.
 I think there is a cleaner one.

Nope.. a double[][] is a dymanic array of dynamic arrays, further "vector.dup" only dups the "dynamic array", not the "of dynamic arrays", you have to do it manually as you have below. This is a good example of where a standard template for dup'ing [][]'s is a good idea.

yup.. and as far as we don't have implicit tempate instantiation, maybe adding dup2, dup3, ... dup9? would be a good idea code-length-wise ;] one point of view would be that it's good to write the full form to realize the performance cost that one pays for copying multidimensional (or should i say - nested) arrays. on the other hand, though... sometimes you just don't care and want it written quickly. other idea: "dupN" ("dupn") since the compiler will know how far the arrays are nested / what is their dimension it can call "dup" for each of them yet another: make that recursive dup'ing default and rename the single-depth dup to dup1... heh, any other ideas ? or maybe there's somebody who want's to format /q my ideas ;)

Nov 14 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Sun, 14 Nov 2004 22:48:50 -0600, Garett Bass <gtbass studiotekne.com> 
wrote:
 how about .dup and .deep?  I would have preferred .copy &
 .deepCopy, since that is the terminology I usually see in
 programming texts, however, there are difficulties creating
 a true, general, deep copy method.

True, especially where pointers are involved. Do you simply copy the pointer, or the data it points at? With references to classes you could rely on there being a constructor taking a reference to another instance of the class. With value types i.e. structs,int,float,double,etc.. you can do a simple memory copy. But pointers.. you might want to copy the pointer, you might want to copy the data pointed to, you might want to do neither. i.e. set the new one to null. Perhaps we could rely on a free function in the form: TYPE *copy(TYPE *original) { } Regan
 "h3r3tic" <foo bar.baz> wrote in message
 news:cn8t4g$8nb$1 digitaldaemon.com...
 Regan Heath wrote:

 On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira
 Simões <Kobold netcabo.pt> wrote:

 I found this solution... but it seems too obscure.
 I think there is a cleaner one.

Nope.. a double[][] is a dymanic array of dynamic arrays, further "vector.dup" only dups the "dynamic array", not the "of dynamic arrays", you have to do it manually as you have below. This is a good example of where a standard template for dup'ing [][]'s is a good idea.

yup.. and as far as we don't have implicit tempate instantiation, maybe adding dup2, dup3, ... dup9? would be a good idea code-length-wise ;] one point of view would be that it's good to write the full form to realize the performance cost that one pays for copying multidimensional (or should i say - nested) arrays. on the other hand, though... sometimes you just don't care and want it written quickly. other idea: "dupN" ("dupn") since the compiler will know how far the arrays are nested / what is their dimension it can call "dup" for each of them yet another: make that recursive dup'ing default and rename the single-depth dup to dup1... heh, any other ideas ? or maybe there's somebody who want's to format /q my ideas ;)


-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 2004
parent reply "Garett Bass" <gtbass studiotekne.com> writes:
Personally, I find the Java approach with object.clone() and interface 
Clonable very appealing. 
Nov 15 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> 
wrote:
 Personally, I find the Java approach with object.clone() and interface
 Clonable very appealing.

Correct me if I'm wrong, but java does not have pointers, right? Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 2004
parent reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
Regan Heath wrote:
 On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> 
 wrote:
 
 Personally, I find the Java approach with object.clone() and interface
 Clonable very appealing.

Correct me if I'm wrong, but java does not have pointers, right? Regan

You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not. Regards, Sjoerd
Nov 15 2004
parent reply Regan Heath <regan netwin.co.nz> writes:
On Tue, 16 Nov 2004 00:09:55 +0100, Sjoerd van Leent 
<svanleent wanadoo.nl> wrote:
 Regan Heath wrote:
 On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass 
 <gtbass studiotekne.com> wrote:

 Personally, I find the Java approach with object.clone() and interface
 Clonable very appealing.

Correct me if I'm wrong, but java does not have pointers, right? Regan

You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.

I thought they were 'references'? Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 15 2004
parent reply "Simon Buchan" <currently no.where> writes:
On Tue, 16 Nov 2004 12:36:16 +1300, Regan Heath <regan netwin.co.nz> wrote:

 On Tue, 16 Nov 2004 00:09:55 +0100, Sjoerd van Leent  
 <svanleent wanadoo.nl> wrote:
 Regan Heath wrote:
 On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass  
 <gtbass studiotekne.com> wrote:

 Personally, I find the Java approach with object.clone() and interface
 Clonable very appealing.

Correct me if I'm wrong, but java does not have pointers, right? Regan

You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.

I thought they were 'references'? Regan

As in, you cant go <code> SomeClass* pCurrentObj = someObjArray[0]; do { something(pCurrentObj++); while(pCurrentObj); </code> (this assumes of course that the array is null terminated) and other DangerMouse things like that. Now, if you have anything with pointers in a non-legacy structure, is it not likely that they are to keep track of someone elses object? If you own an object and just dont want to continously carry it around, you use references instead, right? (of course, it's mostly code style) -- Using Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 17 2004
parent Regan Heath <regan netwin.co.nz> writes:
On Wed, 17 Nov 2004 21:28:54 +1300, Simon Buchan <currently no.where> 
wrote:
 On Tue, 16 Nov 2004 12:36:16 +1300, Regan Heath <regan netwin.co.nz> 
 wrote:

 On Tue, 16 Nov 2004 00:09:55 +0100, Sjoerd van Leent  
 <svanleent wanadoo.nl> wrote:
 Regan Heath wrote:
 On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass  
 <gtbass studiotekne.com> wrote:

 Personally, I find the Java approach with object.clone() and 
 interface
 Clonable very appealing.

Correct me if I'm wrong, but java does not have pointers, right? Regan

You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.

I thought they were 'references'? Regan

As in, you cant go <code> SomeClass* pCurrentObj = someObjArray[0]; do { something(pCurrentObj++); while(pCurrentObj); </code> (this assumes of course that the array is null terminated) and other DangerMouse things like that.

Yes (whether it's 'dangerous' or not is beside the point)
 Now, if you have anything with pointers in a non-legacy structure, is 
 it  not
 likely that they are to keep track of someone elses object?

It could be, what about .. struct big { .. lots of members .. } class Foo { big *one; Foo() { one = null; } ~Foo() { if (one) { delete one; one = null; } } void read_the_one(File f) { one = new big(); ..read data from file.. } } in other words a large struct (used for it's memory layout and lack of vtable etc) which is not always in existance, i.e. only created when necessary.
 If you own
 an object and just dont want to continously carry it around, you use
 references instead, right? (of course, it's mostly code style)

Unless it's a struct, as above. Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
Nov 17 2004