// Filename:    stl_uninitialized.h// Comment By:  凝霜
// E-mail:      mdl2009@vip.qq.com
// Blog:        http://blog.csdn.net/mdl13412// 主要接口:
//
//      template <class InputIterator, class ForwardIterator>
//      inline ForwardIterator
//      uninitialized_copy(InputIterator first, InputIterator last,
//                         ForwardIterator result)
//      将[first, last)的对象复制一份到[result, result + (last - first))
//      对于char和wchar_t提供特化版本, 以获取最佳效率
//      注: commit or rollback
//
//      template <class InputIterator, class Size, class ForwardIterator>
//      inline pair<InputIterator, ForwardIterator>
//      uninitialized_copy_n(InputIterator first, Size count,
//                           ForwardIterator result)
//      从first开始, 复制count个对象到[result, result + n)
//      注: commit or rollback
//
//      template <class ForwardIterator, class T>
//      inline void uninitialized_fill(ForwardIterator first,
//                                     ForwardIterator last,
//                                     const T& x)
//      将x复制到pfirst, last)
//      注: commit or rollback
//
//      template <class ForwardIterator, class Size, class T>
//      inline ForwardIterator uninitialized_fill_n(ForwardIterator first,
//                                                  Size n, const T& x)
//      复制n个x对象到[first, first + n)
//      注: commit or rollback/*** 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.*** Copyright (c) 1996,1997* Silicon Graphics Computer Systems, Inc.** 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.  Silicon Graphics makes no* representations about the suitability of this software for any* purpose.  It is provided "as is" without express or implied warranty.*//* NOTE: This is an internal header file, included by other STL headers.*   You should not attempt to use it directly.*/#ifndef __SGI_STL_INTERNAL_UNINITIALIZED_H
#define __SGI_STL_INTERNAL_UNINITIALIZED_H__STL_BEGIN_NAMESPACE// uninitialized_copy()实现部分//                         特化char版本
//                      |---------------> (char *, ...)  <---    字串串专用版本
//                      |  特化wchar_t版本                   |--- 调用memmove()
// uninitialized_copy ------------------> (wchar_t, ...) <---    获取最佳效率
//                      |
//                      |
//                      | 泛化   调用 __uninitialized_copy()
//                      |----->  根据类型是否为POD进行函数派发
//                                           |
//                                           |---------------- Is POD?
//                                           |
//              -------------------------------------
//           No |                                   | Yes
//              ↓                                   |
// __uninitialized_copy_aux(..., __false_type)      |
// for ( ; first != last; ++first, ++cur)           |
//      construct(&*cur, *first);                   ↓
//                                   __uninitialized_copy_aux(..., __true_type)
//                                   copy(first, last, result)// 如果copy construction和operator =等效, 并且destructor is trivial
// 那么就可以使用本函数
// 返回值为目标地址的end
// 注: 使用copy()进行复制的时候, 调用的是对象的operator =,
//     所以要满足copy construction和operator =等效,
//     destructor is trivial保证在此版本中不会进行析构,
//     以保证效率
template <class InputIterator, class ForwardIterator>
inline ForwardIterator
__uninitialized_copy_aux(InputIterator first, InputIterator last,ForwardIterator result,__true_type)
{// 调用的是STL算法copy()return copy(first, last, result);
}// 如果copy construction和operator =不等效, 那么就要调用construct()进行构造
template <class InputIterator, class ForwardIterator>
ForwardIterator
__uninitialized_copy_aux(InputIterator first, InputIterator last,ForwardIterator result,__false_type)
{ForwardIterator cur = result;__STL_TRY {// 因为copy construction和operator =不等效,// 则必须每个对象都以其它对象为蓝本进行构造// 本实作中使用的是placement new进行构造for ( ; first != last; ++first, ++cur)construct(&*cur, *first);return cur;}// commit or rollback// 如果分配失败就stack unwinding,// 保证所有对象都被析构__STL_UNWIND(destroy(result, cur));
}// 派发函数, traits出T是否为POD, 然后进行派发
template <class InputIterator, class ForwardIterator, class T>
inline ForwardIterator
__uninitialized_copy(InputIterator first, InputIterator last,ForwardIterator result, T*)
{// POD = Plain Old Data// 其具有trvial constructor/destructor/copy constructor/operator =// 所有的C++内置基本数据类型和传统C struct都属于PODtypedef typename __type_traits<T>::is_POD_type is_POD;// 根据是否为POD类型进行派发, 以保证效率return __uninitialized_copy_aux(first, last, result, is_POD());
}template <class InputIterator, class ForwardIterator>
inline ForwardIteratoruninitialized_copy(InputIterator first, InputIterator last,ForwardIterator result)
{// 调用派发函数, 根据是否为POD决议出最佳效率的函数return __uninitialized_copy(first, last, result, value_type(result));
}// 提供给char专用, 效率最优化
inline char* uninitialized_copy(const char* first, const char* last,char* result)
{memmove(result, first, last - first);return result + (last - first);
}// 提供给wchar_t专用, 效率最优化
inline wchar_t* uninitialized_copy(const wchar_t* first, const wchar_t* last,wchar_t* result)
{memmove(result, first, sizeof(wchar_t) * (last - first));return result + (last - first);
}// uninitialized_copy_n()实现部分//                         uninitialized_copy_n
//                                  |
//                                  |------------ 判断迭代器first的类别
//                                  |
//               -----------------------------------------
// InputIterator |                                       | RandomAccessIterator
//               |                                       |
//               ↓                                       |
// __uninitialized_copy_n(..., input_iterator_tag)       |
// for ( ; count > 0 ; --count, ++first, ++cur)          |
//    construct(&*cur, *first);                          |
//                                                       ↓
//                    __uninitialized_copy_n(..., random_access_iterator_tag)
//                    last = first + count;
//                    uninitialized_copy(first, last, result)// POD版本
template <class InputIterator, class Size, class ForwardIterator>
pair<InputIterator, ForwardIterator>
__uninitialized_copy_n(InputIterator first, Size count,ForwardIterator result,input_iterator_tag)
{ForwardIterator cur = result;__STL_TRY {for ( ; count > 0 ; --count, ++first, ++cur)construct(&*cur, *first);return pair<InputIterator, ForwardIterator>(first, cur);}__STL_UNWIND(destroy(result, cur));
}// 非POD版本
// 对于支持随机存取的迭代器, 可以直接使用uninitialized_copy()进行复制
template <class RandomAccessIterator, class Size, class ForwardIterator>
inline pair<RandomAccessIterator, ForwardIterator>
__uninitialized_copy_n(RandomAccessIterator first, Size count,ForwardIterator result,random_access_iterator_tag)
{RandomAccessIterator last = first + count;return make_pair(last, uninitialized_copy(first, last, result));
}template <class InputIterator, class Size, class ForwardIterator>
inline pair<InputIterator, ForwardIterator>
uninitialized_copy_n(InputIterator first, Size count,ForwardIterator result)
{return __uninitialized_copy_n(first, count, result,iterator_category(first));
}// uninitialized_fill()实现部分//                           uninitialized_fill
//                                   |
//                                   |
//                                   ↓
//                       调用__uninitialized_fill()
//                       根据类型是否为POD进行函数派发
//                                   |
//                                   |---------------- Is POD?
//                   No 泛化版本      |      Yes 特化版本
//               -------------------------------------------
//               |                                         |
//               |                                         |
//               ↓                                         |
// __uninitialized_fill_aux(..., __false_type)             |
// for ( ; cur != last; ++cur)                             |
//    construct(&*cur, x);                                 ↓
//                                __uninitialized_fill_aux(..., __true_type)
//                                fill(first, last, x);// POD版本
// 如果copy construction和operator =等效, 并且destructor is trivial
// 那么就可以使用本函数
template <class ForwardIterator, class T>
inline void
__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,const T& x, __true_type)
{fill(first, last, x);
}// 非POD版本
template <class ForwardIterator, class T>
void
__uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,const T& x, __false_type)
{ForwardIterator cur = first;__STL_TRY {for ( ; cur != last; ++cur)construct(&*cur, x);}__STL_UNWIND(destroy(first, cur));
}// 派发函数
template <class ForwardIterator, class T, class T1>
inline void __uninitialized_fill(ForwardIterator first, ForwardIterator last,const T& x, T1*)
{typedef typename __type_traits<T1>::is_POD_type is_POD;__uninitialized_fill_aux(first, last, x, is_POD());}template <class ForwardIterator, class T>
inline void uninitialized_fill(ForwardIterator first, ForwardIterator last,const T& x)
{__uninitialized_fill(first, last, x, value_type(first));
}// uninitialized_fill_n()实现部分//                           uninitialized_fill_n
//                                   |
//                                   |
//                                   ↓
//                      调用__uninitialized_fill_n()
//                      根据类型是否为POD进行函数派发
//                                   |
//                                   |---------------- Is POD?
//                   No 泛化版本      |      Yes 特化版本
//               -------------------------------------------
//               |                                         |
//               |                                         |
//               ↓                                         |
// __uninitialized_fill_n_aux(..., __false_type)           |
// for ( ; n > 0; --n, ++cur)                              |
//    construct(&*cur, x);                                 |
//                                                         ↓
//                               __uninitialized_fill_n_aux(..., __true_type)
//                               fill_n(first, n, x);// 如果copy construction和operator =等效, 并且destructor is trivial
// 那么就可以使用本函数
template <class ForwardIterator, class Size, class T>
inline ForwardIterator
__uninitialized_fill_n_aux(ForwardIterator first, Size n,const T& x, __true_type)
{return fill_n(first, n, x);
}template <class ForwardIterator, class Size, class T>
ForwardIterator
__uninitialized_fill_n_aux(ForwardIterator first, Size n,const T& x, __false_type)
{ForwardIterator cur = first;__STL_TRY {for ( ; n > 0; --n, ++cur)construct(&*cur, x);return cur;}__STL_UNWIND(destroy(first, cur));
}template <class ForwardIterator, class Size, class T, class T1>
inline ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,const T& x, T1*)
{typedef typename __type_traits<T1>::is_POD_type is_POD;return __uninitialized_fill_n_aux(first, n, x, is_POD());}template <class ForwardIterator, class Size, class T>
inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,const T& x)
{return __uninitialized_fill_n(first, n, x, value_type(first));
}// 其它函数实现// Copies [first1, last1) into [result, result + (last1 - first1)), and
// copies [first2, last2) into
// [result, result + (last1 - first1) + (last2 - first2)).// 我认为应该是把[first2, last2)copy到
// [result + (last1 - first1), result + (last1 - first1) + (last2 - first2))
// 大家可以讨论一下
template <class InputIterator1, class InputIterator2, class ForwardIterator>
inline ForwardIterator
__uninitialized_copy_copy(InputIterator1 first1, InputIterator1 last1,InputIterator2 first2, InputIterator2 last2,ForwardIterator result)
{ForwardIterator mid = uninitialized_copy(first1, last1, result);__STL_TRY {return uninitialized_copy(first2, last2, mid);}__STL_UNWIND(destroy(result, mid));
}// Fills [result, mid) with x, and copies [first, last) into
//  [mid, mid + (last - first)).
template <class ForwardIterator, class T, class InputIterator>
inline ForwardIterator
__uninitialized_fill_copy(ForwardIterator result, ForwardIterator mid,const T& x,InputIterator first, InputIterator last)
{uninitialized_fill(result, mid, x);__STL_TRY {return uninitialized_copy(first, last, mid);}__STL_UNWIND(destroy(result, mid));
}// Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
//  fills [first2 + (last1 - first1), last2) with x.
template <class InputIterator, class ForwardIterator, class T>
inline void
__uninitialized_copy_fill(InputIterator first1, InputIterator last1,ForwardIterator first2, ForwardIterator last2,const T& x)
{ForwardIterator mid2 = uninitialized_copy(first1, last1, first2);__STL_TRY {uninitialized_fill(mid2, last2, x);}__STL_UNWIND(destroy(first2, mid2));
}__STL_END_NAMESPACE#endif /* __SGI_STL_INTERNAL_UNINITIALIZED_H */// Local Variables:
// mode:C++
// End:

