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.

1009 lines
26 KiB

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