Jump to page: 1 2
Thread overview
Anyone come up with some cool algorithms/templates for D lately?
Apr 18, 2008
janderson
Apr 18, 2008
Saaa
Apr 18, 2008
Lionello Lunesu
Apr 18, 2008
Bill Baxter
Apr 18, 2008
Michel Fortin
Apr 18, 2008
Janice Caron
Apr 18, 2008
Jesse Phillips
Apr 18, 2008
Robert Fraser
Apr 18, 2008
Lionello Lunesu
Apr 19, 2008
Bill Baxter
Apr 18, 2008
Bill Baxter
Apr 18, 2008
janderson
Apr 18, 2008
Walter Bright
Apr 18, 2008
Bill Baxter
Apr 18, 2008
BCS
Apr 18, 2008
janderson
Apr 18, 2008
BCS
Apr 18, 2008
Brian Palmer
Apr 18, 2008
Robert Fraser
April 18, 2008
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
April 18, 2008
I'd expect interesting topics with a new release.
Have you noticed; it's been the longest wait since at least a year.


> So I'm requesting people to post "cool stuff" they figured out how to do in D.
>
> -Joel


April 18, 2008
You're right, but still: the last release introduced struct destructors, which have been requested a couple of times (mostly C++ folk, probably) and I, too, am eagerly awaiting cool code making use of struct destructors.

L.


"Saaa" <empty@needmail.com> wrote in message news:fu93d0$1cfh$1@digitalmars.com...
> I'd expect interesting topics with a new release.
> Have you noticed; it's been the longest wait since at least a year.
>
>
>> So I'm requesting people to post "cool stuff" they figured out how to do in D.
>>
>> -Joel
>
> 

April 18, 2008
Psst -- don't tell anyone but ...
opDot... coming to a DMD near you soon.

--bb

Lionello Lunesu wrote:
> You're right, but still: the last release introduced struct destructors, which have been requested a couple of times (mostly C++ folk, probably) and I, too, am eagerly awaiting cool code making use of struct destructors.
> 
> L.
> 
> 
> "Saaa" <empty@needmail.com> wrote in message news:fu93d0$1cfh$1@digitalmars.com...
>> I'd expect interesting topics with a new release.
>> Have you noticed; it's been the longest wait since at least a year.
>>
>>
>>> So I'm requesting people to post "cool stuff" they figured out how to do in D.

April 18, 2008
janderson wrote:
> 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.

I'm still flabbergasted by the cool things you can do inside fixed-length matrix/vector templates.

For instance (this is all from my matrix/vector classes in OpenMesh/D which are basically a complete rewrite of the original C++ versions)

struct MatrixT(T, int M, int N)
{
    alias T Scalar;

    union {
        Scalar[M*N] values_ = void;
        static if(M<=10 && N<=10) {
            mixin(_gen_elements!(M,N)("Scalar"));
        }
    }
}

// Generate the elements m00,m01,m02... elements
// For matrices with dimensions < 10 x 10 only.
private string _gen_elements(int M, int N)(string type) {
    static assert(M<=10);
    static assert(N<=10);
    char[] Num = "0123456789";
    string ret;
    for(int col=0; col<N; ++col) {
        ret ~= type ~ " ";
        for (int row=0; row<M; row++) {
            ret ~= "m" ~ Num[row] ~ Num[col];
            if (row!=M-1) ret ~=  ", ";
        }
        ret ~= ";\n";
    }
    return ret;
}

This creates aliases to each element of the matrix of the form m00, m01, m02, but only if the matrix is small enough.  I have no idea how you'd do that in C++.  I don't think it's really possible.


Here's another -- generating the unrolled code for multiplying two matrices together:

/// Multiply MxN matrix time NxP matrix
void mat_mult_ret(T,int M,int N,int P)(
    /*const*/ref MatrixT!(T,M,N) A,
    /*const*/ref MatrixT!(T,N,P) B,
    inout MatrixT!(T,M,P) ret)
{
    with(ret) {
        mixin(_gen_mat_mult_body!(M,N,P)("A","B"));
    }
}

