www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - dup and arrays

reply Ellery Newcomer <ellery-newcomer utulsa.edu> writes:
I just noticed that dup does not dup deep.

In a two second search I couldn't find any reason for or against, but 
I'd kinda like it if

auto r2 = r.dup;
r2[i][j] = 0;
r[i][j] = 1;
assert(r2[i][j] != r[i][j]);

held.
Mar 26 2010
parent reply strtr <strtr spam.com> writes:
Ellery Newcomer Wrote:

 I just noticed that dup does not dup deep.
 
 In a two second search I couldn't find any reason for or against, but 
 I'd kinda like it if
 
 auto r2 = r.dup;
 r2[i][j] = 0;
 r[i][j] = 1;
 assert(r2[i][j] != r[i][j]);
 
 held.

There have been discussions about this before. Here is the first I found (couldn't find my own .ddup request :) http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=12631
Mar 26 2010
parent Philippe Sigaud <philippe.sigaud gmail.com> writes:
--00032555a272a6cfed0482c243e3
Content-Type: text/plain; charset=ISO-8859-1

On Sat, Mar 27, 2010 at 03:34, strtr <strtr spam.com> wrote:

 Ellery Newcomer Wrote:

 I just noticed that dup does not dup deep.

 In a two second search I couldn't find any reason for or against, but
 I'd kinda like it if

 auto r2 = r.dup;
 r2[i][j] = 0;
 r[i][j] = 1;
 assert(r2[i][j] != r[i][j]);

 held.

There have been discussions about this before. Here is the first I found (couldn't find my own .ddup request :) http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=12631

I sent a proposal for deep dup a few hours after your discussion, bu no one commented on it. And now I can't find it... hmm. Maybe the mail didn't make it. Anyway, here it is: template TypeofDeepdup(T) { alias typeof(deepdup(T.init)) TypeofDeepdup; } ref Unqual!T deepdup(T)(T t) if (is(T == struct) && !is(T.Types)) { staticMap!(TypeofDeepdup, typeof(t.tupleof)) tup; foreach(i,Type; tup) { tup[i] = deepdup(t.tupleof[i]);} return Unqual!T(tup); } Tuple!(staticMap!(TypeofDeepdup, T.Types)) deepdup(T)(T t) if (is(T.Types)) // Tuples { staticMap!(TypeofDeepdup, T.Types) tup; foreach(i,Type; tup) { tup[i] = deepdup(t.field[i]);} return tuple(tup); } Unqual!T deepdup(T)(T t) if (is(T == class)) { staticMap!(TypeofDeepdup, typeof(t.tupleof)) tup; foreach(i,Type; tup) { tup[i] = deepdup(t.tupleof[i]);} return new Unqual!T(tup); } TypeofDeepdup!(ElementType!T)[] deepdup(T)(T t) if (isDynamicArray!T) { auto result = new TypeofDeepdup!(ElementType!T)[](t.length); foreach(i, elem; t) result[i] = deepdup(elem); return result; } TypeofDeepdup!(ElementType!T)[T.length] deepdup(T)(T t) if (isStaticArray!T) { TypeofDeepdup!(ElementType!T)[T.length] result = t; foreach(ref elem; result) elem = deepdup(elem); return result; } TypeofDeepdup!T* deepdup(T)(T* t) { return &deepdup(*t); } Unqual!T deepdup(T)(T t) if (!is(T == struct) && !is(T == class) && !isArray!T && !is(T.Types) && !isPointer!T) { return cast(Unqual!T)t; } It seems to work well enough for my purposes, but still has some limitations: for example, it unqualifies everything (no more const / immutable after dupping). Philippe --00032555a272a6cfed0482c243e3 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable <br><br><div class=3D"gmail_quote">On Sat, Mar 27, 2010 at 03:34, strtr <sp= an dir=3D"ltr">&lt;<a href=3D"mailto:strtr spam.com">strtr spam.com</a>&gt;= </span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin: 0pt 0p= t 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;"=

<br> &gt; I just noticed that dup does not dup deep.<br> &gt;<br> &gt; In a two second search I couldn&#39;t find any reason for or against, = but<br> &gt; I&#39;d kinda like it if<br> &gt;<br> &gt; auto r2 =3D r.dup;<br> &gt; r2[i][j] =3D 0;<br> &gt; r[i][j] =3D 1;<br> &gt; assert(r2[i][j] !=3D r[i][j]);<br> &gt;<br> &gt; held.<br> <br> </div></div>There have been discussions about this before.<br> Here is the first I found (couldn&#39;t find my own .ddup request :)<br> <a href=3D"http://www.digitalmars.com/webnews/newsgroups.php?art_group=3Ddi= gitalmars.D&amp;article_id=3D12631" target=3D"_blank">http://www.digitalmar= s.com/webnews/newsgroups.php?art_group=3Ddigitalmars.D&amp;article_id=3D126= 31</a></blockquote> <div>=A0<br>I sent a proposal for deep dup a few hours after your discussio= n, bu no one commented on it. And now I can&#39;t find it... hmm. Maybe the= mail didn&#39;t make it.<br></div></div><br>Anyway, here it is:<br><br>tem= plate TypeofDeepdup(T)<br> {<br>=A0=A0=A0 alias typeof(deepdup(T.init)) TypeofDeepdup;<br>}<br><br>ref= Unqual!T deepdup(T)(T t) if (is(T =3D=3D struct) &amp;&amp; !is(T.Types))<= br>{<br>=A0=A0=A0 staticMap!(TypeofDeepdup, typeof(t.tupleof)) tup;<br>=A0= =A0=A0 foreach(i,Type; tup) { tup[i] =3D deepdup(t.tupleof[i]);}<br> =A0=A0=A0 return Unqual!T(tup);<br>}<br><br>Tuple!(staticMap!(TypeofDeepdup= , T.Types))<br>deepdup(T)(T t) if (is(T.Types)) // Tuples<br>{<br>=A0=A0=A0= staticMap!(TypeofDeepdup, T.Types) tup;<br>=A0=A0=A0 foreach(i,Type; tup) = { tup[i] =3D deepdup(t.field[i]);}<br> =A0=A0=A0 return tuple(tup);<br>}<br><br>Unqual!T deepdup(T)(T t) if (is(T = =3D=3D class))<br>{<br>=A0=A0=A0 staticMap!(TypeofDeepdup, typeof(t.tupleof= )) tup;<br>=A0=A0=A0 foreach(i,Type; tup) { tup[i] =3D deepdup(t.tupleof[i]= );}<br>=A0=A0=A0 return new Unqual!T(tup);<br> }<br><br>TypeofDeepdup!(ElementType!T)[] deepdup(T)(T t) if (isDynamicArray= !T)<br>{<br>=A0=A0=A0 auto result =3D new TypeofDeepdup!(ElementType!T)[](t= .length);<br>=A0=A0=A0 foreach(i, elem; t) result[i] =3D deepdup(elem);<br>= =A0=A0=A0 return result;<br> }<br><br>TypeofDeepdup!(ElementType!T)[T.length] deepdup(T)(T t) if (isStat= icArray!T)<br>{<br>=A0=A0=A0 TypeofDeepdup!(ElementType!T)[T.length] result= =3D t;<br>=A0=A0=A0 foreach(ref elem; result) elem =3D deepdup(elem);<br>= =A0=A0=A0 return result;<br> }<br><br>TypeofDeepdup!T* deepdup(T)(T* t)<br>{<br>=A0=A0=A0 return &amp;de= epdup(*t);<br>}<br><br>Unqual!T deepdup(T)(T t) if (!is(T =3D=3D struct) &a= mp;&amp; !is(T =3D=3D class) &amp;&amp; !isArray!T &amp;&amp; !is(T.Types) = &amp;&amp; !isPointer!T)<br> {<br>=A0=A0=A0 return cast(Unqual!T)t;<br>}<br><br>It seems to work well en= ough for my purposes, but still has some limitations: for example, it unqua= lifies everything (no more const / immutable after dupping).<br><br>Philipp= e<br> <br> --00032555a272a6cfed0482c243e3--
Mar 26 2010