_uninitialized.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_UNINITIALIZED_H
  29. #define _STLP_INTERNAL_UNINITIALIZED_H
  30. #ifndef _STLP_INTERNAL_CSTRING
  31. # include <stl/_cstring.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_ALGOBASE_H
  34. # include <stl/_algobase.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_CONSTRUCT_H
  37. # include <stl/_construct.h>
  38. #endif
  39. _STLP_BEGIN_NAMESPACE
  40. _STLP_MOVE_TO_PRIV_NAMESPACE
  41. // uninitialized_copy
  42. template <class _InputIter, class _OutputIter, class _Distance>
  43. inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
  44. _OutputIter __result, _Distance*) {
  45. _OutputIter __cur = __result;
  46. _STLP_TRY {
  47. for ( ; __first != __last; ++__first, ++__cur)
  48. _Param_Construct(&*__cur, *__first);
  49. return __cur;
  50. }
  51. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
  52. _STLP_RET_AFTER_THROW(__cur)
  53. }
  54. template <class _InputIter, class _OutputIter, class _Distance>
  55. inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
  56. _OutputIter __result, const input_iterator_tag &, _Distance* __d)
  57. { return __ucopy(__first, __last, __result, __d); }
  58. #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  59. template <class _InputIter, class _OutputIter, class _Distance>
  60. inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
  61. _OutputIter __result, const forward_iterator_tag &, _Distance* __d)
  62. { return __ucopy(__first, __last, __result, __d); }
  63. template <class _InputIter, class _OutputIter, class _Distance>
  64. inline _OutputIter __ucopy(_InputIter __first, _InputIter __last,
  65. _OutputIter __result, const bidirectional_iterator_tag &, _Distance* __d)
  66. { return __ucopy(__first, __last, __result, __d); }
  67. #endif
  68. template <class _RandomAccessIter, class _OutputIter, class _Distance>
  69. inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last,
  70. _OutputIter __result, const random_access_iterator_tag &, _Distance*) {
  71. _OutputIter __cur = __result;
  72. _STLP_TRY {
  73. for (_Distance __n = __last - __first; __n > 0; --__n) {
  74. _Param_Construct(&*__cur, *__first);
  75. ++__first;
  76. ++__cur;
  77. }
  78. return __cur;
  79. }
  80. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
  81. _STLP_RET_AFTER_THROW(__cur)
  82. }
  83. //Used internaly
  84. template <class _RandomAccessIter, class _OutputIter>
  85. inline _OutputIter __ucopy(_RandomAccessIter __first, _RandomAccessIter __last, _OutputIter __result)
  86. { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
  87. inline void*
  88. __ucopy_trivial(const void* __first, const void* __last, void* __result) {
  89. //dums: this version can use memcpy (__copy_trivial can't)
  90. return (__last == __first) ? __result :
  91. ((char*)memcpy(__result, __first, ((const char*)__last - (const char*)__first))) +
  92. ((const char*)__last - (const char*)__first);
  93. }
  94. template <class _InputIter, class _OutputIter>
  95. inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
  96. const __false_type& /*TrivialUCopy*/)
  97. { return __ucopy(__first, __last, __result, random_access_iterator_tag(), (ptrdiff_t*)0); }
  98. template <class _InputIter, class _OutputIter>
  99. inline _OutputIter __ucopy_ptrs(_InputIter __first, _InputIter __last, _OutputIter __result,
  100. const __true_type& /*TrivialUCopy*/) {
  101. // we know they all pointers, so this cast is OK
  102. // return (_OutputIter)__copy_trivial(&(*__first), &(*__last), &(*__result));
  103. return (_OutputIter)__ucopy_trivial(__first, __last, __result);
  104. }
  105. template <class _InputIter, class _OutputIter>
  106. inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
  107. const __true_type& /*BothPtrType*/) {
  108. return __ucopy_ptrs(__first, __last, __result,
  109. _UseTrivialUCopy(_STLP_VALUE_TYPE(__first, _InputIter),
  110. _STLP_VALUE_TYPE(__result, _OutputIter))._Answer());
  111. }
  112. template <class _InputIter, class _OutputIter>
  113. inline _OutputIter __ucopy_aux(_InputIter __first, _InputIter __last, _OutputIter __result,
  114. const __false_type& /*BothPtrType*/) {
  115. return __ucopy(__first, __last, __result,
  116. _STLP_ITERATOR_CATEGORY(__first, _InputIter),
  117. _STLP_DISTANCE_TYPE(__first, _InputIter));
  118. }
  119. _STLP_MOVE_TO_STD_NAMESPACE
  120. template <class _InputIter, class _ForwardIter>
  121. inline _ForwardIter
  122. uninitialized_copy(_InputIter __first, _InputIter __last, _ForwardIter __result)
  123. { return _STLP_PRIV __ucopy_aux(__first, __last, __result, _BothPtrType< _InputIter, _ForwardIter>::_Answer()); }
  124. inline char*
  125. uninitialized_copy(const char* __first, const char* __last, char* __result)
  126. { return (char*)_STLP_PRIV __ucopy_trivial(__first, __last, __result); }
  127. # if defined (_STLP_HAS_WCHAR_T) // dwa 8/15/97
  128. inline wchar_t*
  129. uninitialized_copy(const wchar_t* __first, const wchar_t* __last, wchar_t* __result)
  130. { return (wchar_t*)_STLP_PRIV __ucopy_trivial (__first, __last, __result); }
  131. # endif
  132. // uninitialized_copy_n (not part of the C++ standard)
  133. _STLP_MOVE_TO_PRIV_NAMESPACE
  134. template <class _InputIter, class _Size, class _ForwardIter>
  135. _STLP_INLINE_LOOP
  136. pair<_InputIter, _ForwardIter>
  137. __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result,
  138. const input_iterator_tag &) {
  139. _ForwardIter __cur = __result;
  140. _STLP_TRY {
  141. for ( ; __count > 0 ; --__count, ++__first, ++__cur)
  142. _Param_Construct(&*__cur, *__first);
  143. return pair<_InputIter, _ForwardIter>(__first, __cur);
  144. }
  145. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __cur))
  146. _STLP_RET_AFTER_THROW((pair<_InputIter, _ForwardIter>(__first, __cur)))
  147. }
  148. # if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  149. template <class _InputIter, class _Size, class _ForwardIterator>
  150. inline pair<_InputIter, _ForwardIterator>
  151. __ucopy_n(_InputIter __first, _Size __count,
  152. _ForwardIterator __result,
  153. const forward_iterator_tag &)
  154. { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
  155. template <class _InputIter, class _Size, class _ForwardIterator>
  156. inline pair<_InputIter, _ForwardIterator>
  157. __ucopy_n(_InputIter __first, _Size __count,
  158. _ForwardIterator __result,
  159. const bidirectional_iterator_tag &)
  160. { return __ucopy_n(__first, __count, __result, input_iterator_tag()); }
  161. # endif
  162. template <class _RandomAccessIter, class _Size, class _ForwardIter>
  163. inline pair<_RandomAccessIter, _ForwardIter>
  164. __ucopy_n(_RandomAccessIter __first, _Size __count, _ForwardIter __result,
  165. const random_access_iterator_tag &) {
  166. _RandomAccessIter __last = __first + __count;
  167. return pair<_RandomAccessIter, _ForwardIter>(__last, uninitialized_copy(__first, __last, __result));
  168. }
  169. // This is used internally in <rope> , which is extension itself.
  170. template <class _InputIter, class _Size, class _ForwardIter>
  171. inline pair<_InputIter, _ForwardIter>
  172. __ucopy_n(_InputIter __first, _Size __count, _ForwardIter __result)
  173. { return _STLP_PRIV __ucopy_n(__first, __count, __result, _STLP_ITERATOR_CATEGORY(__first, _InputIter)); }
  174. #if !defined (_STLP_NO_EXTENSIONS)
  175. _STLP_MOVE_TO_STD_NAMESPACE
  176. template <class _InputIter, class _Size, class _ForwardIter>
  177. inline pair<_InputIter, _ForwardIter>
  178. uninitialized_copy_n(_InputIter __first, _Size __count, _ForwardIter __result)
  179. { return _STLP_PRIV __ucopy_n(__first, __count, __result); }
  180. _STLP_MOVE_TO_PRIV_NAMESPACE
  181. #endif
  182. template <class _ForwardIter, class _Tp, class _Distance>
  183. inline void __ufill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x, _Distance*) {
  184. _ForwardIter __cur = __first;
  185. _STLP_TRY {
  186. for ( ; __cur != __last; ++__cur)
  187. _Param_Construct(&*__cur, __x);
  188. }
  189. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
  190. }
  191. template <class _ForwardIter, class _Tp, class _Distance>
  192. inline void __ufill(_ForwardIter __first, _ForwardIter __last,
  193. const _Tp& __x, const input_iterator_tag &, _Distance* __d)
  194. { __ufill(__first, __last, __x, __d); }
  195. #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  196. template <class _ForwardIter, class _Tp, class _Distance>
  197. inline void __ufill(_ForwardIter __first, _ForwardIter __last,
  198. const _Tp& __x, const forward_iterator_tag &, _Distance* __d)
  199. { __ufill(__first, __last, __x, __d); }
  200. template <class _ForwardIter, class _Tp, class _Distance>
  201. inline void __ufill(_ForwardIter __first, _ForwardIter __last,
  202. const _Tp& __x, const bidirectional_iterator_tag &, _Distance* __d)
  203. { __ufill(__first, __last, __x, __d); }
  204. #endif
  205. template <class _ForwardIter, class _Tp, class _Distance>
  206. inline void __ufill(_ForwardIter __first, _ForwardIter __last,
  207. const _Tp& __x, const random_access_iterator_tag &, _Distance*) {
  208. _ForwardIter __cur = __first;
  209. _STLP_TRY {
  210. for (_Distance __n = __last - __first; __n > 0; --__n, ++__cur)
  211. _Param_Construct(&*__cur, __x);
  212. }
  213. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
  214. }
  215. _STLP_MOVE_TO_STD_NAMESPACE
  216. template <class _ForwardIter, class _Tp>
  217. inline void uninitialized_fill(_ForwardIter __first, _ForwardIter __last, const _Tp& __x) {
  218. _STLP_PRIV __ufill(__first, __last, __x,
  219. _STLP_ITERATOR_CATEGORY(__first, _ForwardIter),
  220. _STLP_DISTANCE_TYPE(__first, _ForwardIter));
  221. }
  222. // Specialization: for one-byte types we can use memset.
  223. inline void uninitialized_fill(unsigned char* __first, unsigned char* __last,
  224. const unsigned char& __val) {
  225. unsigned char __tmp = __val;
  226. memset(__first, __tmp, __last - __first);
  227. }
  228. #if !defined (_STLP_NO_SIGNED_BUILTINS)
  229. inline void uninitialized_fill(signed char* __first, signed char* __last,
  230. const signed char& __val) {
  231. signed char __tmp = __val;
  232. memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
  233. }
  234. #endif
  235. inline void uninitialized_fill(char* __first, char* __last, const char& __val) {
  236. char __tmp = __val;
  237. memset(__first, __STATIC_CAST(unsigned char,__tmp), __last - __first);
  238. }
  239. _STLP_MOVE_TO_PRIV_NAMESPACE
  240. template <class _ForwardIter, class _Size, class _Tp>
  241. inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
  242. _ForwardIter __cur = __first;
  243. _STLP_TRY {
  244. for ( ; __n > 0; --__n, ++__cur)
  245. _Param_Construct(&*__cur, __x);
  246. }
  247. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first, __cur))
  248. return __cur;
  249. }
  250. template <class _ForwardIter, class _Size, class _Tp>
  251. inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
  252. const input_iterator_tag &)
  253. { return __ufill_n(__first, __n, __x); }
  254. #if defined (_STLP_NONTEMPL_BASE_MATCH_BUG)
  255. template <class _ForwardIter, class _Size, class _Tp>
  256. inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
  257. const forward_iterator_tag &)
  258. { return __ufill_n(__first, __n, __x); }
  259. template <class _ForwardIter, class _Size, class _Tp>
  260. inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
  261. const bidirectional_iterator_tag &)
  262. { return __ufill_n(__first, __n, __x); }
  263. #endif
  264. template <class _ForwardIter, class _Size, class _Tp>
  265. inline _ForwardIter __uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x) {
  266. _ForwardIter __last = __first + __n;
  267. __ufill(__first, __last, __x, random_access_iterator_tag(), (ptrdiff_t*)0);
  268. return __last;
  269. }
  270. template <class _ForwardIter, class _Size, class _Tp>
  271. inline _ForwardIter __ufill_n(_ForwardIter __first, _Size __n, const _Tp& __x,
  272. const random_access_iterator_tag &)
  273. { return __uninitialized_fill_n(__first, __n, __x); }
  274. /* __uninitialized_init is an internal algo to init a range with a value
  275. * built using default constructor. It is only called with pointer as
  276. * iterator.
  277. */
  278. template <class _ForwardIter, class _Size, class _Tp>
  279. inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
  280. const __false_type& /*_HasDefaultZero*/)
  281. { return __uninitialized_fill_n(__first, __n, __val); }
  282. template <class _ForwardIter, class _Size, class _Tp>
  283. inline _ForwardIter __uinit_aux_aux(_ForwardIter __first, _Size __n, const _Tp& /* __val */,
  284. const __true_type& /*_HasDefaultZero*/) {
  285. memset((unsigned char*)__first, 0, __n * sizeof(_Tp));
  286. return __first + __n;
  287. }
  288. template <class _ForwardIter, class _Size, class _Tp>
  289. inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp&,
  290. const __true_type& /*_TrivialInit*/)
  291. { return __first + __n; }
  292. template <class _ForwardIter, class _Size, class _Tp>
  293. inline _ForwardIter __uinit_aux(_ForwardIter __first, _Size __n, const _Tp& __val,
  294. const __false_type& /*_TrivialInit*/)
  295. { return __uinit_aux_aux(__first, __n, __val, _HasDefaultZeroValue(__first)._Answer()); }
  296. template <class _ForwardIter, class _Size, class _Tp>
  297. inline _ForwardIter __uninitialized_init(_ForwardIter __first, _Size __n, const _Tp& __val)
  298. { return __uinit_aux(__first, __n, __val, _UseTrivialInit(__first)._Answer()); }
  299. _STLP_MOVE_TO_STD_NAMESPACE
  300. template <class _ForwardIter, class _Size, class _Tp>
  301. inline void
  302. uninitialized_fill_n(_ForwardIter __first, _Size __n, const _Tp& __x)
  303. { _STLP_PRIV __ufill_n(__first, __n, __x, _STLP_ITERATOR_CATEGORY(__first, _ForwardIter)); }
  304. // Extensions: __uninitialized_copy_copy, __uninitialized_copy_fill,
  305. // __uninitialized_fill_copy.
  306. // __uninitialized_copy_copy
  307. // Copies [first1, last1) into [result, result + (last1 - first1)), and
  308. // copies [first2, last2) into
  309. // [result + (last1 - first1), result + (last1 - first1) + (last2 - first2)).
  310. _STLP_MOVE_TO_PRIV_NAMESPACE
  311. template <class _InputIter1, class _InputIter2, class _ForwardIter>
  312. inline _ForwardIter
  313. __uninitialized_copy_copy(_InputIter1 __first1, _InputIter1 __last1,
  314. _InputIter2 __first2, _InputIter2 __last2,
  315. _ForwardIter __result) {
  316. _ForwardIter __new_result = uninitialized_copy(__first1, __last1, __result);
  317. _STLP_TRY {
  318. return uninitialized_copy(__first2, __last2, __new_result);
  319. }
  320. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __new_result))
  321. _STLP_RET_AFTER_THROW(__result)
  322. }
  323. // __uninitialized_fill_copy
  324. // Fills [result, mid) with x, and copies [first, last) into
  325. // [mid, mid + (last - first)).
  326. template <class _ForwardIter, class _Tp, class _InputIter>
  327. inline _ForwardIter
  328. __uninitialized_fill_copy(_ForwardIter __result, _ForwardIter __mid, const _Tp& __x,
  329. _InputIter __first, _InputIter __last) {
  330. uninitialized_fill(__result, __mid, __x);
  331. _STLP_TRY {
  332. return uninitialized_copy(__first, __last, __mid);
  333. }
  334. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__result, __mid))
  335. _STLP_RET_AFTER_THROW(__result)
  336. }
  337. // __uninitialized_copy_fill
  338. // Copies [first1, last1) into [first2, first2 + (last1 - first1)), and
  339. // fills [first2 + (last1 - first1), last2) with x.
  340. template <class _Iter, class _Tp>
  341. inline void
  342. __uninitialized_copy_fill(_Iter __first1, _Iter __last1, _Iter __first2, _Iter __last2,
  343. const _Tp& __x) {
  344. _Iter __mid2 = uninitialized_copy(__first1, __last1, __first2);
  345. _STLP_TRY {
  346. uninitialized_fill(__mid2, __last2, __x);
  347. }
  348. _STLP_UNWIND(_STLP_STD::_Destroy_Range(__first2, __mid2))
  349. }
  350. /* __uninitialized_move:
  351. * This function is used internaly and only with pointers as iterators.
  352. */
  353. template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
  354. inline _ForwardIter
  355. __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
  356. _TrivialUCpy __trivial_ucpy, const __false_type& /*_Movable*/)
  357. { return __ucopy_ptrs(__first, __last, __result, __trivial_ucpy); }
  358. template <class _InputIter, class _ForwardIter, class _TrivialUCpy>
  359. _STLP_INLINE_LOOP
  360. _ForwardIter
  361. __uninitialized_move(_InputIter __first, _InputIter __last, _ForwardIter __result,
  362. _TrivialUCpy , const __true_type& /*_Movable*/) {
  363. //Move constructor should not throw so we do not need to take care of exceptions here.
  364. for (ptrdiff_t __n = __last - __first ; __n > 0; --__n) {
  365. _Move_Construct(&*__result, *__first);
  366. ++__first; ++__result;
  367. }
  368. return __result;
  369. }
  370. _STLP_MOVE_TO_STD_NAMESPACE
  371. _STLP_END_NAMESPACE
  372. #endif /* _STLP_INTERNAL_UNINITIALIZED_H */
  373. // Local Variables:
  374. // mode:C++
  375. // End: