1. stl_uninitialized.h
  2. // Filename: stl_uninitialized.h
  3.  
  4. // Comment By: 凝霜
  5. // E-mail: mdl2009@vip.qq.com
  6. // Blog: http://blog.csdn.net/mdl13412
  7.  
  8. // 主要接口:
  9. //
  10. // template <class InputIterator, class ForwardIterator>
  11. // inline ForwardIterator
  12. // uninitialized_copy(InputIterator first, InputIterator last,
  13. // ForwardIterator result)
  14. // 将[first, last)的对象复制一份到[result, result + (last - first))
  15. // 对于char和wchar_t提供特化版本, 以获取最佳效率
  16. // 注: commit or rollback
  17. //
  18. // template <class InputIterator, class Size, class ForwardIterator>
  19. // inline pair<InputIterator, ForwardIterator>
  20. // uninitialized_copy_n(InputIterator first, Size count,
  21. // ForwardIterator result)
  22. // 从first开始, 复制count个对象到[result, result + n)
  23. // 注: commit or rollback
  24. //
  25. // template <class ForwardIterator, class T>
  26. // inline void uninitialized_fill(ForwardIterator first,
  27. // ForwardIterator last,
  28. // const T& x)
  29. // 将x复制到pfirst, last)
  30. // 注: commit or rollback
  31. //
  32. // template <class ForwardIterator, class Size, class T>
  33. // inline ForwardIterator uninitialized_fill_n(ForwardIterator first,
  34. // Size n, const T& x)
  35. // 复制n个x对象到[first, first + n)
  36. // 注: commit or rollback
  37.  
  38. /*
  39. *
  40. * Copyright (c) 1994
  41. * Hewlett-Packard Company
  42. *
  43. * Permission to use, copy, modify, distribute and sell this software
  44. * and its documentation for any purpose is hereby granted without fee,
  45. * provided that the above copyright notice appear in all copies and
  46. * that both that copyright notice and this permission notice appear
  47. * in supporting documentation. Hewlett-Packard Company makes no
  48. * representations about the suitability of this software for any
  49. * purpose. It is provided "as is" without express or implied warranty.
  50. *
  51. *
  52. * Copyright (c) 1996,1997
  53. * Silicon Graphics Computer Systems, Inc.
  54. *
  55. * Permission to use, copy, modify, distribute and sell this software
  56. * and its documentation for any purpose is hereby granted without fee,
  57. * provided that the above copyright notice appear in all copies and
  58. * that both that copyright notice and this permission notice appear
  59. * in supporting documentation. Silicon Graphics makes no
  60. * representations about the suitability of this software for any
  61. * purpose. It is provided "as is" without express or implied warranty.
  62. */
  63.  
  64. /* NOTE: This is an internal header file, included by other STL headers.
  65. * You should not attempt to use it directly.
  66. */
  67.  
  68. #ifndef __SGI_STL_INTERNAL_UNINITIALIZED_H
  69. #define __SGI_STL_INTERNAL_UNINITIALIZED_H
  70.  
  71. __STL_BEGIN_NAMESPACE
  72.  
  73. ////////////////////////////////////////////////////////////////////////////////
  74. // uninitialized_copy()实现部分
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // 特化char版本
  77. // |---------------> (char *, ...) <--- 字串串专用版本
  78. // | 特化wchar_t版本 |--- 调用memmove()
  79. // uninitialized_copy ------------------> (wchar_t, ...) <--- 获取最佳效率
  80. // |
  81. // |
  82. // | 泛化 调用 __uninitialized_copy()
  83. // |-----> 根据类型是否为POD进行函数派发
  84. // |
  85. // |---------------- Is POD?
  86. // |
  87. // -------------------------------------
  88. // No | | Yes
  89. // ↓ |
  90. // __uninitialized_copy_aux(..., __false_type) |
  91. // for ( ; first != last; ++first, ++cur) |
  92. // construct(&*cur, *first); ↓
  93. // __uninitialized_copy_aux(..., __true_type)
  94. // copy(first, last, result)
  95. ////////////////////////////////////////////////////////////////////////////////
  96.  
  97. // 如果copy construction和operator =等效, 并且destructor is trivial
  98. // 那么就可以使用本函数
  99. // 返回值为目标地址的end
  100. // 注: 使用copy()进行复制的时候, 调用的是对象的operator =,
  101. // 所以要满足copy construction和operator =等效,
  102. // destructor is trivial保证在此版本中不会进行析构,
  103. // 以保证效率
  104. template <class InputIterator, class ForwardIterator>
  105. inline ForwardIterator
  106. __uninitialized_copy_aux(InputIterator first, InputIterator last,
  107. ForwardIterator result,
  108. __true_type)
  109. {
  110. // 调用的是STL算法copy()
  111. return copy(first, last, result);
  112. }
  113.  
  114. // 如果copy construction和operator =不等效, 那么就要调用construct()进行构造
  115. template <class InputIterator, class ForwardIterator>
  116. ForwardIterator
  117. __uninitialized_copy_aux(InputIterator first, InputIterator last,
  118. ForwardIterator result,
  119. __false_type)
  120. {
  121. ForwardIterator cur = result;
  122. __STL_TRY {
  123. // 因为copy construction和operator =不等效,
  124. // 则必须每个对象都以其它对象为蓝本进行构造
  125. // 本实作中使用的是placement new进行构造
  126. for ( ; first != last; ++first, ++cur)
  127. construct(&*cur, *first);
  128. return cur;
  129. }
  130. // commit or rollback
  131. // 如果分配失败就stack unwinding,
  132. // 保证所有对象都被析构
  133. __STL_UNWIND(destroy(result, cur));
  134. }
  135.  
  136. // 派发函数, traits出T是否为POD, 然后进行派发
  137. template <class InputIterator, class ForwardIterator, class T>
  138. inline ForwardIterator
  139. __uninitialized_copy(InputIterator first, InputIterator last,
  140. ForwardIterator result, T*)
  141. {
  142. // POD = Plain Old Data
  143. // 其具有trvial constructor/destructor/copy constructor/operator =
  144. // 所有的C++内置基本数据类型和传统C struct都属于POD
  145. typedef typename __type_traits<T>::is_POD_type is_POD;
  146.  
  147. // 根据是否为POD类型进行派发, 以保证效率
  148. return __uninitialized_copy_aux(first, last, result, is_POD());
  149. }
  150.  
  151. template <class InputIterator, class ForwardIterator>
  152. inline ForwardIterator
  153. uninitialized_copy(InputIterator first, InputIterator last,
  154. ForwardIterator result)
  155. {
  156. // 调用派发函数, 根据是否为POD决议出最佳效率的函数
  157. return __uninitialized_copy(first, last, result, value_type(result));
  158. }
  159.  
  160. // 提供给char专用, 效率最优化
  161. inline char* uninitialized_copy(const char* first, const char* last,
  162. char* result)
  163. {
  164. memmove(result, first, last - first);
  165. return result + (last - first);
  166. }
  167.  
  168. // 提供给wchar_t专用, 效率最优化
  169. inline wchar_t* uninitialized_copy(const wchar_t* first, const wchar_t* last,
  170. wchar_t* result)
  171. {
  172. memmove(result, first, sizeof(wchar_t) * (last - first));
  173. return result + (last - first);
  174. }
  175.  
  176. ////////////////////////////////////////////////////////////////////////////////
  177. // uninitialized_copy_n()实现部分
  178. ////////////////////////////////////////////////////////////////////////////////
  179. // uninitialized_copy_n
  180. // |
  181. // |------------ 判断迭代器first的类别
  182. // |
  183. // -----------------------------------------
  184. // InputIterator | | RandomAccessIterator
  185. // | |
  186. // ↓ |
  187. // __uninitialized_copy_n(..., input_iterator_tag) |
  188. // for ( ; count > 0 ; --count, ++first, ++cur) |
  189. // construct(&*cur, *first); |
  190. // ↓
  191. // __uninitialized_copy_n(..., random_access_iterator_tag)
  192. // last = first + count;
  193. // uninitialized_copy(first, last, result)
  194. ////////////////////////////////////////////////////////////////////////////////
  195.  
  196. // POD版本
  197. template <class InputIterator, class Size, class ForwardIterator>
  198. pair<InputIterator, ForwardIterator>
  199. __uninitialized_copy_n(InputIterator first, Size count,
  200. ForwardIterator result,
  201. input_iterator_tag)
  202. {
  203. ForwardIterator cur = result;
  204. __STL_TRY {
  205. for ( ; count > ; --count, ++first, ++cur)
  206. construct(&*cur, *first);
  207. return pair<InputIterator, ForwardIterator>(first, cur);
  208. }
  209. __STL_UNWIND(destroy(result, cur));
  210. }
  211.  
  212. // 非POD版本
  213. // 对于支持随机存取的迭代器, 可以直接使用uninitialized_copy()进行复制
  214. template <class RandomAccessIterator, class Size, class ForwardIterator>
  215. inline pair<RandomAccessIterator, ForwardIterator>
  216. __uninitialized_copy_n(RandomAccessIterator first, Size count,
  217. ForwardIterator result,
  218. random_access_iterator_tag)
  219. {
  220. RandomAccessIterator last = first + count;
  221. return make_pair(last, uninitialized_copy(first, last, result));
  222. }
  223.  
  224. template <class InputIterator, class Size, class ForwardIterator>
  225. inline pair<InputIterator, ForwardIterator>
  226. uninitialized_copy_n(InputIterator first, Size count,
  227. ForwardIterator result)
  228. {
  229. return __uninitialized_copy_n(first, count, result,
  230. iterator_category(first));
  231. }
  232.  
  233. ////////////////////////////////////////////////////////////////////////////////
  234. // uninitialized_fill()实现部分
  235. ////////////////////////////////////////////////////////////////////////////////
  236. // uninitialized_fill
  237. // |
  238. // |
  239. // ↓
  240. // 调用__uninitialized_fill()
  241. // 根据类型是否为POD进行函数派发
  242. // |
  243. // |---------------- Is POD?
  244. // No 泛化版本 | Yes 特化版本
  245. // -------------------------------------------
  246. // | |
  247. // | |
  248. // ↓ |
  249. // __uninitialized_fill_aux(..., __false_type) |
  250. // for ( ; cur != last; ++cur) |
  251. // construct(&*cur, x); ↓
  252. // __uninitialized_fill_aux(..., __true_type)
  253. // fill(first, last, x);
  254. ////////////////////////////////////////////////////////////////////////////////
  255.  
  256. // POD版本
  257. // 如果copy construction和operator =等效, 并且destructor is trivial
  258. // 那么就可以使用本函数
  259. template <class ForwardIterator, class T>
  260. inline void
  261. __uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
  262. const T& x, __true_type)
  263. {
  264. fill(first, last, x);
  265. }
  266.  
  267. // 非POD版本
  268. template <class ForwardIterator, class T>
  269. void
  270. __uninitialized_fill_aux(ForwardIterator first, ForwardIterator last,
  271. const T& x, __false_type)
  272. {
  273. ForwardIterator cur = first;
  274. __STL_TRY {
  275. for ( ; cur != last; ++cur)
  276. construct(&*cur, x);
  277. }
  278. __STL_UNWIND(destroy(first, cur));
  279. }
  280.  
  281. // 派发函数
  282. template <class ForwardIterator, class T, class T1>
  283. inline void __uninitialized_fill(ForwardIterator first, ForwardIterator last,
  284. const T& x, T1*)
  285. {
  286. typedef typename __type_traits<T1>::is_POD_type is_POD;
  287. __uninitialized_fill_aux(first, last, x, is_POD());
  288.  
  289. }
  290.  
  291. template <class ForwardIterator, class T>
  292. inline void uninitialized_fill(ForwardIterator first, ForwardIterator last,
  293. const T& x)
  294. {
  295. __uninitialized_fill(first, last, x, value_type(first));
  296. }
  297.  
  298. ////////////////////////////////////////////////////////////////////////////////
  299. // uninitialized_fill_n()实现部分
  300. ////////////////////////////////////////////////////////////////////////////////
  301. // uninitialized_fill_n
  302. // |
  303. // |
  304. // ↓
  305. // 调用__uninitialized_fill_n()
  306. // 根据类型是否为POD进行函数派发
  307. // |
  308. // |---------------- Is POD?
  309. // No 泛化版本 | Yes 特化版本
  310. // -------------------------------------------
  311. // | |
  312. // | |
  313. // ↓ |
  314. // __uninitialized_fill_n_aux(..., __false_type) |
  315. // for ( ; n > 0; --n, ++cur) |
  316. // construct(&*cur, x); |
  317. // ↓
  318. // __uninitialized_fill_n_aux(..., __true_type)
  319. // fill_n(first, n, x);
  320. ////////////////////////////////////////////////////////////////////////////////
  321.  
  322. // 如果copy construction和operator =等效, 并且destructor is trivial
  323. // 那么就可以使用本函数
  324. template <class ForwardIterator, class Size, class T>
  325. inline ForwardIterator
  326. __uninitialized_fill_n_aux(ForwardIterator first, Size n,
  327. const T& x, __true_type)
  328. {
  329. return fill_n(first, n, x);
  330. }
  331.  
  332. template <class ForwardIterator, class Size, class T>
  333. ForwardIterator
  334. __uninitialized_fill_n_aux(ForwardIterator first, Size n,
  335. const T& x, __false_type)
  336. {
  337. ForwardIterator cur = first;
  338. __STL_TRY {
  339. for ( ; n > ; --n, ++cur)
  340. construct(&*cur, x);
  341. return cur;
  342. }
  343. __STL_UNWIND(destroy(first, cur));
  344. }
  345.  
  346. template <class ForwardIterator, class Size, class T, class T1>
  347. inline ForwardIterator __uninitialized_fill_n(ForwardIterator first, Size n,
  348. const T& x, T1*)
  349. {
  350. typedef typename __type_traits<T1>::is_POD_type is_POD;
  351. return __uninitialized_fill_n_aux(first, n, x, is_POD());
  352.  
  353. }
  354.  
  355. template <class ForwardIterator, class Size, class T>
  356. inline ForwardIterator uninitialized_fill_n(ForwardIterator first, Size n,
  357. const T& x)
  358. {
  359. return __uninitialized_fill_n(first, n, x, value_type(first));
  360. }
  361.  
  362. ////////////////////////////////////////////////////////////////////////////////
  363. // 其它函数实现
  364. ////////////////////////////////////////////////////////////////////////////////
  365.  
  366. // Copies [first1, last1) into [result, result + (last1 - first1)), and
  367. // copies [first2, last2) into
  368. // [result, result + (last1 - first1) + (last2 - first2)).
  369.  
  370. // 我认为应该是把[first2, last2)copy到
  371. // [result + (last1 - first1), result + (last1 - first1) + (last2 - first2))
  372. // 大家可以讨论一下
  373. template <class InputIterator1, class InputIterator2, class ForwardIterator>
  374. inline ForwardIterator
  375. __uninitialized_copy_copy(InputIterator1 first1, InputIterator1 last1,
  376. InputIterator2 first2, InputIterator2 last2,
  377. ForwardIterator result)
  378. {
  379. ForwardIterator mid = uninitialized_copy(first1, last1, result);
  380. __STL_TRY {
  381. return uninitialized_copy(first2, last2, mid);
  382. }
  383. __STL_UNWIND(destroy(result, mid));
  384. }
  385.  
  386. // Fills [result, mid) with x, and copies [first, last) into
  387. // [mid, mid + (last - first)).
  388. template <class ForwardIterator, class T, class InputIterator>
  389. inline ForwardIterator
  390. __uninitialized_fill_copy(ForwardIterator result, ForwardIterator mid,
  391. const T& x,
  392. InputIterator first, InputIterator last)
  393. {
  394. uninitialized_fill(result, mid, x);
  395. __STL_TRY {
  396. return uninitialized_copy(first, last, mid);
  397. }
  398. __STL_UNWIND(destroy(result, mid));
  399. }
  400.  
  401. // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
  402. // fills [first2 + (last1 - first1), last2) with x.
  403. template <class InputIterator, class ForwardIterator, class T>
  404. inline void
  405. __uninitialized_copy_fill(InputIterator first1, InputIterator last1,
  406. ForwardIterator first2, ForwardIterator last2,
  407. const T& x)
  408. {
  409. ForwardIterator mid2 = uninitialized_copy(first1, last1, first2);
  410. __STL_TRY {
  411. uninitialized_fill(mid2, last2, x);
  412. }
  413. __STL_UNWIND(destroy(first2, mid2));
  414. }
  415.  
  416. __STL_END_NAMESPACE
  417.  
  418. #endif /* __SGI_STL_INTERNAL_UNINITIALIZED_H */
  419.  
  420. // Local Variables:
  421. // mode:C++
  422. // End:

