Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1457 lines
36 KiB

  1. // vector standard header
  2. #pragma once
  3. #ifndef _VECTOR_
  4. #define _VECTOR_
  5. #include <memory>
  6. #include <stdexcept>
  7. #pragma pack(push,8)
  8. #pragma warning(push,3)
  9. #pragma warning(disable: 4244)
  10. _STD_BEGIN
  11. // TEMPLATE CLASS _Vector_val
  12. template<class _Ty, class _Alloc>
  13. class _Vector_val
  14. { // base class for vector to hold allocator _Alval
  15. protected:
  16. _Vector_val(_Alloc _Al = _Alloc())
  17. : _Alval(_Al)
  18. { // construct allocator from _Al
  19. }
  20. typedef typename _Alloc::_TEMPLATE_MEMBER
  21. rebind<_Ty>::other _Alty;
  22. _Alty _Alval; // allocator object for values
  23. };
  24. // TEMPLATE CLASS vector
  25. template<class _Ty,
  26. class _Ax = allocator<_Ty> >
  27. class vector
  28. : public _Vector_val<_Ty, _Ax>
  29. { // varying size array of values
  30. public:
  31. typedef vector<_Ty, _Ax> _Myt;
  32. typedef _Vector_val<_Ty, _Ax> _Mybase;
  33. typedef typename _Mybase::_Alty _Alloc;
  34. typedef _Alloc allocator_type;
  35. typedef typename _Alloc::size_type size_type;
  36. typedef typename _Alloc::difference_type difference_type;
  37. typedef typename _Alloc::pointer _Tptr;
  38. typedef typename _Alloc::const_pointer _Ctptr;
  39. typedef _Tptr pointer;
  40. typedef _Ctptr const_pointer;
  41. typedef typename _Alloc::reference reference;
  42. typedef typename _Alloc::const_reference const_reference;
  43. typedef typename _Alloc::value_type value_type;
  44. typedef _Ptrit<value_type, difference_type, _Tptr,
  45. reference, _Tptr, reference> iterator;
  46. typedef _Ptrit<value_type, difference_type, _Ctptr,
  47. const_reference, _Tptr, reference> const_iterator;
  48. typedef std::reverse_iterator<iterator>
  49. reverse_iterator;
  50. typedef std::reverse_iterator<const_iterator>
  51. const_reverse_iterator;
  52. vector()
  53. : _Mybase()
  54. { // construct empty vector
  55. _Buy(0);
  56. }
  57. explicit vector(const _Alloc& _Al)
  58. : _Mybase(_Al)
  59. { // construct empty vector with allocator
  60. _Buy(0);
  61. }
  62. explicit vector(size_type _Count)
  63. : _Mybase()
  64. { // construct from _Count * _Ty()
  65. _Ty _Val = _Ty(); // may throw
  66. if (_Buy(_Count))
  67. _Construct_n(_Count, _Val);
  68. }
  69. vector(size_type _Count, const _Ty& _Val)
  70. : _Mybase()
  71. { // construct from _Count * _Val
  72. if (_Buy(_Count))
  73. _Construct_n(_Count, _Val);
  74. }
  75. vector(size_type _Count, const _Ty& _Val, const _Alloc& _Al)
  76. : _Mybase(_Al)
  77. { // construct from _Count * _Val, with allocator
  78. if (_Buy(_Count))
  79. _Construct_n(_Count, _Val);
  80. }
  81. vector(const _Myt& _Right)
  82. : _Mybase(_Right._Alval)
  83. { // construct by copying _Right
  84. if (_Buy(_Right.size()))
  85. _TRY_BEGIN
  86. _Mylast = _Ucopy(_Right.begin(), _Right.end(), _Myfirst);
  87. _CATCH_ALL
  88. _Tidy();
  89. _RERAISE;
  90. _CATCH_END
  91. }
  92. template<class _Iter>
  93. vector(_Iter _First, _Iter _Last)
  94. : _Mybase()
  95. { // construct from [_First, _Last)
  96. _Construct(_First, _Last, _Iter_cat(_First));
  97. }
  98. template<class _Iter>
  99. vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
  100. : _Mybase(_Al)
  101. { // construct from [_First, _Last), with allocator
  102. _Construct(_First, _Last, _Iter_cat(_First));
  103. }
  104. template<class _Iter>
  105. void _Construct(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  106. { // initialize with _Count * _Val
  107. size_type _Size = (size_type)_Count;
  108. if (_Buy(_Size))
  109. _Construct_n(_Size, _Val);
  110. }
  111. template<class _Iter>
  112. void _Construct(_Iter _First, _Iter _Last, input_iterator_tag)
  113. { // initialize with [_First, _Last), input iterators
  114. _Buy(0);
  115. _TRY_BEGIN
  116. insert(begin(), _First, _Last);
  117. _CATCH_ALL
  118. _Tidy();
  119. _RERAISE;
  120. _CATCH_END
  121. }
  122. void _Construct_n(size_type _Count, const _Ty& _Val)
  123. { // construct from _Count * _Val
  124. _TRY_BEGIN
  125. _Mylast = _Ufill(_Myfirst, _Count, _Val);
  126. _CATCH_ALL
  127. _Tidy();
  128. _RERAISE;
  129. _CATCH_END
  130. }
  131. ~vector()
  132. { // destroy the object
  133. _Tidy();
  134. }
  135. _Myt& operator=(const _Myt& _Right)
  136. { // assign _Right
  137. if (this == &_Right)
  138. ; // nothing to do
  139. else if (_Right.size() == 0)
  140. clear(); // new sequence empty, free storage
  141. else if (_Right.size() <= size())
  142. { // enough elements, copy new and destroy old
  143. pointer _Ptr = copy(_Right.begin(), _Right.end(), _Myfirst);
  144. _Destroy(_Ptr, _Mylast);
  145. _Mylast = _Myfirst + _Right.size();
  146. }
  147. else if (_Right.size() <= capacity())
  148. { // enough room, copy and construct new
  149. const_iterator _Where = _Right.begin() + size();
  150. copy(_Right.begin(), _Where, _Myfirst);
  151. _Mylast = _Ucopy(_Where, _Right.end(), _Mylast);
  152. }
  153. else
  154. { // not enough room, allocate new array and construct new
  155. _Destroy(_Myfirst, _Mylast);
  156. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  157. if (_Buy(_Right.size()))
  158. _Mylast = _Ucopy(_Right.begin(), _Right.end(), _Myfirst);
  159. }
  160. return (*this);
  161. }
  162. void reserve(size_type _Count)
  163. { // determine new minimum length of allocated storage
  164. if (max_size() < _Count)
  165. _Xlen(); // result too long
  166. else if (capacity() < _Count)
  167. { // not enough room, reallocate
  168. pointer _Ptr = this->_Alval.allocate(_Count, (void *)0);
  169. _TRY_BEGIN
  170. _Ucopy(begin(), end(), _Ptr);
  171. _CATCH_ALL
  172. this->_Alval.deallocate(_Ptr, _Count);
  173. _RERAISE;
  174. _CATCH_END
  175. size_type _Size = size();
  176. if (_Myfirst != 0)
  177. { // destroy old array
  178. _Destroy(_Myfirst, _Mylast);
  179. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  180. }
  181. _Myend = _Ptr + _Count;
  182. _Mylast = _Ptr + _Size;
  183. _Myfirst = _Ptr;
  184. }
  185. }
  186. size_type capacity() const
  187. { // return current length of allocated storage
  188. return (_Myfirst == 0 ? 0 : _Myend - _Myfirst);
  189. }
  190. iterator begin()
  191. { // return iterator for beginning of mutable sequence
  192. return (iterator(_Myfirst));
  193. }
  194. const_iterator begin() const
  195. { // return iterator for beginning of nonmutable sequence
  196. return (const_iterator(_Myfirst));
  197. }
  198. iterator end()
  199. { // return iterator for end of mutable sequence
  200. return (iterator(_Mylast));
  201. }
  202. const_iterator end() const
  203. { // return iterator for end of nonmutable sequence
  204. return (const_iterator(_Mylast));
  205. }
  206. reverse_iterator rbegin()
  207. { // return iterator for beginning of reversed mutable sequence
  208. return (reverse_iterator(end()));
  209. }
  210. const_reverse_iterator rbegin() const
  211. { // return iterator for beginning of reversed nonmutable sequence
  212. return (const_reverse_iterator(end()));
  213. }
  214. reverse_iterator rend()
  215. { // return iterator for end of reversed mutable sequence
  216. return (reverse_iterator(begin()));
  217. }
  218. const_reverse_iterator rend() const
  219. { // return iterator for end of reversed nonmutable sequence
  220. return (const_reverse_iterator(begin()));
  221. }
  222. void resize(size_type _Newsize)
  223. { // determine new length, padding with _Ty() elements as needed
  224. resize(_Newsize, _Ty());
  225. }
  226. void resize(size_type _Newsize, _Ty _Val)
  227. { // determine new length, padding with _Val elements as needed
  228. if (size() < _Newsize)
  229. _Insert_n(end(), _Newsize - size(), _Val);
  230. else if (_Newsize < size())
  231. erase(begin() + _Newsize, end());
  232. }
  233. size_type size() const
  234. { // return length of sequence
  235. return (_Myfirst == 0 ? 0 : _Mylast - _Myfirst);
  236. }
  237. size_type max_size() const
  238. { // return maximum possible length of sequence
  239. return (this->_Alval.max_size());
  240. }
  241. bool empty() const
  242. { // test if sequence is empty
  243. return (size() == 0);
  244. }
  245. _Alloc get_allocator() const
  246. { // return allocator object for values
  247. return (this->_Alval);
  248. }
  249. const_reference at(size_type _Off) const
  250. { // subscript nonmutable sequence with checking
  251. if (size() <= _Off)
  252. _Xran();
  253. return (*(begin() + _Off));
  254. }
  255. reference at(size_type _Off)
  256. { // subscript mutable sequence with checking
  257. if (size() <= _Off)
  258. _Xran();
  259. return (*(begin() + _Off));
  260. }
  261. reference operator[](size_type _Off)
  262. { // subscript mutable sequence
  263. return (*(begin() + _Off));
  264. }
  265. const_reference operator[](size_type _Off) const
  266. { // subscript nonmutable sequence
  267. return (*(begin() + _Off));
  268. }
  269. reference front()
  270. { // return first element of mutable sequence
  271. return (*begin());
  272. }
  273. const_reference front() const
  274. { // return first element of nonmutable sequence
  275. return (*begin());
  276. }
  277. reference back()
  278. { // return last element of mutable sequence
  279. return (*(end() - 1));
  280. }
  281. const_reference back() const
  282. { // return last element of nonmutable sequence
  283. return (*(end() - 1));
  284. }
  285. void push_back(const _Ty& _Val)
  286. { // insert element at end
  287. if (size() < capacity())
  288. _Mylast = _Ufill(_Mylast, 1, _Val);
  289. else
  290. insert(end(), _Val);
  291. }
  292. void pop_back()
  293. { // erase element at end
  294. if (!empty())
  295. { // erase last element
  296. _Destroy(_Mylast - 1, _Mylast);
  297. --_Mylast;
  298. }
  299. }
  300. template<class _Iter>
  301. void assign(_Iter _First, _Iter _Last)
  302. { // assign [_First, _Last)
  303. _Assign(_First, _Last, _Iter_cat(_First));
  304. }
  305. template<class _Iter>
  306. void _Assign(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  307. { // assign _Count * _Val
  308. _Assign_n((size_type)_Count, (_Ty)_Val);
  309. }
  310. template<class _Iter>
  311. void _Assign(_Iter _First, _Iter _Last, input_iterator_tag)
  312. { // assign [_First, _Last), input iterators
  313. erase(begin(), end());
  314. insert(begin(), _First, _Last);
  315. }
  316. void assign(size_type _Count, const _Ty& _Val)
  317. { // assign _Count * _Val
  318. _Assign_n(_Count, _Val);
  319. }
  320. iterator insert(iterator _Where, const _Ty& _Val)
  321. { // insert _Val at _Where
  322. size_type _Off = size() == 0 ? 0 : _Where - begin();
  323. _Insert_n(_Where, (size_type)1, _Val);
  324. return (begin() + _Off);
  325. }
  326. void insert(iterator _Where, size_type _Count, const _Ty& _Val)
  327. { // insert _Count * _Val at _Where
  328. _Insert_n(_Where, _Count, _Val);
  329. }
  330. template<class _Iter>
  331. void insert(iterator _Where, _Iter _First, _Iter _Last)
  332. { // insert [_First, _Last) at _Where
  333. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  334. }
  335. template<class _Iter>
  336. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  337. _Int_iterator_tag)
  338. { // insert _Count * _Val at _Where
  339. _Insert_n(_Where, (size_type)_First, (_Ty)_Last);
  340. }
  341. template<class _Iter>
  342. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  343. input_iterator_tag)
  344. { // insert [_First, _Last) at _Where, input iterators
  345. for (; _First != _Last; ++_First, ++_Where)
  346. _Where = insert(_Where, *_First);
  347. }
  348. template<class _Iter>
  349. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  350. forward_iterator_tag)
  351. { // insert [_First, _Last) at _Where, forward iterators
  352. size_type _Count = 0;
  353. _Distance(_First, _Last, _Count);
  354. size_type _Capacity = capacity();
  355. if (_Count == 0)
  356. ;
  357. else if (max_size() - size() < _Count)
  358. _Xlen(); // result too long
  359. else if (_Capacity < size() + _Count)
  360. { // not enough room, reallocate
  361. _Capacity = max_size() - _Capacity / 2 < _Capacity
  362. ? 0 : _Capacity + _Capacity / 2; // try to grow by 50%
  363. if (_Capacity < size() + _Count)
  364. _Capacity = size() + _Count;
  365. pointer _Newvec = this->_Alval.allocate(_Capacity, (void *)0);
  366. pointer _Ptr = _Newvec;
  367. _TRY_BEGIN
  368. _Ptr = _Ucopy(begin(), _Where, _Newvec); // copy prefix
  369. _Ptr = _Ucopy(_First, _Last, _Ptr); // add new stuff
  370. _Ucopy(_Where, end(), _Ptr); // copy suffix
  371. _CATCH_ALL
  372. _Destroy(_Newvec, _Ptr);
  373. this->_Alval.deallocate(_Newvec, _Capacity);
  374. _RERAISE;
  375. _CATCH_END
  376. _Count += size();
  377. if (_Myfirst != 0)
  378. { // destroy and deallocate old array
  379. _Destroy(_Myfirst, _Mylast);
  380. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  381. }
  382. _Myend = _Newvec + _Capacity;
  383. _Mylast = _Newvec + _Count;
  384. _Myfirst = _Newvec;
  385. }
  386. else if ((size_type)(end() - _Where) < _Count)
  387. { // new stuff spills off end
  388. _Ucopy(_Where, end(), _Where.base() + _Count); // copy suffix
  389. _Iter _Mid = _First;
  390. advance(_Mid, end() - _Where);
  391. _TRY_BEGIN
  392. _Ucopy(_Mid, _Last, _Mylast); // insert new stuff off end
  393. _CATCH_ALL
  394. _Destroy(_Where.base() + _Count, _Mylast + _Count);
  395. _RERAISE;
  396. _CATCH_END
  397. _Mylast += _Count;
  398. copy(_First, _Mid, _Where); // insert up to old end
  399. }
  400. else
  401. { // new stuff can all be assigned
  402. iterator _Oldend = end();
  403. _Mylast = _Ucopy(_Oldend - _Count, _Oldend,
  404. _Mylast); // copy suffix
  405. copy_backward(_Where, _Oldend - _Count, _Oldend); // copy hole
  406. copy(_First, _Last, _Where); // insert into hole
  407. }
  408. }
  409. iterator erase(iterator _Where)
  410. { // erase element at where
  411. copy(_Where + 1, end(), _Where);
  412. _Destroy(_Mylast - 1, _Mylast);
  413. --_Mylast;
  414. return (_Where);
  415. }
  416. iterator erase(iterator _First, iterator _Last)
  417. { // erase [_First, _Last)
  418. if (_First != _Last)
  419. { // worth doing, copy down over hole
  420. pointer _Ptr = copy(_Last, end(), _First.base());
  421. _Destroy(_Ptr, _Mylast);
  422. _Mylast = _Ptr;
  423. }
  424. return (_First);
  425. }
  426. void clear()
  427. { // erase all
  428. _Tidy();
  429. }
  430. bool _Eq(const _Myt& _Right) const
  431. { // test for vector equality
  432. return (size() == _Right.size()
  433. && equal(begin(), end(), _Right.begin()));
  434. }
  435. bool _Lt(const _Myt& _Right) const
  436. { // test if this < _Right for vectors
  437. return (lexicographical_compare(begin(), end(),
  438. _Right.begin(), _Right.end()));
  439. }
  440. void swap(_Myt& _Right)
  441. { // exchange contents with _Right
  442. if (this->_Alval == _Right._Alval)
  443. { // same allocator, swap control information
  444. std::swap(_Myfirst, _Right._Myfirst);
  445. std::swap(_Mylast, _Right._Mylast);
  446. std::swap(_Myend, _Right._Myend);
  447. }
  448. else
  449. { // different allocator, do multiple assigns
  450. _Myt _Ts = *this; *this = _Right, _Right = _Ts;
  451. }
  452. }
  453. friend void swap(_Myt& _Left, _Myt& _Right)
  454. { // swap _Left and _Right vectors
  455. _Left.swap(_Right);
  456. }
  457. protected:
  458. void _Assign_n(size_type _Count, const _Ty& _Val)
  459. { // assign _Count * _Val
  460. _Ty _Tmp = _Val; // in case _Val is in sequence
  461. erase(begin(), end());
  462. insert(begin(), _Count, _Tmp);
  463. }
  464. bool _Buy(size_type _Capacity)
  465. { // allocate array with _Capacity elements
  466. _Myfirst = 0, _Mylast = 0, _Myend = 0;
  467. if (_Capacity == 0)
  468. return (false);
  469. else if (max_size() < _Capacity)
  470. _Xlen(); // result too long
  471. else
  472. { // nonempty array, allocate storage
  473. _Myfirst = this->_Alval.allocate(_Capacity, (void *)0);
  474. _Mylast = _Myfirst;
  475. _Myend = _Myfirst + _Capacity;
  476. }
  477. return (true);
  478. }
  479. void _Destroy(pointer _First, pointer _Last)
  480. { // destroy [_First, _Last) using allocator
  481. _Destroy_range(_First, _Last, this->_Alval);
  482. }
  483. void _Tidy()
  484. { // free all storage
  485. if (_Myfirst != 0)
  486. { // something to free, destroy and deallocate it
  487. _Destroy(_Myfirst, _Mylast);
  488. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  489. }
  490. _Myfirst = 0, _Mylast = 0, _Myend = 0;
  491. }
  492. template<class _Iter>
  493. pointer _Ucopy(_Iter _First, _Iter _Last, pointer _Ptr)
  494. { // copy initializing [_First, _Last), using allocator
  495. return (_Uninitialized_copy(_First, _Last,
  496. _Ptr, this->_Alval));
  497. }
  498. void _Insert_n(iterator _Where, size_type _Count, const _Ty& _Val)
  499. { // insert _Count * _Val at _Where
  500. _Ty _Tmp = _Val; // in case _Val is in sequence
  501. size_type _Capacity = capacity();
  502. if (_Count == 0)
  503. ;
  504. else if (max_size() - size() < _Count)
  505. _Xlen(); // result too long
  506. else if (_Capacity < size() + _Count)
  507. { // not enough room, reallocate
  508. _Capacity = max_size() - _Capacity / 2 < _Capacity
  509. ? 0 : _Capacity + _Capacity / 2; // try to grow by 50%
  510. if (_Capacity < size() + _Count)
  511. _Capacity = size() + _Count;
  512. pointer _Newvec = this->_Alval.allocate(_Capacity, (void *)0);
  513. pointer _Ptr = _Newvec;
  514. _TRY_BEGIN
  515. _Ptr = _Ucopy(begin(), _Where, _Newvec); // copy prefix
  516. _Ptr = _Ufill(_Ptr, _Count, _Tmp); // add new stuff
  517. _Ucopy(_Where, end(), _Ptr); // copy suffix
  518. _CATCH_ALL
  519. _Destroy(_Newvec, _Ptr);
  520. this->_Alval.deallocate(_Newvec, _Capacity);
  521. _RERAISE;
  522. _CATCH_END
  523. _Count += size();
  524. if (_Myfirst != 0)
  525. { // destroy and deallocate old array
  526. _Destroy(_Myfirst, _Mylast);
  527. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  528. }
  529. _Myend = _Newvec + _Capacity;
  530. _Mylast = _Newvec + _Count;
  531. _Myfirst = _Newvec;
  532. }
  533. else if ((size_type)(end() - _Where) < _Count)
  534. { // new stuff spills off end
  535. _Ucopy(_Where, end(), _Where.base() + _Count); // copy suffix
  536. _TRY_BEGIN
  537. _Ufill(_Mylast, _Count - (end() - _Where),
  538. _Tmp); // insert new stuff off end
  539. _CATCH_ALL
  540. _Destroy(_Where.base() + _Count, _Mylast + _Count);
  541. _RERAISE;
  542. _CATCH_END
  543. _Mylast += _Count;
  544. fill(_Where, end() - _Count, _Tmp); // insert up to old end
  545. }
  546. else
  547. { // new stuff can all be assigned
  548. iterator _Oldend = end();
  549. _Mylast = _Ucopy(_Oldend - _Count, _Oldend,
  550. _Mylast); // copy suffix
  551. copy_backward(_Where, _Oldend - _Count, _Oldend); // copy hole
  552. fill(_Where, _Where + _Count, _Tmp); // insert into hole
  553. }
  554. }
  555. pointer _Ufill(pointer _Ptr, size_type _Count, const _Ty &_Val)
  556. { // copy initializing _Count * _Val, using allocator
  557. _Uninitialized_fill_n(_Ptr, _Count, _Val, this->_Alval);
  558. return (_Ptr + _Count);
  559. }
  560. void _Xlen() const
  561. { // report a length_error
  562. _THROW(length_error, "vector<T> too long");
  563. }
  564. void _Xran() const
  565. { // report an out_of_range error
  566. _THROW(out_of_range, "invalid vector<T> subscript");
  567. }
  568. pointer _Myfirst; // pointer to beginning of array
  569. pointer _Mylast; // pointer to current end of sequence
  570. pointer _Myend; // pointer to end of array
  571. };
  572. // vector TEMPLATE FUNCTIONS
  573. template<class _Ty, class _Alloc> inline
  574. bool operator==(const vector<_Ty, _Alloc>& _Left,
  575. const vector<_Ty, _Alloc>& _Right)
  576. { // test for vector equality
  577. return (_Left._Eq(_Right));
  578. }
  579. template<class _Ty, class _Alloc> inline
  580. bool operator!=(const vector<_Ty, _Alloc>& _Left,
  581. const vector<_Ty, _Alloc>& _Right)
  582. { // test for vector inequality
  583. return (!(_Left == _Right));
  584. }
  585. template<class _Ty, class _Alloc> inline
  586. bool operator<(const vector<_Ty, _Alloc>& _Left,
  587. const vector<_Ty, _Alloc>& _Right)
  588. { // test if _Left < _Right for vectors
  589. return (_Left._Lt(_Right));
  590. }
  591. template<class _Ty, class _Alloc> inline
  592. bool operator>(const vector<_Ty, _Alloc>& _Left,
  593. const vector<_Ty, _Alloc>& _Right)
  594. { // test if _Left > _Right for vectors
  595. return (_Right < _Left);
  596. }
  597. template<class _Ty, class _Alloc> inline
  598. bool operator<=(const vector<_Ty, _Alloc>& _Left,
  599. const vector<_Ty, _Alloc>& _Right)
  600. { // test if _Left <= _Right for vectors
  601. return (!(_Right < _Left));
  602. }
  603. template<class _Ty, class _Alloc> inline
  604. bool operator>=(const vector<_Ty, _Alloc>& _Left,
  605. const vector<_Ty, _Alloc>& _Right)
  606. { // test if _Left >= _Right for vectors
  607. return (!(_Left < _Right));
  608. }
  609. // CLASS vector<bool>
  610. typedef unsigned _Vbase; // word type for vector<bool> representation
  611. const int _VBITS = 8 * sizeof (_Vbase); // at least CHAR_BITS bits per word
  612. typedef allocator<_Vbase> _Bool_allocator;
  613. template<> class vector<_Bool, _Bool_allocator>
  614. { // varying size array of bits
  615. public:
  616. typedef _Bool_allocator _Alloc;
  617. typedef _Alloc::size_type size_type;
  618. typedef _Alloc::difference_type _Dift;
  619. typedef std::vector<_Vbase, _Alloc> _Vbtype;
  620. typedef std::vector<_Bool, _Alloc> _Myt;
  621. typedef _Dift difference_type;
  622. typedef _Bool _Ty;
  623. typedef _Alloc allocator_type;
  624. // CLASS reference
  625. class reference
  626. { // reference to a bit within a base word
  627. public:
  628. reference()
  629. : _Mask(0), _Myptr(0)
  630. { // construct with null word pointer
  631. }
  632. reference(size_t _Off, _Vbase *_Ptr)
  633. : _Mask((_Vbase)(1 << _Off)), _Myptr(_Ptr)
  634. { // construct with bit offset _Off in word *_Ptr
  635. }
  636. reference& operator=(const reference& _Right)
  637. { // assign reference _Right to bit
  638. return (*this = bool(_Right));
  639. }
  640. reference& operator=(bool _Val)
  641. { // assign _Val to bit
  642. if (_Val)
  643. *_Myptr |= _Mask;
  644. else
  645. *_Myptr &= ~_Mask;
  646. return (*this);
  647. }
  648. void flip()
  649. { // toggle the bit
  650. *_Myptr ^= _Mask;
  651. }
  652. bool operator~() const
  653. { // test if bit is reset
  654. return (!bool(*this));
  655. }
  656. operator bool() const
  657. { // test if bit is set
  658. return ((*_Myptr & _Mask) != 0);
  659. }
  660. protected:
  661. _Vbase _Mask; // bit selection mask
  662. _Vbase *_Myptr; // pointer to base word
  663. };
  664. typedef reference _Reft;
  665. typedef bool const_reference;
  666. typedef bool value_type;
  667. #define _VB_TYPENAME
  668. // CLASS const_iterator
  669. class const_iterator
  670. : public _Ranit<_Bool, _Dift, const_reference *, const_reference>
  671. { // iterator for nonmutable vector<bool>
  672. public:
  673. typedef random_access_iterator_tag iterator_category;
  674. typedef _Bool value_type;
  675. typedef _Dift difference_type;
  676. typedef const_reference *pointer;
  677. typedef const_reference reference;
  678. const_iterator()
  679. : _Myoff(0), _Myptr(0)
  680. { // construct with null pointer
  681. }
  682. const_iterator(size_t _Off,
  683. _VB_TYPENAME _Vbtype::const_iterator _Where)
  684. : _Myoff(_Off), _Myptr(_Where.base())
  685. { // construct with offset _Off at iterator _Where
  686. }
  687. const_reference operator*() const
  688. { // return designated object
  689. return (_Reft(_Myoff, (_Vbase *)_Myptr));
  690. }
  691. const_iterator& operator++()
  692. { // preincrement
  693. _Inc();
  694. return (*this);
  695. }
  696. const_iterator operator++(int)
  697. { // postincrement
  698. const_iterator _Tmp = *this;
  699. _Inc();
  700. return (_Tmp);
  701. }
  702. const_iterator& operator--()
  703. { // predecrement
  704. _Dec();
  705. return (*this);
  706. }
  707. const_iterator operator--(int)
  708. { // postdecrement
  709. const_iterator _Tmp = *this;
  710. _Dec();
  711. return (_Tmp);
  712. }
  713. const_iterator& operator+=(difference_type _Off)
  714. { // increment by integer
  715. _Myoff += _Off;
  716. _Myptr += _Myoff / _VBITS;
  717. _Myoff %= _VBITS;
  718. return (*this);
  719. }
  720. const_iterator operator+(difference_type _Off) const
  721. { // return this + integer
  722. const_iterator _Tmp = *this;
  723. return (_Tmp += _Off);
  724. }
  725. const_iterator& operator-=(difference_type _Off)
  726. { // decrement by integer
  727. return (*this += -_Off);
  728. }
  729. const_iterator operator-(difference_type _Off) const
  730. { // return this - integer
  731. const_iterator _Tmp = *this;
  732. return (_Tmp -= _Off);
  733. }
  734. difference_type operator-(const const_iterator _Right) const
  735. { // return difference of iterators
  736. return (_VBITS * (_Myptr - _Right._Myptr)
  737. + (difference_type)_Myoff
  738. - (difference_type)_Right._Myoff);
  739. }
  740. const_reference operator[](difference_type _Off) const
  741. { // subscript
  742. return (*(*this + _Off));
  743. }
  744. bool operator==(const const_iterator& _Right) const
  745. { // test for iterator equality
  746. return (_Myptr == _Right._Myptr && _Myoff == _Right._Myoff);
  747. }
  748. bool operator!=(const const_iterator& _Right) const
  749. { // test for iterator inequality
  750. return (!(*this == _Right));
  751. }
  752. bool operator<(const const_iterator& _Right) const
  753. { // test if this < _Right
  754. return (_Myptr < _Right._Myptr
  755. || _Myptr == _Right._Myptr && _Myoff < _Right._Myoff);
  756. }
  757. bool operator>(const const_iterator& _Right) const
  758. { // test if this > _Right
  759. return (_Right < *this);
  760. }
  761. bool operator<=(const const_iterator& _Right) const
  762. { // test if this <= _Right
  763. return (!(_Right < *this));
  764. }
  765. bool operator>=(const const_iterator& _Right) const
  766. { // test if this >= _Right
  767. return (!(*this < _Right));
  768. }
  769. friend const_iterator operator+(difference_type _Off,
  770. const const_iterator& _Right)
  771. { // return iterator + integer
  772. return (_Right + _Off);
  773. }
  774. protected:
  775. void _Dec()
  776. { // decrement bit position
  777. if (_Myoff != 0)
  778. --_Myoff;
  779. else
  780. _Myoff = _VBITS - 1, --_Myptr;
  781. }
  782. void _Inc()
  783. { // increment bit position
  784. if (_Myoff < _VBITS - 1)
  785. ++_Myoff;
  786. else
  787. _Myoff = 0, ++_Myptr;
  788. }
  789. size_t _Myoff; // offset of bit in word
  790. const _Vbase *_Myptr; // pointer to base of word array
  791. };
  792. // CLASS iterator
  793. class iterator
  794. : public const_iterator
  795. { // iterator for mutable vector<bool>
  796. public:
  797. typedef random_access_iterator_tag iterator_category;
  798. typedef _Bool value_type;
  799. typedef _Dift difference_type;
  800. typedef _Reft *pointer;
  801. typedef _Reft reference;
  802. iterator()
  803. : const_iterator()
  804. { // construct with null pointer
  805. }
  806. iterator(size_t _Off, _VB_TYPENAME _Vbtype::iterator _Where)
  807. : const_iterator(_Off, _Where)
  808. { // construct with offset _Off at iterator _Where
  809. }
  810. reference operator*() const
  811. { // return designated object
  812. return (_Reft(_Myoff, (_Vbase *)_Myptr));
  813. }
  814. iterator& operator++()
  815. { // preincrement
  816. _Inc();
  817. return (*this);
  818. }
  819. iterator operator++(int)
  820. { // postincrement
  821. iterator _Tmp = *this;
  822. _Inc();
  823. return (_Tmp);
  824. }
  825. iterator& operator--()
  826. { // predecrement
  827. _Dec();
  828. return (*this);
  829. }
  830. iterator operator--(int)
  831. { // postdecrement
  832. iterator _Tmp = *this;
  833. _Dec();
  834. return (_Tmp);
  835. }
  836. iterator& operator+=(difference_type _Off)
  837. { // increment by integer
  838. _Myoff += _Off;
  839. _Myptr += _Myoff / _VBITS;
  840. _Myoff %= _VBITS;
  841. return (*this);
  842. }
  843. iterator operator+(difference_type _Off) const
  844. { // return this + integer
  845. iterator _Tmp = *this;
  846. return (_Tmp += _Off);
  847. }
  848. iterator& operator-=(difference_type _Off)
  849. { // decrement by integer
  850. return (*this += -_Off);
  851. }
  852. iterator operator-(difference_type _Off) const
  853. { // return this - integer
  854. iterator _Tmp = *this;
  855. return (_Tmp -= _Off);
  856. }
  857. difference_type operator-(const iterator _Right) const
  858. { // return difference of iterators
  859. return (_VBITS * (_Myptr - _Right._Myptr)
  860. + (difference_type)_Myoff
  861. - (difference_type)_Right._Myoff);
  862. }
  863. reference operator[](difference_type _Off) const
  864. { // subscript
  865. return (*(*this + _Off));
  866. }
  867. friend iterator operator+(difference_type _Off,
  868. const iterator& _Right)
  869. { // return iterator + integer
  870. return (_Right + _Off);
  871. }
  872. };
  873. typedef iterator pointer;
  874. typedef const_iterator const_pointer;
  875. typedef std::reverse_iterator<iterator> reverse_iterator;
  876. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  877. vector()
  878. : _Mysize(0), _Myvec()
  879. { // construct empty vector
  880. }
  881. explicit vector(const _Alloc& _Al)
  882. : _Mysize(0), _Myvec(_Al)
  883. { // construct empty vector, with allocator
  884. }
  885. explicit vector(size_type _Count, bool _Val = false)
  886. : _Mysize(0), _Myvec(_Nw(_Count), (_Vbase)(_Val ? -1 : 0))
  887. { // construct from _Count * _Val
  888. _Trim(_Count);
  889. }
  890. vector(size_type _Count, bool _Val, const _Alloc& _Al)
  891. : _Mysize(0), _Myvec(_Nw(_Count), (_Vbase)(_Val ? -1 : 0), _Al)
  892. { // construct from _Count * _Val, with allocator
  893. _Trim(_Count);
  894. }
  895. template<class _Iter>
  896. vector(_Iter _First, _Iter _Last)
  897. : _Mysize(0), _Myvec()
  898. { // construct from [_First, _Last)
  899. _BConstruct(_First, _Last, _Iter_cat(_First));
  900. }
  901. template<class _Iter>
  902. vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
  903. : _Mysize(0), _Myvec(_Al)
  904. { // construct from [_First, _Last), with allocator
  905. _BConstruct(_First, _Last, _Iter_cat(_First));
  906. }
  907. template<class _Iter>
  908. void _BConstruct(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  909. { // initialize from _Count * _Val
  910. size_type _Num = (size_type)_Count;
  911. _Myvec.assign(_Num, (_Ty)_Val ? -1 : 0);
  912. _Trim(_Num);
  913. }
  914. template<class _Iter>
  915. void _BConstruct(_Iter _First, _Iter _Last, input_iterator_tag)
  916. { // initialize from [_First, _Last), input iterators
  917. insert(begin(), _First, _Last);
  918. }
  919. ~vector()
  920. { // destroy the object
  921. _Mysize = 0;
  922. }
  923. void reserve(size_type _Count)
  924. { // determine new minimum length of allocated storage
  925. _Myvec.reserve(_Nw(_Count));
  926. }
  927. size_type capacity() const
  928. { // return current length of allocated storage
  929. return (_Myvec.capacity() * _VBITS);
  930. }
  931. iterator begin()
  932. { // return iterator for beginning of mutable sequence
  933. return (iterator(0, _Myvec.begin()));
  934. }
  935. const_iterator begin() const
  936. { // return iterator for beginning of nonmutable sequence
  937. return (const_iterator(0, _Myvec.begin()));
  938. }
  939. iterator end()
  940. { // return iterator for end of mutable sequence
  941. iterator _Tmp = begin();
  942. if (0 < _Mysize)
  943. _Tmp += _Mysize;
  944. return (_Tmp);
  945. }
  946. const_iterator end() const
  947. { // return iterator for end of nonmutable sequence
  948. const_iterator _Tmp = begin();
  949. if (0 < _Mysize)
  950. _Tmp += _Mysize;
  951. return (_Tmp);
  952. }
  953. reverse_iterator rbegin()
  954. { // return iterator for beginning of reversed mutable sequence
  955. return (reverse_iterator(end()));
  956. }
  957. const_reverse_iterator rbegin() const
  958. { // return iterator for beginning of reversed nonmutable sequence
  959. return (const_reverse_iterator(end()));
  960. }
  961. reverse_iterator rend()
  962. { // return iterator for end of reversed mutable sequence
  963. return (reverse_iterator(begin()));
  964. }
  965. const_reverse_iterator rend() const
  966. { // return iterator for end of reversed nonmutable sequence
  967. return (const_reverse_iterator(begin()));
  968. }
  969. void resize(size_type _Newsize, bool _Val = false)
  970. { // determine new length, padding with _Val elements as needed
  971. if (size() < _Newsize)
  972. _Insert_n(end(), _Newsize - size(), _Val);
  973. else if (_Newsize < size())
  974. erase(begin() + _Newsize, end());
  975. }
  976. size_type size() const
  977. { // return length of sequence
  978. return (_Mysize);
  979. }
  980. size_type max_size() const
  981. { // return maximum possible length of sequence
  982. const size_type _Maxsize = _Myvec.max_size();
  983. return (_Maxsize < (size_type)(-1) / _VBITS
  984. ? _Maxsize * _VBITS : (size_type)(-1));
  985. }
  986. bool empty() const
  987. { // test if sequence is empty
  988. return (size() == 0);
  989. }
  990. _Alloc get_allocator() const
  991. { // return allocator object for values
  992. return (_Myvec.get_allocator());
  993. }
  994. const_reference at(size_type _Off) const
  995. { // subscript nonmutable sequence with checking
  996. if (size() <= _Off)
  997. _Xran();
  998. return (*(begin() + _Off));
  999. }
  1000. reference at(size_type _Off)
  1001. { // subscript mutable sequence with checking
  1002. if (size() <= _Off)
  1003. _Xran();
  1004. return (*(begin() + _Off));
  1005. }
  1006. const_reference operator[](size_type _Off) const
  1007. { // subscript nonmutable sequence
  1008. return (*(begin() + _Off));
  1009. }
  1010. reference operator[](size_type _Off)
  1011. { // subscript mutable sequence
  1012. return (*(begin() + _Off));
  1013. }
  1014. reference front()
  1015. { // return first element of mutable sequence
  1016. return (*begin());
  1017. }
  1018. const_reference front() const
  1019. { // return first element of nonmutable sequence
  1020. return (*begin());
  1021. }
  1022. reference back()
  1023. { // return last element of mutable sequence
  1024. return (*(end() - 1));
  1025. }
  1026. const_reference back() const
  1027. { // return last element of nonmutable sequence
  1028. return (*(end() - 1));
  1029. }
  1030. void push_back(bool _Val)
  1031. { // insert element at end
  1032. insert(end(), _Val);
  1033. }
  1034. void pop_back()
  1035. { // erase element at end
  1036. if (!empty())
  1037. erase(end() - 1);
  1038. }
  1039. template<class _Iter>
  1040. void assign(_Iter _First, _Iter _Last)
  1041. { // assign [_First, _Last)
  1042. _Assign(_First, _Last, _Iter_cat(_First));
  1043. }
  1044. template<class _Iter>
  1045. void _Assign(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  1046. { // assign _Count * _Val
  1047. _Assign_n((size_type)_Count, (bool)_Val);
  1048. }
  1049. template<class _Iter>
  1050. void _Assign(_Iter _First, _Iter _Last, input_iterator_tag)
  1051. { // assign [_First, _Last), input iterators
  1052. erase(begin(), end());
  1053. insert(begin(), _First, _Last);
  1054. }
  1055. void assign(size_type _Count, bool _Val)
  1056. { // assign _Count * _Val
  1057. _Assign_n(_Count, _Val);
  1058. }
  1059. iterator insert(iterator _Where, bool _Val)
  1060. { // insert _Val at _Where
  1061. size_type _Off = _Where - begin();
  1062. _Insert_n(_Where, (size_type)1, _Val);
  1063. return (begin() + _Off);
  1064. }
  1065. void insert(iterator _Where, size_type _Count, bool _Val)
  1066. { // insert _Count * _Val at _Where
  1067. _Insert_n(_Where, _Count, _Val);
  1068. }
  1069. template<class _Iter>
  1070. void insert(iterator _Where, _Iter _First, _Iter _Last)
  1071. { // insert [_First, _Last) at _Where
  1072. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  1073. }
  1074. template<class _Iter>
  1075. void _Insert(iterator _Where, _Iter _Count, _Iter _Val,
  1076. _Int_iterator_tag)
  1077. { // insert _Count * _Val at _Where
  1078. _Insert_n(_Where, (size_type)_Count, (bool)_Val);
  1079. }
  1080. template<class _Iter>
  1081. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  1082. input_iterator_tag)
  1083. { // insert [_First, _Last) at _Where, input iterators
  1084. size_type _Off = _Where - begin();
  1085. for (; _First != _Last; ++_First, ++_Off)
  1086. insert(begin() + _Off, *_First);
  1087. }
  1088. template<class _Iter>
  1089. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  1090. forward_iterator_tag)
  1091. { // insert [_First, _Last) at _Where, forward iterators
  1092. size_type _Capacity = 0;
  1093. _Distance(_First, _Last, _Capacity);
  1094. if (_Capacity == 0)
  1095. ;
  1096. else if (max_size() - size() < _Capacity)
  1097. _Xlen(); // result too long
  1098. else
  1099. { // worth doing
  1100. if (size() == 0)
  1101. { // originally empty, just make room
  1102. _Myvec.resize(_Nw(size() + _Capacity), 0);
  1103. _Where = begin();
  1104. }
  1105. else
  1106. { // make room and copy down suffix
  1107. size_type _Off = _Where - begin();
  1108. _Myvec.resize(_Nw(size() + _Capacity), 0);
  1109. _Where = begin() + _Off;
  1110. copy_backward(_Where, end(), end() + _Capacity);
  1111. }
  1112. copy(_First, _Last, _Where); // add new stuff
  1113. _Mysize += _Capacity;
  1114. }
  1115. }
  1116. iterator erase(iterator _Where)
  1117. { // erase element at _Where
  1118. copy(_Where + 1, end(), _Where);
  1119. _Trim(_Mysize - 1);
  1120. return (_Where);
  1121. }
  1122. iterator erase(iterator _First, iterator _Last)
  1123. { // erase [_First, _Last)
  1124. iterator _Next = copy(_Last, end(), _First);
  1125. _Trim(_Next - begin());
  1126. return (_First);
  1127. }
  1128. void clear()
  1129. { // erase all elements
  1130. erase(begin(), end());
  1131. }
  1132. void flip()
  1133. { // toggle all elements
  1134. for (_Vbtype::iterator _Next = _Myvec.begin();
  1135. _Next != _Myvec.end(); ++_Next)
  1136. *_Next = (_Vbase)~*_Next;
  1137. _Trim(_Mysize);
  1138. }
  1139. bool _Eq(const _Myt& _Right) const
  1140. { // test for vector<bool> equality
  1141. return (_Mysize == _Right._Mysize && _Myvec == _Right._Myvec);
  1142. }
  1143. bool _Lt(const _Myt& _Right) const
  1144. { // test if this < _Right for vector<bool>
  1145. return (lexicographical_compare(begin(), end(),
  1146. _Right.begin(), _Right.end()));
  1147. }
  1148. void swap(_Myt& _Right)
  1149. { // exchange contents with right
  1150. std::swap(_Mysize, _Right._Mysize);
  1151. _Myvec.swap(_Right._Myvec);
  1152. }
  1153. friend void swap(_Myt& _Left, _Myt& _Right)
  1154. { // swap _Left and _Right vector<bool>s
  1155. _Left.swap(_Right);
  1156. }
  1157. static void swap(reference _Left, reference _Right)
  1158. { // swap _Left and _Right vector<bool> elements
  1159. bool _Val = _Left;
  1160. _Left = _Right;
  1161. _Right = _Val;
  1162. }
  1163. protected:
  1164. void _Assign_n(size_type _Count, bool _Val)
  1165. { // assign _Count * _Val
  1166. erase(begin(), end());
  1167. _Insert_n(begin(), _Count, _Val);
  1168. }
  1169. void _Insert_n(iterator _Where, size_type _Count, bool _Val)
  1170. { // insert _Count * _Val at _Where
  1171. if (_Count == 0)
  1172. ;
  1173. else if (max_size() - size() < _Count)
  1174. _Xlen(); // result too long
  1175. else
  1176. { // worth doing
  1177. if (size() == 0)
  1178. { // originally empty, just make room
  1179. _Myvec.resize(_Nw(size() + _Count), 0);
  1180. _Where = begin();
  1181. }
  1182. else
  1183. { // make room and copy down suffix
  1184. size_type _Off = _Where - begin();
  1185. _Myvec.resize(_Nw(size() + _Count), 0);
  1186. _Where = begin() + _Off;
  1187. copy_backward(_Where, end(), end() + _Count);
  1188. }
  1189. fill(_Where, _Where + _Count, _Val); // add new stuff
  1190. _Mysize += _Count;
  1191. }
  1192. }
  1193. static size_type _Nw(size_type _Count)
  1194. { // return number of base words from number of bits
  1195. return ((_Count + _VBITS - 1) / _VBITS);
  1196. }
  1197. void _Trim(size_type _Size)
  1198. { // trim base vector to exact length in bits
  1199. if (max_size() < _Size)
  1200. _Xlen(); // result too long
  1201. size_type _Words = _Nw(_Size);
  1202. if (_Words < _Myvec.size())
  1203. _Myvec.erase(_Myvec.begin() + _Words, _Myvec.end());
  1204. _Mysize = _Size;
  1205. _Size %= _VBITS;
  1206. if (0 < _Size)
  1207. _Myvec[_Words - 1] &= (_Vbase)((1 << _Size) - 1);
  1208. }
  1209. void _Xlen() const
  1210. { // report a length_error
  1211. _THROW(length_error, "vector<bool> too long");
  1212. }
  1213. void _Xran() const
  1214. { // throw an out_of_range error
  1215. _THROW(out_of_range, "invalid vector<bool> subscript");
  1216. }
  1217. size_type _Mysize; // current length of sequence
  1218. _Vbtype _Myvec; // base vector of words
  1219. };
  1220. typedef vector<_Bool, _Bool_allocator> _Bvector;
  1221. _STD_END
  1222. #pragma warning(default: 4244)
  1223. #pragma warning(pop)
  1224. #pragma pack(pop)
  1225. #endif /* _VECTOR_ */
  1226. /*
  1227. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  1228. * Consult your license regarding permissions and restrictions.
  1229. */
  1230. /*
  1231. * This file is derived from software bearing the following
  1232. * restrictions:
  1233. *
  1234. * Copyright (c) 1994
  1235. * Hewlett-Packard Company
  1236. *
  1237. * Permission to use, copy, modify, distribute and sell this
  1238. * software and its documentation for any purpose is hereby
  1239. * granted without fee, provided that the above copyright notice
  1240. * appear in all copies and that both that copyright notice and
  1241. * this permission notice appear in supporting documentation.
  1242. * Hewlett-Packard Company makes no representations about the
  1243. * suitability of this software for any purpose. It is provided
  1244. * "as is" without express or implied warranty.
  1245. V3.10:0009 */