Ideas from the Chapel language (swap)

BCS ao at pathlink.com
Wed Oct 3 21:45:27 PDT 2007


Reply to Gregor,

> 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
> 

swap!(byte)(a, b)
swap!(byte[5])(a, b)





More information about the Digitalmars-d mailing list