www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: Ideas from the Chapel language (swap)

Gregor Richards Wrote:

 Derek Parnell wrote:
 On Wed, 03 Oct 2007 14:53:57 -0400, Bruce Adams wrote:
 bearophile Wrote:

 The Cray Inc is designing the Chapel Language:
 The following notes are from the specification V. 0.750:
 I think it's rather cute, it looks like a cross between C++, Fortress and
Python. Here are few things I think can be interesting for D designers too:

 - Chap. 11.5 page 65, swap operator (useful but probably not enough to justify
a new operator)
swap is very useful especially in exception safe programming. I would like to see swap used as the default implementation of D's transfer constructor (and C++0x's forthcoming move constructor)
Isn't a simple template such this sufficient? template swap(TYPE) { void swap(ref TYPE a, ref TYPE b) { synchronized { TYPE temp = a; a = b; b = temp; } } }
For the ridiculously-insane: void swap(T)(ref T a, ref T b) { synchronized { // this should be some kind of static for ... for (size_t i = 0; i < (a.sizeof/size_t.sizeof); i++) { (cast(size_t*) &a)[i] ^= (cast(size_t*) &b)[i]; (cast(size_t*) &b)[i] = (cast(size_t*) &a)[i] ^ (cast(size_t*) &b)[i]; (cast(size_t*) &a)[i] ^= (cast(size_t*) &b)[i]; } } } Add some loop unrolling and that's more efficient than memcpy :P - Gregor Richards PS: /me <3 XOR swapping
I was just going to mention that; you beat me to it!
Oct 03 2007