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.

466 lines
13 KiB

  1. // list standard header
  2. #ifndef _LIST_
  3. #define _LIST_
  4. #include <cstddef>
  5. #include <functional>
  6. #include <iterator>
  7. #include <memory>
  8. #include <stdexcept>
  9. #include <xutility>
  10. #ifdef _MSC_VER
  11. #pragma pack(push,8)
  12. #endif /* _MSC_VER */
  13. _STD_BEGIN
  14. // TEMPLATE CLASS list
  15. template<class _Ty, class _A = allocator<_Ty> >
  16. class list {
  17. protected:
  18. typedef _POINTER_X(void, _A) _Genptr;
  19. struct _Node;
  20. friend struct _Node;
  21. struct _Node {
  22. _Genptr _Next, _Prev;
  23. _Ty _Value;
  24. };
  25. typedef _POINTER_X(_Node, _A) _Nodeptr;
  26. struct _Acc;
  27. friend struct _Acc;
  28. struct _Acc {
  29. typedef _REFERENCE_X(_Nodeptr, _A) _Nodepref;
  30. typedef typename _A::reference _Vref;
  31. static _Nodepref _Next(_Nodeptr _P)
  32. {return ((_Nodepref)(*_P)._Next); }
  33. static _Nodepref _Prev(_Nodeptr _P)
  34. {return ((_Nodepref)(*_P)._Prev); }
  35. static _Vref _Value(_Nodeptr _P)
  36. {return ((_Vref)(*_P)._Value); }
  37. };
  38. public:
  39. typedef list<_Ty, _A> _Myt;
  40. typedef _A allocator_type;
  41. typedef typename _A::size_type size_type;
  42. typedef typename _A::difference_type difference_type;
  43. typedef typename _A::pointer _Tptr;
  44. typedef typename _A::const_pointer _Ctptr;
  45. typedef typename _A::reference reference;
  46. typedef typename _A::const_reference const_reference;
  47. typedef typename _A::value_type value_type;
  48. // CLASS iterator
  49. class iterator;
  50. friend class iterator;
  51. class iterator : public _Bidit<_Ty, difference_type> {
  52. public:
  53. iterator()
  54. {}
  55. iterator(_Nodeptr _P)
  56. : _Ptr(_P) {}
  57. reference operator*() const
  58. {return (_Acc::_Value(_Ptr)); }
  59. _Tptr operator->() const
  60. {return (&**this); }
  61. iterator& operator++()
  62. {_Ptr = _Acc::_Next(_Ptr);
  63. return (*this); }
  64. iterator operator++(int)
  65. {iterator _Tmp = *this;
  66. ++*this;
  67. return (_Tmp); }
  68. iterator& operator--()
  69. {_Ptr = _Acc::_Prev(_Ptr);
  70. return (*this); }
  71. iterator operator--(int)
  72. {iterator _Tmp = *this;
  73. --*this;
  74. return (_Tmp); }
  75. bool operator==(const iterator& _X) const
  76. {return (_Ptr == _X._Ptr); }
  77. bool operator!=(const iterator& _X) const
  78. {return (!(*this == _X)); }
  79. _Nodeptr _Mynode() const
  80. {return (_Ptr); }
  81. protected:
  82. _Nodeptr _Ptr;
  83. };
  84. // CLASS const_iterator
  85. class const_iterator;
  86. friend class const_iterator;
  87. class const_iterator : public iterator {
  88. public:
  89. const_iterator()
  90. {}
  91. const_iterator(_Nodeptr _P)
  92. : iterator(_P) {}
  93. const_iterator(const iterator& _X)
  94. : iterator(_X) {}
  95. const_reference operator*() const
  96. {return (_Acc::_Value(_Ptr)); }
  97. _Ctptr operator->() const
  98. {return (&**this); }
  99. const_iterator& operator++()
  100. {_Ptr = _Acc::_Next(_Ptr);
  101. return (*this); }
  102. const_iterator operator++(int)
  103. {iterator _Tmp = *this;
  104. ++*this;
  105. return (_Tmp); }
  106. const_iterator& operator--()
  107. {_Ptr = _Acc::_Prev(_Ptr);
  108. return (*this); }
  109. const_iterator operator--(int)
  110. {iterator _Tmp = *this;
  111. --*this;
  112. return (_Tmp); }
  113. bool operator==(const const_iterator& _X) const
  114. {return (_Ptr == _X._Ptr); }
  115. bool operator!=(const const_iterator& _X) const
  116. {return (!(*this == _X)); }
  117. };
  118. typedef reverse_bidirectional_iterator<iterator,
  119. value_type, reference, _Tptr, difference_type>
  120. reverse_iterator;
  121. typedef reverse_bidirectional_iterator<const_iterator,
  122. value_type, const_reference, _Ctptr, difference_type>
  123. const_reverse_iterator;
  124. explicit list(const _A& _Al = _A())
  125. : allocator(_Al),
  126. _Head(_Buynode()), _Size(0) {}
  127. explicit list(size_type _N, const _Ty& _V = _Ty(),
  128. const _A& _Al = _A())
  129. : allocator(_Al),
  130. _Head(_Buynode()), _Size(0)
  131. {insert(begin(), _N, _V); }
  132. list(const _Myt& _X)
  133. : allocator(_X.allocator),
  134. _Head(_Buynode()), _Size(0)
  135. {insert(begin(), _X.begin(), _X.end()); }
  136. list(const _Ty *_F, const _Ty *_L, const _A& _Al = _A())
  137. : allocator(_Al),
  138. _Head(_Buynode()), _Size(0)
  139. {insert(begin(), _F, _L); }
  140. typedef const_iterator _It;
  141. list(_It _F, _It _L, const _A& _Al = _A())
  142. : allocator(_Al),
  143. _Head(_Buynode()), _Size(0)
  144. {insert(begin(), _F, _L); }
  145. ~list()
  146. {erase(begin(), end());
  147. _Freenode(_Head);
  148. _Head = 0, _Size = 0; }
  149. _Myt& operator=(const _Myt& _X)
  150. {if (this != &_X)
  151. {iterator _F1 = begin();
  152. iterator _L1 = end();
  153. const_iterator _F2 = _X.begin();
  154. const_iterator _L2 = _X.end();
  155. for (; _F1 != _L1 && _F2 != _L2; ++_F1, ++_F2)
  156. *_F1 = *_F2;
  157. erase(_F1, _L1);
  158. insert(_L1, _F2, _L2); }
  159. return (*this); }
  160. iterator begin()
  161. {return (iterator(_Acc::_Next(_Head))); }
  162. const_iterator begin() const
  163. {return (const_iterator(_Acc::_Next(_Head))); }
  164. iterator end()
  165. {return (iterator(_Head)); }
  166. const_iterator end() const
  167. {return (const_iterator(_Head)); }
  168. reverse_iterator rbegin()
  169. {return (reverse_iterator(end())); }
  170. const_reverse_iterator rbegin() const
  171. {return (const_reverse_iterator(end())); }
  172. reverse_iterator rend()
  173. {return (reverse_iterator(begin())); }
  174. const_reverse_iterator rend() const
  175. {return (const_reverse_iterator(begin())); }
  176. void resize(size_type _N, _Ty _X = _Ty())
  177. {if (size() < _N)
  178. insert(end(), _N - size(), _X);
  179. else
  180. while (_N < size())
  181. pop_back(); }
  182. size_type size() const
  183. {return (_Size); }
  184. size_type max_size() const
  185. {return (allocator.max_size()); }
  186. bool empty() const
  187. {return (size() == 0); }
  188. _A get_allocator() const
  189. {return (allocator); }
  190. reference front()
  191. {return (*begin()); }
  192. const_reference front() const
  193. {return (*begin()); }
  194. reference back()
  195. {return (*(--end())); }
  196. const_reference back() const
  197. {return (*(--end())); }
  198. void push_front(const _Ty& _X)
  199. {insert(begin(), _X); }
  200. void pop_front()
  201. {erase(begin()); }
  202. void push_back(const _Ty& _X)
  203. {insert(end(), _X); }
  204. void pop_back()
  205. {erase(--end()); }
  206. void assign(_It _F, _It _L)
  207. {erase(begin(), end());
  208. insert(begin(), _F, _L); }
  209. void assign(size_type _N, const _Ty& _X = _Ty())
  210. {erase(begin(), end());
  211. insert(begin(), _N, _X); }
  212. iterator insert(iterator _P, const _Ty& _X = _Ty())
  213. {_Nodeptr _S = _P._Mynode();
  214. _Acc::_Prev(_S) = _Buynode(_S, _Acc::_Prev(_S));
  215. _S = _Acc::_Prev(_S);
  216. _Acc::_Next(_Acc::_Prev(_S)) = _S;
  217. allocator.construct(&_Acc::_Value(_S), _X);
  218. ++_Size;
  219. return (iterator(_S)); }
  220. void insert(iterator _P, size_type _M, const _Ty& _X)
  221. {for (; 0 < _M; --_M)
  222. insert(_P, _X); }
  223. void insert(iterator _P, const _Ty *_F, const _Ty *_L)
  224. {for (; _F != _L; ++_F)
  225. insert(_P, *_F); }
  226. void insert(iterator _P, _It _F, _It _L)
  227. {for (; _F != _L; ++_F)
  228. insert(_P, *_F); }
  229. iterator erase(iterator _P)
  230. {_Nodeptr _S = (_P++)._Mynode();
  231. _Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
  232. _Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
  233. allocator.destroy(&_Acc::_Value(_S));
  234. _Freenode(_S);
  235. --_Size;
  236. return (_P); }
  237. iterator erase(iterator _F, iterator _L)
  238. {while (_F != _L)
  239. erase(_F++);
  240. return (_F); }
  241. void clear()
  242. {erase(begin(), end()); }
  243. void swap(_Myt& _X)
  244. {if (allocator == _X.allocator)
  245. {std::swap(_Head, _X._Head);
  246. std::swap(_Size, _X._Size); }
  247. else
  248. {iterator _P = begin();
  249. splice(_P, _X);
  250. _X.splice(_X.begin(), *this, _P, end()); }}
  251. friend void swap(_Myt& _X, _Myt& _Y)
  252. {_X.swap(_Y); }
  253. void splice(iterator _P, _Myt& _X)
  254. {if (!_X.empty())
  255. {_Splice(_P, _X, _X.begin(), _X.end());
  256. _Size += _X._Size;
  257. _X._Size = 0; }}
  258. void splice(iterator _P, _Myt& _X, iterator _F)
  259. {iterator _L = _F;
  260. if (_P != _F && _P != ++_L)
  261. {_Splice(_P, _X, _F, _L);
  262. ++_Size;
  263. --_X._Size; }}
  264. void splice(iterator _P, _Myt& _X, iterator _F, iterator _L)
  265. {if (_F != _L)
  266. {if (&_X != this)
  267. {difference_type _N = 0;
  268. _Distance(_F, _L, _N);
  269. _Size += _N;
  270. _X._Size -= _N; }
  271. _Splice(_P, _X, _F, _L); }}
  272. void remove(const _Ty& _V)
  273. {iterator _L = end();
  274. for (iterator _F = begin(); _F != _L; )
  275. if (*_F == _V)
  276. erase(_F++);
  277. else
  278. ++_F; }
  279. typedef binder2nd<not_equal_to<_Ty> > _Pr1;
  280. void remove_if(_Pr1 _Pr)
  281. {iterator _L = end();
  282. for (iterator _F = begin(); _F != _L; )
  283. if (_Pr(*_F))
  284. erase(_F++);
  285. else
  286. ++_F; }
  287. void unique()
  288. {iterator _F = begin(), _L = end();
  289. if (_F != _L)
  290. for (iterator _M = _F; ++_M != _L; _M = _F)
  291. if (*_F == *_M)
  292. erase(_M);
  293. else
  294. _F = _M; }
  295. typedef not_equal_to<_Ty> _Pr2;
  296. void unique(_Pr2 _Pr)
  297. {iterator _F = begin(), _L = end();
  298. if (_F != _L)
  299. for (iterator _M = _F; ++_M != _L; _M = _F)
  300. if (_Pr(*_F, *_M))
  301. erase(_M);
  302. else
  303. _F = _M; }
  304. void merge(_Myt& _X)
  305. {if (&_X != this)
  306. {iterator _F1 = begin(), _L1 = end();
  307. iterator _F2 = _X.begin(), _L2 = _X.end();
  308. while (_F1 != _L1 && _F2 != _L2)
  309. if (*_F2 < *_F1)
  310. {iterator _Mid2 = _F2;
  311. _Splice(_F1, _X, _F2, ++_Mid2);
  312. _F2 = _Mid2; }
  313. else
  314. ++_F1;
  315. if (_F2 != _L2)
  316. _Splice(_L1, _X, _F2, _L2);
  317. _Size += _X._Size;
  318. _X._Size = 0; }}
  319. typedef greater<_Ty> _Pr3;
  320. void merge(_Myt& _X, _Pr3 _Pr)
  321. {if (&_X != this)
  322. {iterator _F1 = begin(), _L1 = end();
  323. iterator _F2 = _X.begin(), _L2 = _X.end();
  324. while (_F1 != _L1 && _F2 != _L2)
  325. if (_Pr(*_F2, *_F1))
  326. {iterator _Mid2 = _F2;
  327. _Splice(_F1, _X, _F2, ++_Mid2);
  328. _F2 = _Mid2; }
  329. else
  330. ++_F1;
  331. if (_F2 != _L2)
  332. _Splice(_L1, _X, _F2, _L2);
  333. _Size += _X._Size;
  334. _X._Size = 0; }}
  335. void sort()
  336. {if (2 <= size())
  337. {const size_t _MAXN = 15;
  338. _Myt _X(allocator), _A[_MAXN + 1];
  339. size_t _N = 0;
  340. while (!empty())
  341. {_X.splice(_X.begin(), *this, begin());
  342. size_t _I;
  343. for (_I = 0; _I < _N && !_A[_I].empty(); ++_I)
  344. {_A[_I].merge(_X);
  345. _A[_I].swap(_X); }
  346. if (_I == _MAXN)
  347. _A[_I].merge(_X);
  348. else
  349. {_A[_I].swap(_X);
  350. if (_I == _N)
  351. ++_N; }}
  352. while (0 < _N)
  353. merge(_A[--_N]); }}
  354. void sort(_Pr3 _Pr)
  355. {if (2 <= size())
  356. {const size_t _MAXN = 15;
  357. _Myt _X(allocator), _A[_MAXN + 1];
  358. size_t _N = 0;
  359. while (!empty())
  360. {_X.splice(_X.begin(), *this, begin());
  361. size_t _I;
  362. for (_I = 0; _I < _N && !_A[_I].empty(); ++_I)
  363. {_A[_I].merge(_X, _Pr);
  364. _A[_I].swap(_X); }
  365. if (_I == _MAXN)
  366. _A[_I].merge(_X, _Pr);
  367. else
  368. {_A[_I].swap(_X);
  369. if (_I == _N)
  370. ++_N; }}
  371. while (0 < _N)
  372. merge(_A[--_N], _Pr); }}
  373. void reverse()
  374. {if (2 <= size())
  375. {iterator _L = end();
  376. for (iterator _F = ++begin(); _F != _L; )
  377. {iterator _M = _F;
  378. _Splice(begin(), *this, _M, ++_F); }}}
  379. protected:
  380. _Nodeptr _Buynode(_Nodeptr _Narg = 0, _Nodeptr _Parg = 0)
  381. {_Nodeptr _S = (_Nodeptr)allocator._Charalloc(
  382. 1 * sizeof (_Node));
  383. _Acc::_Next(_S) = _Narg != 0 ? _Narg : _S;
  384. _Acc::_Prev(_S) = _Parg != 0 ? _Parg : _S;
  385. return (_S); }
  386. void _Freenode(_Nodeptr _S)
  387. {allocator.deallocate(_S, 1); }
  388. void _Splice(iterator _P, _Myt& _X, iterator _F, iterator _L)
  389. {if (allocator == _X.allocator)
  390. {_Acc::_Next(_Acc::_Prev(_L._Mynode())) =
  391. _P._Mynode();
  392. _Acc::_Next(_Acc::_Prev(_F._Mynode())) =
  393. _L._Mynode();
  394. _Acc::_Next(_Acc::_Prev(_P._Mynode())) =
  395. _F._Mynode();
  396. _Nodeptr _S = _Acc::_Prev(_P._Mynode());
  397. _Acc::_Prev(_P._Mynode()) =
  398. _Acc::_Prev(_L._Mynode());
  399. _Acc::_Prev(_L._Mynode()) =
  400. _Acc::_Prev(_F._Mynode());
  401. _Acc::_Prev(_F._Mynode()) = _S; }
  402. else
  403. {insert(_P, _F, _L);
  404. _X.erase(_F, _L); }}
  405. void _Xran() const
  406. {_THROW(out_of_range, "invalid list<T> subscript"); }
  407. _A allocator;
  408. _Nodeptr _Head;
  409. size_type _Size;
  410. };
  411. // list TEMPLATE OPERATORS
  412. template<class _Ty, class _A> inline
  413. bool operator==(const list<_Ty, _A>& _X,
  414. const list<_Ty, _A>& _Y)
  415. {return (_X.size() == _Y.size()
  416. && equal(_X.begin(), _X.end(), _Y.begin())); }
  417. template<class _Ty, class _A> inline
  418. bool operator!=(const list<_Ty, _A>& _X,
  419. const list<_Ty, _A>& _Y)
  420. {return (!(_X == _Y)); }
  421. template<class _Ty, class _A> inline
  422. bool operator<(const list<_Ty, _A>& _X,
  423. const list<_Ty, _A>& _Y)
  424. {return (lexicographical_compare(_X.begin(), _X.end(),
  425. _Y.begin(), _Y.end())); }
  426. template<class _Ty, class _A> inline
  427. bool operator>(const list<_Ty, _A>& _X,
  428. const list<_Ty, _A>& _Y)
  429. {return (_Y < _X); }
  430. template<class _Ty, class _A> inline
  431. bool operator<=(const list<_Ty, _A>& _X,
  432. const list<_Ty, _A>& _Y)
  433. {return (!(_Y < _X)); }
  434. template<class _Ty, class _A> inline
  435. bool operator>=(const list<_Ty, _A>& _X,
  436. const list<_Ty, _A>& _Y)
  437. {return (!(_X < _Y)); }
  438. _STD_END
  439. #ifdef _MSC_VER
  440. #pragma pack(pop)
  441. #endif /* _MSC_VER */
  442. #endif /* _LIST_ */
  443. /*
  444. * Copyright (c) 1995 by P.J. Plauger. ALL RIGHTS RESERVED.
  445. * Consult your license regarding permissions and restrictions.
  446. */
  447. /*
  448. * This file is derived from software bearing the following
  449. * restrictions:
  450. *
  451. * Copyright (c) 1994
  452. * Hewlett-Packard Company
  453. *
  454. * Permission to use, copy, modify, distribute and sell this
  455. * software and its documentation for any purpose is hereby
  456. * granted without fee, provided that the above copyright notice
  457. * appear in all copies and that both that copyright notice and
  458. * this permission notice appear in supporting documentation.
  459. * Hewlett-Packard Company makes no representations about the
  460. * suitability of this software for any purpose. It is provided
  461. * "as is" without express or implied warranty.
  462. */