[Home]STLAlgorithmExtensions/CopyN

BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List

Difference (from prior major revision) (no other diffs)

Changed: 26c26
++dest; // output-iterator zeigt hinter (!) das letzte Element
++dest;

The challenge of copy_n is that the algorithmen should work correctly with an std::istream_iterator. Therefor the input iterator have to increment only N-1 times. Here is an implementation we use in our project.

template< typename I, typename SZ, typename O > inline O copy_n( I first, SZ n, O dest ) {

    if( n > 0 )
    {
        *dest = *first;

        int n8 = --n / 8;
        switch( n % 8 )
        {
        case 0: while( n8-- ) 
                {  
                    *++dest = *++first;
        case 7:     *++dest = *++first;
        case 6:     *++dest = *++first;
        case 5:     *++dest = *++first;
        case 4:     *++dest = *++first;
        case 3:     *++dest = *++first;
        case 2:     *++dest = *++first;
        case 1:     *++dest = *++first;
                } ;
        }

        ++dest;
    }
    return( dest );
}

The funny switch is named duff's device. It makes the algorithmus 20-40% faster then a simple for-loop. (Werner Salomon, 6.5.2005)


BOOST WIKI | STLAlgorithmExtensions | RecentChanges | Preferences | Page List | Links List
Edit text of this page | View other revisions
Last edited May 6, 2005 11:57 am (diff)
Search:
Disclaimer: This site not officially maintained by Boost Developers