Angus Leeming <[EMAIL PROTECTED]> writes:

| How seriously do we take Scott Meyer's recommendation "Prefer algorithm calls 
| to hand-written loops"? (Item 43 of Effective STL)
>
| For example, these two functions have identical functionality. Which one is 
| prefereable? (and yes, I chose a deliberately simple example...)
>
| Angus
>
| void func1(Buffer const & buffer, PreviewLoader & ploader)
| {
|       Buffer::inset_iterator it  = buffer.inset_const_iterator_begin();
|       Buffer::inset_iterator end = buffer.inset_const_iterator_end();
>
|       for (; it != end; ++it) {
|               if ((*it)->lyxCode() == Inset::MATH_CODE) {
|                       (*it)->generatePreview(ploader);
|               }
|       }
| }
>
| namespace {
>
| struct FindMathInset {
|       bool operator()(Inset const * inset)
|       {
|               return inset && inset->lyxCode() == Inset::MATH_CODE;
|       }
| };
|  
| } // namespace anon
>
| void func2(Buffer const & buffer, PreviewLoader & ploader)
| {
|       Buffer::inset_iterator it  = buffer.inset_const_iterator_begin();
|       Buffer::inset_iterator end = buffer.inset_const_iterator_end();
>
|       while (it = std::find_if(it, end, FindMathInset()) != end) {
|               (*it++)->generatePreview(ploader);
|       }
| }

but this can be simplified right?
Either with the help of the lambda lib, or by some stl or lyx
algorithm.

Then the code would be:

     Buffer::inset_iterator beg  = buffer.inset_const_iterator_begin();
     Buffer::inset_iterator end = buffer.inset_const_iterator_end();
     for_each(beg, end,
              ActionOnInset(boost::bind(MathInset::generatePreview, ploader),
              Inset::MATH_CODE));


f.ex.

-- 
        Lgb

Reply via email to