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.

698 lines
17 KiB

  1. #pragma once
  2. #ifndef _STLLIST_H_
  3. #define _STLLIST_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. // TEMPLATE CLASS list
  20. template<class _Ty, class _A = allocator<_Ty> >
  21. class list
  22. {
  23. protected:
  24. typedef _POINTER_X(void, _A) _Genptr;
  25. struct _Node;
  26. friend struct _Node;
  27. struct _Node
  28. {
  29. _Genptr _Next, _Prev;
  30. _Ty _Value;
  31. };
  32. typedef _POINTER_X(_Node, _A) _Nodeptr;
  33. struct _Acc;
  34. friend struct _Acc;
  35. struct _Acc
  36. {
  37. typedef _REFERENCE_X(_Nodeptr, _A) _Nodepref;
  38. typedef _A::reference _Vref;
  39. static _Nodepref _Next(_Nodeptr _P)
  40. {
  41. return ((_Nodepref)(*_P)._Next);
  42. }
  43. static _Nodepref _Prev(_Nodeptr _P)
  44. {
  45. return ((_Nodepref)(*_P)._Prev);
  46. }
  47. static _Vref _Value(_Nodeptr _P)
  48. {
  49. return ((_Vref)(*_P)._Value);
  50. }
  51. };
  52. public:
  53. typedef list<_Ty, _A> _Myt;
  54. typedef _A allocator_type;
  55. typedef _A::size_type size_type;
  56. typedef _A::difference_type difference_type;
  57. typedef _A::pointer _Tptr;
  58. typedef _A::const_pointer _Ctptr;
  59. typedef _A::reference reference;
  60. typedef _A::const_reference const_reference;
  61. typedef _A::value_type value_type;
  62. // CLASS iterator
  63. class iterator;
  64. friend class iterator;
  65. class iterator : public _Bidit<_Ty, difference_type>
  66. {
  67. public:
  68. iterator()
  69. {
  70. }
  71. iterator(_Nodeptr _P)
  72. : _Ptr(_P)
  73. {
  74. }
  75. reference operator*() const
  76. {
  77. return (_Acc::_Value(_Ptr));
  78. }
  79. _Tptr operator->() const
  80. {
  81. return (&**this);
  82. }
  83. iterator& operator++()
  84. {
  85. _Ptr = _Acc::_Next(_Ptr);
  86. return (*this);
  87. }
  88. iterator operator++(int)
  89. {
  90. iterator _Tmp = *this;
  91. ++*this;
  92. return (_Tmp);
  93. }
  94. iterator& operator--()
  95. {
  96. _Ptr = _Acc::_Prev(_Ptr);
  97. return (*this);
  98. }
  99. iterator operator--(int)
  100. {
  101. iterator _Tmp = *this;
  102. --*this;
  103. return (_Tmp);
  104. }
  105. bool operator==(const iterator& _X) const
  106. {
  107. return (_Ptr == _X._Ptr);
  108. }
  109. bool operator!=(const iterator& _X) const
  110. {
  111. return (!(*this == _X));
  112. }
  113. _Nodeptr _Mynode() const
  114. {
  115. return (_Ptr);
  116. }
  117. protected:
  118. _Nodeptr _Ptr;
  119. };
  120. // CLASS const_iterator
  121. class const_iterator;
  122. friend class const_iterator;
  123. class const_iterator : public iterator
  124. {
  125. public:
  126. const_iterator()
  127. {
  128. }
  129. const_iterator(_Nodeptr _P)
  130. : iterator(_P)
  131. {
  132. }
  133. const_iterator(const iterator& _X)
  134. : iterator(_X)
  135. {
  136. }
  137. const_reference operator*() const
  138. {
  139. return (_Acc::_Value(_Ptr));
  140. }
  141. _Ctptr operator->() const
  142. {
  143. return (&**this);
  144. }
  145. const_iterator& operator++()
  146. {
  147. _Ptr = _Acc::_Next(_Ptr);
  148. return (*this);
  149. }
  150. const_iterator operator++(int)
  151. {
  152. iterator _Tmp = *this;
  153. ++*this;
  154. return (_Tmp);
  155. }
  156. const_iterator& operator--()
  157. {
  158. _Ptr = _Acc::_Prev(_Ptr);
  159. return (*this);
  160. }
  161. const_iterator operator--(int)
  162. {
  163. iterator _Tmp = *this;
  164. --*this;
  165. return (_Tmp);
  166. }
  167. bool operator==(const const_iterator& _X) const
  168. {
  169. return (_Ptr == _X._Ptr);
  170. }
  171. bool operator!=(const const_iterator& _X) const
  172. {
  173. return (!(*this == _X));
  174. }
  175. };
  176. typedef reverse_bidirectional_iterator<iterator,
  177. value_type, reference, _Tptr, difference_type>
  178. reverse_iterator;
  179. typedef reverse_bidirectional_iterator<const_iterator,
  180. value_type, const_reference, _Ctptr, difference_type>
  181. const_reverse_iterator;
  182. explicit list(const _A& _Al = _A())
  183. : allocator(_Al),
  184. _Head(_Buynode()), _Size(0)
  185. {
  186. }
  187. explicit list(size_type _N, const _Ty& _V = _Ty(),
  188. const _A& _Al = _A())
  189. : allocator(_Al),
  190. _Head(_Buynode()), _Size(0)
  191. {
  192. insert(begin(), _N, _V);
  193. }
  194. list(const _Myt& _X)
  195. : allocator(_X.allocator),
  196. _Head(_Buynode()), _Size(0)
  197. {
  198. insert(begin(), _X.begin(), _X.end());
  199. }
  200. typedef const_iterator _It;
  201. list(_It _F, _It _L, const _A& _Al = _A())
  202. : allocator(_Al),
  203. _Head(_Buynode()), _Size(0)
  204. {
  205. insert(begin(), _F, _L);
  206. }
  207. ~list()
  208. {
  209. erase(begin(), end());
  210. _Freenode(_Head);
  211. _Head = 0, _Size = 0;
  212. }
  213. _Myt& operator=(const _Myt& _X)
  214. {
  215. if (this != &_X)
  216. {
  217. iterator _F1 = begin();
  218. iterator _L1 = end();
  219. const_iterator _F2 = _X.begin();
  220. const_iterator _L2 = _X.end();
  221. for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
  222. *_F1 = *_F2;
  223. erase(_F1, _L1);
  224. insert(_L1, _F2, _L2);
  225. }
  226. return (*this);
  227. }
  228. iterator begin()
  229. {
  230. return (iterator(_Acc::_Next(_Head)));
  231. }
  232. const_iterator begin() const
  233. {
  234. return (const_iterator(_Acc::_Next(_Head)));
  235. }
  236. iterator end()
  237. {
  238. return (iterator(_Head));
  239. }
  240. const_iterator end() const
  241. {
  242. return (const_iterator(_Head));
  243. }
  244. reverse_iterator rbegin()
  245. {
  246. return (reverse_iterator(end()));
  247. }
  248. const_reverse_iterator rbegin() const
  249. {
  250. return (const_reverse_iterator(end()));
  251. }
  252. reverse_iterator rend()
  253. {
  254. return (reverse_iterator(begin()));
  255. }
  256. const_reverse_iterator rend() const
  257. {
  258. return (const_reverse_iterator(begin()));
  259. }
  260. void resize(size_type _N, _Ty _X = _Ty())
  261. {
  262. if (size() < _N)
  263. insert(end(), _N - size(), _X);
  264. else
  265. while (_N < size())
  266. pop_back();
  267. }
  268. size_type size() const
  269. {
  270. return (_Size);
  271. }
  272. size_type max_size() const
  273. {
  274. return (allocator.max_size());
  275. }
  276. bool empty() const
  277. {
  278. return (size() == 0);
  279. }
  280. _A get_allocator() const
  281. {
  282. return (allocator);
  283. }
  284. reference front()
  285. {
  286. return (*begin());
  287. }
  288. const_reference front() const
  289. {
  290. return (*begin());
  291. }
  292. reference back()
  293. {
  294. return (*(--end()));
  295. }
  296. const_reference back() const
  297. {
  298. return (*(--end()));
  299. }
  300. void push_front(const _Ty& _X)
  301. {
  302. insert(begin(), _X);
  303. }
  304. void pop_front()
  305. {
  306. erase(begin());
  307. }
  308. void push_back(const _Ty& _X)
  309. {
  310. insert(end(), _X);
  311. }
  312. void pop_back()
  313. {
  314. erase(--end());
  315. }
  316. void assign(_It _F, _It _L)
  317. {
  318. erase(begin(), end());
  319. insert(begin(), _F, _L);
  320. }
  321. void assign(size_type _N, const _Ty& _X = _Ty())
  322. {
  323. erase(begin(), end());
  324. insert(begin(), _N, _X);
  325. }
  326. iterator insert(iterator _P, const _Ty& _X = _Ty())
  327. {
  328. _Nodeptr _S = _P._Mynode();
  329. _Acc::_Prev(_S) = _Buynode(_S, _Acc::_Prev(_S));
  330. _S = _Acc::_Prev(_S);
  331. _Acc::_Next(_Acc::_Prev(_S)) = _S;
  332. allocator.construct(&_Acc::_Value(_S), _X);
  333. ++_Size;
  334. return (iterator(_S));
  335. }
  336. void insert(iterator _P, size_type _M, const _Ty& _X)
  337. {
  338. for (; 0 < _M; --_M)
  339. insert(_P, _X);
  340. }
  341. void insert(iterator _P, const _Ty *_F, const _Ty *_L)
  342. {
  343. for (; _F != _L; ++_F)
  344. insert(_P, *_F);
  345. }
  346. void insert(iterator _P, _It _F, _It _L)
  347. {
  348. for (; _F != _L; ++_F)
  349. insert(_P, *_F);
  350. }
  351. iterator erase(iterator _P)
  352. {
  353. _Nodeptr _S = (_P++)._Mynode();
  354. _Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
  355. _Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
  356. allocator.destroy(&_Acc::_Value(_S));
  357. _Freenode(_S);
  358. --_Size;
  359. return (_P);
  360. }
  361. iterator erase(iterator _F, iterator _L)
  362. {
  363. while (_F != _L)
  364. erase(_F++);
  365. return (_F);
  366. }
  367. void clear()
  368. {
  369. erase(begin(), end());
  370. }
  371. void swap(_Myt& _X)
  372. {
  373. if (allocator == _X.allocator)
  374. {
  375. std::swap(_Head, _X._Head);
  376. std::swap(_Size, _X._Size);
  377. }
  378. else
  379. {
  380. iterator _P = begin();
  381. splice(_P, _X);
  382. _X.splice(_X.begin(), *this, _P, end());
  383. }
  384. }
  385. friend void swap(_Myt& _X, _Myt& _Y)
  386. {
  387. _X.swap(_Y);
  388. }
  389. void splice(iterator _P, _Myt& _X)
  390. {
  391. if (!_X.empty())
  392. {
  393. _Splice(_P, _X, _X.begin(), _X.end());
  394. _Size += _X._Size;
  395. _X._Size = 0;
  396. }
  397. }
  398. void splice(iterator _P, _Myt& _X, iterator _F)
  399. {
  400. iterator _L = _F;
  401. if (_P != _F && _P != ++_L)
  402. {
  403. _Splice(_P, _X, _F, _L);
  404. ++_Size;
  405. --_X._Size;
  406. }
  407. }
  408. void splice(iterator _P, _Myt& _X, iterator _F, iterator _L)
  409. {
  410. if (_F != _L)
  411. {
  412. if (&_X != this)
  413. {
  414. size_type _N = 0;
  415. _Distance(_F, _L, _N);
  416. _Size += _N;
  417. _X._Size -= _N;
  418. }
  419. _Splice(_P, _X, _F, _L);
  420. }
  421. }
  422. void remove(const _Ty& _V)
  423. {
  424. iterator _L = end();
  425. for (iterator _F = begin(); _F != _L; )
  426. if (*_F == _V)
  427. erase(_F++);
  428. else
  429. ++_F;
  430. }
  431. typedef binder2nd<not_equal_to<_Ty> > _Pr1;
  432. void remove_if(_Pr1 _Pr)
  433. {
  434. iterator _L = end();
  435. for (iterator _F = begin(); _F != _L; )
  436. if (_Pr(*_F))
  437. erase(_F++);
  438. else
  439. ++_F;
  440. }
  441. void unique()
  442. {
  443. iterator _F = begin(), _L = end();
  444. if (_F != _L)
  445. for (iterator _M = _F; ++_M != _L; _M = _F)
  446. if (*_F == *_M)
  447. erase(_M);
  448. else
  449. _F = _M;
  450. }
  451. typedef not_equal_to<_Ty> _Pr2;
  452. void unique(_Pr2 _Pr)
  453. {
  454. iterator _F = begin(), _L = end();
  455. if (_F != _L)
  456. for (iterator _M = _F; ++_M != _L; _M = _F)
  457. if (_Pr(*_F, *_M))
  458. erase(_M);
  459. else
  460. _F = _M;
  461. }
  462. void merge(_Myt& _X)
  463. {
  464. if (&_X != this)
  465. {
  466. iterator _F1 = begin(), _L1 = end();
  467. iterator _F2 = _X.begin(), _L2 = _X.end();
  468. while (_F1 != _L1 && _F2 != _L2)
  469. if (*_F2 < *_F1)
  470. {
  471. iterator _Mid2 = _F2;
  472. _Splice(_F1, _X, _F2, ++_Mid2);
  473. _F2 = _Mid2;
  474. }
  475. else
  476. ++_F1;
  477. if (_F2 != _L2)
  478. _Splice(_L1, _X, _F2, _L2);
  479. _Size += _X._Size;
  480. _X._Size = 0;
  481. }
  482. }
  483. typedef greater<_Ty> _Pr3;
  484. void merge(_Myt& _X, _Pr3 _Pr)
  485. {
  486. if (&_X != this)
  487. {
  488. iterator _F1 = begin(), _L1 = end();
  489. iterator _F2 = _X.begin(), _L2 = _X.end();
  490. while (_F1 != _L1 && _F2 != _L2)
  491. if (_Pr(*_F2, *_F1))
  492. {
  493. iterator _Mid2 = _F2;
  494. _Splice(_F1, _X, _F2, ++_Mid2);
  495. _F2 = _Mid2;
  496. }
  497. else
  498. ++_F1;
  499. if (_F2 != _L2)
  500. _Splice(_L1, _X, _F2, _L2);
  501. _Size += _X._Size;
  502. _X._Size = 0;
  503. }
  504. }
  505. void sort()
  506. {
  507. if (2 <= size())
  508. {
  509. const size_t _MAXN = 15;
  510. _Myt _X(allocator), _A[_MAXN + 1];
  511. size_t _N = 0;
  512. while (!empty())
  513. {
  514. _X.splice(_X.begin(), *this, begin());
  515. size_t _I;
  516. for (_I = 0; _I < _N && !_A[_I].empty(); ++_I)
  517. {
  518. _A[_I].merge(_X);
  519. _A[_I].swap(_X);
  520. }
  521. if (_I == _MAXN)
  522. _A[_I].merge(_X);
  523. else
  524. {
  525. _A[_I].swap(_X);
  526. if (_I == _N)
  527. ++_N;
  528. }
  529. }
  530. while (0 < _N)
  531. merge(_A[--_N]);
  532. }
  533. }
  534. void sort(_Pr3 _Pr)
  535. {
  536. if (2 <= size())
  537. {
  538. const size_t _MAXN = 15;
  539. _Myt _X(allocator), _A[_MAXN + 1];
  540. size_t _N = 0;
  541. while (!empty())
  542. {
  543. _X.splice(_X.begin(), *this, begin());
  544. size_t _I;
  545. for (_I = 0; _I < _N && !_A[_I].empty(); ++_I)
  546. {
  547. _A[_I].merge(_X, _Pr);
  548. _A[_I].swap(_X);
  549. }
  550. if (_I == _MAXN)
  551. _A[_I].merge(_X, _Pr);
  552. else
  553. {
  554. _A[_I].swap(_X);
  555. if (_I == _N)
  556. ++_N;
  557. }
  558. }
  559. while (0 < _N)
  560. merge(_A[--_N], _Pr);
  561. }
  562. }
  563. void reverse()
  564. {
  565. if (2 <= size())
  566. {
  567. iterator _L = end();
  568. for (iterator _F = ++begin(); _F != _L; )
  569. {
  570. iterator _M = _F;
  571. _Splice(begin(), *this, _M, ++_F);
  572. }
  573. }
  574. }
  575. protected:
  576. _Nodeptr _Buynode(_Nodeptr _Narg = 0, _Nodeptr _Parg = 0)
  577. {
  578. _Nodeptr _S = (_Nodeptr)allocator._Charalloc(
  579. 1 * sizeof (_Node));
  580. _Acc::_Next(_S) = _Narg != 0 ? _Narg : _S;
  581. _Acc::_Prev(_S) = _Parg != 0 ? _Parg : _S;
  582. return (_S);
  583. }
  584. void _Freenode(_Nodeptr _S)
  585. {
  586. allocator.deallocate(_S, 1);
  587. }
  588. void _Splice(iterator _P, _Myt& _X, iterator _F, iterator _L)
  589. {
  590. if (allocator == _X.allocator)
  591. {
  592. _Acc::_Next(_Acc::_Prev(_L._Mynode())) =
  593. _P._Mynode();
  594. _Acc::_Next(_Acc::_Prev(_F._Mynode())) =
  595. _L._Mynode();
  596. _Acc::_Next(_Acc::_Prev(_P._Mynode())) =
  597. _F._Mynode();
  598. _Nodeptr _S = _Acc::_Prev(_P._Mynode());
  599. _Acc::_Prev(_P._Mynode()) =
  600. _Acc::_Prev(_L._Mynode());
  601. _Acc::_Prev(_L._Mynode()) =
  602. _Acc::_Prev(_F._Mynode());
  603. _Acc::_Prev(_F._Mynode()) = _S;
  604. }
  605. else
  606. {
  607. insert(_P, _F, _L);
  608. _X.erase(_F, _L);
  609. }
  610. }
  611. void _Xran() const
  612. {
  613. _THROW(out_of_range, "invalid list<T> subscript");
  614. }
  615. _A allocator;
  616. _Nodeptr _Head;
  617. size_type _Size;
  618. };
  619. // list TEMPLATE OPERATORS
  620. template<class _Ty, class _A> inline
  621. bool operator==(const list<_Ty, _A>& _X,
  622. const list<_Ty, _A>& _Y)
  623. {
  624. return (_X.size() == _Y.size()
  625. && equal(_X.begin(), _X.end(), _Y.begin()));
  626. }
  627. template<class _Ty, class _A> inline
  628. bool operator!=(const list<_Ty, _A>& _X,
  629. const list<_Ty, _A>& _Y)
  630. {
  631. return (!(_X == _Y));
  632. }
  633. template<class _Ty, class _A> inline
  634. bool operator<(const list<_Ty, _A>& _X,
  635. const list<_Ty, _A>& _Y)
  636. {
  637. return (lexicographical_compare(_X.begin(), _X.end(),
  638. _Y.begin(), _Y.end()));
  639. }
  640. template<class _Ty, class _A> inline
  641. bool operator>(const list<_Ty, _A>& _X,
  642. const list<_Ty, _A>& _Y)
  643. {
  644. return (_Y < _X);
  645. }
  646. template<class _Ty, class _A> inline
  647. bool operator<=(const list<_Ty, _A>& _X,
  648. const list<_Ty, _A>& _Y)
  649. {
  650. return (!(_Y < _X));
  651. }
  652. template<class _Ty, class _A> inline
  653. bool operator>=(const list<_Ty, _A>& _X,
  654. const list<_Ty, _A>& _Y)
  655. {
  656. return (!(_X < _Y));
  657. }
  658. _STD_END
  659. #ifdef _MSC_VER
  660. #pragma pack(pop)
  661. #endif /* _MSC_VER */
  662. #endif /* _STLLIST_H_ */
  663. /*
  664. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  665. * Consult your license regarding permissions and restrictions.
  666. */
  667. /*
  668. * This file is derived from software bearing the following
  669. * restrictions:
  670. *
  671. * Copyright (c) 1994
  672. * Hewlett-Packard Company
  673. *
  674. * Permission to use, copy, modify, distribute and sell this
  675. * software and its documentation for any purpose is hereby
  676. * granted without fee, provided that the above copyright notice
  677. * appear in all copies and that both that copyright notice and
  678. * this permission notice appear in supporting documentation.
  679. * Hewlett-Packard Company makes no representations about the
  680. * suitability of this software for any purpose. It is provided
  681. * "as is" without express or implied warranty.
  682. */