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.

555 lines
16 KiB

  1. // deque standard header
  2. #ifndef _DEQUE_
  3. #define _DEQUE_
  4. #include <iterator>
  5. #include <memory>
  6. #include <stdexcept>
  7. #include <xutility>
  8. #ifdef _MSC_VER
  9. #pragma pack(push,8)
  10. #endif /* _MSC_VER */
  11. _STD_BEGIN
  12. #define _DEQUEMAPSIZ 2
  13. #define _DEQUESIZ (4096 < sizeof (_Ty) ? 1 : 4096 / sizeof (_Ty))
  14. // TEMPLATE CLASS deque
  15. template<class _Ty, class _A = allocator<_Ty> >
  16. class deque {
  17. public:
  18. typedef deque<_Ty, _A> _Myt;
  19. typedef _A allocator_type;
  20. typedef _A::size_type size_type;
  21. typedef _A::difference_type difference_type;
  22. typedef _A::pointer _Tptr;
  23. typedef _A::const_pointer _Ctptr;
  24. typedef _POINTER_X(_Tptr, _A) _Mapptr;
  25. typedef _A::reference reference;
  26. typedef _A::const_reference const_reference;
  27. typedef _A::value_type value_type;
  28. // CLASS iterator
  29. class iterator : public _Ranit<_Ty, difference_type> {
  30. public:
  31. friend class deque<_Ty, _A>;
  32. iterator()
  33. : _First(0), _Last(0), _Next(0), _Map(0) {}
  34. iterator(_Tptr _P, _Mapptr _M)
  35. : _First(*_M), _Last(*_M + _DEQUESIZ),
  36. _Next(_P), _Map(_M) {}
  37. reference operator*() const
  38. {return (*_Next); }
  39. _Tptr operator->() const
  40. {return (&**this); }
  41. iterator& operator++()
  42. {if (++_Next == _Last)
  43. {_First = *++_Map;
  44. _Last = _First + _DEQUESIZ;
  45. _Next = _First; }
  46. return (*this); }
  47. iterator operator++(int)
  48. {iterator _Tmp = *this;
  49. ++*this;
  50. return (_Tmp); }
  51. iterator& operator--()
  52. {if (_Next == _First)
  53. {_First = *--_Map;
  54. _Last = _First + _DEQUESIZ;
  55. _Next = _Last; }
  56. --_Next;
  57. return (*this); }
  58. iterator operator--(int)
  59. {iterator _Tmp = *this;
  60. --*this;
  61. return (_Tmp); }
  62. iterator& operator+=(difference_type _N)
  63. {_Add(_N);
  64. return (*this); }
  65. iterator& operator-=(difference_type _N)
  66. {return (*this += -_N); }
  67. iterator operator+(difference_type _N) const
  68. {iterator _Tmp = *this;
  69. return (_Tmp += _N); }
  70. iterator operator-(difference_type _N) const
  71. {iterator _Tmp = *this;
  72. return (_Tmp -= _N); }
  73. difference_type operator-(const iterator& _X) const
  74. {return (_Map == _X._Map ? _Next - _X._Next
  75. : _DEQUESIZ * (_Map - _X._Map - 1)
  76. + (_Next - _First) + (_X._Last - _X._Next)); }
  77. reference operator[](difference_type _N) const
  78. {return (*(*this + _N)); }
  79. bool operator==(const iterator& _X) const
  80. {return (_Next == _X._Next); }
  81. bool operator!=(const iterator& _X) const
  82. {return (!(*this == _X)); }
  83. bool operator<(const iterator& _X) const
  84. {return (_Map < _X._Map
  85. || _Map == _X._Map && _Next < _X._Next); }
  86. bool operator<=(const iterator& _X) const
  87. {return (!(_X < *this)); }
  88. bool operator>(const iterator& _X) const
  89. {return (_X < *this); }
  90. bool operator>=(const iterator& _X) const
  91. {return (!(*this < _X)); }
  92. protected:
  93. void _Add(difference_type _N)
  94. {difference_type _Off = _N + _Next - _First;
  95. difference_type _Moff = (0 <= _Off)
  96. ? _Off / _DEQUESIZ
  97. : -((_DEQUESIZ - 1 - _Off) / _DEQUESIZ);
  98. if (_Moff == 0)
  99. _Next += _N;
  100. else
  101. {_Map += _Moff;
  102. _First = *_Map;
  103. _Last = _First + _DEQUESIZ;
  104. _Next = _First + (_Off - _Moff * _DEQUESIZ); }}
  105. _PROTECTED:
  106. _Tptr _First, _Last, _Next;
  107. _Mapptr _Map;
  108. };
  109. // CLASS const_iterator
  110. class const_iterator : public iterator {
  111. public:
  112. const_iterator()
  113. {}
  114. const_iterator(_Tptr _P, _Mapptr _M)
  115. : iterator(_P, _M) {}
  116. const_iterator(const iterator& _X)
  117. : iterator(_X) {}
  118. const_reference operator*() const
  119. {return (*_Next); }
  120. _Ctptr operator->() const
  121. {return (&**this); }
  122. const_iterator& operator++()
  123. {if (++_Next == _Last)
  124. {_First = *++_Map;
  125. _Last = _First + _DEQUESIZ;
  126. _Next = _First; }
  127. return (*this); }
  128. const_iterator operator++(int)
  129. {const_iterator _Tmp = *this;
  130. ++*this;
  131. return (_Tmp); }
  132. const_iterator& operator--()
  133. {if (_Next == _First)
  134. {_First = *--_Map;
  135. _Last = _First + _DEQUESIZ;
  136. _Next = _Last; }
  137. --_Next;
  138. return (*this); }
  139. const_iterator operator--(int)
  140. {const_iterator _Tmp = *this;
  141. --*this;
  142. return (_Tmp); }
  143. const_iterator& operator+=(difference_type _N)
  144. {_Add(_N);
  145. return (*this); }
  146. const_iterator& operator-=(difference_type _N)
  147. {return (*this += -_N); }
  148. const_iterator operator+(difference_type _N) const
  149. {iterator _Tmp = *this;
  150. return (_Tmp += _N); }
  151. const_iterator operator-(difference_type _N) const
  152. {iterator _Tmp = *this;
  153. return (_Tmp -= _N); }
  154. difference_type operator-(const const_iterator& _X) const
  155. {return (_Map == _X._Map ? _Next - _X._Next
  156. : _DEQUESIZ * (_Map - _X._Map - 1)
  157. + (_Next - _First) + (_X._Last - _X._Next)); }
  158. const_reference operator[](difference_type _N) const
  159. {return (*(*this + _N)); }
  160. bool operator==(const const_iterator& _X) const
  161. {return (_Next == _X._Next); }
  162. bool operator!=(const const_iterator& _X) const
  163. {return (!(*this == _X)); }
  164. bool operator<(const const_iterator& _X) const
  165. {return (_Map < _X._Map
  166. || _Map == _X._Map && _Next < _X._Next); }
  167. bool operator<=(const const_iterator& _X) const
  168. {return (!(_X < *this)); }
  169. bool operator>(const const_iterator& _X) const
  170. {return (_X < *this); }
  171. bool operator>=(const const_iterator& _X) const
  172. {return (!(*this < _X)); }
  173. };
  174. typedef reverse_iterator<const_iterator, value_type,
  175. const_reference, _Ctptr, difference_type>
  176. const_reverse_iterator;
  177. typedef reverse_iterator<iterator, value_type,
  178. reference, _Tptr, difference_type>
  179. reverse_iterator;
  180. explicit deque(const _A& _Al = _A())
  181. : allocator(_Al),
  182. _First(), _Last(), _Map(0), _Mapsize(0), _Size(0)
  183. {}
  184. explicit deque(size_type _N, const _Ty& _V = _Ty(),
  185. const _A& _Al = _A())
  186. : allocator(_Al),
  187. _First(), _Last(), _Map(0), _Mapsize(0), _Size(0)
  188. {insert(begin(), _N, _V); }
  189. deque(const _Myt& _X)
  190. : allocator(_X.allocator),
  191. _First(), _Last(), _Map(0), _Mapsize(0), _Size(0)
  192. {copy(_X.begin(), _X.end(), back_inserter(*this)); }
  193. typedef const_iterator _It;
  194. deque(_It _F, _It _L, const _A& _Al = _A())
  195. : allocator(_Al),
  196. _First(), _Last(), _Map(0), _Mapsize(0), _Size(0)
  197. {copy(_F, _L, back_inserter(*this)); }
  198. ~deque()
  199. {while (!empty())
  200. pop_front(); }
  201. _Myt& operator=(const _Myt& _X)
  202. {if (this != &_X)
  203. {iterator _S;
  204. if (_X.size() <= size())
  205. {_S = copy(_X.begin(), _X.end(), begin());
  206. erase(_S, end()); }
  207. else
  208. {const_iterator _Sx = _X.begin() + size();
  209. _S = copy(_X.begin(), _Sx, begin());
  210. copy(_Sx, _X.end(), inserter(*this, _S)); }}
  211. return (*this); }
  212. iterator begin()
  213. {return (_First); }
  214. const_iterator begin() const
  215. {return ((const_iterator)_First); }
  216. iterator end()
  217. {return (_Last); }
  218. const_iterator end() const
  219. {return ((const_iterator)_Last); }
  220. reverse_iterator rbegin()
  221. {return (reverse_iterator(end())); }
  222. const_reverse_iterator rbegin() const
  223. {return (const_reverse_iterator(end())); }
  224. reverse_iterator rend()
  225. {return (reverse_iterator(begin())); }
  226. const_reverse_iterator rend() const
  227. {return (const_reverse_iterator(begin())); }
  228. void resize(size_type _N, _Ty _X = _Ty())
  229. {if (size() < _N)
  230. insert(end(), _N - size(), _X);
  231. else if (_N < size())
  232. erase(begin() + _N, end()); }
  233. size_type size() const
  234. {return (_Size); }
  235. size_type max_size() const
  236. {return (allocator.max_size()); }
  237. bool empty() const
  238. {return (size() == 0); }
  239. _A get_allocator() const
  240. {return (allocator); }
  241. const_reference at(size_type _P) const
  242. {if (size() <= _P)
  243. _Xran();
  244. return (*(begin() + _P)); }
  245. reference at(size_type _P)
  246. {if (size() <= _P)
  247. _Xran();
  248. return (*(begin() + _P)); }
  249. const_reference operator[](size_type _P) const
  250. {return (*(begin() + _P)); }
  251. reference operator[](size_type _P)
  252. {return (*(begin() + _P)); }
  253. reference front()
  254. {return (*begin()); }
  255. const_reference front() const
  256. {return (*begin()); }
  257. reference back()
  258. {return (*(end() - 1)); }
  259. const_reference back() const
  260. {return (*(end() - 1)); }
  261. void push_front(const _Ty& _X)
  262. {if (empty() || _First._Next == _First._First)
  263. _Buyfront();
  264. allocator.construct(--_First._Next, _X);
  265. ++_Size; }
  266. void pop_front()
  267. {allocator.destroy(_First._Next++);
  268. --_Size;
  269. if (empty() || _First._Next == _First._Last)
  270. _Freefront(); }
  271. void push_back(const _Ty& _X)
  272. {if (empty() || _Last._Next == _Last._Last)
  273. {_Buyback();
  274. allocator.construct(_Last._Next++, _X); }
  275. else if (_Last._Next + 1 == _Last._Last)
  276. {allocator.construct(_Last._Next++, _X);
  277. _Buyback(); }
  278. else
  279. allocator.construct(_Last._Next++, _X);
  280. ++_Size; }
  281. void pop_back()
  282. {
  283. if (_Last._Next == _Last._First)
  284. _Freeback();
  285. if (!empty())
  286. allocator.destroy(--_Last._Next);
  287. --_Size;
  288. if ( empty())
  289. _Freeback(); }
  290. void assign(_It _F, _It _L)
  291. {erase(begin(), end());
  292. insert(begin(), _F, _L); }
  293. void assign(size_type _N, const _Ty& _X = _Ty())
  294. {erase(begin(), end());
  295. insert(begin(), _N, _X); }
  296. iterator insert(iterator _P, const _Ty& _X = _Ty())
  297. {if (_P == begin())
  298. {push_front(_X);
  299. return (begin()); }
  300. else if (_P == end())
  301. {push_back(_X);
  302. return (end() - 1); }
  303. else
  304. {iterator _S;
  305. size_type _Off = _P - begin();
  306. if (_Off < size() / 2)
  307. {push_front(front());
  308. _S = begin() + _Off;
  309. copy(begin() + 2, _S + 1, begin() + 1); }
  310. else
  311. {push_back(back());
  312. _S = begin() + _Off;
  313. copy_backward(_S, end() - 2, end() - 1); }
  314. *_S = _X;
  315. return (_S); }}
  316. void insert(iterator _P, size_type _M, const _Ty& _X)
  317. {iterator _S;
  318. size_type _I;
  319. size_type _Off = _P - begin();
  320. size_type _Rem = _Size - _Off;
  321. if (_Off < _Rem)
  322. if (_Off < _M)
  323. {for (_I = _M - _Off; 0 < _I; --_I)
  324. push_front(_X);
  325. for (_I = _Off; 0 < _I; --_I)
  326. push_front(begin()[_M - 1]);
  327. _S = begin() + _M;
  328. fill(_S, _S + _Off, _X); }
  329. else
  330. {for (_I = _M; 0 < _I; --_I)
  331. push_front(begin()[_M - 1]);
  332. _S = begin() + _M;
  333. copy(_S + _M, _S + _Off, _S);
  334. fill(begin() + _Off, _S + _Off, _X); }
  335. else
  336. if (_Rem < _M)
  337. {for (_I = _M - _Rem; 0 < _I; --_I)
  338. push_back(_X);
  339. for (_I = 0; _I < _Rem; ++_I)
  340. push_back(begin()[_Off + _I]);
  341. _S = begin() + _Off;
  342. fill(_S, _S + _Rem, _X); }
  343. else
  344. {for (_I = 0; _I < _M; ++_I)
  345. push_back(begin()[_Off + _Rem - _M + _I]);
  346. _S = begin() + _Off;
  347. copy_backward(_S, _S + _Rem - _M, _S + _Rem);
  348. fill(_S, _S + _M, _X); }}
  349. void insert(iterator _P, _It _F, _It _L)
  350. {size_type _M = 0;
  351. _Distance(_F, _L, _M);
  352. size_type _I;
  353. size_type _Off = _P - begin();
  354. size_type _Rem = _Size - _Off;
  355. if (_Off < _Rem)
  356. if (_Off < _M)
  357. {_It _Qx = _F;
  358. advance(_Qx, _M - _Off);
  359. for (_It _Q = _Qx; _F != _Q; )
  360. push_front(*--_Q);
  361. for (_I = _Off; 0 < _I; --_I)
  362. push_front(begin()[_M - 1]);
  363. copy(_Qx, _L, begin() + _M); }
  364. else
  365. {for (_I = _M; 0 < _I; --_I)
  366. push_front(begin()[_M - 1]);
  367. iterator _S = begin() + _M;
  368. copy(_S + _M, _S + _Off, _S);
  369. copy(_F, _L, begin() + _Off); }
  370. else
  371. if (_Rem < _M)
  372. {_It _Qx = _F;
  373. advance(_Qx, _Rem);
  374. for (_It _Q = _Qx; _Q != _L; ++_Q)
  375. push_back(*_Q);
  376. for (_I = 0; _I < _Rem; ++_I)
  377. push_back(begin()[_Off + _I]);
  378. copy(_F, _Qx, begin() + _Off); }
  379. else
  380. {for (_I = 0; _I < _M; ++_I)
  381. push_back(begin()[_Off + _Rem - _M + _I]);
  382. iterator _S = begin() + _Off;
  383. copy_backward(_S, _S + _Rem - _M, _S + _Rem);
  384. copy(_F, _L, _S); }}
  385. iterator erase(iterator _P)
  386. {return (erase(_P, _P + 1)); }
  387. iterator erase(iterator _F, iterator _L)
  388. {size_type _N = _L - _F;
  389. size_type _M = _F - begin();
  390. if (_M < end() - _L)
  391. {copy_backward(begin(), _F, _L);
  392. for (; 0 < _N; --_N)
  393. pop_front(); }
  394. else
  395. {copy(_L, end(), _F);
  396. for (; 0 < _N; --_N)
  397. pop_back(); }
  398. return (_M == 0 ? begin() : begin() + _M); }
  399. void clear()
  400. {erase(begin(), end()); }
  401. void swap(_Myt& _X)
  402. {if (allocator == _X.allocator)
  403. {std::swap(_First, _X._First);
  404. std::swap(_Last, _X._Last);
  405. std::swap(_Map, _X._Map);
  406. std::swap(_Mapsize, _X._Mapsize);
  407. std::swap(_Size, _X._Size); }
  408. else
  409. {_Myt _Ts = *this; *this = _X, _X = _Ts; }}
  410. friend void swap(_Myt& _X, _Myt& _Y)
  411. {_X.swap(_Y); }
  412. protected:
  413. void _Buyback()
  414. {_Tptr _P = allocator.allocate(_DEQUESIZ, (void *)0);
  415. if (empty())
  416. {_Mapsize = _DEQUEMAPSIZ;
  417. size_type _N = _Mapsize / 2;
  418. _Getmap();
  419. _Setptr(_Map + _N, _P);
  420. _First = iterator(_P + _DEQUESIZ / 2, _Map + _N);
  421. _Last = _First; }
  422. else if (_Last._Map < _Map + (_Mapsize - 1))
  423. {_Setptr(++_Last._Map, _P);
  424. _Last = iterator(_P, _Last._Map); }
  425. else
  426. {difference_type _I = _Last._Map - _First._Map + 1;
  427. _Mapptr _M = _Growmap(2 * _I);
  428. _Setptr(_M + _I, _P);
  429. _First = iterator(_First._Next, _M);
  430. _Last = iterator(_P, _M + _I); }}
  431. void _Buyfront()
  432. {_Tptr _P = allocator.allocate(_DEQUESIZ, (void *)0);
  433. if (empty())
  434. {_Mapsize = _DEQUEMAPSIZ;
  435. size_type _N = _Mapsize / 2;
  436. _Getmap();
  437. _Setptr(_Map + _N, _P);
  438. _First = iterator(_P + (_DEQUESIZ / 2 + 1),
  439. _Map + _N);
  440. _Last = _First; }
  441. else if (_Map < _First._Map)
  442. {_Setptr(--_First._Map, _P);
  443. _First = iterator(_P + _DEQUESIZ, _First._Map); }
  444. else if (_Last._Map == _First._Map)
  445. {_Setptr(_Last._Map++, *_First._Map);
  446. _Setptr(_First._Map+1, *_First._Map);
  447. _Setptr(_First._Map, _P);
  448. _First = iterator(_P + _DEQUESIZ, _First._Map); }
  449. else
  450. {difference_type _I = _Last._Map - _First._Map + 1;
  451. _Mapptr _M = _Growmap(2 * _I);
  452. _Setptr(--_M, _P);
  453. _First = iterator(_P + _DEQUESIZ, _M);
  454. _Last = iterator(_Last._Next, _M + _I); }}
  455. void _Freeback()
  456. {_Freeptr(_Last._Map--);
  457. if (empty())
  458. {
  459. if(_First._Map == _Last._Map)
  460. _Freeptr(_First._Map);
  461. _First = iterator();
  462. _Last = _First;
  463. _Freemap(); }
  464. else
  465. _Last = iterator(*_Last._Map + _DEQUESIZ,
  466. _Last._Map); }
  467. void _Freefront()
  468. {_Freeptr(_First._Map++);
  469. if (empty())
  470. {_First = iterator();
  471. _Last = _First;
  472. _Freemap(); }
  473. else
  474. _First = iterator(*_First._Map, _First._Map); }
  475. void _Xran() const
  476. {_THROW(out_of_range, "invalid deque<T> subscript"); }
  477. void _Freemap()
  478. {allocator.deallocate(_Map, _Mapsize); }
  479. void _Freeptr(_Mapptr _M)
  480. {allocator.deallocate(*_M, _DEQUESIZ); }
  481. void _Getmap()
  482. {_Map = (_Mapptr)allocator._Charalloc(
  483. _Mapsize * sizeof (_Tptr)); }
  484. _Mapptr _Growmap(size_type _Newsize)
  485. {_Mapptr _M = (_Mapptr)allocator._Charalloc(
  486. _Newsize * sizeof (_Tptr));
  487. copy(_First._Map, _Last._Map + 1,
  488. _M + _Newsize / 4);
  489. allocator.deallocate(_Map, _Mapsize);
  490. _Map = _M;
  491. _Mapsize = _Newsize;
  492. return (_M + _Newsize / 4); }
  493. void _Setptr(_Mapptr _M, _Tptr _P)
  494. {*_M = _P; }
  495. _A allocator;
  496. iterator _First, _Last;
  497. _Mapptr _Map;
  498. size_type _Mapsize, _Size;
  499. };
  500. // deque TEMPLATE OPERATORS
  501. template<class _Ty, class _A> inline
  502. bool operator==(const deque<_Ty, _A>& _X,
  503. const deque<_Ty, _A>& _Y)
  504. {return (_X.size() == _Y.size()
  505. && equal(_X.begin(), _X.end(), _Y.begin())); }
  506. template<class _Ty, class _A> inline
  507. bool operator!=(const deque<_Ty, _A>& _X,
  508. const deque<_Ty, _A>& _Y)
  509. {return (!(_X == _Y)); }
  510. template<class _Ty, class _A> inline
  511. bool operator<(const deque<_Ty, _A>& _X,
  512. const deque<_Ty, _A>& _Y)
  513. {return (lexicographical_compare(_X.begin(), _X.end(),
  514. _Y.begin(), _Y.end())); }
  515. template<class _Ty, class _A> inline
  516. bool operator<=(const deque<_Ty, _A>& _X,
  517. const deque<_Ty, _A>& _Y)
  518. {return (!(_Y < _X)); }
  519. template<class _Ty, class _A> inline
  520. bool operator>(const deque<_Ty, _A>& _X,
  521. const deque<_Ty, _A>& _Y)
  522. {return (_Y < _X); }
  523. template<class _Ty, class _A> inline
  524. bool operator>=(const deque<_Ty, _A>& _X,
  525. const deque<_Ty, _A>& _Y)
  526. {return (!(_X < _Y)); }
  527. _STD_END
  528. #ifdef _MSC_VER
  529. #pragma pack(pop)
  530. #endif /* _MSC_VER */
  531. #endif /* _DEQUE_ */
  532. /*
  533. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  534. * Consult your license regarding permissions and restrictions.
  535. */
  536. /*
  537. * This file is derived from software bearing the following
  538. * restrictions:
  539. *
  540. * Copyright (c) 1994
  541. * Hewlett-Packard Company
  542. *
  543. * Permission to use, copy, modify, distribute and sell this
  544. * software and its documentation for any purpose is hereby
  545. * granted without fee, provided that the above copyright notice
  546. * appear in all copies and that both that copyright notice and
  547. * this permission notice appear in supporting documentation.
  548. * Hewlett-Packard Company makes no representations about the
  549. * suitability of this software for any purpose. It is provided
  550. * "as is" without express or implied warranty.
  551. */