digitalmars.D - .dup property
- Miguel Ferreira Simões (22/22) Nov 14 2004 I cannot understand the behaviour of .dup property...
- Miguel Ferreira Simões (9/9) Nov 14 2004 I found this solution... but it seems too obscure.
- Regan Heath (10/18) Nov 14 2004 Nope.. a double[][] is a dymanic array of dynamic arrays, further
- h3r3tic (13/26) Nov 14 2004 yup.. and as far as we don't have implicit tempate instantiation, maybe
- Garett Bass (6/36) Nov 14 2004 how about .dup and .deep? I would have preferred .copy &
- Regan Heath (17/55) Nov 15 2004 True, especially where pointers are involved. Do you simply copy the
- Garett Bass (2/2) Nov 15 2004 Personally, I find the Java approach with object.clone() and interface
- Regan Heath (6/8) Nov 15 2004 Correct me if I'm wrong, but java does not have pointers, right?
- Sjoerd van Leent (5/15) Nov 15 2004 You're wrong. All types in Java *are* pointers. Only primitives (i.e.:
- Regan Heath (6/19) Nov 15 2004 I thought they were 'references'?
- Simon Buchan (17/35) Nov 17 2004 As in, you cant go
- Regan Heath (30/69) Nov 17 2004 Yes (whether it's 'dangerous' or not is beside the point)
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
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
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
Regan Heath wrote:On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira Simões <Kobold netcabo.pt> wrote: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 ;)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.
Nov 14 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: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 ;)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.
Nov 14 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...-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/Regan Heath wrote:On Sun, 14 Nov 2004 23:46:48 -0000, Miguel Ferreira Simões <Kobold netcabo.pt> wrote: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 ;)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.
Nov 15 2004
Personally, I find the Java approach with object.clone() and interface Clonable very appealing.
Nov 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/
Nov 15 2004
Regan Heath wrote:On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> wrote:You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not. Regards, SjoerdPersonally, 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
Nov 15 2004
On Tue, 16 Nov 2004 00:09:55 +0100, Sjoerd van Leent <svanleent wanadoo.nl> wrote:Regan Heath wrote:I thought they were 'references'? Regan -- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/On Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> wrote:You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.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
Nov 15 2004
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: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/Regan Heath wrote:I thought they were 'references'? ReganOn Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> wrote:You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.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
Nov 17 2004
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:Yes (whether it's 'dangerous' or not is beside the point)On Tue, 16 Nov 2004 00:09:55 +0100, Sjoerd van Leent <svanleent wanadoo.nl> wrote: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.Regan Heath wrote:I thought they were 'references'? ReganOn Mon, 15 Nov 2004 16:22:57 -0600, Garett Bass <gtbass studiotekne.com> wrote:You're wrong. All types in Java *are* pointers. Only primitives (i.e.: int, boolean, double) are not.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? ReganNow, 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