STL stl_uninitialized.h的更多相关文章

  1. 自己动手实现STL 03:内存基本处理工具(stl_uninitialized.h)

    一.前言 前面两篇已经编写了内存配置器和建构解构工具函数.这里,就准备编写并介绍下内存基本处理工具函数.比如uninitialized_copy().uninitialized_copy和 unini ...

  2. STL stl_config.h

    stl_config.h . // Filename: stl_config.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // ...

  3. STL defalloc.h

    defalloc.h . // Filename: defalloc.h . . // Comment By: 凝霜 . // E-mail: mdl2009@vip.qq.com . // Blog ...

  4. STL stl_alloc.h

    # // Comment By: 凝霜 # // E-mail: mdl2009@vip.qq.com # // Blog: http://blog.csdn.net/mdl13412 # # // ...

  5. STL stl_construct.h

    stl_construct.h // Filename: stl_construct.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog ...

  6. STL源代码剖析 容器 stl_hashtable.h

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie hashtable ------------------------------------ ...

  7. STL六大组件之——分配器(内存分配,好深奥的东西)

    SGI设计了双层级配置器,第一级配置器直接使用malloc()和free(),第二级配置器则视情况采用不同的策略:当配置区块超过128bytes时,视之为“足够大”,便调用第一级配置器:当配置区小于1 ...

  8. STL源码剖析读书笔记--第四章--序列式容器

    1.什么是序列式容器?什么是关联式容器? 书上给出的解释是,序列式容器中的元素是可序的(可理解为可以按序索引,不管这个索引是像数组一样的随机索引,还是像链表一样的顺序索引),但是元素值在索引顺序的方向 ...

  9. STL源码分析读书笔记--第二章--空间配置器(allocator)

    声明:侯捷先生的STL源码剖析第二章个人感觉讲得蛮乱的,而且跟第三章有关,建议看完第三章再看第二章,网上有人上传了一篇读书笔记,觉得这个读书笔记的内容和编排还不错,我的这篇总结基本就延续了该读书笔记的 ...

