sorting hidden data.
dsimcha
dsimcha at yahoo.com
Wed Sep 29 13:29:21 PDT 2010
== Quote from Andrei Alexandrescu (SeeWebsiteForEmail at erdani.org)'s article
> On 9/29/10 10:43 PDT, Steven Schveighoffer wrote:
> > What I mean is, why is it automatically assumed that if front does not
> > return a ref, the only way to swap is via moveX? If T == int, why must
> > we have a moveFront to deal with it? IMO, all algorithms should default
> > to copying unless alternatives exist.
> Good point. We should arrange things such that all types without
> elaborate copy constructors allow swapping by copying.
> Could you please bugzillize this so it's not forgotten?
> Thanks,
> Andrei
This should already work (I noticed that it didn't a long time ago and checked in
the fix, but I don't remember how thoroughly I tested the fix). Here's the
relevant snippet from std.algorithm:
void swapAt(R)(R r, size_t i1, size_t i2)
{
static if (is(typeof(&r[i1])))
{
swap(r[i1], r[i2]);
}
else
{
if (i1 == i2) return;
auto t1 = moveAt(r, i1);
auto t2 = moveAt(r, i2);
r[i2] = t1;
r[i1] = t2;
}
}
moveAt() simply copies if the range doesn't provide an explicit moveAt() and the
element type doesn't have an elaborate copy constructor.
More information about the Digitalmars-d
mailing list