digitalmars.D - Re: Ideas from the Chapel language (swap)
- Robert Fraser (2/55) Oct 03 2007 I was just going to mention that; you beat me to it!
Gregor Richards Wrote:Derek Parnell wrote:I was just going to mention that; you beat me to it!On Wed, 03 Oct 2007 14:53:57 -0400, Bruce Adams wrote: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 swappingbearophile Wrote: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; } } }The Cray Inc is designing the Chapel Language: http://chapel.cs.washington.edu/ The following notes are from the specification V. 0.750: http://chapel.cs.washington.edu/spec-0.750.pdf 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)
Oct 03 2007