Leaked source code of windows server 2003
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.

1453 lines
37 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. protected:
  454. void _Assign_n(size_type _Count, const _Ty& _Val)
  455. { // assign _Count * _Val
  456. _Ty _Tmp = _Val; // in case _Val is in sequence
  457. erase(begin(), end());
  458. insert(begin(), _Count, _Tmp);
  459. }
  460. bool _Buy(size_type _Capacity)
  461. { // allocate array with _Capacity elements
  462. _Myfirst = 0, _Mylast = 0, _Myend = 0;
  463. if (_Capacity == 0)
  464. return (false);
  465. else if (max_size() < _Capacity)
  466. _Xlen(); // result too long
  467. else
  468. { // nonempty array, allocate storage
  469. _Myfirst = this->_Alval.allocate(_Capacity, (void *)0);
  470. _Mylast = _Myfirst;
  471. _Myend = _Myfirst + _Capacity;
  472. }
  473. return (true);
  474. }
  475. void _Destroy(pointer _First, pointer _Last)
  476. { // destroy [_First, _Last) using allocator
  477. _Destroy_range(_First, _Last, this->_Alval);
  478. }
  479. void _Tidy()
  480. { // free all storage
  481. if (_Myfirst != 0)
  482. { // something to free, destroy and deallocate it
  483. _Destroy(_Myfirst, _Mylast);
  484. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  485. }
  486. _Myfirst = 0, _Mylast = 0, _Myend = 0;
  487. }
  488. template<class _Iter>
  489. pointer _Ucopy(_Iter _First, _Iter _Last, pointer _Ptr)
  490. { // copy initializing [_First, _Last), using allocator
  491. return (_Uninitialized_copy(_First, _Last,
  492. _Ptr, this->_Alval));
  493. }
  494. void _Insert_n(iterator _Where, size_type _Count, const _Ty& _Val)
  495. { // insert _Count * _Val at _Where
  496. _Ty _Tmp = _Val; // in case _Val is in sequence
  497. size_type _Capacity = capacity();
  498. if (_Count == 0)
  499. ;
  500. else if (max_size() - size() < _Count)
  501. _Xlen(); // result too long
  502. else if (_Capacity < size() + _Count)
  503. { // not enough room, reallocate
  504. _Capacity = max_size() - _Capacity / 2 < _Capacity
  505. ? 0 : _Capacity + _Capacity / 2; // try to grow by 50%
  506. if (_Capacity < size() + _Count)
  507. _Capacity = size() + _Count;
  508. pointer _Newvec = this->_Alval.allocate(_Capacity, (void *)0);
  509. pointer _Ptr = _Newvec;
  510. _TRY_BEGIN
  511. _Ptr = _Ucopy(begin(), _Where, _Newvec); // copy prefix
  512. _Ptr = _Ufill(_Ptr, _Count, _Tmp); // add new stuff
  513. _Ucopy(_Where, end(), _Ptr); // copy suffix
  514. _CATCH_ALL
  515. _Destroy(_Newvec, _Ptr);
  516. this->_Alval.deallocate(_Newvec, _Capacity);
  517. _RERAISE;
  518. _CATCH_END
  519. _Count += size();
  520. if (_Myfirst != 0)
  521. { // destroy and deallocate old array
  522. _Destroy(_Myfirst, _Mylast);
  523. this->_Alval.deallocate(_Myfirst, _Myend - _Myfirst);
  524. }
  525. _Myend = _Newvec + _Capacity;
  526. _Mylast = _Newvec + _Count;
  527. _Myfirst = _Newvec;
  528. }
  529. else if ((size_type)(end() - _Where) < _Count)
  530. { // new stuff spills off end
  531. _Ucopy(_Where, end(), _Where.base() + _Count); // copy suffix
  532. _TRY_BEGIN
  533. _Ufill(_Mylast, _Count - (end() - _Where),
  534. _Tmp); // insert new stuff off end
  535. _CATCH_ALL
  536. _Destroy(_Where.base() + _Count, _Mylast + _Count);
  537. _RERAISE;
  538. _CATCH_END
  539. _Mylast += _Count;
  540. fill(_Where, end() - _Count, _Tmp); // insert up to old end
  541. }
  542. else
  543. { // new stuff can all be assigned
  544. iterator _Oldend = end();
  545. _Mylast = _Ucopy(_Oldend - _Count, _Oldend,
  546. _Mylast); // copy suffix
  547. copy_backward(_Where, _Oldend - _Count, _Oldend); // copy hole
  548. fill(_Where, _Where + _Count, _Tmp); // insert into hole
  549. }
  550. }
  551. pointer _Ufill(pointer _Ptr, size_type _Count, const _Ty &_Val)
  552. { // copy initializing _Count * _Val, using allocator
  553. _Uninitialized_fill_n(_Ptr, _Count, _Val, this->_Alval);
  554. return (_Ptr + _Count);
  555. }
  556. void _Xlen() const
  557. { // report a length_error
  558. _THROW(length_error, "vector<T> too long");
  559. }
  560. void _Xran() const
  561. { // report an out_of_range error
  562. _THROW(out_of_range, "invalid vector<T> subscript");
  563. }
  564. pointer _Myfirst; // pointer to beginning of array
  565. pointer _Mylast; // pointer to current end of sequence
  566. pointer _Myend; // pointer to end of array
  567. };
  568. // vector TEMPLATE FUNCTIONS
  569. template<class _Ty, class _Alloc> inline
  570. bool operator==(const vector<_Ty, _Alloc>& _Left,
  571. const vector<_Ty, _Alloc>& _Right)
  572. { // test for vector equality
  573. return (_Left._Eq(_Right));
  574. }
  575. template<class _Ty, class _Alloc> inline
  576. bool operator!=(const vector<_Ty, _Alloc>& _Left,
  577. const vector<_Ty, _Alloc>& _Right)
  578. { // test for vector inequality
  579. return (!(_Left == _Right));
  580. }
  581. template<class _Ty, class _Alloc> inline
  582. bool operator<(const vector<_Ty, _Alloc>& _Left,
  583. const vector<_Ty, _Alloc>& _Right)
  584. { // test if _Left < _Right for vectors
  585. return (_Left._Lt(_Right));
  586. }
  587. template<class _Ty, class _Alloc> inline
  588. bool operator>(const vector<_Ty, _Alloc>& _Left,
  589. const vector<_Ty, _Alloc>& _Right)
  590. { // test if _Left > _Right for vectors
  591. return (_Right < _Left);
  592. }
  593. template<class _Ty, class _Alloc> inline
  594. bool operator<=(const vector<_Ty, _Alloc>& _Left,
  595. const vector<_Ty, _Alloc>& _Right)
  596. { // test if _Left <= _Right for vectors
  597. return (!(_Right < _Left));
  598. }
  599. template<class _Ty, class _Alloc> inline
  600. bool operator>=(const vector<_Ty, _Alloc>& _Left,
  601. const vector<_Ty, _Alloc>& _Right)
  602. { // test if _Left >= _Right for vectors
  603. return (!(_Left < _Right));
  604. }
  605. template<class _Ty, class _Alloc> inline
  606. void swap(vector<_Ty, _Alloc>& _Left, vector<_Ty, _Alloc>& _Right)
  607. { // swap _Left and _Right vectors
  608. _Left.swap(_Right);
  609. }
  610. // CLASS vector<bool>
  611. typedef unsigned _Vbase; // word type for vector<bool> representation
  612. const int _VBITS = 8 * sizeof (_Vbase); // at least CHAR_BITS bits per word
  613. typedef allocator<_Vbase> _Bool_allocator;
  614. template<> class vector<_Bool, _Bool_allocator>
  615. { // varying size array of bits
  616. public:
  617. typedef _Bool_allocator _Alloc;
  618. typedef _Alloc::size_type size_type;
  619. typedef _Alloc::difference_type _Dift;
  620. typedef std::vector<_Vbase, _Alloc> _Vbtype;
  621. typedef std::vector<_Bool, _Alloc> _Myt;
  622. typedef _Dift difference_type;
  623. typedef _Bool _Ty;
  624. typedef _Alloc allocator_type;
  625. // CLASS reference
  626. class reference
  627. { // reference to a bit within a base word
  628. public:
  629. reference()
  630. : _Mask(0), _Myptr(0)
  631. { // construct with null word pointer
  632. }
  633. reference(size_t _Off, _Vbase *_Ptr)
  634. : _Mask((_Vbase)(1 << _Off)), _Myptr(_Ptr)
  635. { // construct with bit offset _Off in word *_Ptr
  636. }
  637. reference& operator=(const reference& _Right)
  638. { // assign reference _Right to bit
  639. return (*this = bool(_Right));
  640. }
  641. reference& operator=(bool _Val)
  642. { // assign _Val to bit
  643. if (_Val)
  644. *_Myptr |= _Mask;
  645. else
  646. *_Myptr &= ~_Mask;
  647. return (*this);
  648. }
  649. void flip()
  650. { // toggle the bit
  651. *_Myptr ^= _Mask;
  652. }
  653. bool operator~() const
  654. { // test if bit is reset
  655. return (!bool(*this));
  656. }
  657. operator bool() const
  658. { // test if bit is set
  659. return ((*_Myptr & _Mask) != 0);
  660. }
  661. protected:
  662. _Vbase _Mask; // bit selection mask
  663. _Vbase *_Myptr; // pointer to base word
  664. };
  665. typedef reference _Reft;
  666. typedef bool const_reference;
  667. typedef bool value_type;
  668. #define _VB_TYPENAME
  669. // CLASS const_iterator
  670. class const_iterator
  671. : public _Ranit<_Bool, _Dift, const_reference *, const_reference>
  672. { // iterator for nonmutable vector<bool>
  673. public:
  674. typedef random_access_iterator_tag iterator_category;
  675. typedef _Bool value_type;
  676. typedef _Dift difference_type;
  677. typedef const_reference *pointer;
  678. typedef const_reference reference;
  679. const_iterator()
  680. : _Myoff(0), _Myptr(0)
  681. { // construct with null pointer
  682. }
  683. const_iterator(size_t _Off,
  684. _VB_TYPENAME _Vbtype::const_iterator _Where)
  685. : _Myoff(_Off), _Myptr(_Where.base())
  686. { // construct with offset _Off at iterator _Where
  687. }
  688. const_reference operator*() const
  689. { // return designated object
  690. return (_Reft(_Myoff, (_Vbase *)_Myptr));
  691. }
  692. const_iterator& operator++()
  693. { // preincrement
  694. _Inc();
  695. return (*this);
  696. }
  697. const_iterator operator++(int)
  698. { // postincrement
  699. const_iterator _Tmp = *this;
  700. _Inc();
  701. return (_Tmp);
  702. }
  703. const_iterator& operator--()
  704. { // predecrement
  705. _Dec();
  706. return (*this);
  707. }
  708. const_iterator operator--(int)
  709. { // postdecrement
  710. const_iterator _Tmp = *this;
  711. _Dec();
  712. return (_Tmp);
  713. }
  714. const_iterator& operator+=(difference_type _Off)
  715. { // increment by integer
  716. _Myoff += _Off;
  717. _Myptr += _Myoff / _VBITS;
  718. _Myoff %= _VBITS;
  719. return (*this);
  720. }
  721. const_iterator operator+(difference_type _Off) const
  722. { // return this + integer
  723. const_iterator _Tmp = *this;
  724. return (_Tmp += _Off);
  725. }
  726. const_iterator& operator-=(difference_type _Off)
  727. { // decrement by integer
  728. return (*this += -_Off);
  729. }
  730. const_iterator operator-(difference_type _Off) const
  731. { // return this - integer
  732. const_iterator _Tmp = *this;
  733. return (_Tmp -= _Off);
  734. }
  735. difference_type operator-(const const_iterator _Right) const
  736. { // return difference of iterators
  737. return (_VBITS * (_Myptr - _Right._Myptr)
  738. + (difference_type)_Myoff
  739. - (difference_type)_Right._Myoff);
  740. }
  741. const_reference operator[](difference_type _Off) const
  742. { // subscript
  743. return (*(*this + _Off));
  744. }
  745. bool operator==(const const_iterator& _Right) const
  746. { // test for iterator equality
  747. return (_Myptr == _Right._Myptr && _Myoff == _Right._Myoff);
  748. }
  749. bool operator!=(const const_iterator& _Right) const
  750. { // test for iterator inequality
  751. return (!(*this == _Right));
  752. }
  753. bool operator<(const const_iterator& _Right) const
  754. { // test if this < _Right
  755. return (_Myptr < _Right._Myptr
  756. || _Myptr == _Right._Myptr && _Myoff < _Right._Myoff);
  757. }
  758. bool operator>(const const_iterator& _Right) const
  759. { // test if this > _Right
  760. return (_Right < *this);
  761. }
  762. bool operator<=(const const_iterator& _Right) const
  763. { // test if this <= _Right
  764. return (!(_Right < *this));
  765. }
  766. bool operator>=(const const_iterator& _Right) const
  767. { // test if this >= _Right
  768. return (!(*this < _Right));
  769. }
  770. friend const_iterator operator+(difference_type _Off,
  771. const const_iterator& _Right)
  772. { // return iterator + integer
  773. return (_Right + _Off);
  774. }
  775. protected:
  776. void _Dec()
  777. { // decrement bit position
  778. if (_Myoff != 0)
  779. --_Myoff;
  780. else
  781. _Myoff = _VBITS - 1, --_Myptr;
  782. }
  783. void _Inc()
  784. { // increment bit position
  785. if (_Myoff < _VBITS - 1)
  786. ++_Myoff;
  787. else
  788. _Myoff = 0, ++_Myptr;
  789. }
  790. size_t _Myoff; // offset of bit in word
  791. const _Vbase *_Myptr; // pointer to base of word array
  792. };
  793. // CLASS iterator
  794. class iterator
  795. : public const_iterator
  796. { // iterator for mutable vector<bool>
  797. public:
  798. typedef random_access_iterator_tag iterator_category;
  799. typedef _Bool value_type;
  800. typedef _Dift difference_type;
  801. typedef _Reft *pointer;
  802. typedef _Reft reference;
  803. iterator()
  804. : const_iterator()
  805. { // construct with null pointer
  806. }
  807. iterator(size_t _Off, _VB_TYPENAME _Vbtype::iterator _Where)
  808. : const_iterator(_Off, _Where)
  809. { // construct with offset _Off at iterator _Where
  810. }
  811. reference operator*() const
  812. { // return designated object
  813. return (_Reft(_Myoff, (_Vbase *)_Myptr));
  814. }
  815. iterator& operator++()
  816. { // preincrement
  817. _Inc();
  818. return (*this);
  819. }
  820. iterator operator++(int)
  821. { // postincrement
  822. iterator _Tmp = *this;
  823. _Inc();
  824. return (_Tmp);
  825. }
  826. iterator& operator--()
  827. { // predecrement
  828. _Dec();
  829. return (*this);
  830. }
  831. iterator operator--(int)
  832. { // postdecrement
  833. iterator _Tmp = *this;
  834. _Dec();
  835. return (_Tmp);
  836. }
  837. iterator& operator+=(difference_type _Off)
  838. { // increment by integer
  839. _Myoff += _Off;
  840. _Myptr += _Myoff / _VBITS;
  841. _Myoff %= _VBITS;
  842. return (*this);
  843. }
  844. iterator operator+(difference_type _Off) const
  845. { // return this + integer
  846. iterator _Tmp = *this;
  847. return (_Tmp += _Off);
  848. }
  849. iterator& operator-=(difference_type _Off)
  850. { // decrement by integer
  851. return (*this += -_Off);
  852. }
  853. iterator operator-(difference_type _Off) const
  854. { // return this - integer
  855. iterator _Tmp = *this;
  856. return (_Tmp -= _Off);
  857. }
  858. difference_type operator-(const iterator _Right) const
  859. { // return difference of iterators
  860. return (_VBITS * (_Myptr - _Right._Myptr)
  861. + (difference_type)_Myoff
  862. - (difference_type)_Right._Myoff);
  863. }
  864. reference operator[](difference_type _Off) const
  865. { // subscript
  866. return (*(*this + _Off));
  867. }
  868. friend iterator operator+(difference_type _Off,
  869. const iterator& _Right)
  870. { // return iterator + integer
  871. return (_Right + _Off);
  872. }
  873. };
  874. typedef iterator pointer;
  875. typedef const_iterator const_pointer;
  876. typedef std::reverse_iterator<iterator> reverse_iterator;
  877. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  878. vector()
  879. : _Mysize(0), _Myvec()
  880. { // construct empty vector
  881. }
  882. explicit vector(const _Alloc& _Al)
  883. : _Mysize(0), _Myvec(_Al)
  884. { // construct empty vector, with allocator
  885. }
  886. explicit vector(size_type _Count, bool _Val = false)
  887. : _Mysize(0), _Myvec(_Nw(_Count), (_Vbase)(_Val ? -1 : 0))
  888. { // construct from _Count * _Val
  889. _Trim(_Count);
  890. }
  891. vector(size_type _Count, bool _Val, const _Alloc& _Al)
  892. : _Mysize(0), _Myvec(_Nw(_Count), (_Vbase)(_Val ? -1 : 0), _Al)
  893. { // construct from _Count * _Val, with allocator
  894. _Trim(_Count);
  895. }
  896. template<class _Iter>
  897. vector(_Iter _First, _Iter _Last)
  898. : _Mysize(0), _Myvec()
  899. { // construct from [_First, _Last)
  900. _BConstruct(_First, _Last, _Iter_cat(_First));
  901. }
  902. template<class _Iter>
  903. vector(_Iter _First, _Iter _Last, const _Alloc& _Al)
  904. : _Mysize(0), _Myvec(_Al)
  905. { // construct from [_First, _Last), with allocator
  906. _BConstruct(_First, _Last, _Iter_cat(_First));
  907. }
  908. template<class _Iter>
  909. void _BConstruct(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  910. { // initialize from _Count * _Val
  911. size_type _Num = (size_type)_Count;
  912. _Myvec.assign(_Num, (_Ty)_Val ? -1 : 0);
  913. _Trim(_Num);
  914. }
  915. template<class _Iter>
  916. void _BConstruct(_Iter _First, _Iter _Last, input_iterator_tag)
  917. { // initialize from [_First, _Last), input iterators
  918. insert(begin(), _First, _Last);
  919. }
  920. ~vector()
  921. { // destroy the object
  922. _Mysize = 0;
  923. }
  924. void reserve(size_type _Count)
  925. { // determine new minimum length of allocated storage
  926. _Myvec.reserve(_Nw(_Count));
  927. }
  928. size_type capacity() const
  929. { // return current length of allocated storage
  930. return (_Myvec.capacity() * _VBITS);
  931. }
  932. iterator begin()
  933. { // return iterator for beginning of mutable sequence
  934. return (iterator(0, _Myvec.begin()));
  935. }
  936. const_iterator begin() const
  937. { // return iterator for beginning of nonmutable sequence
  938. return (const_iterator(0, _Myvec.begin()));
  939. }
  940. iterator end()
  941. { // return iterator for end of mutable sequence
  942. iterator _Tmp = begin();
  943. if (0 < _Mysize)
  944. _Tmp += _Mysize;
  945. return (_Tmp);
  946. }
  947. const_iterator end() const
  948. { // return iterator for end of nonmutable sequence
  949. const_iterator _Tmp = begin();
  950. if (0 < _Mysize)
  951. _Tmp += _Mysize;
  952. return (_Tmp);
  953. }
  954. reverse_iterator rbegin()
  955. { // return iterator for beginning of reversed mutable sequence
  956. return (reverse_iterator(end()));
  957. }
  958. const_reverse_iterator rbegin() const
  959. { // return iterator for beginning of reversed nonmutable sequence
  960. return (const_reverse_iterator(end()));
  961. }
  962. reverse_iterator rend()
  963. { // return iterator for end of reversed mutable sequence
  964. return (reverse_iterator(begin()));
  965. }
  966. const_reverse_iterator rend() const
  967. { // return iterator for end of reversed nonmutable sequence
  968. return (const_reverse_iterator(begin()));
  969. }
  970. void resize(size_type _Newsize, bool _Val = false)
  971. { // determine new length, padding with _Val elements as needed
  972. if (size() < _Newsize)
  973. _Insert_n(end(), _Newsize - size(), _Val);
  974. else if (_Newsize < size())
  975. erase(begin() + _Newsize, end());
  976. }
  977. size_type size() const
  978. { // return length of sequence
  979. return (_Mysize);
  980. }
  981. size_type max_size() const
  982. { // return maximum possible length of sequence
  983. const size_type _Maxsize = _Myvec.max_size();
  984. return (_Maxsize < (size_type)(-1) / _VBITS
  985. ? _Maxsize * _VBITS : (size_type)(-1));
  986. }
  987. bool empty() const
  988. { // test if sequence is empty
  989. return (size() == 0);
  990. }
  991. _Alloc get_allocator() const
  992. { // return allocator object for values
  993. return (_Myvec.get_allocator());
  994. }
  995. const_reference at(size_type _Off) const
  996. { // subscript nonmutable sequence with checking
  997. if (size() <= _Off)
  998. _Xran();
  999. return (*(begin() + _Off));
  1000. }
  1001. reference at(size_type _Off)
  1002. { // subscript mutable sequence with checking
  1003. if (size() <= _Off)
  1004. _Xran();
  1005. return (*(begin() + _Off));
  1006. }
  1007. const_reference operator[](size_type _Off) const
  1008. { // subscript nonmutable sequence
  1009. return (*(begin() + _Off));
  1010. }
  1011. reference operator[](size_type _Off)
  1012. { // subscript mutable sequence
  1013. return (*(begin() + _Off));
  1014. }
  1015. reference front()
  1016. { // return first element of mutable sequence
  1017. return (*begin());
  1018. }
  1019. const_reference front() const
  1020. { // return first element of nonmutable sequence
  1021. return (*begin());
  1022. }
  1023. reference back()
  1024. { // return last element of mutable sequence
  1025. return (*(end() - 1));
  1026. }
  1027. const_reference back() const
  1028. { // return last element of nonmutable sequence
  1029. return (*(end() - 1));
  1030. }
  1031. void push_back(bool _Val)
  1032. { // insert element at end
  1033. insert(end(), _Val);
  1034. }
  1035. void pop_back()
  1036. { // erase element at end
  1037. if (!empty())
  1038. erase(end() - 1);
  1039. }
  1040. template<class _Iter>
  1041. void assign(_Iter _First, _Iter _Last)
  1042. { // assign [_First, _Last)
  1043. _Assign(_First, _Last, _Iter_cat(_First));
  1044. }
  1045. template<class _Iter>
  1046. void _Assign(_Iter _Count, _Iter _Val, _Int_iterator_tag)
  1047. { // assign _Count * _Val
  1048. _Assign_n((size_type)_Count, (bool)_Val);
  1049. }
  1050. template<class _Iter>
  1051. void _Assign(_Iter _First, _Iter _Last, input_iterator_tag)
  1052. { // assign [_First, _Last), input iterators
  1053. erase(begin(), end());
  1054. insert(begin(), _First, _Last);
  1055. }
  1056. void assign(size_type _Count, bool _Val)
  1057. { // assign _Count * _Val
  1058. _Assign_n(_Count, _Val);
  1059. }
  1060. iterator insert(iterator _Where, bool _Val)
  1061. { // insert _Val at _Where
  1062. size_type _Off = _Where - begin();
  1063. _Insert_n(_Where, (size_type)1, _Val);
  1064. return (begin() + _Off);
  1065. }
  1066. void insert(iterator _Where, size_type _Count, bool _Val)
  1067. { // insert _Count * _Val at _Where
  1068. _Insert_n(_Where, _Count, _Val);
  1069. }
  1070. template<class _Iter>
  1071. void insert(iterator _Where, _Iter _First, _Iter _Last)
  1072. { // insert [_First, _Last) at _Where
  1073. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  1074. }
  1075. template<class _Iter>
  1076. void _Insert(iterator _Where, _Iter _Count, _Iter _Val,
  1077. _Int_iterator_tag)
  1078. { // insert _Count * _Val at _Where
  1079. _Insert_n(_Where, (size_type)_Count, (bool)_Val);
  1080. }
  1081. template<class _Iter>
  1082. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  1083. input_iterator_tag)
  1084. { // insert [_First, _Last) at _Where, input iterators
  1085. size_type _Off = _Where - begin();
  1086. for (; _First != _Last; ++_First, ++_Off)
  1087. insert(begin() + _Off, *_First);
  1088. }
  1089. template<class _Iter>
  1090. void _Insert(iterator _Where, _Iter _First, _Iter _Last,
  1091. forward_iterator_tag)
  1092. { // insert [_First, _Last) at _Where, forward iterators
  1093. size_type _Capacity = 0;
  1094. _Distance(_First, _Last, _Capacity);
  1095. if (_Capacity == 0)
  1096. ;
  1097. else if (max_size() - size() < _Capacity)
  1098. _Xlen(); // result too long
  1099. else
  1100. { // worth doing
  1101. if (size() == 0)
  1102. { // originally empty, just make room
  1103. _Myvec.resize(_Nw(size() + _Capacity), 0);
  1104. _Where = begin();
  1105. }
  1106. else
  1107. { // make room and copy down suffix
  1108. size_type _Off = _Where - begin();
  1109. _Myvec.resize(_Nw(size() + _Capacity), 0);
  1110. _Where = begin() + _Off;
  1111. copy_backward(_Where, end(), end() + _Capacity);
  1112. }
  1113. copy(_First, _Last, _Where); // add new stuff
  1114. _Mysize += _Capacity;
  1115. }
  1116. }
  1117. iterator erase(iterator _Where)
  1118. { // erase element at _Where
  1119. copy(_Where + 1, end(), _Where);
  1120. _Trim(_Mysize - 1);
  1121. return (_Where);
  1122. }
  1123. iterator erase(iterator _First, iterator _Last)
  1124. { // erase [_First, _Last)
  1125. iterator _Next = copy(_Last, end(), _First);
  1126. _Trim(_Next - begin());
  1127. return (_First);
  1128. }
  1129. void clear()
  1130. { // erase all elements
  1131. erase(begin(), end());
  1132. }
  1133. void flip()
  1134. { // toggle all elements
  1135. for (_Vbtype::iterator _Next = _Myvec.begin();
  1136. _Next != _Myvec.end(); ++_Next)
  1137. *_Next = (_Vbase)~*_Next;
  1138. _Trim(_Mysize);
  1139. }
  1140. bool _Eq(const _Myt& _Right) const
  1141. { // test for vector<bool> equality
  1142. return (_Mysize == _Right._Mysize && _Myvec == _Right._Myvec);
  1143. }
  1144. bool _Lt(const _Myt& _Right) const
  1145. { // test if this < _Right for vector<bool>
  1146. return (lexicographical_compare(begin(), end(),
  1147. _Right.begin(), _Right.end()));
  1148. }
  1149. void swap(_Myt& _Right)
  1150. { // exchange contents with right
  1151. std::swap(_Mysize, _Right._Mysize);
  1152. _Myvec.swap(_Right._Myvec);
  1153. }
  1154. static void swap(reference _Left, reference _Right)
  1155. { // swap _Left and _Right vector<bool> elements
  1156. bool _Val = _Left;
  1157. _Left = _Right;
  1158. _Right = _Val;
  1159. }
  1160. protected:
  1161. void _Assign_n(size_type _Count, bool _Val)
  1162. { // assign _Count * _Val
  1163. erase(begin(), end());
  1164. _Insert_n(begin(), _Count, _Val);
  1165. }
  1166. void _Insert_n(iterator _Where, size_type _Count, bool _Val)
  1167. { // insert _Count * _Val at _Where
  1168. if (_Count == 0)
  1169. ;
  1170. else if (max_size() - size() < _Count)
  1171. _Xlen(); // result too long
  1172. else
  1173. { // worth doing
  1174. if (size() == 0)
  1175. { // originally empty, just make room
  1176. _Myvec.resize(_Nw(size() + _Count), 0);
  1177. _Where = begin();
  1178. }
  1179. else
  1180. { // make room and copy down suffix
  1181. size_type _Off = _Where - begin();
  1182. _Myvec.resize(_Nw(size() + _Count), 0);
  1183. _Where = begin() + _Off;
  1184. copy_backward(_Where, end(), end() + _Count);
  1185. }
  1186. fill(_Where, _Where + _Count, _Val); // add new stuff
  1187. _Mysize += _Count;
  1188. }
  1189. }
  1190. static size_type _Nw(size_type _Count)
  1191. { // return number of base words from number of bits
  1192. return ((_Count + _VBITS - 1) / _VBITS);
  1193. }
  1194. void _Trim(size_type _Size)
  1195. { // trim base vector to exact length in bits
  1196. if (max_size() < _Size)
  1197. _Xlen(); // result too long
  1198. size_type _Words = _Nw(_Size);
  1199. if (_Words < _Myvec.size())
  1200. _Myvec.erase(_Myvec.begin() + _Words, _Myvec.end());
  1201. _Mysize = _Size;
  1202. _Size %= _VBITS;
  1203. if (0 < _Size)
  1204. _Myvec[_Words - 1] &= (_Vbase)((1 << _Size) - 1);
  1205. }
  1206. void _Xlen() const
  1207. { // report a length_error
  1208. _THROW(length_error, "vector<bool> too long");
  1209. }
  1210. void _Xran() const
  1211. { // throw an out_of_range error
  1212. _THROW(out_of_range, "invalid vector<bool> subscript");
  1213. }
  1214. size_type _Mysize; // current length of sequence
  1215. _Vbtype _Myvec; // base vector of words
  1216. };
  1217. typedef vector<_Bool, _Bool_allocator> _Bvector;
  1218. _STD_END
  1219. #pragma warning(default: 4244)
  1220. #pragma warning(pop)
  1221. #pragma pack(pop)
  1222. #endif /* _VECTOR_ */
  1223. /*
  1224. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  1225. * Consult your license regarding permissions and restrictions.
  1226. */
  1227. /*
  1228. * This file is derived from software bearing the following
  1229. * restrictions:
  1230. *
  1231. * Copyright (c) 1994
  1232. * Hewlett-Packard Company
  1233. *
  1234. * Permission to use, copy, modify, distribute and sell this
  1235. * software and its documentation for any purpose is hereby
  1236. * granted without fee, provided that the above copyright notice
  1237. * appear in all copies and that both that copyright notice and
  1238. * this permission notice appear in supporting documentation.
  1239. * Hewlett-Packard Company makes no representations about the
  1240. * suitability of this software for any purpose. It is provided
  1241. * "as is" without express or implied warranty.
  1242. V3.10:0009 */