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.

1021 lines
27 KiB

  1. // deque standard header
  2. #pragma once
  3. #ifndef _DEQUE_
  4. #define _DEQUE_
  5. #include <memory>
  6. #include <stdexcept>
  7. #pragma pack(push,8)
  8. #pragma warning(push,3)
  9. _STD_BEGIN
  10. // TEMPLATE CLASS _Deque_map
  11. template<class _Ty,
  12. class _Alloc>
  13. class _Deque_map
  14. { // ultimate base class for deque to hold allocator _Almap
  15. protected:
  16. _Deque_map(_Alloc _Al)
  17. : _Almap(_Al)
  18. { // construct allocator from _Al
  19. }
  20. typedef typename _Alloc::_TEMPLATE_MEMBER rebind<_Ty>::other::pointer
  21. _Tptr;
  22. typename _Alloc::_TEMPLATE_MEMBER rebind<_Tptr>::other
  23. _Almap; // allocator object for maps
  24. };
  25. // TEMPLATE CLASS _Deque_val
  26. template<class _Ty,
  27. class _Alloc>
  28. class _Deque_val
  29. : public _Deque_map<_Ty, _Alloc>
  30. { // base class for deque to hold allocator _Alval
  31. protected:
  32. _Deque_val(_Alloc _Al = _Alloc())
  33. : _Deque_map<_Ty, _Alloc>(_Al), _Alval(_Al)
  34. { // construct allocator and base from _Al
  35. }
  36. typedef typename _Alloc::_TEMPLATE_MEMBER rebind<_Ty>::other
  37. _Alty;
  38. _Alty _Alval; // allocator object for stored elements
  39. };
  40. // TEMPLATE CLASS deque
  41. template<class _Ty,
  42. class _Ax = allocator<_Ty> >
  43. class deque
  44. : public _Deque_val<_Ty, _Ax>
  45. { // circular queue of pointers to blocks
  46. public:
  47. enum
  48. { // deque parameters
  49. _DEQUEMAPSIZ = 8, /* minimum map size, at least 1 */
  50. _DEQUESIZ = sizeof (_Ty) <= 1 ? 16
  51. : sizeof (_Ty) <= 2 ? 8
  52. : sizeof (_Ty) <= 4 ? 4
  53. : sizeof (_Ty) <= 8 ? 2 : 1}; // elements per block
  54. typedef deque<_Ty, _Ax> _Myt;
  55. typedef _Deque_val<_Ty, _Ax> _Mybase;
  56. typedef typename _Mybase::_Alty _Alloc;
  57. typedef _Alloc allocator_type;
  58. typedef typename _Alloc::size_type size_type;
  59. typedef typename _Alloc::difference_type _Dift;
  60. typedef _Dift difference_type;
  61. typedef typename _Alloc::pointer _Tptr;
  62. typedef typename _Alloc::const_pointer _Ctptr;
  63. typedef _Tptr pointer;
  64. typedef _Ctptr const_pointer;
  65. typedef _POINTER_X(_Tptr, _Alloc) _Mapptr;
  66. typedef typename _Alloc::reference _Reft;
  67. typedef _Reft reference;
  68. typedef typename _Alloc::const_reference const_reference;
  69. typedef typename _Alloc::value_type value_type;
  70. // CLASS const_iterator
  71. class const_iterator;
  72. friend class const_iterator;
  73. class const_iterator
  74. : public _Ranit<_Ty, _Dift, _Ctptr, const_reference>
  75. { // iterator for nonmutable deque
  76. public:
  77. typedef random_access_iterator_tag iterator_category;
  78. typedef _Ty value_type;
  79. typedef _Dift difference_type;
  80. typedef _Ctptr pointer;
  81. typedef const_reference reference;
  82. const_iterator()
  83. : _Myoff(0), _Mydeque(0)
  84. { // construct with null deque pointer
  85. }
  86. const_iterator(difference_type _Off,
  87. const deque<_Ty, _Alloc> *_Pdeque)
  88. : _Myoff(_Off), _Mydeque(_Pdeque)
  89. { // construct with offset _Off in *_Pdeque
  90. }
  91. const_reference operator*() const
  92. { // return designated object
  93. size_type _Block = _Myoff / _DEQUESIZ;
  94. size_type _Off = _Myoff - _Block * _DEQUESIZ;
  95. if (_Mydeque->_Mapsize <= _Block)
  96. _Block -= _Mydeque->_Mapsize;
  97. return ((_Mydeque->_Map)[_Block][_Off]);
  98. }
  99. _Ctptr operator->() const
  100. { // return pointer to class object
  101. return (&**this);
  102. }
  103. const_iterator& operator++()
  104. { // preincrement
  105. ++_Myoff;
  106. return (*this);
  107. }
  108. const_iterator operator++(int)
  109. { // postincrement
  110. const_iterator _Tmp = *this;
  111. ++*this;
  112. return (_Tmp);
  113. }
  114. const_iterator& operator--()
  115. { // predecrement
  116. --_Myoff;
  117. return (*this);
  118. }
  119. const_iterator operator--(int)
  120. { // postdecrement
  121. const_iterator _Tmp = *this;
  122. --*this;
  123. return (_Tmp);
  124. }
  125. const_iterator& operator+=(difference_type _Off)
  126. { // increment by integer
  127. _Myoff += _Off;
  128. return (*this);
  129. }
  130. const_iterator operator+(difference_type _Off) const
  131. { // return this + integer
  132. const_iterator _Tmp = *this;
  133. return (_Tmp += _Off);
  134. }
  135. const_iterator& operator-=(difference_type _Off)
  136. { // decrement by integer
  137. return (*this += -_Off);
  138. }
  139. const_iterator operator-(difference_type _Off) const
  140. { // return this - integer
  141. const_iterator _Tmp = *this;
  142. return (_Tmp -= _Off);
  143. }
  144. difference_type operator-(const const_iterator& _Right) const
  145. { // return difference of iterators
  146. return (_Myoff - _Right._Myoff);
  147. }
  148. const_reference operator[](difference_type _Off) const
  149. { // subscript
  150. return (*(*this + _Off));
  151. }
  152. bool operator==(const const_iterator& _Right) const
  153. { // test for iterator equality
  154. return (_Mydeque == _Right._Mydeque && _Myoff == _Right._Myoff);
  155. }
  156. bool operator!=(const const_iterator& _Right) const
  157. { // test for iterator inequality
  158. return (!(*this == _Right));
  159. }
  160. bool operator<(const const_iterator& _Right) const
  161. { // test if this < _Right
  162. return (_Myoff < _Right._Myoff);
  163. }
  164. bool operator>(const const_iterator& _Right) const
  165. { // test if this > _Right
  166. return (_Right < *this);
  167. }
  168. bool operator<=(const const_iterator& _Right) const
  169. { // test if this <= _Right
  170. return (!(_Right < *this));
  171. }
  172. bool operator>=(const const_iterator& _Right) const
  173. { // test if this >= _Right
  174. return (!(*this < _Right));
  175. }
  176. friend const_iterator operator+(difference_type _Off,
  177. const const_iterator& _Right)
  178. { // return iterator + integer
  179. return (_Right + _Off);
  180. }
  181. protected:
  182. difference_type _Myoff; // offset of element in deque
  183. const deque<_Ty, _Alloc> *_Mydeque; // pointer to deque
  184. };
  185. // CLASS iterator
  186. class iterator;
  187. friend class iterator;
  188. class iterator
  189. : public const_iterator
  190. { // iterator for mutable deque
  191. public:
  192. typedef random_access_iterator_tag iterator_category;
  193. typedef _Ty value_type;
  194. typedef _Dift difference_type;
  195. typedef _Tptr pointer;
  196. typedef _Reft reference;
  197. iterator()
  198. { // construct with null deque pointer
  199. }
  200. iterator(difference_type _Off, const deque<_Ty, _Alloc> *_Pdeque)
  201. : const_iterator(_Off, _Pdeque)
  202. { // construct with offset _Off in *_Pdeque
  203. }
  204. reference operator*() const
  205. { // return designated object
  206. size_type _Block = _Myoff / _DEQUESIZ;
  207. size_type _Off = _Myoff - _Block * _DEQUESIZ;
  208. if (_Mydeque->_Mapsize <= _Block)
  209. _Block -= _Mydeque->_Mapsize;
  210. return ((_Mydeque->_Map)[_Block][_Off]);
  211. }
  212. _Tptr operator->() const
  213. { // return pointer to class object
  214. return (&**this);
  215. }
  216. iterator& operator++()
  217. { // preincrement
  218. ++_Myoff;
  219. return (*this);
  220. }
  221. iterator operator++(int)
  222. { // postincrement
  223. iterator _Tmp = *this;
  224. ++*this;
  225. return (_Tmp);
  226. }
  227. iterator& operator--()
  228. { // predecrement
  229. --_Myoff;
  230. return (*this);
  231. }
  232. iterator operator--(int)
  233. { // postdecrement
  234. iterator _Tmp = *this;
  235. --*this;
  236. return (_Tmp);
  237. }
  238. iterator& operator+=(difference_type _Off)
  239. { // increment by integer
  240. _Myoff += _Off;
  241. return (*this);
  242. }
  243. iterator operator+(difference_type _Off) const
  244. { // return this + integer
  245. iterator _Tmp = *this;
  246. return (_Tmp += _Off);
  247. }
  248. iterator& operator-=(difference_type _Off)
  249. { // decrement by integer
  250. return (*this += -_Off);
  251. }
  252. iterator operator-(difference_type _Off) const
  253. { // return this - integer
  254. iterator _Tmp = *this;
  255. return (_Tmp -= _Off);
  256. }
  257. difference_type operator-(const iterator& _Right) const
  258. { // return difference of iterators
  259. return (_Myoff - _Right._Myoff);
  260. }
  261. reference operator[](difference_type _Off) const
  262. { // subscript
  263. return (*(*this + _Off));
  264. }
  265. friend iterator operator+(difference_type _Off,
  266. const iterator& _Right)
  267. { // return iterator + integer
  268. return (_Right + _Off);
  269. }
  270. };
  271. typedef std::reverse_iterator<iterator> reverse_iterator;
  272. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  273. deque()
  274. : _Mybase(), _Map(0),
  275. _Mapsize(0), _Myoff(0), _Mysize(0)
  276. { // construct empty deque
  277. }
  278. explicit deque(const _Alloc& _Al)
  279. : _Mybase(_Al), _Map(0),
  280. _Mapsize(0), _Myoff(0), _Mysize(0)
  281. { // construct empty deque with allocator
  282. }
  283. explicit deque(size_type _Count)
  284. : _Mybase(), _Map(0),
  285. _Mapsize(0), _Myoff(0), _Mysize(0)
  286. { // construct from _Count * _Ty()
  287. _Construct_n(_Count, _Ty());
  288. }
  289. deque(size_type _Count, const _Ty& _Val)
  290. : _Mybase(), _Map(0),
  291. _Mapsize(0), _Myoff(0), _Mysize(0)
  292. { // construct from _Count * _Val
  293. _Construct_n(_Count, _Val);
  294. }
  295. deque(size_type _Count, const _Ty& _Val, const _Alloc& _Al)
  296. : _Mybase(_Al), _Map(0),
  297. _Mapsize(0), _Myoff(0), _Mysize(0)
  298. { // construct from _Count * _Val with allocator
  299. _Construct_n(_Count, _Val);
  300. }
  301. deque(const _Myt& _Right)
  302. : _Mybase(_Right._Alval), _Map(0),
  303. _Mapsize(0), _Myoff(0), _Mysize(0)
  304. { // construct by copying _Right
  305. _TRY_BEGIN
  306. insert(begin(), _Right.begin(), _Right.end());
  307. _CATCH_ALL
  308. _Tidy();
  309. _RERAISE;
  310. _CATCH_END
  311. }
  312. template<class _It>
  313. deque(_It _First, _It _Last)
  314. : _Mybase(), _Map(0),
  315. _Mapsize(0), _Myoff(0), _Mysize(0)
  316. { // construct from [_First, _Last)
  317. _Construct(_First, _Last, _Iter_cat(_First));
  318. }
  319. template<class _It>
  320. deque(_It _First, _It _Last, const _Alloc& _Al)
  321. : _Mybase(_Al), _Map(0),
  322. _Mapsize(0), _Myoff(0), _Mysize(0)
  323. { // construct from [_First, _Last) with allocator
  324. _Construct(_First, _Last, _Iter_cat(_First));
  325. }
  326. template<class _It>
  327. void _Construct(_It _Count, _It _Val, _Int_iterator_tag)
  328. { // initialize from _Count * _Val
  329. _Construct_n((size_type)_Count, (_Ty)_Val);
  330. }
  331. template<class _It>
  332. void _Construct(_It _First, _It _Last, input_iterator_tag)
  333. { // initialize from [_First, _Last), input iterators
  334. _TRY_BEGIN
  335. insert(begin(), _First, _Last);
  336. _CATCH_ALL
  337. _Tidy();
  338. _RERAISE;
  339. _CATCH_END
  340. }
  341. void _Construct_n(size_type _Count, const _Ty& _Val)
  342. { // construct from _Count * _Val
  343. _TRY_BEGIN
  344. _Insert_n(begin(), _Count, _Val);
  345. _CATCH_ALL
  346. _Tidy();
  347. _RERAISE;
  348. _CATCH_END
  349. }
  350. ~deque()
  351. { // destroy the deque
  352. _Tidy();
  353. }
  354. _Myt& operator=(const _Myt& _Right)
  355. { // assign _Right
  356. if (this == &_Right)
  357. ;
  358. else if (_Right.size() == 0)
  359. clear();
  360. else if (_Right.size() <= size())
  361. { // new sequence not longer, assign elements and erase unused
  362. iterator _Mid = copy(_Right.begin(), _Right.end(), begin());
  363. erase(_Mid, end());
  364. }
  365. else
  366. { // new sequence longer, assign elements and append rest
  367. const_iterator _Mid = _Right.begin() + size();
  368. copy(_Right.begin(), _Mid, begin());
  369. insert(end(), _Mid, _Right.end());
  370. }
  371. return (*this);
  372. }
  373. iterator begin()
  374. { // return iterator for beginning of mutable sequence
  375. return (iterator(_Myoff, this));
  376. }
  377. const_iterator begin() const
  378. { // return iterator for beginning of nonmutable sequence
  379. return (const_iterator(_Myoff, this));
  380. }
  381. iterator end()
  382. { // return iterator for end of mutable sequence
  383. return (iterator(_Myoff + _Mysize, this));
  384. }
  385. const_iterator end() const
  386. { // return iterator for end of nonmutable sequence
  387. return (const_iterator(_Myoff + _Mysize, this));
  388. }
  389. reverse_iterator rbegin()
  390. { // return iterator for beginning of reversed mutable sequence
  391. return (reverse_iterator(end()));
  392. }
  393. const_reverse_iterator rbegin() const
  394. { // return iterator for beginning of reversed nonmutable sequence
  395. return (const_reverse_iterator(end()));
  396. }
  397. reverse_iterator rend()
  398. { // return iterator for end of reversed mutable sequence
  399. return (reverse_iterator(begin()));
  400. }
  401. const_reverse_iterator rend() const
  402. { // return iterator for end of reversed nonmutable sequence
  403. return (const_reverse_iterator(begin()));
  404. }
  405. void resize(size_type _Newsize)
  406. { // determine new length, padding with _Ty() elements as needed
  407. resize(_Newsize, _Ty());
  408. }
  409. void resize(size_type _Newsize, _Ty _Val)
  410. { // determine new length, padding with _Val elements as needed
  411. if (size() < _Newsize)
  412. _Insert_n(end(), _Newsize - size(), _Val);
  413. else if (_Newsize < size())
  414. erase(begin() + _Newsize, end());
  415. }
  416. size_type size() const
  417. { // return length of sequence
  418. return (_Mysize);
  419. }
  420. size_type max_size() const
  421. { // return maximum possible length of sequence
  422. return (this->_Alval.max_size());
  423. }
  424. bool empty() const
  425. { // test if sequence is empty
  426. return (size() == 0);
  427. }
  428. allocator_type get_allocator() const
  429. { // return allocator object for values
  430. return (this->_Alval);
  431. }
  432. const_reference at(size_type _Pos) const
  433. { // subscript nonmutable sequence with checking
  434. if (size() <= _Pos)
  435. _Xran();
  436. return (*(begin() + _Pos));
  437. }
  438. reference at(size_type _Pos)
  439. { // subscript mutable sequence with checking
  440. if (size() <= _Pos)
  441. _Xran();
  442. return (*(begin() + _Pos));
  443. }
  444. const_reference operator[](size_type _Pos) const
  445. { // subscript nonmutable sequence
  446. return (*(begin() + _Pos));
  447. }
  448. reference operator[](size_type _Pos)
  449. { // subscript mutable sequence
  450. return (*(begin() + _Pos));
  451. }
  452. reference front()
  453. { // return first element of mutable sequence
  454. return (*begin());
  455. }
  456. const_reference front() const
  457. { // return first element of nonmutable sequence
  458. return (*begin());
  459. }
  460. reference back()
  461. { // return last element of mutable sequence
  462. return (*(end() - 1));
  463. }
  464. const_reference back() const
  465. { // return last element of nonmutable sequence
  466. return (*(end() - 1));
  467. }
  468. void push_front(const _Ty& _Val)
  469. { // insert element at beginning
  470. if (_Myoff % _DEQUESIZ == 0
  471. && _Mapsize <= (_Mysize + _DEQUESIZ) / _DEQUESIZ)
  472. _Growmap(1);
  473. size_type _Newoff = _Myoff != 0 ? _Myoff
  474. : _Mapsize * _DEQUESIZ;
  475. size_type _Block = --_Newoff / _DEQUESIZ;
  476. if (_Map[_Block] == 0)
  477. _Map[_Block] = this->_Alval.allocate(_DEQUESIZ, (void *)0);
  478. this->_Alval.construct(_Map[_Block] + _Newoff % _DEQUESIZ, _Val);
  479. _Myoff = _Newoff;
  480. ++_Mysize;
  481. }
  482. void pop_front()
  483. { // erase element at beginning
  484. if (!empty())
  485. { // something to erase, do it
  486. size_type _Block = _Myoff / _DEQUESIZ;
  487. this->_Alval.destroy(_Map[_Block] + _Myoff % _DEQUESIZ);
  488. if (_Mapsize * _DEQUESIZ <= ++_Myoff)
  489. _Myoff = 0;
  490. if (--_Mysize == 0)
  491. _Myoff = 0;
  492. }
  493. }
  494. void push_back(const _Ty& _Val)
  495. { // insert element at end
  496. if ((_Myoff + _Mysize) % _DEQUESIZ == 0
  497. && _Mapsize <= (_Mysize + _DEQUESIZ) / _DEQUESIZ)
  498. _Growmap(1);
  499. size_type _Newoff = _Myoff + _Mysize;
  500. size_type _Block = _Newoff / _DEQUESIZ;
  501. if (_Mapsize <= _Block)
  502. _Block -= _Mapsize;
  503. if (_Map[_Block] == 0)
  504. _Map[_Block] = this->_Alval.allocate(_DEQUESIZ, (void *)0);
  505. this->_Alval.construct(_Map[_Block] + _Newoff % _DEQUESIZ, _Val);
  506. ++_Mysize;
  507. }
  508. void pop_back()
  509. { // erase element at end
  510. if (!empty())
  511. { // something to erase, do it
  512. size_type _Newoff = _Mysize + _Myoff - 1;
  513. size_type _Block = _Newoff / _DEQUESIZ;
  514. if (_Mapsize <= _Block)
  515. _Block -= _Mapsize;
  516. this->_Alval.destroy(_Map[_Block] + _Newoff % _DEQUESIZ);
  517. if (--_Mysize == 0)
  518. _Myoff = 0;
  519. }
  520. }
  521. template<class _It>
  522. void assign(_It _First, _It _Last)
  523. { // assign [_First, _Last)
  524. _Assign(_First, _Last, _Iter_cat(_First));
  525. }
  526. template<class _It>
  527. void _Assign(_It _Count, _It _Val, _Int_iterator_tag)
  528. { // assign _Count * _Val
  529. _Assign_n((size_type)_Count, (_Ty)_Val);
  530. }
  531. template<class _It>
  532. void _Assign(_It _First, _It _Last, input_iterator_tag)
  533. { // assign [_First, _Last), input iterators
  534. erase(begin(), end());
  535. insert(begin(), _First, _Last);
  536. }
  537. void assign(size_type _Count, const _Ty& _Val)
  538. { // assign _Count * _Val
  539. _Assign_n(_Count, _Val);
  540. }
  541. iterator insert(iterator _Where, const _Ty& _Val)
  542. { // insert _Val at _Where
  543. if (_Where == begin())
  544. { // insert at front
  545. push_front(_Val);
  546. return (begin());
  547. }
  548. else if (_Where == end())
  549. { // insert at back
  550. push_back(_Val);
  551. return (end() - 1);
  552. }
  553. else
  554. { // insert inside sequence
  555. iterator _Mid;
  556. size_type _Off = _Where - begin();
  557. _Ty _Tmp = _Val; // in case _Val is in sequence
  558. if (_Off < size() / 2)
  559. { // closer to front, push to front then copy
  560. push_front(front());
  561. _Mid = begin() + _Off;
  562. copy(begin() + 2, _Mid + 1, begin() + 1);
  563. }
  564. else
  565. { // closer to back, push to back then copy
  566. push_back(back());
  567. _Mid = begin() + _Off;
  568. copy_backward(_Mid, end() - 2, end() - 1);
  569. }
  570. *_Mid = _Tmp; // store inserted value
  571. return (_Mid);
  572. }
  573. }
  574. void insert(iterator _Where, size_type _Count, const _Ty& _Val)
  575. { // insert _Count * _Val at _Where
  576. _Insert_n(_Where, _Count, _Val);
  577. }
  578. template<class _It>
  579. void insert(iterator _Where, _It _First, _It _Last)
  580. { // insert [_First, _Last) at _Where
  581. _Insert(_Where, _First, _Last, _Iter_cat(_First));
  582. }
  583. template<class _It>
  584. void _Insert(iterator _Where, _It _Count, _It _Val,
  585. _Int_iterator_tag)
  586. { // insert _Count * _Val at _Where
  587. _Insert_n(_Where, (size_type)_Count, (_Ty)_Val);
  588. }
  589. template<class _It>
  590. void _Insert(iterator _Where, _It _First, _It _Last,
  591. input_iterator_tag)
  592. { // insert [_First, _Last) at _Where, input iterators
  593. size_type _Off = _Where - begin();
  594. for (; _First != _Last; ++_First, ++_Off)
  595. insert(begin() + _Off, *_First);
  596. }
  597. template<class _It>
  598. void _Insert(iterator _Where, _It _First, _It _Last,
  599. bidirectional_iterator_tag)
  600. { // insert [_First, _Last) at _Where, bidirectional iterators
  601. size_type _Count = 0;
  602. _Distance(_First, _Last, _Count);
  603. size_type _Num;
  604. size_type _Off = _Where - begin();
  605. size_type _Rem = _Mysize - _Off;
  606. if (_Off < _Rem)
  607. if (_Off < _Count) // closer to front
  608. { // insert longer than prefix
  609. _It _Mid = _First;
  610. advance(_Mid, _Count - _Off);
  611. for (_It _Next = _Mid; _First != _Next; )
  612. push_front(*--_Next); // push head of insert
  613. for (_Num = _Off; 0 < _Num; --_Num)
  614. push_front(begin()[_Count - 1]); // push prefix
  615. copy(_Mid, _Last, begin() + _Count); // copy rest of insert
  616. }
  617. else
  618. { // insert not longer than prefix
  619. for (_Num = _Count; 0 < _Num; --_Num)
  620. push_front(begin()[_Count - 1]); // push part of prefix
  621. iterator _Mid = begin() + _Count;
  622. copy(_Mid + _Count, _Mid + _Off, _Mid); // copy rest of prefix
  623. copy(_First, _Last, begin() + _Off); // copy in insert
  624. }
  625. else
  626. if (_Rem < _Count) // closer to back
  627. { // insert longer than suffix
  628. _It _Mid = _First;
  629. advance(_Mid, _Rem);
  630. for (_It _Next = _Mid; _Next != _Last; ++_Next)
  631. push_back(*_Next); // push tail of insert
  632. for (_Num = 0; _Num < _Rem; ++_Num)
  633. push_back(begin()[_Off + _Num]); // push suffix
  634. copy(_First, _Mid, begin() + _Off); // copy rest of insert
  635. }
  636. else
  637. { // insert not longer than suffix
  638. for (_Num = 0; _Num < _Count; ++_Num)
  639. push_back(begin()[_Off + _Rem
  640. - _Count + _Num]); // push part of suffix
  641. iterator _Mid = begin() + _Off;
  642. copy_backward(_Mid, _Mid + _Rem - _Count,
  643. _Mid + _Rem); // copy rest of prefix
  644. copy(_First, _Last, _Mid); // copy in values
  645. }
  646. }
  647. iterator erase(iterator _Where)
  648. { // erase element at _Where
  649. return (erase(_Where, _Where + 1));
  650. }
  651. iterator erase(iterator _First, iterator _Last)
  652. { // erase [_First, _Last)
  653. size_type _Count = _Last - _First;
  654. size_type _Off = _First - begin();
  655. if (_Off < (size_type)(end() - _Last))
  656. { // closer to front
  657. copy_backward(begin(), _First, _Last); // copy over hole
  658. for (; 0 < _Count; --_Count)
  659. pop_front(); // pop copied elements
  660. }
  661. else
  662. { // closer to back
  663. copy(_Last, end(), _First); // copy over hole
  664. for (; 0 < _Count; --_Count)
  665. pop_back(); // pop copied elements
  666. }
  667. return (_Off == 0 ? begin() : begin() + _Off);
  668. }
  669. void clear()
  670. { // erase all
  671. _Tidy();
  672. }
  673. void swap(_Myt& _Right)
  674. { // exchange contents with _Right
  675. if (_Alval == _Right._Alval)
  676. { // same allocator, swap control information
  677. std::swap(_Map, _Right._Map);
  678. std::swap(_Mapsize, _Right._Mapsize);
  679. std::swap(_Myoff, _Right._Myoff);
  680. std::swap(_Mysize, _Right._Mysize);
  681. }
  682. else
  683. { // different allocator, do multiple assigns
  684. _Myt _Ts = *this; *this = _Right, _Right = _Ts;
  685. }
  686. }
  687. protected:
  688. void _Assign_n(size_type _Count, const _Ty& _Val)
  689. { // assign _Count * _Val
  690. _Ty _Tmp = _Val; // in case _Val is in sequence
  691. erase(begin(), end());
  692. _Insert_n(begin(), _Count, _Tmp);
  693. }
  694. void _Insert_n(iterator _Where, size_type _Count, const _Ty& _Val)
  695. { // insert _Count * _Val at _Where
  696. iterator _Mid;
  697. size_type _Num;
  698. size_type _Off = _Where - begin();
  699. size_type _Rem = _Mysize - _Off;
  700. if (_Off < _Rem)
  701. if (_Off < _Count) // closer to front
  702. { // insert longer than prefix
  703. for (_Num = _Count - _Off; 0 < _Num; --_Num)
  704. push_front(_Val); // push excess values
  705. for (_Num = _Off; 0 < _Num; --_Num)
  706. push_front(begin()[_Count - 1]); // push prefix
  707. _Mid = begin() + _Count;
  708. fill(_Mid, _Mid + _Off, _Val); // fill in rest of values
  709. }
  710. else
  711. { // insert not longer than prefix
  712. for (_Num = _Count; 0 < _Num; --_Num)
  713. push_front(begin()[_Count - 1]); // push part of prefix
  714. _Mid = begin() + _Count;
  715. _Ty _Tmp = _Val; // in case _Val is in sequence
  716. copy(_Mid + _Count, _Mid + _Off, _Mid); // copy rest of prefix
  717. fill(begin() + _Off, _Mid + _Off, _Tmp); // fill in values
  718. }
  719. else
  720. if (_Rem < _Count) // closer to back
  721. { // insert longer than suffix
  722. for (_Num = _Count - _Rem; 0 < _Num; --_Num)
  723. push_back(_Val); // push excess values
  724. for (_Num = 0; _Num < _Rem; ++_Num)
  725. push_back(begin()[_Off + _Num]); // push suffix
  726. _Mid = begin() + _Off;
  727. fill(_Mid, _Mid + _Rem, _Val); // fill in rest of values
  728. }
  729. else
  730. { // insert not longer than prefix
  731. for (_Num = 0; _Num < _Count; ++_Num)
  732. push_back(begin()[_Off + _Rem
  733. - _Count + _Num]); // push part of prefix
  734. _Mid = begin() + _Off;
  735. _Ty _Tmp = _Val; // in case _Val is in sequence
  736. copy_backward(_Mid, _Mid + _Rem - _Count,
  737. _Mid + _Rem); // copy rest of prefix
  738. fill(_Mid, _Mid + _Count, _Tmp); // fill in values
  739. }
  740. }
  741. void _Xlen() const
  742. { // report length error
  743. _THROW(length_error, "deque<T> too long");
  744. }
  745. void _Xran() const
  746. { // report range error
  747. _THROW(out_of_range, "invalid deque<T> subscript");
  748. }
  749. void _Growmap(size_type _Count)
  750. { // grow map by _Count pointers
  751. if (max_size() / _DEQUESIZ - _Mapsize < _Count)
  752. _Xlen(); // result too long
  753. size_type _Inc = _Mapsize / 2; // try to grow by 50%
  754. if (_Inc < _DEQUEMAPSIZ)
  755. _Inc = _DEQUEMAPSIZ;
  756. if (_Count < _Inc && _Mapsize <= max_size() / _DEQUESIZ - _Inc)
  757. _Count = _Inc;
  758. size_type _Myboff = _Myoff / _DEQUESIZ;
  759. _Mapptr _Newmap = this->_Almap.allocate(_Mapsize + _Count, (void *)0);
  760. _Mapptr _Myptr = _Newmap + _Myboff;
  761. _Myptr = _Uninitialized_copy(_Map + _Myboff,
  762. _Map + _Mapsize, _Myptr, _Almap); // copy from initial to end
  763. if (_Myboff <= _Count)
  764. { // increment greater than offset of initial block
  765. _Myptr = _Uninitialized_copy(_Map,
  766. _Map + _Myboff, _Myptr, _Almap); // copy rest of old
  767. _Uninitialized_fill_n(_Myptr, _Count - _Myboff,
  768. (_Tptr)0, _Almap); // clear suffix of new
  769. _Uninitialized_fill_n(_Newmap, _Myboff,
  770. (_Tptr)0, _Almap); // clear prefix of new
  771. }
  772. else
  773. { // increment not greater than offset of initial block
  774. _Uninitialized_copy(_Map,
  775. _Map + _Count, _Myptr, _Almap); // copy more old to end
  776. _Myptr = _Uninitialized_copy(_Map + _Count,
  777. _Map + _Myboff, _Newmap, _Almap); // copy rest of old
  778. _Uninitialized_fill_n(_Myptr, _Count,
  779. (_Tptr)0, _Almap); // clear rest to initial block
  780. }
  781. _Destroy_range(_Map + _Myboff, _Map + _Mapsize, _Almap);
  782. this->_Almap.deallocate(_Map, _Mapsize); // free storage for old
  783. _Map = _Newmap; // point at new
  784. _Mapsize += _Count;
  785. }
  786. void _Tidy()
  787. { // free all storage
  788. while (!empty())
  789. pop_back();
  790. for (size_type _Count = _Mapsize; 0 < _Count; )
  791. { // free storage for a block and destroy pointer
  792. this->_Alval.deallocate(*(_Map + --_Count), _DEQUESIZ);
  793. this->_Almap.destroy(_Map + _Count);
  794. }
  795. this->_Almap.deallocate(_Map, _Mapsize); // free storage for map
  796. _Mapsize = 0;
  797. _Map = 0;
  798. }
  799. _Mapptr _Map; // pointer to array of pointers to blocks
  800. size_type _Mapsize; // size of map array
  801. size_type _Myoff; // offset of initial element
  802. size_type _Mysize; // current length of sequence
  803. };
  804. // deque TEMPLATE FUNCTIONS
  805. template<class _Ty,
  806. class _Alloc> inline
  807. bool operator==(const deque<_Ty, _Alloc>& _Left,
  808. const deque<_Ty, _Alloc>& _Right)
  809. { // test for deque equality
  810. return (_Left.size() == _Right.size()
  811. && equal(_Left.begin(), _Left.end(), _Right.begin()));
  812. }
  813. template<class _Ty,
  814. class _Alloc> inline
  815. bool operator!=(const deque<_Ty, _Alloc>& _Left,
  816. const deque<_Ty, _Alloc>& _Right)
  817. { // test for deque inequality
  818. return (!(_Left == _Right));
  819. }
  820. template<class _Ty,
  821. class _Alloc> inline
  822. bool operator<(const deque<_Ty, _Alloc>& _Left,
  823. const deque<_Ty, _Alloc>& _Right)
  824. { // test if _Left < _Right for deques
  825. return (lexicographical_compare(_Left.begin(), _Left.end(),
  826. _Right.begin(), _Right.end()));
  827. }
  828. template<class _Ty,
  829. class _Alloc> inline
  830. bool operator<=(const deque<_Ty, _Alloc>& _Left,
  831. const deque<_Ty, _Alloc>& _Right)
  832. { // test if _Left <= _Right for deques
  833. return (!(_Right < _Left));
  834. }
  835. template<class _Ty,
  836. class _Alloc> inline
  837. bool operator>(const deque<_Ty, _Alloc>& _Left,
  838. const deque<_Ty, _Alloc>& _Right)
  839. { // test if _Left > _Right for deques
  840. return (_Right < _Left);
  841. }
  842. template<class _Ty,
  843. class _Alloc> inline
  844. bool operator>=(const deque<_Ty, _Alloc>& _Left,
  845. const deque<_Ty, _Alloc>& _Right)
  846. { // test if _Left >= _Right for deques
  847. return (!(_Left < _Right));
  848. }
  849. template <class _Ty,
  850. class _Alloc> inline
  851. void swap(deque<_Ty, _Alloc>& _Left, deque<_Ty, _Alloc>& _Right)
  852. { // swap _Left and _Right deques
  853. _Left.swap(_Right);
  854. }
  855. _STD_END
  856. #pragma warning(pop)
  857. #pragma pack(pop)
  858. #endif /* _DEQUE_ */
  859. /*
  860. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  861. * Consult your license regarding permissions and restrictions.
  862. */
  863. /*
  864. * This file is derived from software bearing the following
  865. * restrictions:
  866. *
  867. * Copyright (c) 1994
  868. * Hewlett-Packard Company
  869. *
  870. * Permission to use, copy, modify, distribute and sell this
  871. * software and its documentation for any purpose is hereby
  872. * granted without fee, provided that the above copyright notice
  873. * appear in all copies and that both that copyright notice and
  874. * this permission notice appear in supporting documentation.
  875. * Hewlett-Packard Company makes no representations about the
  876. * suitability of this software for any purpose. It is provided
  877. * "as is" without express or implied warranty.
  878. V3.10:0009 */