Jump to page: 1 2
Thread overview
.dup property
Nov 15, 2004
Regan Heath
Nov 15, 2004
h3r3tic
Nov 15, 2004
Garett Bass
General deep copy WAS (Re: .dup property)
Nov 15, 2004
Regan Heath
Nov 15, 2004
Garett Bass
Nov 15, 2004
Regan Heath
Nov 15, 2004
Sjoerd van Leent
Nov 15, 2004
Regan Heath
Nov 17, 2004
Simon Buchan
Nov 17, 2004
Regan Heath
November 14, 2004
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


November 14, 2004
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


November 15, 2004
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/
November 15, 2004
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 ;)
November 15, 2004
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 ;)


November 15, 2004
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/
November 15, 2004
Personally, I find the Java approach with object.clone() and interface Clonable very appealing.


November 15, 2004
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/
November 15, 2004
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
November 15, 2004
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/
« First   ‹ Prev
1 2