1*38fd1498Szrj // Queue implementation -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /* 26*38fd1498Szrj * 27*38fd1498Szrj * Copyright (c) 1994 28*38fd1498Szrj * Hewlett-Packard Company 29*38fd1498Szrj * 30*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 31*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 32*38fd1498Szrj * provided that the above copyright notice appear in all copies and 33*38fd1498Szrj * that both that copyright notice and this permission notice appear 34*38fd1498Szrj * in supporting documentation. Hewlett-Packard Company makes no 35*38fd1498Szrj * representations about the suitability of this software for any 36*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 37*38fd1498Szrj * 38*38fd1498Szrj * 39*38fd1498Szrj * Copyright (c) 1996,1997 40*38fd1498Szrj * Silicon Graphics Computer Systems, Inc. 41*38fd1498Szrj * 42*38fd1498Szrj * Permission to use, copy, modify, distribute and sell this software 43*38fd1498Szrj * and its documentation for any purpose is hereby granted without fee, 44*38fd1498Szrj * provided that the above copyright notice appear in all copies and 45*38fd1498Szrj * that both that copyright notice and this permission notice appear 46*38fd1498Szrj * in supporting documentation. Silicon Graphics makes no 47*38fd1498Szrj * representations about the suitability of this software for any 48*38fd1498Szrj * purpose. It is provided "as is" without express or implied warranty. 49*38fd1498Szrj */ 50*38fd1498Szrj 51*38fd1498Szrj /** @file bits/stl_queue.h 52*38fd1498Szrj * This is an internal header file, included by other library headers. 53*38fd1498Szrj * Do not attempt to use it directly. @headername{queue} 54*38fd1498Szrj */ 55*38fd1498Szrj 56*38fd1498Szrj #ifndef _STL_QUEUE_H 57*38fd1498Szrj #define _STL_QUEUE_H 1 58*38fd1498Szrj 59*38fd1498Szrj #include <bits/concept_check.h> 60*38fd1498Szrj #include <debug/debug.h> 61*38fd1498Szrj #if __cplusplus >= 201103L 62*38fd1498Szrj # include <bits/uses_allocator.h> 63*38fd1498Szrj #endif 64*38fd1498Szrj 65*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 66*38fd1498Szrj { 67*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 68*38fd1498Szrj 69*38fd1498Szrj /** 70*38fd1498Szrj * @brief A standard container giving FIFO behavior. 71*38fd1498Szrj * 72*38fd1498Szrj * @ingroup sequences 73*38fd1498Szrj * 74*38fd1498Szrj * @tparam _Tp Type of element. 75*38fd1498Szrj * @tparam _Sequence Type of underlying sequence, defaults to deque<_Tp>. 76*38fd1498Szrj * 77*38fd1498Szrj * Meets many of the requirements of a 78*38fd1498Szrj * <a href="tables.html#65">container</a>, 79*38fd1498Szrj * but does not define anything to do with iterators. Very few of the 80*38fd1498Szrj * other standard container interfaces are defined. 81*38fd1498Szrj * 82*38fd1498Szrj * This is not a true container, but an @e adaptor. It holds another 83*38fd1498Szrj * container, and provides a wrapper interface to that container. The 84*38fd1498Szrj * wrapper is what enforces strict first-in-first-out %queue behavior. 85*38fd1498Szrj * 86*38fd1498Szrj * The second template parameter defines the type of the underlying 87*38fd1498Szrj * sequence/container. It defaults to std::deque, but it can be any type 88*38fd1498Szrj * that supports @c front, @c back, @c push_back, and @c pop_front, 89*38fd1498Szrj * such as std::list or an appropriate user-defined type. 90*38fd1498Szrj * 91*38fd1498Szrj * Members not found in @a normal containers are @c container_type, 92*38fd1498Szrj * which is a typedef for the second Sequence parameter, and @c push and 93*38fd1498Szrj * @c pop, which are standard %queue/FIFO operations. 94*38fd1498Szrj */ 95*38fd1498Szrj template<typename _Tp, typename _Sequence = deque<_Tp> > 96*38fd1498Szrj class queue 97*38fd1498Szrj { 98*38fd1498Szrj #ifdef _GLIBCXX_CONCEPT_CHECKS 99*38fd1498Szrj // concept requirements 100*38fd1498Szrj typedef typename _Sequence::value_type _Sequence_value_type; 101*38fd1498Szrj # if __cplusplus < 201103L 102*38fd1498Szrj __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 103*38fd1498Szrj # endif 104*38fd1498Szrj __glibcxx_class_requires(_Sequence, _FrontInsertionSequenceConcept) 105*38fd1498Szrj __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept) 106*38fd1498Szrj __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 107*38fd1498Szrj #endif 108*38fd1498Szrj 109*38fd1498Szrj template<typename _Tp1, typename _Seq1> 110*38fd1498Szrj friend bool 111*38fd1498Szrj operator==(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); 112*38fd1498Szrj 113*38fd1498Szrj template<typename _Tp1, typename _Seq1> 114*38fd1498Szrj friend bool 115*38fd1498Szrj operator<(const queue<_Tp1, _Seq1>&, const queue<_Tp1, _Seq1>&); 116*38fd1498Szrj 117*38fd1498Szrj #if __cplusplus >= 201103L 118*38fd1498Szrj template<typename _Alloc> 119*38fd1498Szrj using _Uses = typename 120*38fd1498Szrj enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; 121*38fd1498Szrj #endif 122*38fd1498Szrj 123*38fd1498Szrj public: 124*38fd1498Szrj typedef typename _Sequence::value_type value_type; 125*38fd1498Szrj typedef typename _Sequence::reference reference; 126*38fd1498Szrj typedef typename _Sequence::const_reference const_reference; 127*38fd1498Szrj typedef typename _Sequence::size_type size_type; 128*38fd1498Szrj typedef _Sequence container_type; 129*38fd1498Szrj 130*38fd1498Szrj protected: 131*38fd1498Szrj /* Maintainers wondering why this isn't uglified as per style 132*38fd1498Szrj * guidelines should note that this name is specified in the standard, 133*38fd1498Szrj * C++98 [23.2.3.1]. 134*38fd1498Szrj * (Why? Presumably for the same reason that it's protected instead 135*38fd1498Szrj * of private: to allow derivation. But none of the other 136*38fd1498Szrj * containers allow for derivation. Odd.) 137*38fd1498Szrj */ 138*38fd1498Szrj /// @c c is the underlying container. 139*38fd1498Szrj _Sequence c; 140*38fd1498Szrj 141*38fd1498Szrj public: 142*38fd1498Szrj /** 143*38fd1498Szrj * @brief Default constructor creates no elements. 144*38fd1498Szrj */ 145*38fd1498Szrj #if __cplusplus < 201103L 146*38fd1498Szrj explicit 147*38fd1498Szrj queue(const _Sequence& __c = _Sequence()) 148*38fd1498Szrj : c(__c) { } 149*38fd1498Szrj #else 150*38fd1498Szrj template<typename _Seq = _Sequence, typename _Requires = typename 151*38fd1498Szrj enable_if<is_default_constructible<_Seq>::value>::type> 152*38fd1498Szrj queue() 153*38fd1498Szrj : c() { } 154*38fd1498Szrj 155*38fd1498Szrj explicit 156*38fd1498Szrj queue(const _Sequence& __c) 157*38fd1498Szrj : c(__c) { } 158*38fd1498Szrj 159*38fd1498Szrj explicit 160*38fd1498Szrj queue(_Sequence&& __c) 161*38fd1498Szrj : c(std::move(__c)) { } 162*38fd1498Szrj 163*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 164*38fd1498Szrj explicit 165*38fd1498Szrj queue(const _Alloc& __a) 166*38fd1498Szrj : c(__a) { } 167*38fd1498Szrj 168*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 169*38fd1498Szrj queue(const _Sequence& __c, const _Alloc& __a) 170*38fd1498Szrj : c(__c, __a) { } 171*38fd1498Szrj 172*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 173*38fd1498Szrj queue(_Sequence&& __c, const _Alloc& __a) 174*38fd1498Szrj : c(std::move(__c), __a) { } 175*38fd1498Szrj 176*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 177*38fd1498Szrj queue(const queue& __q, const _Alloc& __a) 178*38fd1498Szrj : c(__q.c, __a) { } 179*38fd1498Szrj 180*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 181*38fd1498Szrj queue(queue&& __q, const _Alloc& __a) 182*38fd1498Szrj : c(std::move(__q.c), __a) { } 183*38fd1498Szrj #endif 184*38fd1498Szrj 185*38fd1498Szrj /** 186*38fd1498Szrj * Returns true if the %queue is empty. 187*38fd1498Szrj */ 188*38fd1498Szrj bool 189*38fd1498Szrj empty() const 190*38fd1498Szrj { return c.empty(); } 191*38fd1498Szrj 192*38fd1498Szrj /** Returns the number of elements in the %queue. */ 193*38fd1498Szrj size_type 194*38fd1498Szrj size() const 195*38fd1498Szrj { return c.size(); } 196*38fd1498Szrj 197*38fd1498Szrj /** 198*38fd1498Szrj * Returns a read/write reference to the data at the first 199*38fd1498Szrj * element of the %queue. 200*38fd1498Szrj */ 201*38fd1498Szrj reference 202*38fd1498Szrj front() 203*38fd1498Szrj { 204*38fd1498Szrj __glibcxx_requires_nonempty(); 205*38fd1498Szrj return c.front(); 206*38fd1498Szrj } 207*38fd1498Szrj 208*38fd1498Szrj /** 209*38fd1498Szrj * Returns a read-only (constant) reference to the data at the first 210*38fd1498Szrj * element of the %queue. 211*38fd1498Szrj */ 212*38fd1498Szrj const_reference 213*38fd1498Szrj front() const 214*38fd1498Szrj { 215*38fd1498Szrj __glibcxx_requires_nonempty(); 216*38fd1498Szrj return c.front(); 217*38fd1498Szrj } 218*38fd1498Szrj 219*38fd1498Szrj /** 220*38fd1498Szrj * Returns a read/write reference to the data at the last 221*38fd1498Szrj * element of the %queue. 222*38fd1498Szrj */ 223*38fd1498Szrj reference 224*38fd1498Szrj back() 225*38fd1498Szrj { 226*38fd1498Szrj __glibcxx_requires_nonempty(); 227*38fd1498Szrj return c.back(); 228*38fd1498Szrj } 229*38fd1498Szrj 230*38fd1498Szrj /** 231*38fd1498Szrj * Returns a read-only (constant) reference to the data at the last 232*38fd1498Szrj * element of the %queue. 233*38fd1498Szrj */ 234*38fd1498Szrj const_reference 235*38fd1498Szrj back() const 236*38fd1498Szrj { 237*38fd1498Szrj __glibcxx_requires_nonempty(); 238*38fd1498Szrj return c.back(); 239*38fd1498Szrj } 240*38fd1498Szrj 241*38fd1498Szrj /** 242*38fd1498Szrj * @brief Add data to the end of the %queue. 243*38fd1498Szrj * @param __x Data to be added. 244*38fd1498Szrj * 245*38fd1498Szrj * This is a typical %queue operation. The function creates an 246*38fd1498Szrj * element at the end of the %queue and assigns the given data 247*38fd1498Szrj * to it. The time complexity of the operation depends on the 248*38fd1498Szrj * underlying sequence. 249*38fd1498Szrj */ 250*38fd1498Szrj void 251*38fd1498Szrj push(const value_type& __x) 252*38fd1498Szrj { c.push_back(__x); } 253*38fd1498Szrj 254*38fd1498Szrj #if __cplusplus >= 201103L 255*38fd1498Szrj void 256*38fd1498Szrj push(value_type&& __x) 257*38fd1498Szrj { c.push_back(std::move(__x)); } 258*38fd1498Szrj 259*38fd1498Szrj #if __cplusplus > 201402L 260*38fd1498Szrj template<typename... _Args> 261*38fd1498Szrj decltype(auto) 262*38fd1498Szrj emplace(_Args&&... __args) 263*38fd1498Szrj { return c.emplace_back(std::forward<_Args>(__args)...); } 264*38fd1498Szrj #else 265*38fd1498Szrj template<typename... _Args> 266*38fd1498Szrj void 267*38fd1498Szrj emplace(_Args&&... __args) 268*38fd1498Szrj { c.emplace_back(std::forward<_Args>(__args)...); } 269*38fd1498Szrj #endif 270*38fd1498Szrj #endif 271*38fd1498Szrj 272*38fd1498Szrj /** 273*38fd1498Szrj * @brief Removes first element. 274*38fd1498Szrj * 275*38fd1498Szrj * This is a typical %queue operation. It shrinks the %queue by one. 276*38fd1498Szrj * The time complexity of the operation depends on the underlying 277*38fd1498Szrj * sequence. 278*38fd1498Szrj * 279*38fd1498Szrj * Note that no data is returned, and if the first element's 280*38fd1498Szrj * data is needed, it should be retrieved before pop() is 281*38fd1498Szrj * called. 282*38fd1498Szrj */ 283*38fd1498Szrj void 284*38fd1498Szrj pop() 285*38fd1498Szrj { 286*38fd1498Szrj __glibcxx_requires_nonempty(); 287*38fd1498Szrj c.pop_front(); 288*38fd1498Szrj } 289*38fd1498Szrj 290*38fd1498Szrj #if __cplusplus >= 201103L 291*38fd1498Szrj void 292*38fd1498Szrj swap(queue& __q) 293*38fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 294*38fd1498Szrj noexcept(__is_nothrow_swappable<_Sequence>::value) 295*38fd1498Szrj #else 296*38fd1498Szrj noexcept(__is_nothrow_swappable<_Tp>::value) 297*38fd1498Szrj #endif 298*38fd1498Szrj { 299*38fd1498Szrj using std::swap; 300*38fd1498Szrj swap(c, __q.c); 301*38fd1498Szrj } 302*38fd1498Szrj #endif // __cplusplus >= 201103L 303*38fd1498Szrj }; 304*38fd1498Szrj 305*38fd1498Szrj /** 306*38fd1498Szrj * @brief Queue equality comparison. 307*38fd1498Szrj * @param __x A %queue. 308*38fd1498Szrj * @param __y A %queue of the same type as @a __x. 309*38fd1498Szrj * @return True iff the size and elements of the queues are equal. 310*38fd1498Szrj * 311*38fd1498Szrj * This is an equivalence relation. Complexity and semantics depend on the 312*38fd1498Szrj * underlying sequence type, but the expected rules are: this relation is 313*38fd1498Szrj * linear in the size of the sequences, and queues are considered equivalent 314*38fd1498Szrj * if their sequences compare equal. 315*38fd1498Szrj */ 316*38fd1498Szrj template<typename _Tp, typename _Seq> 317*38fd1498Szrj inline bool 318*38fd1498Szrj operator==(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 319*38fd1498Szrj { return __x.c == __y.c; } 320*38fd1498Szrj 321*38fd1498Szrj /** 322*38fd1498Szrj * @brief Queue ordering relation. 323*38fd1498Szrj * @param __x A %queue. 324*38fd1498Szrj * @param __y A %queue of the same type as @a x. 325*38fd1498Szrj * @return True iff @a __x is lexicographically less than @a __y. 326*38fd1498Szrj * 327*38fd1498Szrj * This is an total ordering relation. Complexity and semantics 328*38fd1498Szrj * depend on the underlying sequence type, but the expected rules 329*38fd1498Szrj * are: this relation is linear in the size of the sequences, the 330*38fd1498Szrj * elements must be comparable with @c <, and 331*38fd1498Szrj * std::lexicographical_compare() is usually used to make the 332*38fd1498Szrj * determination. 333*38fd1498Szrj */ 334*38fd1498Szrj template<typename _Tp, typename _Seq> 335*38fd1498Szrj inline bool 336*38fd1498Szrj operator<(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 337*38fd1498Szrj { return __x.c < __y.c; } 338*38fd1498Szrj 339*38fd1498Szrj /// Based on operator== 340*38fd1498Szrj template<typename _Tp, typename _Seq> 341*38fd1498Szrj inline bool 342*38fd1498Szrj operator!=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 343*38fd1498Szrj { return !(__x == __y); } 344*38fd1498Szrj 345*38fd1498Szrj /// Based on operator< 346*38fd1498Szrj template<typename _Tp, typename _Seq> 347*38fd1498Szrj inline bool 348*38fd1498Szrj operator>(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 349*38fd1498Szrj { return __y < __x; } 350*38fd1498Szrj 351*38fd1498Szrj /// Based on operator< 352*38fd1498Szrj template<typename _Tp, typename _Seq> 353*38fd1498Szrj inline bool 354*38fd1498Szrj operator<=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 355*38fd1498Szrj { return !(__y < __x); } 356*38fd1498Szrj 357*38fd1498Szrj /// Based on operator< 358*38fd1498Szrj template<typename _Tp, typename _Seq> 359*38fd1498Szrj inline bool 360*38fd1498Szrj operator>=(const queue<_Tp, _Seq>& __x, const queue<_Tp, _Seq>& __y) 361*38fd1498Szrj { return !(__x < __y); } 362*38fd1498Szrj 363*38fd1498Szrj #if __cplusplus >= 201103L 364*38fd1498Szrj template<typename _Tp, typename _Seq> 365*38fd1498Szrj inline 366*38fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 367*38fd1498Szrj // Constrained free swap overload, see p0185r1 368*38fd1498Szrj typename enable_if<__is_swappable<_Seq>::value>::type 369*38fd1498Szrj #else 370*38fd1498Szrj void 371*38fd1498Szrj #endif 372*38fd1498Szrj swap(queue<_Tp, _Seq>& __x, queue<_Tp, _Seq>& __y) 373*38fd1498Szrj noexcept(noexcept(__x.swap(__y))) 374*38fd1498Szrj { __x.swap(__y); } 375*38fd1498Szrj 376*38fd1498Szrj template<typename _Tp, typename _Seq, typename _Alloc> 377*38fd1498Szrj struct uses_allocator<queue<_Tp, _Seq>, _Alloc> 378*38fd1498Szrj : public uses_allocator<_Seq, _Alloc>::type { }; 379*38fd1498Szrj #endif // __cplusplus >= 201103L 380*38fd1498Szrj 381*38fd1498Szrj /** 382*38fd1498Szrj * @brief A standard container automatically sorting its contents. 383*38fd1498Szrj * 384*38fd1498Szrj * @ingroup sequences 385*38fd1498Szrj * 386*38fd1498Szrj * @tparam _Tp Type of element. 387*38fd1498Szrj * @tparam _Sequence Type of underlying sequence, defaults to vector<_Tp>. 388*38fd1498Szrj * @tparam _Compare Comparison function object type, defaults to 389*38fd1498Szrj * less<_Sequence::value_type>. 390*38fd1498Szrj * 391*38fd1498Szrj * This is not a true container, but an @e adaptor. It holds 392*38fd1498Szrj * another container, and provides a wrapper interface to that 393*38fd1498Szrj * container. The wrapper is what enforces priority-based sorting 394*38fd1498Szrj * and %queue behavior. Very few of the standard container/sequence 395*38fd1498Szrj * interface requirements are met (e.g., iterators). 396*38fd1498Szrj * 397*38fd1498Szrj * The second template parameter defines the type of the underlying 398*38fd1498Szrj * sequence/container. It defaults to std::vector, but it can be 399*38fd1498Szrj * any type that supports @c front(), @c push_back, @c pop_back, 400*38fd1498Szrj * and random-access iterators, such as std::deque or an 401*38fd1498Szrj * appropriate user-defined type. 402*38fd1498Szrj * 403*38fd1498Szrj * The third template parameter supplies the means of making 404*38fd1498Szrj * priority comparisons. It defaults to @c less<value_type> but 405*38fd1498Szrj * can be anything defining a strict weak ordering. 406*38fd1498Szrj * 407*38fd1498Szrj * Members not found in @a normal containers are @c container_type, 408*38fd1498Szrj * which is a typedef for the second Sequence parameter, and @c 409*38fd1498Szrj * push, @c pop, and @c top, which are standard %queue operations. 410*38fd1498Szrj * 411*38fd1498Szrj * @note No equality/comparison operators are provided for 412*38fd1498Szrj * %priority_queue. 413*38fd1498Szrj * 414*38fd1498Szrj * @note Sorting of the elements takes place as they are added to, 415*38fd1498Szrj * and removed from, the %priority_queue using the 416*38fd1498Szrj * %priority_queue's member functions. If you access the elements 417*38fd1498Szrj * by other means, and change their data such that the sorting 418*38fd1498Szrj * order would be different, the %priority_queue will not re-sort 419*38fd1498Szrj * the elements for you. (How could it know to do so?) 420*38fd1498Szrj */ 421*38fd1498Szrj template<typename _Tp, typename _Sequence = vector<_Tp>, 422*38fd1498Szrj typename _Compare = less<typename _Sequence::value_type> > 423*38fd1498Szrj class priority_queue 424*38fd1498Szrj { 425*38fd1498Szrj #ifdef _GLIBCXX_CONCEPT_CHECKS 426*38fd1498Szrj // concept requirements 427*38fd1498Szrj typedef typename _Sequence::value_type _Sequence_value_type; 428*38fd1498Szrj # if __cplusplus < 201103L 429*38fd1498Szrj __glibcxx_class_requires(_Tp, _SGIAssignableConcept) 430*38fd1498Szrj # endif 431*38fd1498Szrj __glibcxx_class_requires(_Sequence, _SequenceConcept) 432*38fd1498Szrj __glibcxx_class_requires(_Sequence, _RandomAccessContainerConcept) 433*38fd1498Szrj __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 434*38fd1498Szrj __glibcxx_class_requires4(_Compare, bool, _Tp, _Tp, 435*38fd1498Szrj _BinaryFunctionConcept) 436*38fd1498Szrj #endif 437*38fd1498Szrj 438*38fd1498Szrj #if __cplusplus >= 201103L 439*38fd1498Szrj template<typename _Alloc> 440*38fd1498Szrj using _Uses = typename 441*38fd1498Szrj enable_if<uses_allocator<_Sequence, _Alloc>::value>::type; 442*38fd1498Szrj #endif 443*38fd1498Szrj 444*38fd1498Szrj public: 445*38fd1498Szrj typedef typename _Sequence::value_type value_type; 446*38fd1498Szrj typedef typename _Sequence::reference reference; 447*38fd1498Szrj typedef typename _Sequence::const_reference const_reference; 448*38fd1498Szrj typedef typename _Sequence::size_type size_type; 449*38fd1498Szrj typedef _Sequence container_type; 450*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 451*38fd1498Szrj // DR 2684. priority_queue lacking comparator typedef 452*38fd1498Szrj typedef _Compare value_compare; 453*38fd1498Szrj 454*38fd1498Szrj protected: 455*38fd1498Szrj // See queue::c for notes on these names. 456*38fd1498Szrj _Sequence c; 457*38fd1498Szrj _Compare comp; 458*38fd1498Szrj 459*38fd1498Szrj public: 460*38fd1498Szrj /** 461*38fd1498Szrj * @brief Default constructor creates no elements. 462*38fd1498Szrj */ 463*38fd1498Szrj #if __cplusplus < 201103L 464*38fd1498Szrj explicit 465*38fd1498Szrj priority_queue(const _Compare& __x = _Compare(), 466*38fd1498Szrj const _Sequence& __s = _Sequence()) 467*38fd1498Szrj : c(__s), comp(__x) 468*38fd1498Szrj { std::make_heap(c.begin(), c.end(), comp); } 469*38fd1498Szrj #else 470*38fd1498Szrj template<typename _Seq = _Sequence, typename _Requires = typename 471*38fd1498Szrj enable_if<__and_<is_default_constructible<_Compare>, 472*38fd1498Szrj is_default_constructible<_Seq>>::value>::type> 473*38fd1498Szrj priority_queue() 474*38fd1498Szrj : c(), comp() { } 475*38fd1498Szrj 476*38fd1498Szrj explicit 477*38fd1498Szrj priority_queue(const _Compare& __x, const _Sequence& __s) 478*38fd1498Szrj : c(__s), comp(__x) 479*38fd1498Szrj { std::make_heap(c.begin(), c.end(), comp); } 480*38fd1498Szrj 481*38fd1498Szrj explicit 482*38fd1498Szrj priority_queue(const _Compare& __x, _Sequence&& __s = _Sequence()) 483*38fd1498Szrj : c(std::move(__s)), comp(__x) 484*38fd1498Szrj { std::make_heap(c.begin(), c.end(), comp); } 485*38fd1498Szrj 486*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 487*38fd1498Szrj explicit 488*38fd1498Szrj priority_queue(const _Alloc& __a) 489*38fd1498Szrj : c(__a), comp() { } 490*38fd1498Szrj 491*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 492*38fd1498Szrj priority_queue(const _Compare& __x, const _Alloc& __a) 493*38fd1498Szrj : c(__a), comp(__x) { } 494*38fd1498Szrj 495*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 496*38fd1498Szrj priority_queue(const _Compare& __x, const _Sequence& __c, 497*38fd1498Szrj const _Alloc& __a) 498*38fd1498Szrj : c(__c, __a), comp(__x) { } 499*38fd1498Szrj 500*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 501*38fd1498Szrj priority_queue(const _Compare& __x, _Sequence&& __c, const _Alloc& __a) 502*38fd1498Szrj : c(std::move(__c), __a), comp(__x) { } 503*38fd1498Szrj 504*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 505*38fd1498Szrj priority_queue(const priority_queue& __q, const _Alloc& __a) 506*38fd1498Szrj : c(__q.c, __a), comp(__q.comp) { } 507*38fd1498Szrj 508*38fd1498Szrj template<typename _Alloc, typename _Requires = _Uses<_Alloc>> 509*38fd1498Szrj priority_queue(priority_queue&& __q, const _Alloc& __a) 510*38fd1498Szrj : c(std::move(__q.c), __a), comp(std::move(__q.comp)) { } 511*38fd1498Szrj #endif 512*38fd1498Szrj 513*38fd1498Szrj /** 514*38fd1498Szrj * @brief Builds a %queue from a range. 515*38fd1498Szrj * @param __first An input iterator. 516*38fd1498Szrj * @param __last An input iterator. 517*38fd1498Szrj * @param __x A comparison functor describing a strict weak ordering. 518*38fd1498Szrj * @param __s An initial sequence with which to start. 519*38fd1498Szrj * 520*38fd1498Szrj * Begins by copying @a __s, inserting a copy of the elements 521*38fd1498Szrj * from @a [first,last) into the copy of @a __s, then ordering 522*38fd1498Szrj * the copy according to @a __x. 523*38fd1498Szrj * 524*38fd1498Szrj * For more information on function objects, see the 525*38fd1498Szrj * documentation on @link functors functor base 526*38fd1498Szrj * classes@endlink. 527*38fd1498Szrj */ 528*38fd1498Szrj #if __cplusplus < 201103L 529*38fd1498Szrj template<typename _InputIterator> 530*38fd1498Szrj priority_queue(_InputIterator __first, _InputIterator __last, 531*38fd1498Szrj const _Compare& __x = _Compare(), 532*38fd1498Szrj const _Sequence& __s = _Sequence()) 533*38fd1498Szrj : c(__s), comp(__x) 534*38fd1498Szrj { 535*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last); 536*38fd1498Szrj c.insert(c.end(), __first, __last); 537*38fd1498Szrj std::make_heap(c.begin(), c.end(), comp); 538*38fd1498Szrj } 539*38fd1498Szrj #else 540*38fd1498Szrj template<typename _InputIterator> 541*38fd1498Szrj priority_queue(_InputIterator __first, _InputIterator __last, 542*38fd1498Szrj const _Compare& __x, 543*38fd1498Szrj const _Sequence& __s) 544*38fd1498Szrj : c(__s), comp(__x) 545*38fd1498Szrj { 546*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last); 547*38fd1498Szrj c.insert(c.end(), __first, __last); 548*38fd1498Szrj std::make_heap(c.begin(), c.end(), comp); 549*38fd1498Szrj } 550*38fd1498Szrj 551*38fd1498Szrj template<typename _InputIterator> 552*38fd1498Szrj priority_queue(_InputIterator __first, _InputIterator __last, 553*38fd1498Szrj const _Compare& __x = _Compare(), 554*38fd1498Szrj _Sequence&& __s = _Sequence()) 555*38fd1498Szrj : c(std::move(__s)), comp(__x) 556*38fd1498Szrj { 557*38fd1498Szrj __glibcxx_requires_valid_range(__first, __last); 558*38fd1498Szrj c.insert(c.end(), __first, __last); 559*38fd1498Szrj std::make_heap(c.begin(), c.end(), comp); 560*38fd1498Szrj } 561*38fd1498Szrj #endif 562*38fd1498Szrj 563*38fd1498Szrj /** 564*38fd1498Szrj * Returns true if the %queue is empty. 565*38fd1498Szrj */ 566*38fd1498Szrj bool 567*38fd1498Szrj empty() const 568*38fd1498Szrj { return c.empty(); } 569*38fd1498Szrj 570*38fd1498Szrj /** Returns the number of elements in the %queue. */ 571*38fd1498Szrj size_type 572*38fd1498Szrj size() const 573*38fd1498Szrj { return c.size(); } 574*38fd1498Szrj 575*38fd1498Szrj /** 576*38fd1498Szrj * Returns a read-only (constant) reference to the data at the first 577*38fd1498Szrj * element of the %queue. 578*38fd1498Szrj */ 579*38fd1498Szrj const_reference 580*38fd1498Szrj top() const 581*38fd1498Szrj { 582*38fd1498Szrj __glibcxx_requires_nonempty(); 583*38fd1498Szrj return c.front(); 584*38fd1498Szrj } 585*38fd1498Szrj 586*38fd1498Szrj /** 587*38fd1498Szrj * @brief Add data to the %queue. 588*38fd1498Szrj * @param __x Data to be added. 589*38fd1498Szrj * 590*38fd1498Szrj * This is a typical %queue operation. 591*38fd1498Szrj * The time complexity of the operation depends on the underlying 592*38fd1498Szrj * sequence. 593*38fd1498Szrj */ 594*38fd1498Szrj void 595*38fd1498Szrj push(const value_type& __x) 596*38fd1498Szrj { 597*38fd1498Szrj c.push_back(__x); 598*38fd1498Szrj std::push_heap(c.begin(), c.end(), comp); 599*38fd1498Szrj } 600*38fd1498Szrj 601*38fd1498Szrj #if __cplusplus >= 201103L 602*38fd1498Szrj void 603*38fd1498Szrj push(value_type&& __x) 604*38fd1498Szrj { 605*38fd1498Szrj c.push_back(std::move(__x)); 606*38fd1498Szrj std::push_heap(c.begin(), c.end(), comp); 607*38fd1498Szrj } 608*38fd1498Szrj 609*38fd1498Szrj template<typename... _Args> 610*38fd1498Szrj void 611*38fd1498Szrj emplace(_Args&&... __args) 612*38fd1498Szrj { 613*38fd1498Szrj c.emplace_back(std::forward<_Args>(__args)...); 614*38fd1498Szrj std::push_heap(c.begin(), c.end(), comp); 615*38fd1498Szrj } 616*38fd1498Szrj #endif 617*38fd1498Szrj 618*38fd1498Szrj /** 619*38fd1498Szrj * @brief Removes first element. 620*38fd1498Szrj * 621*38fd1498Szrj * This is a typical %queue operation. It shrinks the %queue 622*38fd1498Szrj * by one. The time complexity of the operation depends on the 623*38fd1498Szrj * underlying sequence. 624*38fd1498Szrj * 625*38fd1498Szrj * Note that no data is returned, and if the first element's 626*38fd1498Szrj * data is needed, it should be retrieved before pop() is 627*38fd1498Szrj * called. 628*38fd1498Szrj */ 629*38fd1498Szrj void 630*38fd1498Szrj pop() 631*38fd1498Szrj { 632*38fd1498Szrj __glibcxx_requires_nonempty(); 633*38fd1498Szrj std::pop_heap(c.begin(), c.end(), comp); 634*38fd1498Szrj c.pop_back(); 635*38fd1498Szrj } 636*38fd1498Szrj 637*38fd1498Szrj #if __cplusplus >= 201103L 638*38fd1498Szrj void 639*38fd1498Szrj swap(priority_queue& __pq) 640*38fd1498Szrj noexcept(__and_< 641*38fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 642*38fd1498Szrj __is_nothrow_swappable<_Sequence>, 643*38fd1498Szrj #else 644*38fd1498Szrj __is_nothrow_swappable<_Tp>, 645*38fd1498Szrj #endif 646*38fd1498Szrj __is_nothrow_swappable<_Compare> 647*38fd1498Szrj >::value) 648*38fd1498Szrj { 649*38fd1498Szrj using std::swap; 650*38fd1498Szrj swap(c, __pq.c); 651*38fd1498Szrj swap(comp, __pq.comp); 652*38fd1498Szrj } 653*38fd1498Szrj #endif // __cplusplus >= 201103L 654*38fd1498Szrj }; 655*38fd1498Szrj 656*38fd1498Szrj // No equality/comparison operators are provided for priority_queue. 657*38fd1498Szrj 658*38fd1498Szrj #if __cplusplus >= 201103L 659*38fd1498Szrj template<typename _Tp, typename _Sequence, typename _Compare> 660*38fd1498Szrj inline 661*38fd1498Szrj #if __cplusplus > 201402L || !defined(__STRICT_ANSI__) // c++1z or gnu++11 662*38fd1498Szrj // Constrained free swap overload, see p0185r1 663*38fd1498Szrj typename enable_if<__and_<__is_swappable<_Sequence>, 664*38fd1498Szrj __is_swappable<_Compare>>::value>::type 665*38fd1498Szrj #else 666*38fd1498Szrj void 667*38fd1498Szrj #endif 668*38fd1498Szrj swap(priority_queue<_Tp, _Sequence, _Compare>& __x, 669*38fd1498Szrj priority_queue<_Tp, _Sequence, _Compare>& __y) 670*38fd1498Szrj noexcept(noexcept(__x.swap(__y))) 671*38fd1498Szrj { __x.swap(__y); } 672*38fd1498Szrj 673*38fd1498Szrj template<typename _Tp, typename _Sequence, typename _Compare, 674*38fd1498Szrj typename _Alloc> 675*38fd1498Szrj struct uses_allocator<priority_queue<_Tp, _Sequence, _Compare>, _Alloc> 676*38fd1498Szrj : public uses_allocator<_Sequence, _Alloc>::type { }; 677*38fd1498Szrj #endif // __cplusplus >= 201103L 678*38fd1498Szrj 679*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 680*38fd1498Szrj } // namespace 681*38fd1498Szrj 682*38fd1498Szrj #endif /* _STL_QUEUE_H */ 683