随机推荐

  1. java jdbc连接数据库,Properties 属性设置参数方法

    今天在整合为数据库发现在配置中实现的赋值方式,可以用代码实现.特记录下共以后参考: 代码:        // 操作数据库        Connection conn; String strData ...

  2. 研究怎么运用xcode处理常见的调试问题

    本文转载至 http://blog.csdn.net/zhuzhihai1988/article/details/7749022 所谓磨刀不误砍柴工,这里菜鸟我在研究怎么运用xcode处理常见的调试问 ...

  3. 如何在iOS中使用libxml

    本文转载至 http://blog.csdn.net/cloudhsu/article/details/8087628 1. 选择xcode工程设定 2. 选择target 3. 选择Summary ...

  4. iOS框架你了解多少?

    1.iOS 系统可以分为以下四层,每个框架对应IOS系统里的一层,每层建立在它下面层的上面.应该尽量使用上层的框架来代替下面的框架.更高层次的框架是对底层框架基于对象的抽象.以下列出几个iOS开发的常 ...

  5. antd-mobile的例子--cnode

    antd-mobile    简单的例子 预览地址 https://shenggen1987.github.io/antd-mobile-roadhog/#/crm/pages/users githu ...

  6. python基础17 ---继承补充知识

    一.继承的顺序 1.在python中的类可以集成多个类,既然是继承多个类就有类的寻找顺序这么一说.其寻找方法就有广度优先和深度优先两种. 2.当类是新式类,多继承的情况下会按照广度优先的顺序查找. 如 ...

  7. Python基础(1)_python介绍、简单运算符

    Python执行一个程序分为三个阶段 阶段一:先启动python解释器 阶段二:python解释器把硬盘中的文件读入到内存中 阶段三:python解释器解释执行刚刚读入内存的代码 二.编程语言的分类: ...

  8. selenium主要功能封装

    最近实习需要使用selenium这一自动化工具对公司的运维监控系统进行自动化爬取数据,编写代码过程中负责带我的杰哥让我参考借鉴他们公司外包的运维监控系统代码,在项目中我看到了对selenium主要各功 ...

  9. 剑指offer——不能被继承的类

    方法一:通过将类的构造函数和析构函数声明成private来防止子类继承.声明静态的方法来构造和析构类的对象. 但是用起来不是很方便.只能得到在堆上的实例,而不能得到在栈上的实例. 方法二:构造辅助类C ...

  10. <linux硬件及硬盘分区>关于硬盘的规划和使用细节

    ps:期末考试 终于结束了,这下我也终于有时间开始继续经营我的博客.这个学期上的一些课真的非常有用,感觉很多课程细地讲都可以写成非常精致的技术博文,比如流水线技术,数据库的一些技术,大学里的考试考的内 ...