Guest User

Untitled

a guest
May 17th, 2015
521
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 80.31 KB | None | 0 0
  1. // vector standard header
  2. #pragma once
  3. #ifndef _VECTOR_
  4. #define _VECTOR_
  5. #ifndef RC_INVOKED
  6. #include <xmemory>
  7. #include <stdexcept>
  8.  
  9. #pragma pack(push,_CRT_PACKING)
  10. #pragma warning(push,3)
  11. #pragma push_macro("new")
  12. #undef new
  13.  
  14. #pragma warning(disable: 4127)
  15. #pragma warning(disable: 4244)
  16.  
  17. _STD_BEGIN
  18. #define _VECTOR_ORPHAN_RANGE (_ITERATOR_DEBUG_LEVEL == 2)
  19.  
  20. // TEMPLATE CLASS _Vector_const_iterator
  21. template<class _Myvec>
  22. class _Vector_const_iterator
  23. : public _Iterator012<random_access_iterator_tag,
  24. typename _Myvec::value_type,
  25. typename _Myvec::difference_type,
  26. typename _Myvec::const_pointer,
  27. typename _Myvec::const_reference,
  28. _Iterator_base>
  29. { // iterator for nonmutable vector
  30. public:
  31. typedef _Vector_const_iterator<_Myvec> _Myiter;
  32. typedef random_access_iterator_tag iterator_category;
  33.  
  34. typedef typename _Myvec::value_type value_type;
  35. typedef typename _Myvec::difference_type difference_type;
  36. typedef typename _Myvec::const_pointer pointer;
  37. typedef typename _Myvec::const_reference reference;
  38. typedef typename _Myvec::pointer _Tptr;
  39.  
  40. _Vector_const_iterator()
  41. : _Ptr()
  42. { // construct with null pointer
  43. }
  44.  
  45. _Vector_const_iterator(_Tptr _Parg, const _Container_base *_Pvector)
  46. : _Ptr(_Parg)
  47. { // construct with pointer _Parg
  48. this->_Adopt(_Pvector);
  49. }
  50.  
  51. typedef pointer _Unchecked_type;
  52.  
  53. _Myiter& _Rechecked(_Unchecked_type _Right)
  54. { // reset from unchecked iterator
  55. this->_Ptr = (_Tptr)_Right;
  56. return (*this);
  57. }
  58.  
  59. _Unchecked_type _Unchecked() const
  60. { // make an unchecked iterator
  61. return (_Unchecked_type(this->_Ptr));
  62. }
  63.  
  64. reference operator*() const
  65. { // return designated object
  66. #if _ITERATOR_DEBUG_LEVEL == 2
  67. if (this->_Getcont() == 0
  68. || this->_Ptr == 0
  69. || this->_Ptr < ((_Myvec *)this->_Getcont())->_Myfirst
  70. || ((_Myvec *)this->_Getcont())->_Mylast <= this->_Ptr)
  71. { // report error
  72. _DEBUG_ERROR("vector iterator not dereferencable");
  73. _SCL_SECURE_OUT_OF_RANGE;
  74. }
  75.  
  76. #elif _ITERATOR_DEBUG_LEVEL == 1
  77. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  78. _SCL_SECURE_VALIDATE_RANGE(
  79. this->_Ptr != _Tptr()
  80. && ((_Myvec *)this->_Getcont())->_Myfirst <= this->_Ptr
  81. && this->_Ptr < ((_Myvec *)this->_Getcont())->_Mylast);
  82. #endif /* _ITERATOR_DEBUG_LEVEL */
  83.  
  84. _Analysis_assume_(this->_Ptr != _Tptr());
  85.  
  86. return (*this->_Ptr);
  87. }
  88.  
  89. pointer operator->() const
  90. { // return pointer to class object
  91. return (_STD pointer_traits<pointer>::pointer_to(**this));
  92. }
  93.  
  94. _Myiter& operator++()
  95. { // preincrement
  96. #if _ITERATOR_DEBUG_LEVEL == 2
  97. if (this->_Getcont() == 0
  98. || this->_Ptr == 0
  99. || ((_Myvec *)this->_Getcont())->_Mylast <= this->_Ptr)
  100. { // report error
  101. _DEBUG_ERROR("vector iterator not incrementable");
  102. _SCL_SECURE_OUT_OF_RANGE;
  103. }
  104.  
  105. #elif _ITERATOR_DEBUG_LEVEL == 1
  106. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  107. _SCL_SECURE_VALIDATE_RANGE(
  108. this->_Ptr != _Tptr()
  109. && this->_Ptr < ((_Myvec *)this->_Getcont())->_Mylast);
  110. #endif /* _ITERATOR_DEBUG_LEVEL */
  111.  
  112. ++this->_Ptr;
  113. return (*this);
  114. }
  115.  
  116. _Myiter operator++(int)
  117. { // postincrement
  118. _Myiter _Tmp = *this;
  119. ++*this;
  120. return (_Tmp);
  121. }
  122.  
  123. _Myiter& operator--()
  124. { // predecrement
  125. #if _ITERATOR_DEBUG_LEVEL == 2
  126. if (this->_Getcont() == 0
  127. || this->_Ptr == 0
  128. || this->_Ptr <= ((_Myvec *)this->_Getcont())->_Myfirst)
  129. { // report error
  130. _DEBUG_ERROR("vector iterator not decrementable");
  131. _SCL_SECURE_OUT_OF_RANGE;
  132. }
  133.  
  134. #elif _ITERATOR_DEBUG_LEVEL == 1
  135. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  136. _SCL_SECURE_VALIDATE_RANGE(
  137. this->_Ptr != _Tptr()
  138. && ((_Myvec *)this->_Getcont())->_Myfirst < this->_Ptr);
  139. #endif /* _ITERATOR_DEBUG_LEVEL */
  140.  
  141. --this->_Ptr;
  142. return (*this);
  143. }
  144.  
  145. _Myiter operator--(int)
  146. { // postdecrement
  147. _Myiter _Tmp = *this;
  148. --*this;
  149. return (_Tmp);
  150. }
  151.  
  152. _Myiter& operator+=(difference_type _Off)
  153. { // increment by integer
  154. #if _ITERATOR_DEBUG_LEVEL == 2
  155. if (this->_Getcont() == 0
  156. || this->_Ptr + _Off < ((_Myvec *)this->_Getcont())->_Myfirst
  157. || ((_Myvec *)this->_Getcont())->_Mylast < this->_Ptr + _Off)
  158. { // report error
  159. _DEBUG_ERROR("vector iterator + offset out of range");
  160. _SCL_SECURE_OUT_OF_RANGE;
  161. }
  162.  
  163. #elif _ITERATOR_DEBUG_LEVEL == 1
  164. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  165. _SCL_SECURE_VALIDATE_RANGE(
  166. ((_Myvec *)this->_Getcont())->_Myfirst <= this->_Ptr + _Off
  167. && this->_Ptr + _Off <= ((_Myvec *)this->_Getcont())->_Mylast);
  168. #endif /* _ITERATOR_DEBUG_LEVEL */
  169.  
  170. _Ptr += _Off;
  171. return (*this);
  172. }
  173.  
  174. _Myiter operator+(difference_type _Off) const
  175. { // return this + integer
  176. _Myiter _Tmp = *this;
  177. return (_Tmp += _Off);
  178. }
  179.  
  180. _Myiter& operator-=(difference_type _Off)
  181. { // decrement by integer
  182. return (*this += -_Off);
  183. }
  184.  
  185. _Myiter operator-(difference_type _Off) const
  186. { // return this - integer
  187. _Myiter _Tmp = *this;
  188. return (_Tmp -= _Off);
  189. }
  190.  
  191. difference_type operator-(const _Myiter& _Right) const
  192. { // return difference of iterators
  193. _Compat(_Right);
  194. return (this->_Ptr - _Right._Ptr);
  195. }
  196.  
  197. reference operator[](difference_type _Off) const
  198. { // subscript
  199. return (*(*this + _Off));
  200. }
  201.  
  202. bool operator==(const _Myiter& _Right) const
  203. { // test for iterator equality
  204. _Compat(_Right);
  205. return (this->_Ptr == _Right._Ptr);
  206. }
  207.  
  208. bool operator!=(const _Myiter& _Right) const
  209. { // test for iterator inequality
  210. return (!(*this == _Right));
  211. }
  212.  
  213. bool operator<(const _Myiter& _Right) const
  214. { // test if this < _Right
  215. _Compat(_Right);
  216. return (this->_Ptr < _Right._Ptr);
  217. }
  218.  
  219. bool operator>(const _Myiter& _Right) const
  220. { // test if this > _Right
  221. return (_Right < *this);
  222. }
  223.  
  224. bool operator<=(const _Myiter& _Right) const
  225. { // test if this <= _Right
  226. return (!(_Right < *this));
  227. }
  228.  
  229. bool operator>=(const _Myiter& _Right) const
  230. { // test if this >= _Right
  231. return (!(*this < _Right));
  232. }
  233.  
  234. #if _ITERATOR_DEBUG_LEVEL == 2
  235. void _Compat(const _Myiter& _Right) const
  236. { // test for compatible iterator pair
  237. if (this->_Getcont() == 0
  238. || this->_Getcont() != _Right._Getcont())
  239. { // report error
  240. _DEBUG_ERROR("vector iterators incompatible");
  241. _SCL_SECURE_INVALID_ARGUMENT;
  242. }
  243. }
  244.  
  245. #elif _ITERATOR_DEBUG_LEVEL == 1
  246. void _Compat(const _Myiter& _Right) const
  247. { // test for compatible iterator pair
  248. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  249. _SCL_SECURE_VALIDATE_RANGE(this->_Getcont() == _Right._Getcont());
  250. }
  251.  
  252. #else /* _ITERATOR_DEBUG_LEVEL == 0 */
  253. void _Compat(const _Myiter&) const
  254. { // test for compatible iterator pair
  255. }
  256. #endif /* _ITERATOR_DEBUG_LEVEL */
  257.  
  258. _Tptr _Ptr; // pointer to element in vector
  259. };
  260.  
  261. template<class _Myvec> inline
  262. typename _Vector_const_iterator<_Myvec>::_Unchecked_type
  263. _Unchecked(_Vector_const_iterator<_Myvec> _Iter)
  264. { // convert to unchecked
  265. return (_Iter._Unchecked());
  266. }
  267.  
  268. template<class _Myvec> inline
  269. _Vector_const_iterator<_Myvec>&
  270. _Rechecked(_Vector_const_iterator<_Myvec>& _Iter,
  271. typename _Vector_const_iterator<_Myvec>
  272. ::_Unchecked_type _Right)
  273. { // convert to checked
  274. return (_Iter._Rechecked(_Right));
  275. }
  276.  
  277. template<class _Myvec> inline
  278. _Vector_const_iterator<_Myvec> operator+(
  279. typename _Vector_const_iterator<_Myvec>::difference_type _Off,
  280. _Vector_const_iterator<_Myvec> _Next)
  281. { // add offset to iterator
  282. return (_Next += _Off);
  283. }
  284.  
  285. // TEMPLATE CLASS _Vector_iterator
  286. template<class _Myvec>
  287. class _Vector_iterator
  288. : public _Vector_const_iterator<_Myvec>
  289. { // iterator for mutable vector
  290. public:
  291. typedef _Vector_iterator<_Myvec> _Myiter;
  292. typedef _Vector_const_iterator<_Myvec> _Mybase;
  293. typedef random_access_iterator_tag iterator_category;
  294.  
  295. typedef typename _Myvec::value_type value_type;
  296. typedef typename _Myvec::difference_type difference_type;
  297. typedef typename _Myvec::pointer pointer;
  298. typedef typename _Myvec::reference reference;
  299.  
  300. _Vector_iterator()
  301. { // construct with null vector pointer
  302. }
  303.  
  304. _Vector_iterator(pointer _Parg, const _Container_base *_Pvector)
  305. : _Mybase(_Parg, _Pvector)
  306. { // construct with pointer _Parg
  307. }
  308.  
  309. typedef pointer _Unchecked_type;
  310.  
  311. _Myiter& _Rechecked(_Unchecked_type _Right)
  312. { // reset from unchecked iterator
  313. this->_Ptr = _Right;
  314. return (*this);
  315. }
  316.  
  317. _Unchecked_type _Unchecked() const
  318. { // make an unchecked iterator
  319. return (_Unchecked_type(this->_Ptr));
  320. }
  321.  
  322. reference operator*() const
  323. { // return designated object
  324. return ((reference)**(_Mybase *)this);
  325. }
  326.  
  327. pointer operator->() const
  328. { // return pointer to class object
  329. return (_STD pointer_traits<pointer>::pointer_to(**this));
  330. }
  331.  
  332. _Myiter& operator++()
  333. { // preincrement
  334. ++*(_Mybase *)this;
  335. return (*this);
  336. }
  337.  
  338. _Myiter operator++(int)
  339. { // postincrement
  340. _Myiter _Tmp = *this;
  341. ++*this;
  342. return (_Tmp);
  343. }
  344.  
  345. _Myiter& operator--()
  346. { // predecrement
  347. --*(_Mybase *)this;
  348. return (*this);
  349. }
  350.  
  351. _Myiter operator--(int)
  352. { // postdecrement
  353. _Myiter _Tmp = *this;
  354. --*this;
  355. return (_Tmp);
  356. }
  357.  
  358. _Myiter& operator+=(difference_type _Off)
  359. { // increment by integer
  360. *(_Mybase *)this += _Off;
  361. return (*this);
  362. }
  363.  
  364. _Myiter operator+(difference_type _Off) const
  365. { // return this + integer
  366. _Myiter _Tmp = *this;
  367. return (_Tmp += _Off);
  368. }
  369.  
  370. _Myiter& operator-=(difference_type _Off)
  371. { // decrement by integer
  372. return (*this += -_Off);
  373. }
  374.  
  375. _Myiter operator-(difference_type _Off) const
  376. { // return this - integer
  377. _Myiter _Tmp = *this;
  378. return (_Tmp -= _Off);
  379. }
  380.  
  381. difference_type operator-(const _Mybase& _Right) const
  382. { // return difference of iterators
  383. return (*(_Mybase *)this - _Right);
  384. }
  385.  
  386. reference operator[](difference_type _Off) const
  387. { // subscript
  388. return (*(*this + _Off));
  389. }
  390. };
  391.  
  392. template<class _Myvec> inline
  393. typename _Vector_iterator<_Myvec>::_Unchecked_type
  394. _Unchecked(_Vector_iterator<_Myvec> _Iter)
  395. { // convert to unchecked
  396. return (_Iter._Unchecked());
  397. }
  398.  
  399. template<class _Myvec> inline
  400. _Vector_iterator<_Myvec>&
  401. _Rechecked(_Vector_iterator<_Myvec>& _Iter,
  402. typename _Vector_iterator<_Myvec>
  403. ::_Unchecked_type _Right)
  404. { // convert to checked
  405. return (_Iter._Rechecked(_Right));
  406. }
  407.  
  408. template<class _Myvec> inline
  409. _Vector_iterator<_Myvec> operator+(
  410. typename _Vector_iterator<_Myvec>::difference_type _Off,
  411. _Vector_iterator<_Myvec> _Next)
  412. { // add offset to iterator
  413. return (_Next += _Off);
  414. }
  415.  
  416. // vector TYPE WRAPPERS
  417. template<class _Value_type,
  418. class _Size_type,
  419. class _Difference_type,
  420. class _Pointer,
  421. class _Const_pointer,
  422. class _Reference,
  423. class _Const_reference>
  424. struct _Vec_iter_types
  425. { // wraps types needed by iterators
  426. typedef _Value_type value_type;
  427. typedef _Size_type size_type;
  428. typedef _Difference_type difference_type;
  429. typedef _Pointer pointer;
  430. typedef _Const_pointer const_pointer;
  431. typedef _Reference reference;
  432. typedef _Const_reference const_reference;
  433. };
  434.  
  435. template<class _Ty,
  436. class _Alloc0>
  437. struct _Vec_base_types
  438. { // types needed for a container base
  439. typedef _Alloc0 _Alloc;
  440. typedef _Vec_base_types<_Ty, _Alloc> _Myt;
  441.  
  442. typedef _Wrap_alloc<_Alloc> _Alty0;
  443. typedef typename _Alty0::template rebind<_Ty>::other _Alty;
  444.  
  445.  
  446. typedef typename _Alty::pointer _Tptr;
  447. typedef typename _Alty::template rebind<_Tptr>::other _Alpty;
  448.  
  449. typedef typename _If<_Is_simple_alloc<_Alty>::value,
  450. _Simple_types<typename _Alty::value_type>,
  451. _Vec_iter_types<typename _Alty::value_type,
  452. typename _Alty::size_type,
  453. typename _Alty::difference_type,
  454. typename _Alty::pointer,
  455. typename _Alty::const_pointer,
  456. typename _Alty::reference,
  457. typename _Alty::const_reference> >::type
  458. _Val_types;
  459. };
  460.  
  461. // TEMPLATE CLASS _Vector_val
  462. template<class _Val_types>
  463. class _Vector_val
  464. : public _Container_base
  465. { // base class for vector to hold data
  466. public:
  467. typedef _Vector_val<_Val_types> _Myt;
  468.  
  469. typedef typename _Val_types::value_type value_type;
  470. typedef typename _Val_types::size_type size_type;
  471. typedef typename _Val_types::difference_type difference_type;
  472. typedef typename _Val_types::pointer pointer;
  473. typedef typename _Val_types::const_pointer const_pointer;
  474. typedef typename _Val_types::reference reference;
  475. typedef typename _Val_types::const_reference const_reference;
  476.  
  477. typedef _Vector_iterator<_Myt> iterator;
  478. typedef _Vector_const_iterator<_Myt> const_iterator;
  479.  
  480. _Vector_val()
  481. { // initialize values
  482. _Myfirst = pointer();
  483. _Mylast = pointer();
  484. _Myend = pointer();
  485. }
  486.  
  487. pointer _Myfirst; // pointer to beginning of array
  488. pointer _Mylast; // pointer to current end of sequence
  489. pointer _Myend; // pointer to end of array
  490. };
  491.  
  492. // TEMPLATE CLASS _Vector_alloc
  493. template<bool _Al_has_storage,
  494. class _Alloc_types>
  495. class _Vector_alloc
  496. : public _Vector_val<typename _Alloc_types::_Val_types>
  497. { // base class for vector to hold allocator with storage
  498. public:
  499. typename _Alloc_types::_Alty _Alval; // allocator object
  500.  
  501. typedef _Vector_alloc<_Al_has_storage, _Alloc_types> _Myt;
  502. typedef typename _Alloc_types::_Alloc _Alloc;
  503. typedef typename _Alloc_types::_Alty _Alty;
  504.  
  505. #if _ITERATOR_DEBUG_LEVEL == 0
  506. _Vector_alloc(const _Alloc& _Al = _Alloc())
  507. : _Alval(_Al)
  508. { // construct allocator from _Al
  509. }
  510.  
  511. void _Change_alloc(const _Alty& _Al)
  512. { // replace old allocator
  513. this->_Alval = _Al;
  514. }
  515.  
  516. void _Swap_alloc(_Myt& _Right)
  517. { // swap allocators
  518. _Swap_adl(this->_Alval, _Right._Alval);
  519. }
  520.  
  521. #else /* _ITERATOR_DEBUG_LEVEL == 0 */
  522. _Vector_alloc(const _Alty& _Al = _Alty())
  523. : _Alval(_Al)
  524. { // construct allocator from _Al
  525. _Alloc_proxy();
  526. }
  527.  
  528. ~_Vector_alloc() _NOEXCEPT
  529. { // destroy proxy
  530. _Free_proxy();
  531. }
  532.  
  533. void _Change_alloc(const _Alty& _Al)
  534. { // replace old allocator
  535. _Free_proxy();
  536. this->_Alval = _Al;
  537. _Alloc_proxy();
  538. }
  539.  
  540. void _Swap_alloc(_Myt& _Right)
  541. { // swap allocators
  542. _Swap_adl(this->_Alval, _Right._Alval);
  543. _Swap_adl(this->_Myproxy, _Right._Myproxy);
  544. }
  545.  
  546. void _Alloc_proxy()
  547. { // construct proxy from _Alval
  548. typename _Alty::template rebind<_Container_proxy>::other
  549. _Alproxy(this->_Alval);
  550. this->_Myproxy = _Alproxy.allocate(1);
  551. _Alproxy.construct(this->_Myproxy, _Container_proxy());
  552. this->_Myproxy->_Mycont = this;
  553. }
  554.  
  555. void _Free_proxy()
  556. { // destroy proxy
  557. typename _Alty::template rebind<_Container_proxy>::other
  558. _Alproxy(this->_Alval);
  559. this->_Orphan_all();
  560. _Alproxy.destroy(this->_Myproxy);
  561. _Alproxy.deallocate(this->_Myproxy, 1);
  562. this->_Myproxy = 0;
  563. }
  564. #endif /* _ITERATOR_DEBUG_LEVEL == 0 */
  565.  
  566. _Alty& _Getal()
  567. { // get reference to allocator
  568. return (this->_Alval);
  569. }
  570.  
  571. const _Alty& _Getal() const
  572. { // get reference to allocator
  573. return (this->_Alval);
  574. }
  575. };
  576.  
  577. template<class _Alloc_types>
  578. class _Vector_alloc<false, _Alloc_types>
  579. : public _Vector_val<typename _Alloc_types::_Val_types>
  580. { // base class for vector to hold allocator with no storage
  581. public:
  582. typedef _Vector_alloc<false, _Alloc_types> _Myt;
  583. typedef typename _Alloc_types::_Alloc _Alloc;
  584.  
  585. typedef typename _Alloc_types::_Alty _Alty;
  586.  
  587. #if _ITERATOR_DEBUG_LEVEL == 0
  588. _Vector_alloc(const _Alloc& = _Alloc())
  589. { // construct allocator from _Al
  590. }
  591.  
  592. void _Change_alloc(const _Alty&)
  593. { // replace old allocator
  594. }
  595.  
  596. void _Swap_alloc(_Myt&)
  597. { // swap allocators
  598. }
  599.  
  600. #else /* _ITERATOR_DEBUG_LEVEL == 0 */
  601. _Vector_alloc(const _Alloc& = _Alloc())
  602. { // construct allocator from _Al
  603. _Alloc_proxy();
  604. }
  605.  
  606. ~_Vector_alloc() _NOEXCEPT
  607. { // destroy proxy
  608. _Free_proxy();
  609. }
  610.  
  611. void _Change_alloc(const _Alty&)
  612. { // replace old allocator
  613. }
  614.  
  615. void _Swap_alloc(_Myt& _Right)
  616. { // swap allocators
  617. _Swap_adl(this->_Myproxy, _Right._Myproxy);
  618. }
  619.  
  620. void _Alloc_proxy()
  621. { // construct proxy from _Alval
  622. typename _Alty::template rebind<_Container_proxy>::other
  623. _Alproxy;
  624. this->_Myproxy = _Alproxy.allocate(1);
  625. _Alproxy.construct(this->_Myproxy, _Container_proxy());
  626. this->_Myproxy->_Mycont = this;
  627. }
  628.  
  629. void _Free_proxy()
  630. { // destroy proxy
  631. typename _Alty::template rebind<_Container_proxy>::other
  632. _Alproxy;
  633. this->_Orphan_all();
  634. _Alproxy.destroy(this->_Myproxy);
  635. _Alproxy.deallocate(this->_Myproxy, 1);
  636. this->_Myproxy = 0;
  637. }
  638. #endif /* _ITERATOR_DEBUG_LEVEL == 0 */
  639.  
  640. _Alty _Getal() const
  641. { // get reference to allocator
  642. return (_Alty());
  643. }
  644. };
  645.  
  646. // TEMPLATE CLASS vector
  647. template<class _Ty,
  648. class _Alloc = allocator<_Ty> >
  649. class vector
  650. : public _Vector_alloc<!is_empty<_Alloc>::value,
  651. _Vec_base_types<_Ty, _Alloc> >
  652. { // varying size array of values
  653. public:
  654. typedef vector<_Ty, _Alloc> _Myt;
  655. typedef _Vector_alloc<!is_empty<_Alloc>::value,
  656. _Vec_base_types<_Ty, _Alloc> > _Mybase;
  657. typedef _Alloc allocator_type;
  658.  
  659. typedef typename _Mybase::_Alty _Alty;
  660.  
  661. typedef typename _Mybase::value_type value_type;
  662. typedef typename _Mybase::size_type size_type;
  663. typedef typename _Mybase::difference_type difference_type;
  664. typedef typename _Mybase::pointer pointer;
  665. typedef typename _Mybase::const_pointer const_pointer;
  666. typedef typename _Mybase::reference reference;
  667. typedef typename _Mybase::const_reference const_reference;
  668.  
  669. #define _VICONT(it) it._Getcont()
  670. #define _VIPTR(it) (it)._Ptr
  671.  
  672. typedef typename _Mybase::iterator iterator;
  673. typedef typename _Mybase::const_iterator const_iterator;
  674.  
  675. typedef _STD reverse_iterator<iterator> reverse_iterator;
  676. typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
  677.  
  678. vector()
  679. : _Mybase()
  680. { // construct empty vector
  681. }
  682.  
  683. explicit vector(const _Alloc& _Al)
  684. : _Mybase(_Al)
  685. { // construct empty vector, allocator
  686. }
  687.  
  688. explicit vector(size_type _Count)
  689. : _Mybase()
  690. { // construct from _Count * value_type()
  691. if (_Buy(_Count))
  692. { // nonzero, fill it
  693. _Alty _Alval(this->_Getal());
  694. _TRY_BEGIN
  695. _Uninitialized_default_fill_n(this->_Myfirst, _Count, _Alval);
  696. this->_Mylast += _Count;
  697. _CATCH_ALL
  698. _Tidy();
  699. _RERAISE;
  700. _CATCH_END
  701. }
  702. }
  703.  
  704. vector(size_type _Count, const value_type& _Val)
  705. : _Mybase()
  706. { // construct from _Count * _Val
  707. _Construct_n(_Count, _STD addressof(_Val));
  708. }
  709.  
  710. vector(size_type _Count, const value_type& _Val, const _Alloc& _Al)
  711. : _Mybase(_Al)
  712. { // construct from _Count * _Val, allocator
  713. _Construct_n(_Count, _STD addressof(_Val));
  714. }
  715.  
  716. vector(const _Myt& _Right)
  717.  
  718. : _Mybase(_Right._Getal().select_on_container_copy_construction())
  719.  
  720.  
  721. { // construct by copying _Right
  722. if (_Buy(_Right.size()))
  723. _TRY_BEGIN
  724. this->_Mylast = _Ucopy(_Right.begin(), _Right.end(),
  725. this->_Myfirst);
  726. _CATCH_ALL
  727. _Tidy();
  728. _RERAISE;
  729. _CATCH_END
  730. }
  731.  
  732. vector(const _Myt& _Right, const _Alloc& _Al)
  733. : _Mybase(_Al)
  734. { // construct by copying _Right, allocator
  735. if (_Buy(_Right.size()))
  736. _TRY_BEGIN
  737. this->_Mylast = _Ucopy(_Right.begin(), _Right.end(),
  738. this->_Myfirst);
  739. _CATCH_ALL
  740. _Tidy();
  741. _RERAISE;
  742. _CATCH_END
  743. }
  744.  
  745. template<class _Iter,
  746. class = typename enable_if<_Is_iterator<_Iter>::value,
  747. void>:: type>
  748. vector(_Iter _First, _Iter _Last)
  749. : _Mybase()
  750. { // construct from [_First, _Last)
  751. _Construct(_First, _Last);
  752. }
  753.  
  754. template<class _Iter,
  755. class = typename enable_if<_Is_iterator<_Iter>::value,
  756. void>:: type>
  757. vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
  758. : _Mybase(_Al)
  759. { // construct from [_First, _Last) with allocator
  760. _Construct(_First, _Last);
  761. }
  762.  
  763. template<class _Iter>
  764. void _Construct(_Iter _First, _Iter _Last)
  765. { // initialize with [_First, _Last)
  766. _Construct(_First, _Last, _Iter_cat(_First));
  767. }
  768.  
  769. template<class _Iter>
  770. void _Construct(_Iter _First, _Iter _Last, input_iterator_tag)
  771. { // initialize with [_First, _Last), input iterators
  772. _TRY_BEGIN
  773.  
  774. for (; _First != _Last; ++_First)
  775. emplace_back(*_First);
  776.  
  777. _CATCH_ALL
  778. _Tidy();
  779. _RERAISE;
  780. _CATCH_END
  781. }
  782.  
  783. template<class _Iter>
  784. void _Construct(_Iter _First, _Iter _Last, forward_iterator_tag)
  785. { // initialize with [_First, _Last), forward iterators
  786. if (_Buy(_STD distance(_First, _Last)))
  787. { // nonzero, fill it
  788. _TRY_BEGIN
  789. this->_Mylast = _Ucopy(_First, _Last, this->_Myfirst);
  790. _CATCH_ALL
  791. _Tidy();
  792. _RERAISE;
  793. _CATCH_END
  794. }
  795. }
  796.  
  797. void _Construct_n(size_type _Count, const value_type *_Pval)
  798. { // construct from _Count * *_Pval
  799. if (_Buy(_Count))
  800. { // nonzero, fill it
  801. _TRY_BEGIN
  802. this->_Mylast = _Ufill(this->_Myfirst, _Count, _Pval);
  803. _CATCH_ALL
  804. _Tidy();
  805. _RERAISE;
  806. _CATCH_END
  807. }
  808. }
  809.  
  810. vector(_Myt&& _Right)
  811. : _Mybase(_Right._Getal())
  812. { // construct by moving _Right
  813. _Assign_rv(_STD forward<_Myt>(_Right), true_type());
  814. }
  815.  
  816. vector(_Myt&& _Right, const _Alloc& _Al)
  817. : _Mybase(_Al)
  818. { // construct by moving _Right, allocator
  819. _Assign_rv(_STD forward<_Myt>(_Right));
  820. }
  821.  
  822. _Myt& operator=(_Myt&& _Right)
  823. { // assign by moving _Right
  824. if (this != &_Right)
  825. { // different, assign it
  826. _Tidy();
  827. if (_Alty::propagate_on_container_move_assignment::value
  828. && this->_Getal() != _Right._Getal())
  829. this->_Change_alloc(_Right._Getal());
  830.  
  831. _Assign_rv(_STD forward<_Myt>(_Right));
  832. }
  833. return (*this);
  834. }
  835.  
  836. void _Assign_rv(_Myt&& _Right, true_type)
  837. { // move from _Right, stealing its contents
  838. this->_Swap_all((_Myt&)_Right);
  839. this->_Myfirst = _Right._Myfirst;
  840. this->_Mylast = _Right._Mylast;
  841. this->_Myend = _Right._Myend;
  842.  
  843. _Right._Myfirst = pointer();
  844. _Right._Mylast = pointer();
  845. _Right._Myend = pointer();
  846. }
  847.  
  848. void _Assign_rv(_Myt&& _Right, false_type)
  849. { // move from _Right, possibly moving its contents
  850. if (get_allocator() == _Right.get_allocator())
  851. _Assign_rv(_STD forward<_Myt>(_Right), true_type());
  852. else
  853. _Construct(_STD make_move_iterator(_Right.begin()),
  854. _STD make_move_iterator(_Right.end()));
  855. }
  856.  
  857. void _Assign_rv(_Myt&& _Right)
  858. { // assign by moving _Right
  859. _Assign_rv(_STD forward<_Myt>(_Right),
  860. typename _Alty::propagate_on_container_move_assignment());
  861. }
  862.  
  863.  
  864. void push_back(value_type&& _Val)
  865. { // insert by moving into element at end
  866. if (_Inside(_STD addressof(_Val)))
  867. { // push back an element
  868. size_type _Idx = _STD addressof(_Val) - this->_Myfirst;
  869. if (this->_Mylast == this->_Myend)
  870. _Reserve(1);
  871. _Orphan_range(this->_Mylast, this->_Mylast);
  872. this->_Getal().construct(this->_Mylast,
  873. _STD forward<value_type>(this->_Myfirst[_Idx]));
  874. ++this->_Mylast;
  875. }
  876. else
  877. { // push back a non-element
  878. if (this->_Mylast == this->_Myend)
  879. _Reserve(1);
  880. _Orphan_range(this->_Mylast, this->_Mylast);
  881. this->_Getal().construct(this->_Mylast,
  882. _STD forward<value_type>(_Val));
  883. ++this->_Mylast;
  884. }
  885. }
  886.  
  887. iterator insert(const_iterator _Where, _Ty&& _Val)
  888. { // insert by moving _Val at _Where
  889. return (emplace(_Where, _STD move(_Val)));
  890. }
  891.  
  892. template<class... _Valty>
  893. void emplace_back(_Valty&&... _Val)
  894. { // insert by moving into element at end
  895. if (this->_Mylast == this->_Myend)
  896. _Reserve(1);
  897. _Orphan_range(this->_Mylast, this->_Mylast);
  898. this->_Getal().construct(this->_Mylast,
  899. _STD forward<_Valty>(_Val)...);
  900. ++this->_Mylast;
  901. }
  902.  
  903. template<class... _Valty>
  904. iterator emplace(const_iterator _Where, _Valty&&... _Val)
  905. { // insert by moving _Val at _Where
  906. size_type _Off = _VIPTR(_Where) - this->_Myfirst;
  907.  
  908. #if _ITERATOR_DEBUG_LEVEL == 2
  909. if (size() < _Off)
  910. _DEBUG_ERROR("vector emplace iterator outside range");
  911. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  912.  
  913. emplace_back(_STD forward<_Valty>(_Val)...);
  914. _STD rotate(begin() + _Off, end() - 1, end());
  915. return (begin() + _Off);
  916. }
  917.  
  918.  
  919. vector(_XSTD initializer_list<value_type> _Ilist,
  920. const _Alloc& _Al = allocator_type())
  921. : _Mybase(_Al)
  922. { // construct from initializer_list
  923. insert(begin(), _Ilist.begin(), _Ilist.end());
  924. }
  925.  
  926. _Myt& operator=(_XSTD initializer_list<value_type> _Ilist)
  927. { // assign initializer_list
  928. assign(_Ilist.begin(), _Ilist.end());
  929. return (*this);
  930. }
  931.  
  932. void assign(_XSTD initializer_list<value_type> _Ilist)
  933. { // assign initializer_list
  934. assign(_Ilist.begin(), _Ilist.end());
  935. }
  936.  
  937. iterator insert(const_iterator _Where,
  938. _XSTD initializer_list<value_type> _Ilist)
  939. { // insert initializer_list
  940. return (insert(_Where, _Ilist.begin(), _Ilist.end()));
  941. }
  942.  
  943. ~vector() _NOEXCEPT
  944. { // destroy the object
  945. _Tidy();
  946. }
  947.  
  948. _Myt& operator=(const _Myt& _Right)
  949. { // assign _Right
  950. if (this != &_Right)
  951. { // different, assign it
  952. if (this->_Getal() != _Right._Getal()
  953. && _Alty::propagate_on_container_copy_assignment::value)
  954. { // change allocator before copying
  955. _Tidy();
  956. this->_Change_alloc(_Right._Getal());
  957. }
  958.  
  959. this->_Orphan_all();
  960.  
  961. if (_Right.empty())
  962. clear(); // new sequence empty, erase existing sequence
  963. else if (_Right.size() <= size())
  964. { // enough elements, copy new and destroy old
  965. pointer _Ptr = _Copy_impl(_Right._Myfirst,
  966. _Right._Mylast, this->_Myfirst); // copy new
  967. _Destroy(_Ptr, this->_Mylast); // destroy old
  968. this->_Mylast = this->_Myfirst + _Right.size();
  969. }
  970. else if (_Right.size() <= capacity())
  971. { // enough room, copy and construct new
  972. pointer _Ptr = _Right._Myfirst + size();
  973. _Copy_impl(_Right._Myfirst,
  974. _Ptr, this->_Myfirst);
  975. this->_Mylast = _Ucopy(_Ptr, _Right._Mylast, this->_Mylast);
  976. }
  977. else
  978. { // not enough room, allocate new array and construct new
  979. if (this->_Myfirst != pointer())
  980. { // discard old array
  981. _Destroy(this->_Myfirst, this->_Mylast);
  982. this->_Getal().deallocate(this->_Myfirst,
  983. this->_Myend - this->_Myfirst);
  984. }
  985. if (_Buy(_Right.size()))
  986. _TRY_BEGIN
  987. this->_Mylast = _Ucopy(_Right._Myfirst, _Right._Mylast,
  988. this->_Myfirst);
  989. _CATCH_ALL
  990. _Tidy();
  991. _RERAISE;
  992. _CATCH_END
  993. }
  994. }
  995. return (*this);
  996. }
  997.  
  998. void reserve(size_type _Count)
  999. { // determine new minimum length of allocated storage
  1000. if (capacity() < _Count)
  1001. { // something to do, check and reallocate
  1002. if (max_size() < _Count)
  1003. _Xlen();
  1004. _Reallocate(_Count);
  1005. }
  1006. }
  1007.  
  1008. size_type capacity() const _NOEXCEPT
  1009. { // return current length of allocated storage
  1010. return (this->_Myend - this->_Myfirst);
  1011. }
  1012.  
  1013. size_type _Unused_capacity() const _NOEXCEPT
  1014. { // micro-optimization for capacity() - size()
  1015. return (this->_Myend - this->_Mylast);
  1016. }
  1017.  
  1018. size_type _Has_unused_capacity() const _NOEXCEPT
  1019. { // micro-optimization for capacity() != size()
  1020. return (this->_Myend != this->_Mylast);
  1021. }
  1022.  
  1023. iterator begin() _NOEXCEPT
  1024. { // return iterator for beginning of mutable sequence
  1025. return (iterator(this->_Myfirst, this));
  1026. }
  1027.  
  1028. const_iterator begin() const _NOEXCEPT
  1029. { // return iterator for beginning of nonmutable sequence
  1030. return (const_iterator(this->_Myfirst, this));
  1031. }
  1032.  
  1033. iterator end() _NOEXCEPT
  1034. { // return iterator for end of mutable sequence
  1035. return (iterator(this->_Mylast, this));
  1036. }
  1037.  
  1038. const_iterator end() const _NOEXCEPT
  1039. { // return iterator for end of nonmutable sequence
  1040. return (const_iterator(this->_Mylast, this));
  1041. }
  1042.  
  1043. iterator _Make_iter(const_iterator _Where) const
  1044. { // make iterator from const_iterator
  1045. return (iterator(_Where._Ptr, this));
  1046. }
  1047.  
  1048. reverse_iterator rbegin() _NOEXCEPT
  1049. { // return iterator for beginning of reversed mutable sequence
  1050. return (reverse_iterator(end()));
  1051. }
  1052.  
  1053. const_reverse_iterator rbegin() const _NOEXCEPT
  1054. { // return iterator for beginning of reversed nonmutable sequence
  1055. return (const_reverse_iterator(end()));
  1056. }
  1057.  
  1058. reverse_iterator rend() _NOEXCEPT
  1059. { // return iterator for end of reversed mutable sequence
  1060. return (reverse_iterator(begin()));
  1061. }
  1062.  
  1063. const_reverse_iterator rend() const _NOEXCEPT
  1064. { // return iterator for end of reversed nonmutable sequence
  1065. return (const_reverse_iterator(begin()));
  1066. }
  1067.  
  1068. const_iterator cbegin() const _NOEXCEPT
  1069. { // return iterator for beginning of nonmutable sequence
  1070. return (((const _Myt *)this)->begin());
  1071. }
  1072.  
  1073. const_iterator cend() const _NOEXCEPT
  1074. { // return iterator for end of nonmutable sequence
  1075. return (((const _Myt *)this)->end());
  1076. }
  1077.  
  1078. const_reverse_iterator crbegin() const _NOEXCEPT
  1079. { // return iterator for beginning of reversed nonmutable sequence
  1080. return (((const _Myt *)this)->rbegin());
  1081. }
  1082.  
  1083. const_reverse_iterator crend() const _NOEXCEPT
  1084. { // return iterator for end of reversed nonmutable sequence
  1085. return (((const _Myt *)this)->rend());
  1086. }
  1087.  
  1088. void shrink_to_fit()
  1089. { // reduce capacity
  1090. if (_Has_unused_capacity())
  1091. { // worth shrinking, do it
  1092. if (empty())
  1093. _Tidy();
  1094. else
  1095. _Reallocate(size());
  1096. }
  1097. }
  1098.  
  1099. void resize(size_type _Newsize)
  1100. { // determine new length, padding as needed
  1101. if (_Newsize < size())
  1102. _Pop_back_n(size() - _Newsize);
  1103. else if (size() < _Newsize)
  1104. { // pad as needed
  1105. _Alty _Alval(this->_Getal());
  1106. _Reserve(_Newsize - size());
  1107. _TRY_BEGIN
  1108. _Uninitialized_default_fill_n(this->_Mylast, _Newsize - size(),
  1109. _Alval);
  1110. _CATCH_ALL
  1111. _Tidy();
  1112. _RERAISE;
  1113. _CATCH_END
  1114. this->_Mylast += _Newsize - size();
  1115. }
  1116. }
  1117.  
  1118. void resize(size_type _Newsize, const value_type& _Val)
  1119. { // determine new length, padding with _Val elements as needed
  1120. if (_Newsize < size())
  1121. _Pop_back_n(size() - _Newsize);
  1122. else if (size() < _Newsize)
  1123. { // pad as needed
  1124. const value_type *_Ptr = _STD addressof(_Val);
  1125.  
  1126. if (_Inside(_Ptr))
  1127. { // padding is inside vector, recompute _Ptr after reserve
  1128. const difference_type _Idx = _Ptr
  1129. - _STD addressof(*this->_Myfirst);
  1130. _Reserve(_Newsize - size());
  1131. _Ptr = _STD addressof(*this->_Myfirst) + _Idx;
  1132. }
  1133. else
  1134. _Reserve(_Newsize - size());
  1135.  
  1136. _TRY_BEGIN
  1137. _Ufill(this->_Mylast, _Newsize - size(), _Ptr);
  1138. _CATCH_ALL
  1139. _Tidy();
  1140. _RERAISE;
  1141. _CATCH_END
  1142. this->_Mylast += _Newsize - size();
  1143. }
  1144. }
  1145.  
  1146. size_type size() const _NOEXCEPT
  1147. { // return length of sequence
  1148. return (this->_Mylast - this->_Myfirst);
  1149. }
  1150.  
  1151. size_type max_size() const _NOEXCEPT
  1152. { // return maximum possible length of sequence
  1153. return (this->_Getal().max_size());
  1154. }
  1155.  
  1156. bool empty() const _NOEXCEPT
  1157. { // test if sequence is empty
  1158. return (this->_Myfirst == this->_Mylast);
  1159. }
  1160.  
  1161. _Alloc get_allocator() const _NOEXCEPT
  1162. { // return allocator object for values
  1163. return (this->_Getal());
  1164. }
  1165.  
  1166. const_reference at(size_type _Pos) const
  1167. { // subscript nonmutable sequence with checking
  1168. if (size() <= _Pos)
  1169. _Xran();
  1170. return (*(this->_Myfirst + _Pos));
  1171. }
  1172.  
  1173. reference at(size_type _Pos)
  1174. { // subscript mutable sequence with checking
  1175. if (size() <= _Pos)
  1176. _Xran();
  1177. return (*(this->_Myfirst + _Pos));
  1178. }
  1179.  
  1180. const_reference operator[](size_type _Pos) const
  1181. { // subscript nonmutable sequence
  1182. #if _ITERATOR_DEBUG_LEVEL == 2
  1183. if (size() <= _Pos)
  1184. { // report error
  1185. _DEBUG_ERROR("vector subscript out of range");
  1186. _SCL_SECURE_OUT_OF_RANGE;
  1187. }
  1188.  
  1189. #elif _ITERATOR_DEBUG_LEVEL == 1
  1190. _SCL_SECURE_VALIDATE_RANGE(_Pos < size());
  1191. #endif /* _ITERATOR_DEBUG_LEVEL */
  1192.  
  1193. return (*(this->_Myfirst + _Pos));
  1194. }
  1195.  
  1196. reference operator[](size_type _Pos)
  1197. { // subscript mutable sequence
  1198. #if _ITERATOR_DEBUG_LEVEL == 2
  1199. if (size() <= _Pos)
  1200. { // report error
  1201. _DEBUG_ERROR("vector subscript out of range");
  1202. _SCL_SECURE_OUT_OF_RANGE;
  1203. }
  1204.  
  1205. #elif _ITERATOR_DEBUG_LEVEL == 1
  1206. _SCL_SECURE_VALIDATE_RANGE(_Pos < size());
  1207. #endif /* _ITERATOR_DEBUG_LEVEL */
  1208.  
  1209. return (*(this->_Myfirst + _Pos));
  1210. }
  1211.  
  1212. pointer data() _NOEXCEPT
  1213. { // return address of first element
  1214. return (this->_Myfirst);
  1215. }
  1216.  
  1217. const_pointer data() const _NOEXCEPT
  1218. { // return address of first element
  1219. return (this->_Myfirst);
  1220. }
  1221.  
  1222. reference front()
  1223. { // return first element of mutable sequence
  1224. return (*begin());
  1225. }
  1226.  
  1227. const_reference front() const
  1228. { // return first element of nonmutable sequence
  1229. return (*begin());
  1230. }
  1231.  
  1232. reference back()
  1233. { // return last element of mutable sequence
  1234. return (*(end() - 1));
  1235. }
  1236.  
  1237. const_reference back() const
  1238. { // return last element of nonmutable sequence
  1239. return (*(end() - 1));
  1240. }
  1241.  
  1242. void push_back(const value_type& _Val)
  1243. { // insert element at end
  1244. if (_Inside(_STD addressof(_Val)))
  1245. { // push back an element
  1246. size_type _Idx = _STD addressof(_Val) - this->_Myfirst;
  1247. if (this->_Mylast == this->_Myend)
  1248. _Reserve(1);
  1249. _Orphan_range(this->_Mylast, this->_Mylast);
  1250. this->_Getal().construct(this->_Mylast,
  1251. this->_Myfirst[_Idx]);
  1252. ++this->_Mylast;
  1253. }
  1254. else
  1255. { // push back a non-element
  1256. if (this->_Mylast == this->_Myend)
  1257. _Reserve(1);
  1258. _Orphan_range(this->_Mylast, this->_Mylast);
  1259. this->_Getal().construct(this->_Mylast,
  1260. _Val);
  1261. ++this->_Mylast;
  1262. }
  1263. }
  1264.  
  1265. #if _ITERATOR_DEBUG_LEVEL == 2
  1266. void pop_back()
  1267. { // erase element at end
  1268. if (empty())
  1269. _DEBUG_ERROR("vector empty before pop");
  1270. else
  1271. { // erase last element
  1272. _Orphan_range(this->_Mylast - 1, this->_Mylast);
  1273. this->_Getal().destroy(this->_Mylast - 1);
  1274. --this->_Mylast;
  1275. }
  1276. }
  1277.  
  1278. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  1279. void pop_back()
  1280. { // erase element at end
  1281. this->_Getal().destroy(this->_Mylast - 1);
  1282. --this->_Mylast;
  1283. }
  1284. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1285.  
  1286. template<class _Iter>
  1287. typename enable_if<_Is_iterator<_Iter>::value,
  1288. void>::type
  1289. assign(_Iter _First, _Iter _Last)
  1290. { // assign [_First, _Last)
  1291. clear();
  1292. _Assign(_First, _Last, _Iter_cat(_First));
  1293. }
  1294.  
  1295. template<class _Iter>
  1296. void _Assign(_Iter _First, _Iter _Last, input_iterator_tag)
  1297. { // assign [_First, _Last), input iterators
  1298. for (; _First != _Last; ++_First)
  1299. emplace_back(*_First);
  1300. }
  1301.  
  1302. template<class _Iter>
  1303. void _Assign(_Iter _First, _Iter _Last, forward_iterator_tag)
  1304. { // assign [_First, _Last), forward iterators
  1305. if (_First == _Last)
  1306. return; // nothing to do
  1307.  
  1308. size_type _Newsize = _STD distance(_First, _Last);
  1309.  
  1310. if (capacity() < _Newsize)
  1311. { // need more room, try to get it
  1312. size_type _Newcapacity = _Grow_to(_Newsize);
  1313. _Tidy();
  1314. _Buy(_Newcapacity);
  1315. }
  1316.  
  1317. this->_Mylast = _Ucopy(_First, _Last, this->_Myfirst);
  1318. }
  1319.  
  1320. void assign(size_type _Count, const value_type& _Val)
  1321. { // assign _Count * _Val
  1322. clear();
  1323. insert(begin(), _Count, _Val);
  1324. }
  1325.  
  1326. iterator insert(const_iterator _Where, const _Ty& _Val)
  1327. { // insert _Val at _Where
  1328. return (_Insert_n(_Where, (size_type)1, _Val));
  1329. }
  1330.  
  1331. iterator insert(const_iterator _Where, size_type _Count,
  1332. const _Ty& _Val)
  1333. { // insert _Count * _Val at _Where
  1334. return (_Insert_n(_Where, _Count, _Val));
  1335. }
  1336.  
  1337. template<class _Iter>
  1338. typename enable_if<_Is_iterator<_Iter>::value,
  1339. iterator>::type
  1340. insert(const_iterator _Where, _Iter _First, _Iter _Last)
  1341. { // insert [_First, _Last) at _Where
  1342. size_type _Off = _VIPTR(_Where) - this->_Myfirst;
  1343. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  1344. return (begin() + _Off);
  1345. }
  1346.  
  1347. template<class _Iter>
  1348. void _Insert(const_iterator _Where, _Iter _First, _Iter _Last,
  1349. input_iterator_tag)
  1350. { // insert [_First, _Last) at _Where, input iterators
  1351. size_type _Off = _VIPTR(_Where) - this->_Myfirst;
  1352.  
  1353. #if _ITERATOR_DEBUG_LEVEL == 2
  1354. if (size() < _Off)
  1355. _DEBUG_ERROR("vector insert iterator outside range");
  1356. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1357.  
  1358. if (_First != _Last)
  1359. { // worth doing, gather at end and rotate into place
  1360. size_type _Oldsize = size();
  1361.  
  1362. _TRY_BEGIN
  1363. for (; _First != _Last; ++_First)
  1364. push_back(*_First); // append
  1365.  
  1366. _CATCH_ALL
  1367. erase(begin() + _Oldsize, end());
  1368. _RERAISE;
  1369. _CATCH_END
  1370.  
  1371. _STD rotate(begin() + _Off, begin() + _Oldsize, end());
  1372. }
  1373. }
  1374.  
  1375. template<class _Iter>
  1376. void _Insert(const_iterator _Where, _Iter _First, _Iter _Last,
  1377. forward_iterator_tag)
  1378. { // insert [_First, _Last) at _Where, forward iterators
  1379. #if _ITERATOR_DEBUG_LEVEL == 2
  1380. if (_VICONT(_Where) != this
  1381. || _VIPTR(_Where) < this->_Myfirst
  1382. || this->_Mylast < _VIPTR(_Where))
  1383. _DEBUG_ERROR("vector insert iterator outside range");
  1384. _DEBUG_RANGE(_First, _Last);
  1385. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1386.  
  1387. size_type _Count = 0;
  1388. _Distance(_First, _Last, _Count);
  1389.  
  1390. if (_Count == 0)
  1391. ;
  1392. else if (_Unused_capacity() < _Count)
  1393. { // not enough room, reallocate
  1394. if (max_size() - size() < _Count)
  1395. _Xlen(); // result too long
  1396.  
  1397. size_type _Capacity = _Grow_to(size() + _Count);
  1398. pointer _Newvec = this->_Getal().allocate(_Capacity);
  1399. pointer _Ptr = _Newvec;
  1400.  
  1401. _TRY_BEGIN
  1402. _Ptr = _Umove(this->_Myfirst, _VIPTR(_Where),
  1403. _Newvec); // copy prefix
  1404. _Ptr = _Ucopy(_First, _Last, _Ptr); // add new stuff
  1405. _Umove(_VIPTR(_Where), this->_Mylast,
  1406. _Ptr); // copy suffix
  1407. _CATCH_ALL
  1408. _Destroy(_Newvec, _Ptr);
  1409. this->_Getal().deallocate(_Newvec, _Capacity);
  1410. _RERAISE;
  1411. _CATCH_END
  1412.  
  1413. _Count += size();
  1414. if (this->_Myfirst != pointer())
  1415. { // destroy and deallocate old array
  1416. _Destroy(this->_Myfirst, this->_Mylast);
  1417. this->_Getal().deallocate(this->_Myfirst,
  1418. this->_Myend - this->_Myfirst);
  1419. }
  1420.  
  1421. this->_Orphan_all();
  1422. this->_Myend = _Newvec + _Capacity;
  1423. this->_Mylast = _Newvec + _Count;
  1424. this->_Myfirst = _Newvec;
  1425. }
  1426. else
  1427. { // new stuff fits, append and rotate into place
  1428. _Ucopy(_First, _Last, this->_Mylast);
  1429. _STD rotate(_VIPTR(_Where), this->_Mylast,
  1430. this->_Mylast + _Count);
  1431. this->_Mylast += _Count;
  1432. _Orphan_range(_VIPTR(_Where), this->_Mylast);
  1433. }
  1434. }
  1435.  
  1436. #if _ITERATOR_DEBUG_LEVEL == 2
  1437. iterator erase(const_iterator _Where)
  1438. { // erase element at where
  1439. if (_VICONT(_Where) != this
  1440. || _VIPTR(_Where) < this->_Myfirst
  1441. || this->_Mylast <= _VIPTR(_Where))
  1442. _DEBUG_ERROR("vector erase iterator outside range");
  1443. _Move(_VIPTR(_Where) + 1, this->_Mylast, _VIPTR(_Where));
  1444. _Destroy(this->_Mylast - 1, this->_Mylast);
  1445. _Orphan_range(_VIPTR(_Where), this->_Mylast);
  1446. --this->_Mylast;
  1447. return (_Make_iter(_Where));
  1448. }
  1449.  
  1450. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  1451. iterator erase(const_iterator _Where)
  1452. { // erase element at where
  1453. _Move(_VIPTR(_Where) + 1, this->_Mylast,
  1454. _VIPTR(_Where));
  1455. _Destroy(this->_Mylast - 1, this->_Mylast);
  1456. --this->_Mylast;
  1457. return (_Make_iter(_Where));
  1458. }
  1459. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1460.  
  1461. iterator erase(const_iterator _First_arg,
  1462. const_iterator _Last_arg)
  1463. { // erase [_First, _Last)
  1464. if (_First_arg == begin() && _Last_arg == end())
  1465. clear();
  1466. else if (_First_arg != _Last_arg)
  1467. { // clear partial
  1468. iterator _First = _Make_iter(_First_arg);
  1469. iterator _Last = _Make_iter(_Last_arg);
  1470.  
  1471. if (_First != _Last)
  1472. { // worth doing, copy down over hole
  1473. #if _ITERATOR_DEBUG_LEVEL == 2
  1474. if (_Last < _First || _VICONT(_First) != this
  1475. || _VIPTR(_First) < this->_Myfirst
  1476. || this->_Mylast < _VIPTR(_Last))
  1477. _DEBUG_ERROR("vector erase iterator outside range");
  1478. pointer _Ptr = _Move(_VIPTR(_Last), this->_Mylast,
  1479. _VIPTR(_First));
  1480. _Orphan_range(_VIPTR(_First), this->_Mylast);
  1481.  
  1482. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  1483. pointer _Ptr = _Move(_VIPTR(_Last), this->_Mylast,
  1484. _VIPTR(_First));
  1485. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1486.  
  1487. _Destroy(_Ptr, this->_Mylast);
  1488. this->_Mylast = _Ptr;
  1489. }
  1490. }
  1491. return (_Make_iter(_First_arg));
  1492. }
  1493.  
  1494. void _Pop_back_n(size_type _Count)
  1495. { // erase _Count elements at end
  1496. pointer _Ptr = this->_Mylast - _Count;
  1497.  
  1498. #if _ITERATOR_DEBUG_LEVEL == 2
  1499. _Orphan_range(_Ptr, this->_Mylast);
  1500. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1501.  
  1502. _Destroy(_Ptr, this->_Mylast);
  1503. this->_Mylast = _Ptr;
  1504. }
  1505.  
  1506. void clear() _NOEXCEPT
  1507. { // erase all
  1508. this->_Orphan_all();
  1509. _Destroy(this->_Myfirst, this->_Mylast);
  1510. this->_Mylast = this->_Myfirst;
  1511. }
  1512.  
  1513. void swap(_Myt& _Right)
  1514. { // exchange contents with _Right
  1515. if (this == &_Right)
  1516. ; // same object, do nothing
  1517. else if (this->_Getal() == _Right._Getal())
  1518. { // same allocator, swap control information
  1519. this->_Swap_all(_Right);
  1520. _Swap_adl(this->_Myfirst, _Right._Myfirst);
  1521. _Swap_adl(this->_Mylast, _Right._Mylast);
  1522. _Swap_adl(this->_Myend, _Right._Myend);
  1523. }
  1524.  
  1525. else if (_Alty::propagate_on_container_swap::value)
  1526. { // swap allocators and control information
  1527. this->_Swap_alloc(_Right);
  1528. _Swap_adl(this->_Myfirst, _Right._Myfirst);
  1529. _Swap_adl(this->_Mylast, _Right._Mylast);
  1530. _Swap_adl(this->_Myend, _Right._Myend);
  1531. }
  1532.  
  1533. else
  1534. { // containers are incompatible
  1535. #if _ITERATOR_DEBUG_LEVEL == 2
  1536. _DEBUG_ERROR("vector containers incompatible for swap");
  1537.  
  1538. #else /* ITERATOR_DEBUG_LEVEL == 2 */
  1539. _XSTD terminate();
  1540. #endif /* ITERATOR_DEBUG_LEVEL == 2 */
  1541. }
  1542. }
  1543.  
  1544. protected:
  1545. bool _Buy(size_type _Capacity)
  1546. { // allocate array with _Capacity elements
  1547. this->_Myfirst = pointer();
  1548. this->_Mylast = pointer();
  1549. this->_Myend = pointer();
  1550.  
  1551. if (_Capacity == 0)
  1552. return (false);
  1553. else if (max_size() < _Capacity)
  1554. _Xlen(); // result too long
  1555. else
  1556. { // nonempty array, allocate storage
  1557. this->_Myfirst = this->_Getal().allocate(_Capacity);
  1558. this->_Mylast = this->_Myfirst;
  1559. this->_Myend = this->_Myfirst + _Capacity;
  1560. }
  1561. return (true);
  1562. }
  1563.  
  1564. void _Destroy(pointer _First, pointer _Last)
  1565. { // destroy [_First, _Last) using allocator
  1566. _Alty _Alval(this->_Getal());
  1567. _Destroy_range(_First, _Last, _Alval);
  1568. }
  1569.  
  1570. size_type _Grow_to(size_type _Count) const
  1571. { // grow by 50% or at least to _Count
  1572. size_type _Capacity = capacity();
  1573.  
  1574. _Capacity = max_size() - _Capacity / 2 < _Capacity
  1575. ? 0 : _Capacity + _Capacity / 2; // try to grow by 50%
  1576. if (_Capacity < _Count)
  1577. _Capacity = _Count;
  1578. return (_Capacity);
  1579. }
  1580.  
  1581. bool _Inside(const value_type *_Ptr) const
  1582. { // test if _Ptr points inside vector
  1583. return (_Ptr < this->_Mylast && this->_Myfirst <= _Ptr);
  1584. }
  1585.  
  1586. void _Reallocate(size_type _Count)
  1587. { // move to array of exactly _Count elements
  1588. pointer _Ptr = this->_Getal().allocate(_Count);
  1589.  
  1590. _TRY_BEGIN
  1591. _Umove(this->_Myfirst, this->_Mylast, _Ptr);
  1592. _CATCH_ALL
  1593. this->_Getal().deallocate(_Ptr, _Count);
  1594. _RERAISE;
  1595. _CATCH_END
  1596.  
  1597. size_type _Size = size();
  1598. if (this->_Myfirst != pointer())
  1599. { // destroy and deallocate old array
  1600. _Destroy(this->_Myfirst, this->_Mylast);
  1601. this->_Getal().deallocate(this->_Myfirst,
  1602. this->_Myend - this->_Myfirst);
  1603. }
  1604.  
  1605. this->_Orphan_all();
  1606. this->_Myend = _Ptr + _Count;
  1607. this->_Mylast = _Ptr + _Size;
  1608. this->_Myfirst = _Ptr;
  1609. }
  1610.  
  1611. void _Reserve(size_type _Count)
  1612. { // ensure room for _Count new elements, grow exponentially
  1613. if (_Unused_capacity() < _Count)
  1614. { // need more room, try to get it
  1615. if (max_size() - size() < _Count)
  1616. _Xlen();
  1617. _Reallocate(_Grow_to(size() + _Count));
  1618. }
  1619. }
  1620.  
  1621. void _Tidy()
  1622. { // free all storage
  1623. if (this->_Myfirst != pointer())
  1624. { // something to free, destroy and deallocate it
  1625. this->_Orphan_all();
  1626. _Destroy(this->_Myfirst, this->_Mylast);
  1627. this->_Getal().deallocate(this->_Myfirst,
  1628. this->_Myend - this->_Myfirst);
  1629. this->_Myfirst = pointer();
  1630. this->_Mylast = pointer();
  1631. this->_Myend = pointer();
  1632. }
  1633. }
  1634.  
  1635. template<class _Iter>
  1636. pointer _Ucopy(_Iter _First, _Iter _Last, pointer _Ptr)
  1637. { // copy initializing [_First, _Last), using allocator
  1638. _Alty _Alval(this->_Getal());
  1639. return (_Uninitialized_copy(_First, _Last,
  1640. _Ptr, _Alval));
  1641. }
  1642.  
  1643. template<class _Iter>
  1644. pointer _Umove(_Iter _First, _Iter _Last, pointer _Ptr)
  1645. { // move initializing [_First, _Last), using allocator
  1646. _Alty _Alval(this->_Getal());
  1647. return (_Uninitialized_move(_First, _Last,
  1648. _Ptr, _Alval));
  1649. }
  1650.  
  1651. iterator _Insert_n(const_iterator _Where,
  1652. size_type _Count, const value_type& _Val)
  1653. { // insert _Count * _Val at _Where
  1654. #if _ITERATOR_DEBUG_LEVEL == 2
  1655. if (_VICONT(_Where) != this
  1656. || _VIPTR(_Where) < this->_Myfirst
  1657. || this->_Mylast < _VIPTR(_Where))
  1658. _DEBUG_ERROR("vector insert iterator outside range");
  1659. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1660.  
  1661. size_type _Off = _VIPTR(_Where) - this->_Myfirst;
  1662. if (_Count == 0)
  1663. ;
  1664. else if (_Unused_capacity() < _Count)
  1665. { // not enough room, reallocate
  1666. if (max_size() - size() < _Count)
  1667. _Xlen(); // result too long
  1668.  
  1669. size_type _Capacity = _Grow_to(size() + _Count);
  1670. pointer _Newvec = this->_Getal().allocate(_Capacity);
  1671. size_type _Whereoff = _VIPTR(_Where) - this->_Myfirst;
  1672. int _Ncopied = 0;
  1673.  
  1674. _TRY_BEGIN
  1675. _Ufill(_Newvec + _Whereoff, _Count,
  1676. _STD addressof(_Val)); // add new stuff
  1677. ++_Ncopied;
  1678. _Umove(this->_Myfirst, _VIPTR(_Where),
  1679. _Newvec); // copy prefix
  1680. ++_Ncopied;
  1681. _Umove(_VIPTR(_Where), this->_Mylast,
  1682. _Newvec + (_Whereoff + _Count)); // copy suffix
  1683. _CATCH_ALL
  1684. if (1 < _Ncopied)
  1685. _Destroy(_Newvec, _Newvec + _Whereoff);
  1686. if (0 < _Ncopied)
  1687. _Destroy(_Newvec + _Whereoff, _Newvec + _Whereoff + _Count);
  1688. this->_Getal().deallocate(_Newvec, _Capacity);
  1689. _RERAISE;
  1690. _CATCH_END
  1691.  
  1692. _Count += size();
  1693. if (this->_Myfirst != pointer())
  1694. { // destroy and deallocate old array
  1695. _Destroy(this->_Myfirst, this->_Mylast);
  1696. this->_Getal().deallocate(this->_Myfirst,
  1697. this->_Myend - this->_Myfirst);
  1698. }
  1699.  
  1700. this->_Orphan_all();
  1701. this->_Myend = _Newvec + _Capacity;
  1702. this->_Mylast = _Newvec + _Count;
  1703. this->_Myfirst = _Newvec;
  1704. }
  1705. else if ((size_type)(this->_Mylast - _VIPTR(_Where))
  1706. < _Count)
  1707. { // new stuff spills off end
  1708. value_type _Tmp = _Val; // in case _Val is in sequence
  1709.  
  1710. _Umove(_VIPTR(_Where), this->_Mylast,
  1711. _VIPTR(_Where) + _Count); // copy suffix
  1712.  
  1713. _TRY_BEGIN
  1714. _Ufill(this->_Mylast,
  1715. _Count - (this->_Mylast - _VIPTR(_Where)),
  1716. _STD addressof(_Tmp)); // insert new stuff off end
  1717. _CATCH_ALL
  1718. _Destroy(_VIPTR(_Where) + _Count,
  1719. this->_Mylast + _Count);
  1720. _RERAISE;
  1721. _CATCH_END
  1722.  
  1723. this->_Mylast += _Count;
  1724. _Orphan_range(_VIPTR(_Where), this->_Mylast);
  1725. _STD fill(_VIPTR(_Where), this->_Mylast - _Count,
  1726. _Tmp); // insert up to old end
  1727. }
  1728. else
  1729. { // new stuff can all be assigned
  1730. value_type _Tmp = _Val; // in case _Val is in sequence
  1731.  
  1732. pointer _Oldend = this->_Mylast;
  1733. this->_Mylast = _Umove(_Oldend - _Count, _Oldend,
  1734. this->_Mylast); // copy suffix
  1735.  
  1736. _Orphan_range(_VIPTR(_Where), this->_Mylast);
  1737. _Copy_backward(_VIPTR(_Where), _Oldend - _Count,
  1738. _Oldend); // copy hole
  1739. _STD fill(_VIPTR(_Where),
  1740. _VIPTR(_Where) + _Count, _Tmp); // insert into hole
  1741. }
  1742. return (begin() + _Off);
  1743. }
  1744.  
  1745. pointer _Ufill(pointer _Ptr, size_type _Count, const value_type *_Pval)
  1746. { // copy initializing _Count * _Val, using allocator
  1747. _Alty _Alval(this->_Getal());
  1748. _Uninitialized_fill_n(_Ptr, _Count, _Pval, _Alval);
  1749. return (_Ptr + _Count);
  1750. }
  1751.  
  1752. __declspec(noreturn) void _Xlen() const
  1753. { // report a length_error
  1754. _Xlength_error("vector<T> too long");
  1755. }
  1756.  
  1757. __declspec(noreturn) void _Xran() const
  1758. { // report an out_of_range error
  1759. _Xout_of_range("invalid vector<T> subscript");
  1760. }
  1761.  
  1762. #if _VECTOR_ORPHAN_RANGE
  1763. void _Orphan_range(pointer _First, pointer _Last) const
  1764. { // orphan iterators within specified (inclusive) range
  1765. _Lockit _Lock(_LOCK_DEBUG);
  1766. const_iterator **_Pnext = (const_iterator **)this->_Getpfirst();
  1767. if (_Pnext != 0)
  1768. while (*_Pnext != 0)
  1769. if ((*_Pnext)->_Ptr < _First || _Last < (*_Pnext)->_Ptr)
  1770. _Pnext = (const_iterator **)(*_Pnext)->_Getpnext();
  1771. else
  1772. { // orphan the iterator
  1773. (*_Pnext)->_Clrcont();
  1774. *_Pnext = *(const_iterator **)(*_Pnext)->_Getpnext();
  1775. }
  1776. }
  1777.  
  1778. #else /* _VECTOR_ORPHAN_RANGE */
  1779. void _Orphan_range(pointer, pointer) const
  1780. { // orphan iterators within specified (inclusive) range
  1781. }
  1782. #endif /* _VECTOR_ORPHAN_RANGE */
  1783. };
  1784.  
  1785. // vector TEMPLATE OPERATORS
  1786.  
  1787. template<class _Ty,
  1788. class _Alloc> inline
  1789. void swap(vector<_Ty, _Alloc>& _Left, vector<_Ty, _Alloc>& _Right)
  1790. { // swap _Left and _Right vectors
  1791. _Left.swap(_Right);
  1792. }
  1793.  
  1794. template<class _Ty,
  1795. class _Alloc> inline
  1796. bool operator==(const vector<_Ty, _Alloc>& _Left,
  1797. const vector<_Ty, _Alloc>& _Right)
  1798. { // test for vector equality
  1799. return (_Left.size() == _Right.size()
  1800. && equal(_Left.begin(), _Left.end(), _Right.begin()));
  1801. }
  1802.  
  1803. template<class _Ty,
  1804. class _Alloc> inline
  1805. bool operator!=(const vector<_Ty, _Alloc>& _Left,
  1806. const vector<_Ty, _Alloc>& _Right)
  1807. { // test for vector inequality
  1808. return (!(_Left == _Right));
  1809. }
  1810.  
  1811. template<class _Ty,
  1812. class _Alloc> inline
  1813. bool operator<(const vector<_Ty, _Alloc>& _Left,
  1814. const vector<_Ty, _Alloc>& _Right)
  1815. { // test if _Left < _Right for vectors
  1816. return (lexicographical_compare(_Left.begin(), _Left.end(),
  1817. _Right.begin(), _Right.end()));
  1818. }
  1819.  
  1820. template<class _Ty,
  1821. class _Alloc> inline
  1822. bool operator>(const vector<_Ty, _Alloc>& _Left,
  1823. const vector<_Ty, _Alloc>& _Right)
  1824. { // test if _Left > _Right for vectors
  1825. return (_Right < _Left);
  1826. }
  1827.  
  1828. template<class _Ty,
  1829. class _Alloc> inline
  1830. bool operator<=(const vector<_Ty, _Alloc>& _Left,
  1831. const vector<_Ty, _Alloc>& _Right)
  1832. { // test if _Left <= _Right for vectors
  1833. return (!(_Right < _Left));
  1834. }
  1835.  
  1836. template<class _Ty,
  1837. class _Alloc> inline
  1838. bool operator>=(const vector<_Ty, _Alloc>& _Left,
  1839. const vector<_Ty, _Alloc>& _Right)
  1840. { // test if _Left >= _Right for vectors
  1841. return (!(_Left < _Right));
  1842. }
  1843.  
  1844. //
  1845. // TEMPLATE CLASS vector<bool, Alloc> AND FRIENDS
  1846. //
  1847. typedef unsigned int _Vbase; // word type for vector<bool> representation
  1848. const int _VBITS = 8 * sizeof (_Vbase); // at least CHAR_BITS bits per word
  1849.  
  1850. // CLASS _Vb_iter_base
  1851. template<class _Alloc>
  1852. class _Vb_iter_base
  1853. : public _Iterator012<random_access_iterator_tag,
  1854. _Bool,
  1855. typename _Alloc::difference_type,
  1856. bool *,
  1857. bool,
  1858. _Iterator_base>
  1859. { // store information common to reference and iterators
  1860. public:
  1861. typedef typename _Alloc::size_type _Sizet;
  1862. typedef vector<_Bool, _Alloc> _Mycont;
  1863.  
  1864. _Vb_iter_base()
  1865. : _Myptr(0), _Myoff(0)
  1866. { // construct with null pointer
  1867. }
  1868.  
  1869. _Vb_iter_base(const _Vbase *_Ptr, _Sizet _Off,
  1870. const _Container_base *_Mypvbool)
  1871. : _Myptr(_Ptr), _Myoff(_Off)
  1872. { // construct with offset and pointer
  1873. this->_Adopt(_Mypvbool);
  1874. }
  1875.  
  1876. void _Advance(_Sizet _Off)
  1877. { // advance iterator by _Off
  1878. _Myoff += _Off;
  1879. _Myptr += _Myoff / _VBITS;
  1880. _Myoff %= _VBITS;
  1881. }
  1882.  
  1883. int _Valid(_Sizet _Inc) const
  1884. { // test for valid incremented offset
  1885. #if _ITERATOR_DEBUG_LEVEL == 2
  1886. _Sizet _Mysize = ((_Mycont *)this->_Getcont())->_Mysize;
  1887.  
  1888. _Inc += _Myoff;
  1889. _Inc += _VBITS * (_Myptr
  1890. - (((_Mycont *)this->_Getcont())->_Myvec)._Myfirst);
  1891. return (_Inc < _Mysize ? -1 : _Inc == _Mysize ? 0 : +1);
  1892.  
  1893. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  1894.  
  1895. return (-1);
  1896. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  1897. }
  1898.  
  1899. const _Vbase *_Myptr;
  1900. _Sizet _Myoff;
  1901. };
  1902.  
  1903. // CLASS _Vb_reference
  1904. template<class _Alloc>
  1905. class _Vb_reference
  1906. : public _Vb_iter_base<_Alloc>
  1907. { // reference to a bit within a base word
  1908. typedef _Vb_iter_base<_Alloc> _Mybase;
  1909. typedef _Vb_reference<_Alloc> _Mytype;
  1910.  
  1911. _Vb_reference() _NOEXCEPT
  1912. { // construct with null pointer (private)
  1913. }
  1914.  
  1915. public:
  1916. _Vb_reference(const _Mybase& _Right)
  1917. : _Mybase(_Right._Myptr, _Right._Myoff, _Right._Getcont())
  1918. { // construct with base
  1919. }
  1920.  
  1921. _Mytype& operator=(const _Mytype& _Right) _NOEXCEPT
  1922. { // assign _Vb_reference _Right to bit
  1923. return (*this = bool(_Right));
  1924. }
  1925.  
  1926. _Mytype& operator=(bool _Val) _NOEXCEPT
  1927. { // assign _Val to bit
  1928. if (_Val)
  1929. *(_Vbase *)_Getptr() |= _Mask();
  1930. else
  1931. *(_Vbase *)_Getptr() &= (~_Mask()); // STET
  1932. return (*this);
  1933. }
  1934.  
  1935. void flip() _NOEXCEPT
  1936. { // toggle the bit
  1937. *(_Vbase *)_Getptr() ^= _Mask();
  1938. }
  1939.  
  1940. operator bool() const _NOEXCEPT
  1941. { // test if bit is set
  1942. return ((*_Getptr() & _Mask()) != 0);
  1943. }
  1944.  
  1945. const _Vbase *_Getptr() const
  1946. { // get pointer to base word
  1947. #if _ITERATOR_DEBUG_LEVEL == 2
  1948. if (this->_Getcont() == 0
  1949. || this->_Myptr == 0
  1950. || 0 <= this->_Valid(0))
  1951. { // report error
  1952. _DEBUG_ERROR("vector<bool> iterator not dereferencable");
  1953. _SCL_SECURE_OUT_OF_RANGE;
  1954. }
  1955.  
  1956. #elif _ITERATOR_DEBUG_LEVEL == 1
  1957. _SCL_SECURE_VALIDATE(this->_Getcont() != 0 && this->_Myptr != 0);
  1958. _SCL_SECURE_VALIDATE_RANGE(this->_Valid(0) < 0);
  1959. #endif /* _ITERATOR_DEBUG_LEVEL */
  1960.  
  1961. return (this->_Myptr);
  1962. }
  1963.  
  1964. protected:
  1965. _Vbase _Mask() const
  1966. { // convert offset to mask
  1967. return ((_Vbase)(1 << this->_Myoff));
  1968. }
  1969. };
  1970.  
  1971. template<class _Alloc> inline
  1972. void swap(_Vb_reference<_Alloc> _Left,
  1973. _Vb_reference<_Alloc> _Right)
  1974. { // swap _Left and _Right vector<bool> elements
  1975. bool _Val = _Left; // NOT _STD swap
  1976. _Left = _Right;
  1977. _Right = _Val;
  1978. }
  1979.  
  1980. // CLASS _Vb_const_iterator
  1981. template<class _Alloc>
  1982. class _Vb_const_iterator
  1983. : public _Vb_iter_base<_Alloc>
  1984. { // iterator for nonmutable vector<bool>
  1985. public:
  1986. typedef _Vb_iter_base<_Alloc> _Mybase;
  1987. typedef _Vb_const_iterator<_Alloc> _Mytype;
  1988.  
  1989. typedef _Vb_reference<_Alloc> _Reft;
  1990. typedef bool const_reference;
  1991.  
  1992. typedef random_access_iterator_tag iterator_category;
  1993. typedef _Bool value_type;
  1994. typedef typename _Alloc::size_type size_type;
  1995. typedef typename _Alloc::difference_type difference_type;
  1996. typedef const_reference *pointer;
  1997. typedef const_reference reference;
  1998.  
  1999. _Vb_const_iterator()
  2000. { // construct with null reference
  2001. }
  2002.  
  2003. _Vb_const_iterator(const _Vbase *_Ptr, const _Container_base *_Mypvbool)
  2004. : _Mybase(_Ptr, 0, _Mypvbool)
  2005. { // construct with offset and pointer
  2006. }
  2007.  
  2008. const_reference operator*() const
  2009. { // return (reference to) designated object
  2010. return (_Reft(*this));
  2011. }
  2012.  
  2013. _Mytype& operator++()
  2014. { // preincrement
  2015. _Inc();
  2016. return (*this);
  2017. }
  2018.  
  2019. _Mytype operator++(int)
  2020. { // postincrement
  2021. _Mytype _Tmp = *this;
  2022. ++*this;
  2023. return (_Tmp);
  2024. }
  2025.  
  2026. _Mytype& operator--()
  2027. { // predecrement
  2028. _Dec();
  2029. return (*this);
  2030. }
  2031.  
  2032. _Mytype operator--(int)
  2033. { // postdecrement
  2034. _Mytype _Tmp = *this;
  2035. --*this;
  2036. return (_Tmp);
  2037. }
  2038.  
  2039. _Mytype& operator+=(difference_type _Off)
  2040. { // increment by integer
  2041. if (_Off < 0 && this->_Myoff < 0 - (size_type)_Off)
  2042. { /* add negative increment */
  2043. this->_Myoff += _Off;
  2044. this->_Myptr -= 1 + ((size_type)(-1) - this->_Myoff) / _VBITS;
  2045. this->_Myoff %= _VBITS;
  2046. }
  2047. else
  2048. { /* add non-negative increment */
  2049. this->_Myoff += _Off;
  2050. this->_Myptr += this->_Myoff / _VBITS;
  2051. this->_Myoff %= _VBITS;
  2052. }
  2053. return (*this);
  2054. }
  2055.  
  2056. _Mytype operator+(difference_type _Off) const
  2057. { // return this + integer
  2058. _Mytype _Tmp = *this;
  2059. return (_Tmp += _Off);
  2060. }
  2061.  
  2062. _Mytype& operator-=(difference_type _Off)
  2063. { // decrement by integer
  2064. return (*this += -_Off);
  2065. }
  2066.  
  2067. _Mytype operator-(difference_type _Off) const
  2068. { // return this - integer
  2069. _Mytype _Tmp = *this;
  2070. return (_Tmp -= _Off);
  2071. }
  2072.  
  2073. difference_type operator-(
  2074. const _Mytype& _Right) const
  2075. { // return difference of iterators
  2076. _Compat(_Right);
  2077. return (_VBITS * (this->_Myptr - _Right._Myptr)
  2078. + (difference_type)this->_Myoff
  2079. - (difference_type)_Right._Myoff);
  2080. }
  2081.  
  2082. const_reference operator[](difference_type _Off) const
  2083. { // subscript
  2084. return (*(*this + _Off));
  2085. }
  2086.  
  2087. bool operator==(const _Mytype& _Right) const
  2088. { // test for iterator equality
  2089. _Compat(_Right);
  2090. return (this->_Myptr == _Right._Myptr
  2091. && this->_Myoff == _Right._Myoff);
  2092. }
  2093.  
  2094. bool operator!=(const _Mytype& _Right) const
  2095. { // test for iterator inequality
  2096. return (!(*this == _Right));
  2097. }
  2098.  
  2099. bool operator<(const _Mytype& _Right) const
  2100. { // test if this < _Right
  2101. _Compat(_Right);
  2102. return (this->_Myptr < _Right._Myptr
  2103. || (this->_Myptr == _Right._Myptr
  2104. && this->_Myoff < _Right._Myoff));
  2105. }
  2106.  
  2107. bool operator>(const _Mytype& _Right) const
  2108. { // test if this > _Right
  2109. return (_Right < *this);
  2110. }
  2111.  
  2112. bool operator<=(const _Mytype& _Right) const
  2113. { // test if this <= _Right
  2114. return (!(_Right < *this));
  2115. }
  2116.  
  2117. bool operator>=(const _Mytype& _Right) const
  2118. { // test if this >= _Right
  2119. return (!(*this < _Right));
  2120. }
  2121.  
  2122. #if _ITERATOR_DEBUG_LEVEL == 2
  2123. void _Compat(const _Mytype& _Right) const
  2124. { // test for compatible iterator pair
  2125. if (this->_Getcont() == 0
  2126. || this->_Getcont() != _Right._Getcont())
  2127. _DEBUG_ERROR("vector<bool> iterators incompatible");
  2128. }
  2129.  
  2130. #elif _ITERATOR_DEBUG_LEVEL == 1
  2131. void _Compat(const _Mytype& _Right) const
  2132. { // test for compatible iterator pair
  2133. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  2134. _SCL_SECURE_VALIDATE_RANGE(this->_Getcont() == _Right._Getcont());
  2135. }
  2136.  
  2137. #else /* _ITERATOR_DEBUG_LEVEL == 0 */
  2138. void _Compat(const _Mytype&) const
  2139. { // test for compatible iterator pair
  2140. }
  2141. #endif /* _ITERATOR_DEBUG_LEVEL */
  2142.  
  2143. void _Dec()
  2144. { // decrement bit position
  2145. if (this->_Myoff != 0)
  2146. --this->_Myoff;
  2147. else
  2148. { // move to previous word
  2149. #if _ITERATOR_DEBUG_LEVEL == 2
  2150. if (this->_Getcont() == 0 || 0 < this->_Valid((size_type)-1))
  2151. { // report error
  2152. _DEBUG_ERROR("vector<bool> iterator not decrementable");
  2153. _SCL_SECURE_OUT_OF_RANGE;
  2154. }
  2155.  
  2156. #elif _ITERATOR_DEBUG_LEVEL == 1
  2157. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  2158. _SCL_SECURE_VALIDATE_RANGE(this->_Valid((size_type)-1) <= 0);
  2159. #endif /* _ITERATOR_DEBUG_LEVEL */
  2160.  
  2161. this->_Myoff = _VBITS - 1;
  2162. --this->_Myptr;
  2163. }
  2164. }
  2165.  
  2166. void _Inc()
  2167. { // increment bit position
  2168. if (this->_Myoff < _VBITS - 1)
  2169. ++this->_Myoff;
  2170. else
  2171. { // move to next word
  2172. #if _ITERATOR_DEBUG_LEVEL == 2
  2173. if (this->_Getcont() == 0 || 0 < this->_Valid(1))
  2174. { // report error
  2175. _DEBUG_ERROR("vector<bool> iterator not incrementable");
  2176. _SCL_SECURE_OUT_OF_RANGE;
  2177. }
  2178.  
  2179. #elif _ITERATOR_DEBUG_LEVEL == 1
  2180. _SCL_SECURE_VALIDATE(this->_Getcont() != 0);
  2181. _SCL_SECURE_VALIDATE_RANGE(this->_Valid(1) <= 0);
  2182. #endif /* _ITERATOR_DEBUG_LEVEL */
  2183.  
  2184. this->_Myoff = 0;
  2185. ++this->_Myptr;
  2186. }
  2187. }
  2188. };
  2189.  
  2190. template<class _Alloc> inline
  2191. _Vb_const_iterator<_Alloc> operator+(
  2192. typename _Alloc::difference_type _Off,
  2193. _Vb_const_iterator<_Alloc> _Right)
  2194. { // return _Right + integer
  2195. return (_Right += _Off);
  2196. }
  2197.  
  2198. template<class _Alloc>
  2199. struct _Is_checked_helper<_Vb_const_iterator<_Alloc> >
  2200. : public true_type
  2201. { // mark _Vb_const_iterator as checked
  2202. };
  2203.  
  2204. // CLASS _Vb_iterator
  2205. template<class _Alloc>
  2206. class _Vb_iterator
  2207. : public _Vb_const_iterator<_Alloc>
  2208. { // iterator for mutable vector<bool>
  2209. public:
  2210. typedef _Vb_const_iterator<_Alloc> _Mybase;
  2211. typedef _Vb_iterator<_Alloc> _Mytype;
  2212.  
  2213. typedef _Vb_reference<_Alloc> _Reft;
  2214. typedef bool const_reference;
  2215.  
  2216. typedef random_access_iterator_tag iterator_category;
  2217. typedef _Bool value_type;
  2218. typedef typename _Alloc::size_type size_type;
  2219. typedef typename _Alloc::difference_type difference_type;
  2220. typedef _Reft *pointer;
  2221. typedef _Reft reference;
  2222.  
  2223. _Vb_iterator()
  2224. { // construct with null reference
  2225. }
  2226.  
  2227. _Vb_iterator(_Vbase *_Ptr, _Container_base *_Mypvbool)
  2228. : _Mybase(_Ptr, _Mypvbool)
  2229. { // construct with offset and pointer
  2230. }
  2231.  
  2232. reference operator*() const
  2233. { // return (reference to) designated object
  2234. return (_Reft(*this));
  2235. }
  2236.  
  2237. _Mytype& operator++()
  2238. { // preincrement
  2239. ++*(_Mybase *)this;
  2240. return (*this);
  2241. }
  2242.  
  2243. _Mytype operator++(int)
  2244. { // postincrement
  2245. _Mytype _Tmp = *this;
  2246. ++*this;
  2247. return (_Tmp);
  2248. }
  2249.  
  2250. _Mytype& operator--()
  2251. { // predecrement
  2252. --*(_Mybase *)this;
  2253. return (*this);
  2254. }
  2255.  
  2256. _Mytype operator--(int)
  2257. { // postdecrement
  2258. _Mytype _Tmp = *this;
  2259. --*this;
  2260. return (_Tmp);
  2261. }
  2262.  
  2263. _Mytype& operator+=(difference_type _Off)
  2264. { // increment by integer
  2265. *(_Mybase *)this += _Off;
  2266. return (*this);
  2267. }
  2268.  
  2269. _Mytype operator+(difference_type _Off) const
  2270. { // return this + integer
  2271. _Mytype _Tmp = *this;
  2272. return (_Tmp += _Off);
  2273. }
  2274.  
  2275. _Mytype& operator-=(difference_type _Off)
  2276. { // decrement by integer
  2277. return (*this += -_Off);
  2278. }
  2279.  
  2280. _Mytype operator-(difference_type _Off) const
  2281. { // return this - integer
  2282. _Mytype _Tmp = *this;
  2283. return (_Tmp -= _Off);
  2284. }
  2285.  
  2286. difference_type operator-(const _Mybase& _Right) const
  2287. { // return difference of iterators
  2288. return (*(_Mybase *)this - _Right);
  2289. }
  2290.  
  2291. reference operator[](difference_type _Off) const
  2292. { // subscript
  2293. return (*(*this + _Off));
  2294. }
  2295. };
  2296.  
  2297. template<class _Alloc> inline
  2298. _Vb_iterator<_Alloc> operator+(typename _Alloc::difference_type _Off,
  2299. _Vb_iterator<_Alloc> _Right)
  2300. { // return _Right + integer
  2301. return (_Right += _Off);
  2302. }
  2303.  
  2304. template<class _Alloc>
  2305. struct _Is_checked_helper<_Vb_iterator<_Alloc> >
  2306. : public true_type
  2307. { // mark _Vb_iterator as checked
  2308. };
  2309.  
  2310. // TEMPLATE CLASS _Vb_val
  2311. template<class _Alloc>
  2312. class _Vb_val
  2313. : public _Container_base
  2314. { // base class for vector<bool> to hold data
  2315. public:
  2316. typedef _STD vector<_Vbase, _Alloc> _Vectype;
  2317. typedef typename _Vectype::_Alty _Alty;
  2318. typedef typename _Alty::size_type size_type;
  2319.  
  2320. _Vb_val(size_type _Count, const bool& _Val, const _Alloc& _Al = _Alloc())
  2321. : _Myvec(_Nw(_Count), (_Vbase)(_Val ? -1 : 0), _Al)
  2322. { // construct _Count * _Val elements with allocator _Al
  2323. _Alloc_proxy();
  2324. _Mysize = 0;
  2325. }
  2326.  
  2327. _Vb_val(const _Vb_val& _Right)
  2328. : _Myvec(_Right._Myvec),
  2329. _Mysize(_Right._Mysize)
  2330. { // copy construct
  2331. _Alloc_proxy();
  2332. }
  2333.  
  2334. _Vb_val(const _Vb_val& _Right, const _Alloc& _Al)
  2335. : _Myvec(_Right._Myvec, _Al),
  2336. _Mysize(_Right._Mysize)
  2337. { // copy construct, allocator
  2338. _Alloc_proxy();
  2339. }
  2340.  
  2341. _Vb_val(_Vb_val&& _Right)
  2342. : _Myvec(_STD forward<_Vectype>(_Right._Myvec)),
  2343. _Mysize(_Right._Mysize)
  2344. { // move construct
  2345. _Right._Mysize = 0;
  2346. _Alloc_proxy();
  2347. }
  2348.  
  2349. _Vb_val(_Vb_val&& _Right, const _Alloc& _Al)
  2350. : _Myvec(_STD forward<_Vectype>(_Right._Myvec), _Al),
  2351. _Mysize(_Right._Mysize)
  2352. { // move construct, allocator
  2353. _Right._Mysize = 0;
  2354. _Alloc_proxy();
  2355. }
  2356.  
  2357. ~_Vb_val() _NOEXCEPT
  2358. { // destroy proxy
  2359. _Free_proxy();
  2360. }
  2361.  
  2362. #if _ITERATOR_DEBUG_LEVEL == 0
  2363. void _Swap_alloc(_Vb_val&)
  2364. { // do nothing
  2365. }
  2366.  
  2367. void _Alloc_proxy()
  2368. { // do nothing
  2369. }
  2370.  
  2371. void _Free_proxy()
  2372. { // do nothing
  2373. }
  2374.  
  2375. #else /* _ITERATOR_DEBUG_LEVEL == 0 */
  2376. void _Swap_alloc(_Vb_val& _Right)
  2377. { // swap proxies to follow allocators in vector
  2378. _Swap_adl(this->_Myproxy, _Right._Myproxy);
  2379. }
  2380.  
  2381. void _Alloc_proxy()
  2382. { // allocate a proxy
  2383. typename _Alty::template rebind<_Container_proxy>::other
  2384. _Alproxy(_Myvec.get_allocator());
  2385. this->_Myproxy = _Alproxy.allocate(1);
  2386. _Alproxy.construct(this->_Myproxy, _Container_proxy());
  2387. this->_Myproxy->_Mycont = this;
  2388. }
  2389.  
  2390. void _Free_proxy()
  2391. { // destroy proxy
  2392. typename _Alty::template rebind<_Container_proxy>::other
  2393. _Alproxy(_Myvec.get_allocator());
  2394. this->_Orphan_all();
  2395. _Alproxy.destroy(this->_Myproxy);
  2396. _Alproxy.deallocate(this->_Myproxy, 1);
  2397. this->_Myproxy = 0;
  2398. }
  2399. #endif /* _ITERATOR_DEBUG_LEVEL == 0 */
  2400.  
  2401. static size_type _Nw(size_type _Count)
  2402. { // return number of base words from number of bits
  2403. return ((_Count + _VBITS - 1) / _VBITS);
  2404. }
  2405.  
  2406. _Vectype _Myvec; // base vector of words
  2407. typename _Alty::size_type _Mysize; // current length of sequence
  2408. };
  2409.  
  2410. // CLASS vector<bool>
  2411.  
  2412. template<class _Alloc>
  2413. class vector<_Bool, _Alloc>
  2414. : public _Vb_val<_Alloc>
  2415. { // varying size array of bits
  2416. public:
  2417. typedef _STD vector<_Bool, _Alloc> _Myt;
  2418. typedef _Vb_val<_Alloc> _Mybase;
  2419. typedef typename _Mybase::_Alty _Alty;
  2420. typedef typename _Mybase::_Vectype _Vectype;
  2421.  
  2422. typedef typename _Alty::size_type size_type;
  2423. typedef typename _Alty::difference_type difference_type;
  2424. typedef _Bool _Ty;
  2425. typedef _Alloc allocator_type;
  2426.  
  2427. typedef _Vb_reference<_Alty> reference;
  2428. typedef bool const_reference;
  2429. typedef bool value_type;
  2430.  
  2431. typedef reference _Reft;
  2432. typedef _Vb_const_iterator<_Alty> const_iterator;
  2433. typedef _Vb_iterator<_Alty> iterator;
  2434.  
  2435. typedef iterator pointer;
  2436. typedef const_iterator const_pointer;
  2437. typedef _STD reverse_iterator<iterator> reverse_iterator;
  2438. typedef _STD reverse_iterator<const_iterator> const_reverse_iterator;
  2439.  
  2440. static const int _VBITS = _STD _VBITS;
  2441. enum {_EEN_VBITS = _VBITS}; // helper for expression evaluator
  2442. vector()
  2443. : _Mybase(0, false)
  2444. { // construct empty vector
  2445. }
  2446.  
  2447. explicit vector(const _Alloc& _Al)
  2448. : _Mybase(0, false, _Al)
  2449. { // construct empty vector, allocator
  2450. }
  2451.  
  2452. explicit vector(size_type _Count, const bool& _Val = false)
  2453. : _Mybase(_Count, _Val)
  2454. { // construct from _Count * _Val
  2455. _Trim(_Count);
  2456. }
  2457.  
  2458. vector(size_type _Count, const bool& _Val, const _Alloc& _Al)
  2459. : _Mybase(_Count, _Val, _Al)
  2460. { // construct from _Count * _Val, allocator
  2461. _Trim(_Count);
  2462. }
  2463.  
  2464. vector(const _Myt& _Right)
  2465. : _Mybase(_Right)
  2466. { // construct by copying _Right
  2467. }
  2468.  
  2469. vector(const _Myt& _Right, const _Alloc& _Al)
  2470. : _Mybase(_Right, _Al)
  2471. { // construct by copying _Right, allocator
  2472. }
  2473.  
  2474. template<class _Iter,
  2475. class = typename enable_if<_Is_iterator<_Iter>::value,
  2476. void>::type>
  2477. vector(_Iter _First, _Iter _Last)
  2478. : _Mybase(0, false)
  2479. { // construct from [_First, _Last)
  2480. _BConstruct(_First, _Last);
  2481. }
  2482.  
  2483. template<class _Iter,
  2484. class = typename enable_if<_Is_iterator<_Iter>::value,
  2485. void>::type>
  2486. vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
  2487. : _Mybase(0, false, _Al)
  2488. { // construct from [_First, _Last), allocator
  2489. _BConstruct(_First, _Last);
  2490. }
  2491.  
  2492. template<class _Iter>
  2493. void _BConstruct(_Iter _First, _Iter _Last)
  2494. { // initialize from [_First, _Last), input iterators
  2495. insert(begin(), _First, _Last);
  2496. }
  2497.  
  2498. vector(_Myt&& _Right)
  2499. : _Mybase(_STD forward<_Myt>(_Right))
  2500. { // move construct by moving _Right
  2501. }
  2502.  
  2503. vector(_Myt&& _Right, const _Alloc& _Al)
  2504. : _Mybase(_STD forward<_Myt>(_Right), _Al)
  2505. { // move construct by moving _Right, allocator
  2506. }
  2507.  
  2508. _Myt& operator=(_Myt&& _Right)
  2509. { // assign by moving _Right
  2510. if (this != &_Right)
  2511. { // different, assign it
  2512. clear();
  2513.  
  2514. if (_Alty::propagate_on_container_move_assignment::value
  2515. && this->get_allocator() != _Right.get_allocator())
  2516. { // assign vector, dumping proxy
  2517. this->_Free_proxy();
  2518. this->_Myvec = _STD move(_Right._Myvec);
  2519. this->_Alloc_proxy();
  2520. }
  2521. else
  2522. this->_Myvec = _STD move(_Right._Myvec);
  2523.  
  2524.  
  2525. this->_Mysize = _Right._Mysize;
  2526. _Right._Mysize = 0;
  2527. }
  2528. return (*this);
  2529. }
  2530.  
  2531. vector(_XSTD initializer_list<bool> _Ilist,
  2532. const _Alloc& _Al = allocator_type())
  2533. : _Mybase(0, false, _Al)
  2534. { // construct from initializer_list
  2535. insert(begin(), _Ilist.begin(), _Ilist.end());
  2536. }
  2537.  
  2538. _Myt& operator=(_XSTD initializer_list<bool> _Ilist)
  2539. { // assign initializer_list
  2540. assign(_Ilist.begin(), _Ilist.end());
  2541. return (*this);
  2542. }
  2543.  
  2544. void assign(_XSTD initializer_list<bool> _Ilist)
  2545. { // assign initializer_list
  2546. assign(_Ilist.begin(), _Ilist.end());
  2547. }
  2548.  
  2549. iterator insert(const_iterator _Where,
  2550. _XSTD initializer_list<bool> _Ilist)
  2551. { // insert initializer_list
  2552. return (insert(_Where, _Ilist.begin(), _Ilist.end()));
  2553. }
  2554.  
  2555. ~vector() _NOEXCEPT
  2556. { // destroy the object
  2557. this->_Mysize = 0;
  2558. }
  2559.  
  2560. _Myt& operator=(const _Myt& _Right)
  2561. { // assign from _Right
  2562. this->_Mysize = _Right._Mysize;
  2563. this->_Myvec = _Right._Myvec;
  2564. return (*this);
  2565. }
  2566.  
  2567. void reserve(size_type _Count)
  2568. { // determine new minimum length of allocated storage
  2569. this->_Myvec.reserve(this->_Nw(_Count));
  2570. }
  2571.  
  2572. size_type capacity() const _NOEXCEPT
  2573. { // return current length of allocated storage
  2574. return (this->_Myvec.capacity() * _VBITS);
  2575. }
  2576.  
  2577. iterator begin() _NOEXCEPT
  2578. { // return iterator for beginning of mutable sequence
  2579. return (iterator((_Vbase *)this->_Myvec._Myfirst, this));
  2580. }
  2581.  
  2582. const_iterator begin() const _NOEXCEPT
  2583. { // return iterator for beginning of nonmutable sequence
  2584. return (const_iterator((_Vbase *)this->_Myvec._Myfirst, this));
  2585. }
  2586.  
  2587. iterator end() _NOEXCEPT
  2588. { // return iterator for end of mutable sequence
  2589. iterator _Tmp = begin();
  2590. if (0 < this->_Mysize)
  2591. _Tmp += this->_Mysize;
  2592. return (_Tmp);
  2593. }
  2594.  
  2595. const_iterator end() const _NOEXCEPT
  2596. { // return iterator for end of nonmutable sequence
  2597. const_iterator _Tmp = begin();
  2598. if (0 < this->_Mysize)
  2599. _Tmp += this->_Mysize;
  2600. return (_Tmp);
  2601. }
  2602.  
  2603. const_iterator cbegin() const _NOEXCEPT
  2604. { // return iterator for beginning of nonmutable sequence
  2605. return (((const _Myt *)this)->begin());
  2606. }
  2607.  
  2608. const_iterator cend() const _NOEXCEPT
  2609. { // return iterator for end of nonmutable sequence
  2610. return (((const _Myt *)this)->end());
  2611. }
  2612.  
  2613. const_reverse_iterator crbegin() const _NOEXCEPT
  2614. { // return iterator for beginning of reversed nonmutable sequence
  2615. return (((const _Myt *)this)->rbegin());
  2616. }
  2617.  
  2618. const_reverse_iterator crend() const _NOEXCEPT
  2619. { // return iterator for end of reversed nonmutable sequence
  2620. return (((const _Myt *)this)->rend());
  2621. }
  2622.  
  2623. void shrink_to_fit()
  2624. { // reduce capacity
  2625. if (this->_Myvec._Has_unused_capacity())
  2626. { // worth shrinking, do it
  2627. _Myt _Tmp(*this);
  2628. swap(_Tmp);
  2629. }
  2630. }
  2631.  
  2632. iterator _Make_iter(const_iterator _Where)
  2633. { // make iterator from const_iterator
  2634. iterator _Tmp = begin();
  2635. if (0 < this->_Mysize)
  2636. _Tmp += _Where - begin();
  2637. return (_Tmp);
  2638. }
  2639.  
  2640. reverse_iterator rbegin() _NOEXCEPT
  2641. { // return iterator for beginning of reversed mutable sequence
  2642. return (reverse_iterator(end()));
  2643. }
  2644.  
  2645. const_reverse_iterator rbegin() const _NOEXCEPT
  2646. { // return iterator for beginning of reversed nonmutable sequence
  2647. return (const_reverse_iterator(end()));
  2648. }
  2649.  
  2650. reverse_iterator rend() _NOEXCEPT
  2651. { // return iterator for end of reversed mutable sequence
  2652. return (reverse_iterator(begin()));
  2653. }
  2654.  
  2655. const_reverse_iterator rend() const _NOEXCEPT
  2656. { // return iterator for end of reversed nonmutable sequence
  2657. return (const_reverse_iterator(begin()));
  2658. }
  2659.  
  2660. void resize(size_type _Newsize, bool _Val = false)
  2661. { // determine new length, padding with _Val elements as needed
  2662. if (size() < _Newsize)
  2663. _Insert_n(end(), _Newsize - size(), _Val);
  2664. else if (_Newsize < size())
  2665. erase(begin() + _Newsize, end());
  2666. }
  2667.  
  2668. size_type size() const _NOEXCEPT
  2669. { // return length of sequence
  2670. return (this->_Mysize);
  2671. }
  2672.  
  2673. size_type max_size() const _NOEXCEPT
  2674. { // return maximum possible length of sequence
  2675. const size_type _Maxsize = this->_Myvec.max_size();
  2676. return (_Maxsize < (size_type)(-1) / _VBITS
  2677. ? _Maxsize * _VBITS : (size_type)(-1));
  2678. }
  2679.  
  2680. bool empty() const _NOEXCEPT
  2681. { // test if sequence is empty
  2682. return (size() == 0);
  2683. }
  2684.  
  2685. _Alloc get_allocator() const _NOEXCEPT
  2686. { // return allocator object for values
  2687. return (this->_Myvec.get_allocator());
  2688. }
  2689.  
  2690. const_reference at(size_type _Off) const
  2691. { // subscript nonmutable sequence with checking
  2692. if (size() <= _Off)
  2693. _Xran();
  2694. return ((*this)[_Off]);
  2695. }
  2696.  
  2697. reference at(size_type _Off)
  2698. { // subscript mutable sequence with checking
  2699. if (size() <= _Off)
  2700. _Xran();
  2701. return ((*this)[_Off]);
  2702. }
  2703.  
  2704. const_reference operator[](size_type _Off) const
  2705. { // subscript nonmutable sequence
  2706. const_iterator _It = begin();
  2707. _It._Advance(_Off);
  2708. return (*_It);
  2709. }
  2710.  
  2711. reference operator[](size_type _Off)
  2712. { // subscript mutable sequence
  2713. iterator _It = begin();
  2714. _It._Advance(_Off);
  2715. return (*_It);
  2716. }
  2717.  
  2718. reference front()
  2719. { // return first element of mutable sequence
  2720. return (*begin());
  2721. }
  2722.  
  2723. const_reference front() const
  2724. { // return first element of nonmutable sequence
  2725. return (*begin());
  2726. }
  2727.  
  2728. reference back()
  2729. { // return last element of mutable sequence
  2730. return (*(end() - 1));
  2731. }
  2732.  
  2733. const_reference back() const
  2734. { // return last element of nonmutable sequence
  2735. return (*(end() - 1));
  2736. }
  2737.  
  2738. void push_back(const bool& _Val)
  2739. { // insert element at end
  2740. insert(end(), _Val);
  2741. }
  2742.  
  2743. void pop_back()
  2744. { // erase element at end
  2745. erase(end() - 1);
  2746. }
  2747.  
  2748. template<class _Iter>
  2749. typename enable_if<_Is_iterator<_Iter>::value,
  2750. void>::type
  2751. assign(_Iter _First, _Iter _Last)
  2752. { // assign [_First, _Last), input iterators
  2753. erase(begin(), end());
  2754. insert(begin(), _First, _Last);
  2755. }
  2756.  
  2757. void assign(size_type _Count, const bool& _Val)
  2758. { // assign _Count * _Val
  2759. erase(begin(), end());
  2760. _Insert_n(begin(), _Count, _Val);
  2761. }
  2762.  
  2763. iterator insert(const_iterator _Where, const bool& _Val)
  2764. { // insert _Val at _Where
  2765. return (_Insert_n(_Where, (size_type)1, _Val));
  2766. }
  2767.  
  2768. iterator insert(const_iterator _Where, size_type _Count,
  2769. const bool& _Val)
  2770. { // insert _Count * _Val at _Where
  2771. return (_Insert_n(_Where, _Count, _Val));
  2772. }
  2773.  
  2774. template<class _Iter>
  2775. typename enable_if<_Is_iterator<_Iter>::value,
  2776. iterator>::type
  2777. insert(const_iterator _Where, _Iter _First, _Iter _Last)
  2778. { // insert [_First, _Last) at _Where
  2779. size_type _Off = _Where - begin();
  2780. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  2781. return (begin() + _Off);
  2782. }
  2783.  
  2784. template<class _Iter>
  2785. void _Insert(const_iterator _Where, _Iter _First, _Iter _Last,
  2786. input_iterator_tag)
  2787. { // insert [_First, _Last) at _Where, input iterators
  2788. size_type _Off = _Where - begin();
  2789.  
  2790. for (; _First != _Last; ++_First, ++_Off)
  2791. insert(begin() + _Off, *_First);
  2792. }
  2793.  
  2794. template<class _Iter>
  2795. void _Insert(const_iterator _Where,
  2796. _Iter _First, _Iter _Last,
  2797. forward_iterator_tag)
  2798. { // insert [_First, _Last) at _Where, forward iterators
  2799. _DEBUG_RANGE(_First, _Last);
  2800. size_type _Count = 0;
  2801. _Distance(_First, _Last, _Count);
  2802.  
  2803. size_type _Off = _Insert_x(_Where, _Count);
  2804. _STD copy(_First, _Last, begin() + _Off);
  2805. }
  2806.  
  2807. iterator erase(const_iterator _Where_arg)
  2808. { // erase element at _Where
  2809. iterator _Where = _Make_iter(_Where_arg);
  2810. size_type _Off = _Where - begin();
  2811.  
  2812. #if _ITERATOR_DEBUG_LEVEL == 2
  2813. if (end() <= _Where)
  2814. _DEBUG_ERROR("vector<bool> erase iterator outside range");
  2815. _STD copy(_Where + 1, end(), _Where);
  2816. _Orphan_range(_Off, this->_Mysize);
  2817.  
  2818. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  2819. _STD copy(_Where + 1, end(), _Where);
  2820. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  2821.  
  2822. _Trim(this->_Mysize - 1);
  2823. return (begin() + _Off);
  2824. }
  2825.  
  2826. iterator erase(const_iterator _First_arg, const_iterator _Last_arg)
  2827. { // erase [_First, _Last)
  2828. iterator _First = _Make_iter(_First_arg);
  2829. iterator _Last = _Make_iter(_Last_arg);
  2830. size_type _Off = _First - begin();
  2831.  
  2832. if (_First != _Last)
  2833. { // worth doing, copy down over hole
  2834. #if _ITERATOR_DEBUG_LEVEL == 2
  2835. if (_Last < _First || end() < _Last)
  2836. _DEBUG_ERROR("vector<bool> erase iterator outside range");
  2837. iterator _Next = _STD copy(_Last, end(), _First);
  2838. size_type _Newsize = _Next - begin();
  2839. _Orphan_range(_Newsize, this->_Mysize);
  2840. _Trim(_Newsize);
  2841.  
  2842. #else /* _ITERATOR_DEBUG_LEVEL == 2 */
  2843. iterator _Next = _STD copy(_Last, end(), _First);
  2844. _Trim(_Next - begin());
  2845. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  2846. }
  2847. return (begin() + _Off);
  2848. }
  2849.  
  2850. void clear() _NOEXCEPT
  2851. { // erase all elements
  2852. erase(begin(), end());
  2853. }
  2854.  
  2855. void flip() _NOEXCEPT
  2856. { // toggle all elements
  2857. for (typename _Vectype::iterator _Next = this->_Myvec.begin();
  2858. _Next != this->_Myvec.end(); ++_Next)
  2859. *_Next = (_Vbase)~*_Next;
  2860. _Trim(this->_Mysize);
  2861. }
  2862.  
  2863. void swap(_Myt& _Right)
  2864. { // exchange contents with right
  2865. if (this == &_Right)
  2866. ; // same object, do nothing
  2867. else if (this->get_allocator() == _Right.get_allocator())
  2868. { // same allocator, swap control information
  2869. this->_Swap_all(_Right);
  2870. this->_Myvec.swap(_Right._Myvec);
  2871. _STD swap(this->_Mysize, _Right._Mysize);
  2872. }
  2873.  
  2874. else if (_Alty::propagate_on_container_swap::value)
  2875. { // swap allocators and control information
  2876. this->_Swap_alloc(_Right);
  2877. this->_Myvec.swap(_Right._Myvec);
  2878. _STD swap(this->_Mysize, _Right._Mysize);
  2879. }
  2880.  
  2881. else
  2882. { // containers are incompatible
  2883. #if _ITERATOR_DEBUG_LEVEL == 2
  2884. _DEBUG_ERROR("vector<bool> containers incompatible for swap");
  2885.  
  2886. #else /* ITERATOR_DEBUG_LEVEL == 2 */
  2887. _XSTD terminate();
  2888. #endif /* ITERATOR_DEBUG_LEVEL == 2 */
  2889. }
  2890. }
  2891.  
  2892. static void swap(reference _Left, reference _Right) _NOEXCEPT
  2893. { // swap _Left and _Right vector<bool> elements
  2894. bool _Val = _Left; // NOT _STD swap
  2895.  
  2896. _Left = _Right;
  2897. _Right = _Val;
  2898. }
  2899.  
  2900. size_t hash() const
  2901. { // hash bits to size_t value by pseudorandomizing transform
  2902. return (_Hash_seq((const unsigned char *)this->_Myvec.data(),
  2903. this->_Myvec.size() * sizeof (_Vbase)));
  2904. }
  2905.  
  2906. iterator _Insert_n(const_iterator _Where,
  2907. size_type _Count, const bool& _Val)
  2908. { // insert _Count * _Val at _Where
  2909. size_type _Off = _Insert_x(_Where, _Count);
  2910. _STD fill(begin() + _Off, begin() + (_Off + _Count), _Val);
  2911. return (begin() + _Off);
  2912. }
  2913.  
  2914. size_type _Insert_x(const_iterator _Where, size_type _Count)
  2915. { // make room to insert _Count elements at _Where
  2916. size_type _Off = _Where - begin();
  2917.  
  2918. #if _ITERATOR_DEBUG_LEVEL == 2
  2919. if (end() < _Where)
  2920. _DEBUG_ERROR("vector<bool> insert iterator outside range");
  2921. bool _Realloc = capacity() - size() < _Count;
  2922. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  2923.  
  2924. if (_Count == 0)
  2925. ;
  2926. else if (max_size() - size() < _Count)
  2927. _Xlen(); // result too long
  2928. else
  2929. { // worth doing
  2930. this->_Myvec.resize(this->_Nw(size() + _Count), 0);
  2931. if (empty())
  2932. this->_Mysize += _Count;
  2933. else
  2934. { // make room and copy down suffix
  2935. iterator _Oldend = end();
  2936. this->_Mysize += _Count;
  2937. _STD copy_backward(begin() + _Off, _Oldend, end());
  2938. }
  2939.  
  2940. #if _ITERATOR_DEBUG_LEVEL == 2
  2941. _Orphan_range(_Realloc ? 0 : _Off, this->_Mysize);
  2942. #endif /* _ITERATOR_DEBUG_LEVEL == 2 */
  2943. }
  2944. return (_Off);
  2945. }
  2946.  
  2947. #if _VECTOR_ORPHAN_RANGE
  2948. void _Orphan_range(size_type _Offlo, size_type _Offhi) const
  2949. { // orphan iterators within specified (inclusive) range
  2950. typedef _Vb_iter_base<_Alty> _Myiterbase;
  2951.  
  2952. _Lockit _Lock(_LOCK_DEBUG);
  2953. _Vbase *_Base = (_Vbase *)this->_Myvec._Myfirst;
  2954.  
  2955. const_iterator **_Pnext = (const_iterator **)this->_Getpfirst();
  2956. if (_Pnext != 0)
  2957. while (*_Pnext != 0)
  2958. { // test offset from beginning of vector
  2959. size_type _Off = _VBITS * ((*_Pnext)->_Myptr - _Base)
  2960. + (*_Pnext)->_Myoff;
  2961. if (_Off < _Offlo || _Offhi < _Off)
  2962. _Pnext = (const_iterator **)(*_Pnext)->_Getpnext();
  2963. else
  2964. { // orphan the iterator
  2965. (*_Pnext)->_Clrcont();
  2966. *_Pnext = *(const_iterator **)(*_Pnext)->_Getpnext();
  2967. }
  2968. }
  2969. }
  2970.  
  2971. #else /* _VECTOR_ORPHAN_RANGE */
  2972. void _Orphan_range(size_type, size_type) const
  2973. { // orphan iterators within specified (inclusive) range
  2974. }
  2975. #endif /* _VECTOR_ORPHAN_RANGE */
  2976.  
  2977. void _Trim(size_type _Size)
  2978. { // trim base vector to exact length in bits
  2979. if (max_size() < _Size)
  2980. _Xlen(); // result too long
  2981. size_type _Words = this->_Nw(_Size);
  2982.  
  2983. if (_Words < this->_Myvec.size())
  2984. this->_Myvec.erase(this->_Myvec.begin() + _Words,
  2985. this->_Myvec.end());
  2986. this->_Mysize = _Size;
  2987. _Size %= _VBITS;
  2988. if (0 < _Size)
  2989. this->_Myvec[_Words - 1] &= (_Vbase)((1 << _Size) - 1);
  2990. }
  2991.  
  2992. __declspec(noreturn) void _Xlen() const
  2993. { // report a length_error
  2994. _Xlength_error("vector<bool> too long");
  2995. }
  2996.  
  2997. __declspec(noreturn) void _Xran() const
  2998. { // report an out_of_range error
  2999. _Xout_of_range("invalid vector<bool> subscript");
  3000. }
  3001. };
  3002.  
  3003. template<class _Alloc> inline
  3004. bool operator==(const vector<bool, _Alloc>& _Left,
  3005. const vector<bool, _Alloc>& _Right)
  3006. { // test for vector equality
  3007. return (_Left.size() == _Right.size()
  3008. && equal(_Left._Myvec.begin(), _Left._Myvec.end(),
  3009. _Right._Myvec.begin()));
  3010. }
  3011.  
  3012. template<class _Alloc> inline
  3013. bool operator!=(const vector<bool, _Alloc>& _Left,
  3014. const vector<bool, _Alloc>& _Right)
  3015. { // test for vector inequality
  3016. return (!(_Left == _Right));
  3017. }
  3018.  
  3019. // TEMPLATE STRUCT SPECIALIZATION hash
  3020. template<class _Alloc>
  3021. struct hash<vector<_Bool, _Alloc> >
  3022. : public unary_function<vector<_Bool, _Alloc>, size_t>
  3023. { // hash functor
  3024. typedef vector<_Bool, _Alloc> _Kty;
  3025.  
  3026. size_t operator()(const _Kty& _Keyval) const
  3027. { // hash _Keyval to size_t value by pseudorandomizing transform
  3028. return (_Keyval.hash());
  3029. }
  3030. };
  3031. _STD_END
  3032.  
  3033. #pragma pop_macro("new")
  3034. #pragma warning(pop)
  3035. #pragma pack(pop)
  3036. #endif /* RC_INVOKED */
  3037. #endif /* _VECTOR_ */
  3038.  
  3039. /*
  3040. * This file is derived from software bearing the following
  3041. * restrictions:
  3042. *
  3043. * Copyright (c) 1994
  3044. * Hewlett-Packard Company
  3045. *
  3046. * Permission to use, copy, modify, distribute and sell this
  3047. * software and its documentation for any purpose is hereby
  3048. * granted without fee, provided that the above copyright notice
  3049. * appear in all copies and that both that copyright notice and
  3050. * this permission notice appear in supporting documentation.
  3051. * Hewlett-Packard Company makes no representations about the
  3052. * suitability of this software for any purpose. It is provided
  3053. * "as is" without express or implied warranty.
  3054. */
  3055.  
  3056. /*
  3057. * Copyright (c) 1992-2012 by P.J. Plauger. ALL RIGHTS RESERVED.
  3058. * Consult your license regarding permissions and restrictions.
  3059. V6.00:0009 */
Add Comment
Please, Sign In to add comment