《STL源码剖析》-- stl_uninitialized.h相关推荐

  1. 《STL源码剖析》笔记——allocator

    六大组件间关系 部分STL文件包含关系 allocator包含于中: 实际实现于三个文件 : 1.stl_construct.h :对象的构造和析构 2.stl_alloc.h空间配置和释放 3.st ...

  2. 《STL源码剖析》学习-- 1.9-- 可能令你困惑的C++语法1

    最近在看侯捷的<STL源码剖析>,虽然感觉自己c++看得比较深一点,还是感觉还多东西不是那么明白,这里将一些细小的东西或者概念记录一下. 有些东西是根据<C++编程思想>理解的 ...

  3. STL源码剖析——P142关于list::sort函数

    在list容器中,由于容器自身组织数据的特殊性,所以list提供了自己的排序函数list::sort, 并且实现得相当巧妙,不过<STL源码剖析>的原文中,我有些许疑问,对于该排序算法,侯 ...

  4. STL源码剖析 stack 栈 概述->(使用deque双端队列 / list链表)作为stack的底层容器

    Stack是一种先进后出的数据结构,他只有一个出口 stack允许 新增元素.移除元素.取得最顶端的元素,但是无法获得stack的内部数据,因此satck没有遍历行为 Stack定义的完整列表 (双端 ...

  5. STL源码剖析 __type_traits

    traits编程 弥补了C++本身的不足 STL只对迭代器进行规范制定出了iterator_traits,SGI在此基础上进一步扩展,产生了__type_traits 双下划线的含义是这个是SGI内部 ...

  6. STL源码剖析 空间配置器 查漏补缺

    ptrdiff_t含义 减去两个指针的结果的带符号整数类型 ptrdiff_t (Type support) - C 中文开发手册 - 开发者手册 - 云+社区 - 腾讯云 std::set_new_ ...

  7. STL源码剖析之配接器

    adapter(配接器)在STL组件的灵活组合运用上,扮演者转换器的角色.adapter来源于一种适配器模式,其功能是:将一个class接口转换为另一个class的接口,使得原本因接口不兼容而不能合作 ...

  8. 【STL源码剖析】list模拟实现 | 适配器实现反向迭代器【超详细的底层算法解释】

    今天博主继续带来STL源码剖析专栏的第三篇博客了! 今天带来list的模拟实现! 话不多说,直接进入我们今天的内容! 前言 那么这里博主先安利一下一些干货满满的专栏啦! 手撕数据结构https://b ...

  9. STL(C++标准库,体系结构及其内核分析)(STL源码剖析)(更新完毕)

    文章目录 介绍 Level 0:使用C++标准库 0 STL六大部件 0.1 六大部件之间的关系 0.2 复杂度 0.3 容器是前闭后开(左闭右开)区间 1 容器的结构与分类 1.1 使用容器Arra ...

  10. STL源码剖析学习七:stack和queue

    STL源码剖析学习七:stack和queue stack是一种先进后出的数据结构,只有一个出口. 允许新增.删除.获取最顶端的元素,没有任何办法可以存取其他元素,不允许有遍历行为. 缺省情况下用deq ...

最新文章

  1. java 线程 连接池_java程序实现线程连接池功能
  2. 让你二十年后仍是人才
  3. ActiveMQ中Topic消费者
  4. 腾讯研发效率领先的秘密:高效率的工具
  5. Requests库实战(四)---爬取肯德基餐厅地址信息
  6. start()和run()的区别
  7. java基础——Vector集合知识点
  8. java面板中添加面板_java – 在面板中添加一个复杂的图像,在一个定制的用户界面中使用按钮...
  9. 随想录(kgdb的基本原理)
  10. extern c作用_extern “C”的作用详解
  11. 12864c与语言字符显示,12864液晶显示C语言程序设计实例
  12. thinksnsv4.6运行php,开源微博系统(ThinkSNS) v4.6.1
  13. 五年级英语测试题软件,小学五年级英语测试题
  14. 三分钟读懂2019苹果秋季发布会:没熬夜的直接进
  15. win10 android软件下载,windows10模拟器安卓版
  16. java 借助modbus协议实现与硬件之间的通讯
  17. maven打包之胖包与瘦包
  18. libevent的使用流程
  19. JavaScript 制作12小时进制的时钟特效
  20. 今日金融词汇--- 高周转模式

热门文章

  1. Java新手学习指南(2020最新推荐版)
  2. c语言:求π的近似值
  3. kafka的生产者如何把消息发送到指定分区里
  4. VmwareTools工具安装
  5. 笔记本固态硬盘温度测试软件,固态硬盘散热测试
  6. Udesk作为国内SaaS客户服务领域的翘楚,将对标海外巨头Oracle丨Xtecher 封面
  7. Vue项目设置ico
  8. Windows 源码编译 nginx (加入nginx-http-flv-module)
  9. Carson带你学Android:这是一份全面详细的属性动画学习攻略!
  10. 解决Duplicate entry '……