Anyone come up with some cool algorithms/templates for D lately?

janderson askme at me.com
Thu Apr 17 22:50:18 PDT 2008


BCS wrote:
> Reply to janderson,
> 
>> In the past people have posted these cool algorithms/templates they
>> figured out how to do in some neat way with D.  Things like wow, this
>> would take INF+ lines in C++ but I can do it in 2 in D.   This seems
>> to have died down a bit.   I always found these topics most
>> interesting.
>>
>> So I'm requesting people to post "cool stuff" they figured out how to
>> do in D.
>>
>> -Joel
>>
> 
> // Sort data using keys as the order
> 
> uint[] keys, data;
> 
> assert(keys.length == data.length);
> 
> ulong[] arr;
> arr.length = keys.length;
> 
> foreach(int i, uint _; keys) arr[i] = (cast(uint)keys[i])<<32 | data[i];
> 
> arr.sort;
> 
> foreach(int i, uint _; keys) data[i] = cast(uint)(arr & 0x0ffff_ffff);
> 
> 

It took me a while to figure out what was going on here.  Anyway I never 
thought of sorting this way.

Another way to do this would be with a struct, although it would be 
non-portable unless you defined a comparison operator (of course).

Anyways great stuff!

-Joel



More information about the Digitalmars-d mailing list