⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mstl_initialization.hpp

📁 一个类STL的多平台可移植的算法容器库,主要用于嵌入式系统编程时的内存管理等方面
💻 HPP
📖 第 1 页 / 共 2 页
字号:
/*
The young Library
Copyright (c) 2005 by 杨桓

Permission to use, copy, modify, distribute and sell this software for any
purpose is hereby granted without fee, provided that the above copyright
notice appear in all copies and that both that copyright notice and this
permission notice appear in supporting documentation.
The author make no representations about the suitability of this software
for any purpose. It is provided "as is" without express or implied warranty.
*/

/*
 * This file is derived from software bearing the following
 * restrictions:
 *
 * Copyright (c) 1994
 * Hewlett-Packard Company
 *
 * Permission to use, copy, modify, distribute and sell this
 * software and its documentation for any purpose is hereby
 * granted without fee, provided that the above copyright notice
 * appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation.
 * Hewlett-Packard Company makes no representations about the
 * suitability of this software for any purpose. It is provided
 * "as is" without express or implied warranty.
 */

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
#ifndef __MACRO_CPLUSPLUS_MINI_STL_INITIALIZATION_HEADER_FILE__
#define __MACRO_CPLUSPLUS_MINI_STL_INITIALIZATION_HEADER_FILE__
//-----------------------------------------------------------------------------
#include "mstl_construct.hpp"
#include "string/mstl_char_function.hpp"
//-----------------------------------------------------------------------------
__MACRO_CPLUSPLUS_MINI_STL_BEGIN_NAMESPACE__
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename InputIterator, typename Integer, typename ForwardIterator >
inline ForwardIterator init_copy_n( InputIterator first,
                                    Integer count,
                                    ForwardIterator result )
{
    typedef  typename iterator_traits<ForwardIterator>::value_type  value_t;
    typedef  typename type_traits<value_t>::is_POD_type             POD;

    if( count > 0 )
        return init_copy_n_aux( first, count, result, POD() );
    else
        return result;
}

template< typename InputIterator, typename Integer, typename ForwardIterator >
ForwardIterator init_copy_n_aux( InputIterator first, Integer count,
                                 ForwardIterator result, false_type )
{
    typedef  typename primal_type<Integer>::primal_t  primal_int;

    ForwardIterator current = result;
    try
    {
        for( primal_int i = 0; i < count; ++i,++first,++current )
            construct( &(*current), *first );
    }
    catch(...)
    {
        destroy( result, current );
        throw;
    }
    return current;
}

template< typename InputIterator, typename Integer, typename ForwardIterator >
ForwardIterator init_copy_n_aux( InputIterator first, Integer count,
                                 ForwardIterator result, true_type )
{
    typedef  typename primal_type<Integer>::primal_t  primal_int;

    for( primal_int i = 0; i < count; ++i,++first,++result )
        *result = *first;
    return result;
}



template< typename T1, typename Integer, typename T2 >
inline T2* init_copy_n( T1* first, Integer count, T2* result )
{
    typedef  double_types_traits<T1, T2>  double_traits;
    typedef  typename double_traits::matching_result  matching;
    typedef  typename double_traits::has_trivial_copy_constructor_t2  cc_t2;

    return ptr_init_copy_n( first, count, result, matching(), cc_t2() );
}

template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
                            same_type, true_type )
{
    memcpy( result, first, count * sizeof(T2) );
    return ( result + count );
}

template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
                            same_type, false_type x )
{
    return init_copy_n_aux( first, count, result, x );
}

template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
                            different_type, true_type x )
{
    return init_copy_n_aux( first, count, result, x );
}

template< typename T1, typename Integer, typename T2 >
inline T2* ptr_init_copy_n( T1* first, Integer count, T2* result,
                            different_type, false_type x )
{
    return init_copy_n_aux( first, count, result, x );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename InputIterator, typename ForwardIterator >
inline ForwardIterator init_copy( InputIterator first, InputIterator last,
                                  ForwardIterator result )
{
    typedef  typename iterator_traits<InputIterator>::iterator_category  itr;
    typedef  typename iterator_traits<ForwardIterator>::value_type  value_t;
    typedef  typename type_traits<value_t>::is_POD_type             POD;

    if( first != last )
        return init_copy_aux( first, last, result, itr(), POD() );
    else
        return result;
}

template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
                               ForwardIterator result,
                               input_iterator_tag, false_type )
{
    ForwardIterator current = result;
    try
    {
        for( ; first != last; ++first,++current )
            construct( &(*current), *first );
    }
    catch(...)
    {
        destroy( result, current );
        throw;
    }
    return current;
}

template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
                               ForwardIterator result,
                               random_access_iterator_tag, false_type )
{
    typedef  typename iterator_traits<InputIterator>::difference_type  diff_t;

    ForwardIterator current = result;
    try
    {
        for( diff_t n = last - first; n > 0; --n,++first,++result )
            construct( &(*current), *first );
    }
    catch(...)
    {
        destroy( result, current );
        throw;
    }
    return current;
}

template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
                               ForwardIterator result,
                               input_iterator_tag, true_type )
{
    for( ; first != last; ++first,++result )
        *result = *first;
    return result;
}

template< typename InputIterator, typename ForwardIterator >
ForwardIterator init_copy_aux( InputIterator first, InputIterator last,
                               ForwardIterator result,
                               random_access_iterator_tag, true_type )
{
    typedef  typename iterator_traits<InputIterator>::difference_type  diff_t;

    for( diff_t n = last - first; n > 0; --n,++first,++result )
        *result = *first;
    return result;
}



template< typename T1, typename T2 >
inline T2* init_copy( T1* first, T1* last, T2* result )
{
    typedef  double_types_traits<T1, T2>  double_traits;
    typedef  typename double_traits::matching_result  matching;
    typedef  typename double_traits::has_trivial_copy_constructor_t2  cc_t2;

    return ptr_init_copy_n( first, last - first, result, matching(), cc_t2() );
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

template< typename ForwardIterator, typename T >
inline void init_fill( ForwardIterator first, ForwardIterator last,
                       const T& value )
{
    typedef  typename iterator_traits<ForwardIterator>::iterator_category  itr;
    typedef  typename iterator_traits<ForwardIterator>::value_type  value_t;
    typedef  typename type_traits<value_t>::is_POD_type             POD;

    if( first != last )
        init_fill_aux( first, last, value, itr(), POD() );
}

template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
                    const T& value, forward_iterator_tag,
                    false_type )
{
    ForwardIterator current = first;
    try
    {
        for( ; current != last; ++current )
            construct( &(*current), value );
    }
    catch(...)
    {
        destroy( first, current );
        throw;
    }
}

template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
                    const T& value, random_access_iterator_tag,
                    false_type )
{
    typedef  typename iterator_traits<ForwardIterator>::difference_type  diff_t;

    ForwardIterator current = first;
    try
    {
        for( diff_t n = last - first; n > 0; --n,++current )
            construct( &(*current), value );
    }
    catch(...)
    {
        destroy( first, current );
        throw;
    }
}

template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
                    const T& value, forward_iterator_tag,
                    true_type )
{
    for( ; first != last; ++first )
        *first = value;
}

template< typename ForwardIterator, typename T >
void init_fill_aux( ForwardIterator first, ForwardIterator last,
                    const T& value, random_access_iterator_tag,
                    true_type )
{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -