_queue.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. *
  3. * Copyright (c) 1994
  4. * Hewlett-Packard Company
  5. *
  6. * Copyright (c) 1996,1997
  7. * Silicon Graphics Computer Systems, Inc.
  8. *
  9. * Copyright (c) 1997
  10. * Moscow Center for SPARC Technology
  11. *
  12. * Copyright (c) 1999
  13. * Boris Fomitchev
  14. *
  15. * This material is provided "as is", with absolutely no warranty expressed
  16. * or implied. Any use is at your own risk.
  17. *
  18. * Permission to use or copy this software for any purpose is hereby granted
  19. * without fee, provided the above notices are retained on all copies.
  20. * Permission to modify the code and to distribute modified code is granted,
  21. * provided the above notices are retained, and a notice that the code was
  22. * modified is included with the above copyright notice.
  23. *
  24. */
  25. /* NOTE: This is an internal header file, included by other STL headers.
  26. * You should not attempt to use it directly.
  27. */
  28. #ifndef _STLP_INTERNAL_QUEUE_H
  29. #define _STLP_INTERNAL_QUEUE_H
  30. #ifndef _STLP_INTERNAL_DEQUE_H
  31. # include <stl/_deque.h>
  32. #endif
  33. #ifndef _STLP_INTERNAL_VECTOR_H
  34. # include <stl/_vector.h>
  35. #endif
  36. #ifndef _STLP_INTERNAL_HEAP_H
  37. # include <stl/_heap.h>
  38. #endif
  39. #ifndef _STLP_INTERNAL_FUNCTION_BASE_H
  40. # include <stl/_function_base.h>
  41. #endif
  42. _STLP_BEGIN_NAMESPACE
  43. # if ! defined ( _STLP_LIMITED_DEFAULT_TEMPLATES )
  44. template <class _Tp, class _Sequence = deque<_Tp> >
  45. # elif defined ( _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS )
  46. # define _STLP_QUEUE_ARGS _Tp
  47. template <class _Tp>
  48. # else
  49. template <class _Tp, class _Sequence>
  50. # endif
  51. class queue
  52. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  53. # if defined (_STLP_QUEUE_ARGS)
  54. : public __stlport_class<queue<_Tp> >
  55. # else
  56. : public __stlport_class<queue<_Tp, _Sequence> >
  57. # endif
  58. #endif
  59. {
  60. # if defined ( _STLP_QUEUE_ARGS )
  61. typedef deque<_Tp> _Sequence;
  62. typedef queue<_Tp> _Self;
  63. # else
  64. typedef queue<_Tp, _Sequence> _Self;
  65. # endif
  66. public:
  67. typedef typename _Sequence::value_type value_type;
  68. typedef typename _Sequence::size_type size_type;
  69. typedef _Sequence container_type;
  70. typedef typename _Sequence::reference reference;
  71. typedef typename _Sequence::const_reference const_reference;
  72. protected:
  73. //c is a Standard name (23.2.3.1), do no make it STLport naming convention compliant.
  74. _Sequence c;
  75. public:
  76. queue() : c() {}
  77. explicit queue(const _Sequence& __c) : c(__c) {}
  78. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  79. queue(__move_source<_Self> src)
  80. : c(_STLP_PRIV _AsMoveSource(src.get().c)) {}
  81. #endif
  82. bool empty() const { return c.empty(); }
  83. size_type size() const { return c.size(); }
  84. reference front() { return c.front(); }
  85. const_reference front() const { return c.front(); }
  86. reference back() { return c.back(); }
  87. const_reference back() const { return c.back(); }
  88. void push(const value_type& __x) { c.push_back(__x); }
  89. void pop() { c.pop_front(); }
  90. const _Sequence& _Get_s() const { return c; }
  91. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  92. void _M_swap_workaround(_Self& __x) {
  93. _Sequence __tmp = c;
  94. c = __x.c;
  95. __x.c = __tmp;
  96. }
  97. #endif
  98. };
  99. #ifndef _STLP_QUEUE_ARGS
  100. # define _STLP_QUEUE_ARGS _Tp, _Sequence
  101. # define _STLP_QUEUE_HEADER_ARGS class _Tp, class _Sequence
  102. #else
  103. # define _STLP_QUEUE_HEADER_ARGS class _Tp
  104. #endif
  105. template < _STLP_QUEUE_HEADER_ARGS >
  106. inline bool _STLP_CALL
  107. operator==(const queue<_STLP_QUEUE_ARGS >& __x, const queue<_STLP_QUEUE_ARGS >& __y) {
  108. return __x._Get_s() == __y._Get_s();
  109. }
  110. template < _STLP_QUEUE_HEADER_ARGS >
  111. inline bool _STLP_CALL
  112. operator<(const queue<_STLP_QUEUE_ARGS >& __x, const queue<_STLP_QUEUE_ARGS >& __y) {
  113. return __x._Get_s() < __y._Get_s();
  114. }
  115. _STLP_RELOPS_OPERATORS( template < _STLP_QUEUE_HEADER_ARGS >, queue<_STLP_QUEUE_ARGS > )
  116. # if !(defined ( _STLP_LIMITED_DEFAULT_TEMPLATES ) || defined ( _STLP_TEMPLATE_PARAM_SUBTYPE_BUG ))
  117. template <class _Tp, class _Sequence = vector<_Tp>,
  118. class _Compare = less<_STLP_HEADER_TYPENAME _Sequence::value_type> >
  119. # elif defined ( _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS )
  120. template <class _Tp>
  121. # else
  122. template <class _Tp, class _Sequence, class _Compare>
  123. # endif
  124. class priority_queue
  125. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND)
  126. # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS)
  127. : public __stlport_class<priority_queue<_Tp> >
  128. # else
  129. : public __stlport_class<priority_queue<_Tp, _Sequence> >
  130. # endif
  131. #endif
  132. {
  133. # ifdef _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS
  134. typedef vector<_Tp> _Sequence;
  135. typedef less< typename vector<_Tp>::value_type> _Compare;
  136. typedef priority_queue<_Tp> _Self;
  137. # else
  138. typedef priority_queue<_Tp, _Sequence, _Compare> _Self;
  139. # endif
  140. public:
  141. typedef typename _Sequence::value_type value_type;
  142. typedef typename _Sequence::size_type size_type;
  143. typedef _Sequence container_type;
  144. typedef typename _Sequence::reference reference;
  145. typedef typename _Sequence::const_reference const_reference;
  146. protected:
  147. //c is a Standard name (23.2.3.2), do no make it STLport naming convention compliant.
  148. _Sequence c;
  149. _Compare comp;
  150. public:
  151. priority_queue() : c() {}
  152. explicit priority_queue(const _Compare& __x) : c(), comp(__x) {}
  153. priority_queue(const _Compare& __x, const _Sequence& __s)
  154. : c(__s), comp(__x)
  155. { make_heap(c.begin(), c.end(), comp); }
  156. #if !defined (_STLP_NO_MOVE_SEMANTIC)
  157. priority_queue(__move_source<_Self> src)
  158. : c(_STLP_PRIV _AsMoveSource(src.get().c)),
  159. comp(_STLP_PRIV _AsMoveSource(src.get().comp)) {}
  160. #endif
  161. #ifdef _STLP_MEMBER_TEMPLATES
  162. template <class _InputIterator>
  163. priority_queue(_InputIterator __first, _InputIterator __last)
  164. : c(__first, __last) { make_heap(c.begin(), c.end(), comp); }
  165. template <class _InputIterator>
  166. priority_queue(_InputIterator __first,
  167. _InputIterator __last, const _Compare& __x)
  168. : c(__first, __last), comp(__x)
  169. { make_heap(c.begin(), c.end(), comp); }
  170. template <class _InputIterator>
  171. priority_queue(_InputIterator __first, _InputIterator __last,
  172. const _Compare& __x, const _Sequence& __s)
  173. : c(__s), comp(__x)
  174. {
  175. c.insert(c.end(), __first, __last);
  176. make_heap(c.begin(), c.end(), comp);
  177. }
  178. #else /* _STLP_MEMBER_TEMPLATES */
  179. priority_queue(const value_type* __first, const value_type* __last)
  180. : c(__first, __last) { make_heap(c.begin(), c.end(), comp); }
  181. priority_queue(const value_type* __first, const value_type* __last,
  182. const _Compare& __x)
  183. : c(__first, __last), comp(__x)
  184. { make_heap(c.begin(), c.end(), comp); }
  185. priority_queue(const value_type* __first, const value_type* __last,
  186. const _Compare& __x, const _Sequence& __c)
  187. : c(__c), comp(__x)
  188. {
  189. c.insert(c.end(), __first, __last);
  190. make_heap(c.begin(), c.end(), comp);
  191. }
  192. #endif /* _STLP_MEMBER_TEMPLATES */
  193. bool empty() const { return c.empty(); }
  194. size_type size() const { return c.size(); }
  195. const_reference top() const { return c.front(); }
  196. void push(const value_type& __x) {
  197. _STLP_TRY {
  198. c.push_back(__x);
  199. push_heap(c.begin(), c.end(), comp);
  200. }
  201. _STLP_UNWIND(c.clear())
  202. }
  203. void pop() {
  204. _STLP_TRY {
  205. pop_heap(c.begin(), c.end(), comp);
  206. c.pop_back();
  207. }
  208. _STLP_UNWIND(c.clear())
  209. }
  210. #if defined (_STLP_USE_PARTIAL_SPEC_WORKAROUND) && !defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  211. void _M_swap_workaround(_Self& __x) {
  212. _Sequence __tmp = c;
  213. c = __x.c;
  214. __x.c = __tmp;
  215. }
  216. #endif
  217. };
  218. #if defined (_STLP_CLASS_PARTIAL_SPECIALIZATION) && !defined (_STLP_NO_MOVE_SEMANTIC)
  219. template <class _Tp, class _Sequence>
  220. struct __move_traits<queue<_Tp, _Sequence> > :
  221. _STLP_PRIV __move_traits_aux<_Sequence>
  222. {};
  223. template <class _Tp, class _Sequence, class _Compare>
  224. struct __move_traits<priority_queue<_Tp, _Sequence, _Compare> > :
  225. _STLP_PRIV __move_traits_aux2<_Sequence, _Compare>
  226. {};
  227. #endif
  228. _STLP_END_NAMESPACE
  229. #undef _STLP_QUEUE_ARGS
  230. #undef _STLP_QUEUE_HEADER_ARGS
  231. #undef comp
  232. #endif /* _STLP_INTERNAL_QUEUE_H */
  233. // Local Variables:
  234. // mode:C++
  235. // End: