Various D language matters
ardita
erza_13 at zbavitu.net
Tue May 12 02:04:34 PDT 2009
Dan Williams Wrote:
>
> "Sam McCall" <tunah.d at tunah.net> wrote in message
> news:cc0pu0$lak$1 at digitaldaemon.com...
> > Dan Williams wrote:
> >
> > > "Walter" <newshound at digitalmars.com> wrote in message
> > > news:cbuutj$sm8$1 at digitaldaemon.com...
> > >
> > >>"Dan Williams" <dnews at ithium.NOSPAM.net> wrote in message
> > >>news:cbuol1$jrb$1 at digitaldaemon.com...
> > >>
> > >>>With D, it would appear that pointers only rarely have to be used, and
> > >
> > > so
> > >
> > >>a
> > >>
> > >>>swap method would mean that people would not have to access the
> pointers
> > >>>directly in this scenario.
> > >>
> > >>Why not this:
> > >>
> > >> T[] foo;
> > >>
> > >> void swap(inout T e1, inout T e2)
> > >> {
> > >> T tmp = e1;
> > >> e1 = e2;
> > >> e2 = tmp;
> > >> }
> > >>
> > >> ...
> > >>
> > >> swap(foo[i], foo[j]);
> > >>
> > >>
> > >>
> > >
> > >
> > > Well, unless I'm mistaken, doesn't that do exactly what I described and
> copy
> > > the actual data around memory?
> > >
> > > Imagine that e1 and e2 are 1Kb each, or 1Mb each, or whatever. Then
> imagine
> > > that your function has to perform, say, 1000 of these swaps. Surely you
> are
> > > going to be copying *far* more data around than is necessary?
> >
> > If they're value types, sure. (The only value types that could be that
> > big I know of are structs and maybe static arrays?). If you're
> > determined to swap them, and not pointers to them, how do you expect the
> > compiler to get the value of e2 into e1 and the value of e1 into e2
> > without doing a lot of copying?
>
> No no no, the point was that I wanted to swap the pointers and not the
> values :) I misunderstood how D did these things (and hence I'm currently
> wearing the Dunce cap for the next few days. The fact that the cap has D on
> it is poetic irony! <g>)
>
>
>
More information about the Digitalmars-d
mailing list