Factoring out looping structure

Doctor J nobody at nowhere.com
Mon Apr 6 13:33:04 PDT 2009


I'm new to template programming and I have a problem for which I think it would be a good match, but I'd like to get some advice on how to go about it.

I have a family of algorithms with the same looping structure, but different particulars:

------------------------
state_t state ;
for (int i = 0 ; i < imax ; ++i)
{
    compute_i(state, i);
    for (int j = 0 ; j < jmax ; ++j)
    {
        compute_j(state,i,j);
        for (int k = 0 ; k < kmax ; ++k)
        {
            compute_k(state,i,j,k);
            for (int m = 0 ; m < mmax ; ++m)
            {
                compute_m(state,i,j,k,m);
            }
            finish_k(state,i,j,k);
        }
        finish_j(state,i,j);
    }
}
--------------------------

I'd like to factor out the particulars from the looping structure, so I can write the loops once and then implement a bunch of variants with different particulars (the looping structure is in fact more complicated than the abstract example given).  Obviously I could do this by passing in an object implementing an interface with all my functions; however, the whole point of this exercise is to avoid the overhead of function calls.  The code needs to be efficient, and (especially the inner loop) functions need to be inlined.

So: what are my options in D?  Templates, delegates, mixins...?  I'd like to avoid string mixins if possible because they're a bit ugly.

Thanks!



More information about the Digitalmars-d-learn mailing list