STLソースプロファイリング[アルゴリズム](一)[stl_numeric.h]=>copy

3908 ワード

//      
/*--------------------------------------------------------------------------------------
* copy         
*/
//      。 
template<class InputIterator, class OutputIterator> // ?     InputIterator   OutputIterator        ,             InputIterator   OutputIterator ?
inline OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result){
//          function object   ,     function      ?
//    function object       ?      function       
//        function object             ,  function            ?
return __copy_dispatch<InputIterator, OutputIterator>() (first, last, result); 
}

//      (           ) const char *   const wchar_t *,           
//    [1]     
inline char *copy(const char* first, const char *last, char *result){
memmove(result, first, last - first);
return result + (last - first); //             ?
}


//    [2]     
inline wchar_t *copy(const wchar_t *first, const wchar_t *last, wchar_t *result){
memmove(result, first, sizeof(wchar_t) * (last - first));
return result + (last - first);
}


/*--------------------------------------------------------------------------------------
* __copy_dispatch        
*/
//copy()               __copy_dispatch()   ,                  
//      
template<class InputIterator, class OutputIterator>
struct __copy_dispatch{
OutputIterator operator()(InputIterator first, InputIterator last, OutputIterator result){
return __copy(first, last, result, iterator_category(first));
}
};

//                    

//     [1],      T *    
template<class T>
struct __copy_dispatch<T *, T*>
{
T *operator()(T *first, T *last, T *result){
typedef typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, result, t());
}
};


//     [2],       const T*    ,      T *    
template<class T>
struct __copy_dispatch<const T*, T*>
{
T *operator()(const T *first, const T *last, T *result){
typedef typename __type_traits<T>::has_trivial_assignment_operator t;
return __copy_t(first, last, result, t());
}
};


/*--------------------------------------------------------------------------------------
*      _copy   
*/
//     InputIterator   、 RandomAccessIter       InputIterator   RandomAccessIter        ,                ? ?
// -->    input_iterator_tag          ,                     
//InputIterator   
template<class InputIterator, class OutputIterator>
inline OutputIterator __copy(InputIterator first, InputIterator last, OutputIterator result, input_iterator_tag){
//     InputIterator   ,        ,        、   
for( ; first != last; ++result, ++first)
*result = *first;
return result;
}
// RandomAccessIter   
template<class RandomAccessIter, class OutputIterator>
inline OutputIterator __copy(RandomAccessIter first, RandomAccessIter last, OutputIterator result, random_access_iterator_tag){
//            __copy_d
return __copy_d(first, last, result, distance_type(first));
}


template<class RandomAccessIter, class OutputIterator, class Distance>
inline OutputIterator __copy_d(RandomAccessIter first, RandomAccessIter last, OutputIterator result, Distance *){
//   n          。   
for(Distance n = last - first; n > 0; --n, ++result, ++first)
*result = *first;
return result;
}


/*--------------------------------------------------------------------------------------
*          copy_t   
*/
//       "          trivial assignment operator"
template<class T>
inline T *__copy_t(const T* first, const T *last, T *result, __true_type){
memmove(result, first, sizeof(T) * (last - first));
return result + (last - first);
}


//       "          non-trivial assignment operator"
template<class T>
inline T *__copy_t(const T* first, const T *last, T *result, __false_type){
return __copy_d(first, last, result, (ptrdiff_t *) 0); //       __copy_d 
}