private string _gen_mat_mult_body(int M, int N, int P)(string A, string B)
{
    string ret;
    for (int row=0; row<M; ++row) {
        for(int col=0; col<P; ++col) {
            ret ~= "        ";
            ret ~= "values_["~ctfe_itoa(col*M+row)~"] = ";
            for(int jj; jj<N; ++jj) {
                string JJ = ctfe_itoa(jj);
                string iA = ctfe_itoa( row+jj *M );
                string iB = ctfe_itoa( jj +col*N );
                ret ~= A ~ ".values_["~iA~"]*"~B~".values_["~iB~"]";
                if (jj != N-1) { ret ~= " + "; }
                else { ret ~= ";\n"; }
            }
        }
        ret ~= "\n";
    }
    return ret;
}


Wow.  The code generating function basically just looks like a standard triply-nested matrix multiply, but instead of doing multiplies it just splits out code that implements multiplies.

I think that's pretty sweet.

--bb
April 18, 2008
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);


April 18, 2008
Bill Baxter wrote:
> janderson wrote:
>> 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.
> 
> I'm still flabbergasted by the cool things you can do inside fixed-length matrix/vector templates.
> 
> For instance (this is all from my matrix/vector classes in OpenMesh/D which are basically a complete rewrite of the original C++ versions)
> 
> struct MatrixT(T, int M, int N)
> {
>     alias T Scalar;
> 
>     union {
>         Scalar[M*N] values_ = void;
>         static if(M<=10 && N<=10) {
>             mixin(_gen_elements!(M,N)("Scalar"));
>         }
>     }
> }
> 
> // Generate the elements m00,m01,m02... elements
> // For matrices with dimensions < 10 x 10 only.
> private string _gen_elements(int M, int N)(string type) {
>     static assert(M<=10);
>     static assert(N<=10);
>     char[] Num = "0123456789";
>     string ret;
>     for(int col=0; col<N; ++col) {
>         ret ~= type ~ " ";
>         for (int row=0; row<M; row++) {
>             ret ~= "m" ~ Num[row] ~ Num[col];
>             if (row!=M-1) ret ~=  ", ";
>         }
>         ret ~= ";\n";
>     }
>     return ret;
> }
> 
> This creates aliases to each element of the matrix of the form m00, m01, m02, but only if the matrix is small enough.  I have no idea how you'd do that in C++.  I don't think it's really possible.
> 
> 
> Here's another -- generating the unrolled code for multiplying two matrices together:
> 
> /// Multiply MxN matrix time NxP matrix
> void mat_mult_ret(T,int M,int N,int P)(
>     /*const*/ref MatrixT!(T,M,N) A,
>     /*const*/ref MatrixT!(T,N,P) B,
>     inout MatrixT!(T,M,P) ret)
> {
>     with(ret) {
>         mixin(_gen_mat_mult_body!(M,N,P)("A","B"));
>     }
> }
> 
> private string _gen_mat_mult_body(int M, int N, int P)(string A, string B)
> {
>     string ret;
>     for (int row=0; row<M; ++row) {
>         for(int col=0; col<P; ++col) {
>             ret ~= "        ";
>             ret ~= "values_["~ctfe_itoa(col*M+row)~"] = ";
>             for(int jj; jj<N; ++jj) {
>                 string JJ = ctfe_itoa(jj);
>                 string iA = ctfe_itoa( row+jj *M );
>                 string iB = ctfe_itoa( jj +col*N );
>                 ret ~= A ~ ".values_["~iA~"]*"~B~".values_["~iB~"]";
>                 if (jj != N-1) { ret ~= " + "; }
>                 else { ret ~= ";\n"; }
>             }
>         }
>         ret ~= "\n";
>     }
>     return ret;
> }
> 
> 
> Wow.  The code generating function basically just looks like a standard triply-nested matrix multiply, but instead of doing multiplies it just splits out code that implements multiplies.
> 
> I think that's pretty sweet.
> 
> --bb

Neat Stuff!
April 18, 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
April 18, 2008
Reply to janderson,

> Anyways great stuff!
> 
> -Joel
> 

BTW: I'm getting paid for that one :)


April 18, 2008
Bill Baxter wrote:
> For instance (this is all from my matrix/vector classes in OpenMesh/D which are basically a complete rewrite of the original C++ versions)

Got a link to your code?

(What you're doing looks pretty cool!)
« First   ‹ Prev
1 2