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.

249 lines
5.9 KiB

  1. // queue standard header
  2. #pragma once
  3. #ifndef _QUEUE_
  4. #define _QUEUE_
  5. #include <algorithm>
  6. #include <deque>
  7. #include <functional>
  8. #include <vector>
  9. #pragma pack(push,8)
  10. #pragma warning(push,3)
  11. _STD_BEGIN
  12. // TEMPLATE CLASS queue
  13. template<class _Ty,
  14. class _Container = deque<_Ty> >
  15. class queue
  16. { // FIFO queue implemented with a container
  17. public:
  18. typedef _Container container_type;
  19. typedef typename _Container::value_type value_type;
  20. typedef typename _Container::size_type size_type;
  21. queue()
  22. : c()
  23. { // construct with empty container
  24. }
  25. explicit queue(const _Container& _Cont)
  26. : c(_Cont)
  27. { // construct by copying specified container
  28. }
  29. bool empty() const
  30. { // test if queue is empty
  31. return (c.empty());
  32. }
  33. size_type size() const
  34. { // return length of queue
  35. return (c.size());
  36. }
  37. value_type& front()
  38. { // return first element of mutable queue
  39. return (c.front());
  40. }
  41. const value_type& front() const
  42. { // return first element of nonmutable queue
  43. return (c.front());
  44. }
  45. value_type& back()
  46. { // return last element of mutable queue
  47. return (c.back());
  48. }
  49. const value_type& back() const
  50. { // return last element of nonmutable queue
  51. return (c.back());
  52. }
  53. void push(const value_type& _Val)
  54. { // insert element at beginning
  55. c.push_back(_Val);
  56. }
  57. void pop()
  58. { // erase element at end
  59. c.pop_front();
  60. }
  61. bool _Eq(const queue<_Ty, _Container>& _Right) const
  62. { // test for queue equality
  63. return (c == _Right.c);
  64. }
  65. bool _Lt(const queue<_Ty, _Container>& _Right) const
  66. { // test if this < _Right for queues
  67. return (c < _Right.c);
  68. }
  69. protected:
  70. _Container c; // the underlying container
  71. };
  72. // queue TEMPLATE FUNCTIONS
  73. template<class _Ty,
  74. class _Container> inline
  75. bool operator==(const queue<_Ty, _Container>& _Left,
  76. const queue<_Ty, _Container>& _Right)
  77. { // test for queue equality
  78. return (_Left._Eq(_Right));
  79. }
  80. template<class _Ty,
  81. class _Container> inline
  82. bool operator!=(const queue<_Ty, _Container>& _Left,
  83. const queue<_Ty, _Container>& _Right)
  84. { // test for queue inequality
  85. return (!(_Left == _Right));
  86. }
  87. template<class _Ty,
  88. class _Container> inline
  89. bool operator<(const queue<_Ty, _Container>& _Left,
  90. const queue<_Ty, _Container>& _Right)
  91. { // test if _Left < _Right for queues
  92. return (_Left._Lt(_Right));
  93. }
  94. template<class _Ty,
  95. class _Container> inline
  96. bool operator>(const queue<_Ty, _Container>& _Left,
  97. const queue<_Ty, _Container>& _Right)
  98. { // test if _Left > _Right for queues
  99. return (_Right < _Left);
  100. }
  101. template<class _Ty,
  102. class _Container> inline
  103. bool operator<=(const queue<_Ty, _Container>& _Left,
  104. const queue<_Ty, _Container>& _Right)
  105. { // test if _Left <= _Right for queues
  106. return (!(_Right < _Left));
  107. }
  108. template<class _Ty,
  109. class _Container> inline
  110. bool operator>=(const queue<_Ty, _Container>& _Left,
  111. const queue<_Ty, _Container>& _Right)
  112. { // test if _Left >= _Right for queues
  113. return (!(_Left < _Right));
  114. }
  115. // TEMPLATE CLASS priority_queue
  116. template<class _Ty,
  117. class _Container = vector<_Ty>,
  118. class _Pr = less<typename _Container::value_type> >
  119. class priority_queue
  120. { // priority queue implemented with a _Container
  121. public:
  122. typedef _Container container_type;
  123. typedef typename _Container::value_type value_type;
  124. typedef typename _Container::size_type size_type;
  125. priority_queue()
  126. : c(), comp()
  127. { // construct with empty container, default comparator
  128. }
  129. explicit priority_queue(const _Pr& _Pred)
  130. : c(), comp(_Pred)
  131. { // construct with empty container, specified comparator
  132. }
  133. priority_queue(const _Pr& _Pred, const _Container& _Cont)
  134. : c(_Cont), comp(_Pred)
  135. { // construct by copying specified container, comparator
  136. make_heap(c.begin(), c.end(), comp);
  137. }
  138. template<class _Iter>
  139. priority_queue(_Iter _First, _Iter _Last)
  140. : c(_First, _Last), comp()
  141. { // construct by copying [_First, _Last), default comparator
  142. make_heap(c.begin(), c.end(), comp);
  143. }
  144. template<class _Iter>
  145. priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred)
  146. : c(_First, _Last), comp(_Pred)
  147. { // construct by copying [_First, _Last), specified comparator
  148. make_heap(c.begin(), c.end(), comp);
  149. }
  150. template<class _Iter>
  151. priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred,
  152. const _Container& _Cont)
  153. : c(_Cont), comp(_Pred)
  154. { // construct by copying [_First, _Last), container, and comparator
  155. c.insert(c.end(), _First, _Last);
  156. make_heap(c.begin(), c.end(), comp);
  157. }
  158. bool empty() const
  159. { // test if queue is empty
  160. return (c.empty());
  161. }
  162. size_type size() const
  163. { // return length of queue
  164. return (c.size());
  165. }
  166. const value_type& top() const
  167. { // return highest-priority element
  168. return (c.front());
  169. }
  170. value_type& top()
  171. { // return mutable highest-priority element (retained)
  172. return (c.front());
  173. }
  174. void push(const value_type& _Pred)
  175. { // insert value in priority order
  176. c.push_back(_Pred);
  177. push_heap(c.begin(), c.end(), comp);
  178. }
  179. void pop()
  180. { // erase highest-priority element
  181. pop_heap(c.begin(), c.end(), comp);
  182. c.pop_back();
  183. }
  184. protected:
  185. _Container c; // the underlying container
  186. _Pr comp; // the comparator functor
  187. };
  188. _STD_END
  189. #pragma warning(pop)
  190. #pragma pack(pop)
  191. #endif /* _QUEUE_ */
  192. /*
  193. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  194. * Consult your license regarding permissions and restrictions.
  195. */
  196. /*
  197. * This file is derived from software bearing the following
  198. * restrictions:
  199. *
  200. * Copyright (c) 1994
  201. * Hewlett-Packard Company
  202. *
  203. * Permission to use, copy, modify, distribute and sell this
  204. * software and its documentation for any purpose is hereby
  205. * granted without fee, provided that the above copyright notice
  206. * appear in all copies and that both that copyright notice and
  207. * this permission notice appear in supporting documentation.
  208. * Hewlett-Packard Company makes no representations about the
  209. * suitability of this software for any purpose. It is provided
  210. * "as is" without express or implied warranty.
  211. V3.10:0009 */