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.

570 lines
16 KiB

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