1*03a78d15Sespie // Queue implementation -*- C++ -*- 2*03a78d15Sespie 3*03a78d15Sespie // Copyright (C) 2001, 2002 Free Software Foundation, Inc. 4*03a78d15Sespie // 5*03a78d15Sespie // This file is part of the GNU ISO C++ Library. This library is free 6*03a78d15Sespie // software; you can redistribute it and/or modify it under the 7*03a78d15Sespie // terms of the GNU General Public License as published by the 8*03a78d15Sespie // Free Software Foundation; either version 2, or (at your option) 9*03a78d15Sespie // any later version. 10*03a78d15Sespie 11*03a78d15Sespie // This library is distributed in the hope that it will be useful, 12*03a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*03a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*03a78d15Sespie // GNU General Public License for more details. 15*03a78d15Sespie 16*03a78d15Sespie // You should have received a copy of the GNU General Public License along 17*03a78d15Sespie // with this library; see the file COPYING. If not, write to the Free 18*03a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 19*03a78d15Sespie // USA. 20*03a78d15Sespie 21*03a78d15Sespie // As a special exception, you may use this file as part of a free software 22*03a78d15Sespie // library without restriction. Specifically, if other files instantiate 23*03a78d15Sespie // templates or use macros or inline functions from this file, or you compile 24*03a78d15Sespie // this file and link it with other files to produce an executable, this 25*03a78d15Sespie // file does not by itself cause the resulting executable to be covered by 26*03a78d15Sespie // the GNU General Public License. This exception does not however 27*03a78d15Sespie // invalidate any other reasons why the executable file might be covered by 28*03a78d15Sespie // the GNU General Public License. 29*03a78d15Sespie 30*03a78d15Sespie /* 31*03a78d15Sespie * 32*03a78d15Sespie * Copyright (c) 1994 33*03a78d15Sespie * Hewlett-Packard Company 34*03a78d15Sespie * 35*03a78d15Sespie * Permission to use, copy, modify, distribute and sell this software 36*03a78d15Sespie * and its documentation for any purpose is hereby granted without fee, 37*03a78d15Sespie * provided that the above copyright notice appear in all copies and 38*03a78d15Sespie * that both that copyright notice and this permission notice appear 39*03a78d15Sespie * in supporting documentation. Hewlett-Packard Company makes no 40*03a78d15Sespie * representations about the suitability of this software for any 41*03a78d15Sespie * purpose. It is provided "as is" without express or implied warranty. 42*03a78d15Sespie * 43*03a78d15Sespie * 44*03a78d15Sespie * Copyright (c) 1996,1997 45*03a78d15Sespie * Silicon Graphics Computer Systems, Inc. 46*03a78d15Sespie * 47*03a78d15Sespie * Permission to use, copy, modify, distribute and sell this software 48*03a78d15Sespie * and its documentation for any purpose is hereby granted without fee, 49*03a78d15Sespie * provided that the above copyright notice appear in all copies and 50*03a78d15Sespie * that both that copyright notice and this permission notice appear 51*03a78d15Sespie * in supporting documentation. Silicon Graphics makes no 52*03a78d15Sespie * representations about the suitability of this software for any 53*03a78d15Sespie * purpose. It is provided "as is" without express or implied warranty. 54*03a78d15Sespie */ 55*03a78d15Sespie 56*03a78d15Sespie /** @file stl_queue.h 57*03a78d15Sespie * This is an internal header file, included by other library headers. 58*03a78d15Sespie * You should not attempt to use it directly. 59*03a78d15Sespie */ 60*03a78d15Sespie 61*03a78d15Sespie #ifndef __GLIBCPP_INTERNAL_QUEUE_H 62*03a78d15Sespie #define __GLIBCPP_INTERNAL_QUEUE_H 63*03a78d15Sespie 64*03a78d15Sespie #include <bits/concept_check.h> 65*03a78d15Sespie 66*03a78d15Sespie namespace std 67*03a78d15Sespie { 68*03a78d15Sespie // Forward declarations of operators < and ==, needed for friend declaration. 69*03a78d15Sespie 70*03a78d15Sespie template <typename _Tp, typename _Sequence = deque<_Tp> > 71*03a78d15Sespie class queue; 72*03a78d15Sespie 73*03a78d15Sespie template <typename _Tp, typename _Seq> 74*03a78d15Sespie inline bool operator==(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&); 75*03a78d15Sespie 76*03a78d15Sespie template <typename _Tp, typename _Seq> 77*03a78d15Sespie inline bool operator<(const queue<_Tp,_Seq>&, const queue<_Tp,_Seq>&); 78*03a78d15Sespie 79*03a78d15Sespie 80*03a78d15Sespie /** 81*03a78d15Sespie * @brief A standard container giving FIFO behavior. 82*03a78d15Sespie * 83*03a78d15Sespie * @ingroup Containers 84*03a78d15Sespie * @ingroup Sequences 85*03a78d15Sespie * 86*03a78d15Sespie * Meets many of the requirements of a 87*03a78d15Sespie * <a href="tables.html#65">container</a>, 88*03a78d15Sespie * but does not define anything to do with iterators. Very few of the 89*03a78d15Sespie * other standard container interfaces are defined. 90*03a78d15Sespie * 91*03a78d15Sespie * This is not a true container, but an @e adaptor. It holds another 92*03a78d15Sespie * container, and provides a wrapper interface to that container. The 93*03a78d15Sespie * wrapper is what enforces strict first-in-first-out %queue behavior. 94*03a78d15Sespie * 95*03a78d15Sespie * The second template parameter defines the type of the underlying 96*03a78d15Sespie * sequence/container. It defaults to std::deque, but it can be any type 97*03a78d15Sespie * that supports @c front, @c back, @c push_back, and @c pop_front, 98*03a78d15Sespie * such as std::list or an appropriate user-defined type. 99*03a78d15Sespie * 100*03a78d15Sespie * Members not found in "normal" containers are @c container_type, 101*03a78d15Sespie * which is a typedef for the second Sequence parameter, and @c push and 102*03a78d15Sespie * @c pop, which are standard %queue/FIFO operations. 103*03a78d15Sespie */ 104*03a78d15Sespie template <typename _Tp, typename _Sequence> 105*03a78d15Sespie class queue 106*03a78d15Sespie { 107*03a78d15Sespie // concept requirements 108*03a78d15Sespie typedef typename _Sequence::value_type _Sequence_value_type; 109*03a78d15Sespie __glibcpp_class_requires(_Tp, _SGIAssignableConcept) 110*03a78d15Sespie __glibcpp_class_requires(_Sequence, _FrontInsertionSequenceConcept) 111*03a78d15Sespie __glibcpp_class_requires(_Sequence, _BackInsertionSequenceConcept) 112*03a78d15Sespie __glibcpp_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 113*03a78d15Sespie 114*03a78d15Sespie template <typename _Tp1, typename _Seq1> 115*03a78d15Sespie friend bool operator== (const queue<_Tp1, _Seq1>&, 116*03a78d15Sespie const queue<_Tp1, _Seq1>&); 117*03a78d15Sespie template <typename _Tp1, typename _Seq1> 118*03a78d15Sespie friend bool operator< (const queue<_Tp1, _Seq1>&, 119*03a78d15Sespie const queue<_Tp1, _Seq1>&); 120*03a78d15Sespie 121*03a78d15Sespie public: 122*03a78d15Sespie typedef typename _Sequence::value_type value_type; 123*03a78d15Sespie typedef typename _Sequence::reference reference; 124*03a78d15Sespie typedef typename _Sequence::const_reference const_reference; 125*03a78d15Sespie typedef typename _Sequence::size_type size_type; 126*03a78d15Sespie typedef _Sequence container_type; 127*03a78d15Sespie 128*03a78d15Sespie protected: 129*03a78d15Sespie /** 130*03a78d15Sespie * 'c' is the underlying container. Maintainers wondering why this isn't 131*03a78d15Sespie * uglified as per style guidelines should note that this name is 132*03a78d15Sespie * specified in the standard, [23.2.3.1]. (Why? Presumably for the same 133*03a78d15Sespie * reason that it's protected instead of private: to allow derivation. 134*03a78d15Sespie * But none of the other containers allow for derivation. Odd.) 135*03a78d15Sespie */ 136*03a78d15Sespie _Sequence c; 137*03a78d15Sespie 138*03a78d15Sespie public: 139*03a78d15Sespie /** 140*03a78d15Sespie * @brief Default constructor creates no elements. 141*03a78d15Sespie */ 142*03a78d15Sespie explicit 143*03a78d15Sespie queue(const _Sequence& __c = _Sequence()) c(__c)144*03a78d15Sespie : c(__c) {} 145*03a78d15Sespie 146*03a78d15Sespie /** 147*03a78d15Sespie * Returns true if the %queue is empty. 148*03a78d15Sespie */ 149*03a78d15Sespie bool empty()150*03a78d15Sespie empty() const { return c.empty(); } 151*03a78d15Sespie 152*03a78d15Sespie /** Returns the number of elements in the %queue. */ 153*03a78d15Sespie size_type size()154*03a78d15Sespie size() const { return c.size(); } 155*03a78d15Sespie 156*03a78d15Sespie /** 157*03a78d15Sespie * Returns a read/write reference to the data at the first element of the 158*03a78d15Sespie * %queue. 159*03a78d15Sespie */ 160*03a78d15Sespie reference front()161*03a78d15Sespie front() { return c.front(); } 162*03a78d15Sespie 163*03a78d15Sespie /** 164*03a78d15Sespie * Returns a read-only (constant) reference to the data at the first 165*03a78d15Sespie * element of the %queue. 166*03a78d15Sespie */ 167*03a78d15Sespie const_reference front()168*03a78d15Sespie front() const { return c.front(); } 169*03a78d15Sespie 170*03a78d15Sespie /** 171*03a78d15Sespie * Returns a read/write reference to the data at the last element of the 172*03a78d15Sespie * %queue. 173*03a78d15Sespie */ 174*03a78d15Sespie reference back()175*03a78d15Sespie back() { return c.back(); } 176*03a78d15Sespie 177*03a78d15Sespie /** 178*03a78d15Sespie * Returns a read-only (constant) reference to the data at the last 179*03a78d15Sespie * element of the %queue. 180*03a78d15Sespie */ 181*03a78d15Sespie const_reference back()182*03a78d15Sespie back() const { return c.back(); } 183*03a78d15Sespie 184*03a78d15Sespie /** 185*03a78d15Sespie * @brief Add data to the end of the %queue. 186*03a78d15Sespie * @param x Data to be added. 187*03a78d15Sespie * 188*03a78d15Sespie * This is a typical %queue operation. The function creates an element at 189*03a78d15Sespie * the end of the %queue and assigns the given data to it. 190*03a78d15Sespie * The time complexity of the operation depends on the underlying 191*03a78d15Sespie * sequence. 192*03a78d15Sespie */ 193*03a78d15Sespie void push(const value_type & __x)194*03a78d15Sespie push(const value_type& __x) { c.push_back(__x); } 195*03a78d15Sespie 196*03a78d15Sespie /** 197*03a78d15Sespie * @brief Removes first element. 198*03a78d15Sespie * 199*03a78d15Sespie * This is a typical %queue operation. It shrinks the %queue by one. 200*03a78d15Sespie * The time complexity of the operation depends on the underlying 201*03a78d15Sespie * sequence. 202*03a78d15Sespie * 203*03a78d15Sespie * Note that no data is returned, and if the first element's data is 204*03a78d15Sespie * needed, it should be retrieved before pop() is called. 205*03a78d15Sespie */ 206*03a78d15Sespie void pop()207*03a78d15Sespie pop() { c.pop_front(); } 208*03a78d15Sespie }; 209*03a78d15Sespie 210*03a78d15Sespie 211*03a78d15Sespie /** 212*03a78d15Sespie * @brief Queue equality comparison. 213*03a78d15Sespie * @param x A %queue. 214*03a78d15Sespie * @param y A %queue of the same type as @a x. 215*03a78d15Sespie * @return True iff the size and elements of the queues are equal. 216*03a78d15Sespie * 217*03a78d15Sespie * This is an equivalence relation. Complexity and semantics depend on the 218*03a78d15Sespie * underlying sequence type, but the expected rules are: this relation is 219*03a78d15Sespie * linear in the size of the sequences, and queues are considered equivalent 220*03a78d15Sespie * if their sequences compare equal. 221*03a78d15Sespie */ 222*03a78d15Sespie template <typename _Tp, typename _Sequence> 223*03a78d15Sespie inline bool 224*03a78d15Sespie operator==(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 225*03a78d15Sespie { return __x.c == __y.c; } 226*03a78d15Sespie 227*03a78d15Sespie /** 228*03a78d15Sespie * @brief Queue ordering relation. 229*03a78d15Sespie * @param x A %queue. 230*03a78d15Sespie * @param y A %queue of the same type as @a x. 231*03a78d15Sespie * @return True iff @a x is lexographically less than @a y. 232*03a78d15Sespie * 233*03a78d15Sespie * This is an total ordering relation. Complexity and semantics depend on 234*03a78d15Sespie * the underlying sequence type, but the expected rules are: this relation 235*03a78d15Sespie * is linear in the size of the sequences, the elements must be comparable 236*03a78d15Sespie * with @c <, and std::lexographical_compare() is usually used to make the 237*03a78d15Sespie * determination. 238*03a78d15Sespie */ 239*03a78d15Sespie template <typename _Tp, typename _Sequence> 240*03a78d15Sespie inline bool 241*03a78d15Sespie operator<(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 242*03a78d15Sespie { return __x.c < __y.c; } 243*03a78d15Sespie 244*03a78d15Sespie /// Based on operator== 245*03a78d15Sespie template <typename _Tp, typename _Sequence> 246*03a78d15Sespie inline bool 247*03a78d15Sespie operator!=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 248*03a78d15Sespie { return !(__x == __y); } 249*03a78d15Sespie 250*03a78d15Sespie /// Based on operator< 251*03a78d15Sespie template <typename _Tp, typename _Sequence> 252*03a78d15Sespie inline bool 253*03a78d15Sespie operator>(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 254*03a78d15Sespie { return __y < __x; } 255*03a78d15Sespie 256*03a78d15Sespie /// Based on operator< 257*03a78d15Sespie template <typename _Tp, typename _Sequence> 258*03a78d15Sespie inline bool 259*03a78d15Sespie operator<=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 260*03a78d15Sespie { return !(__y < __x); } 261*03a78d15Sespie 262*03a78d15Sespie /// Based on operator< 263*03a78d15Sespie template <typename _Tp, typename _Sequence> 264*03a78d15Sespie inline bool 265*03a78d15Sespie operator>=(const queue<_Tp,_Sequence>& __x, const queue<_Tp,_Sequence>& __y) 266*03a78d15Sespie { return !(__x < __y); } 267*03a78d15Sespie 268*03a78d15Sespie 269*03a78d15Sespie /** 270*03a78d15Sespie * @brief A standard container automatically sorting its contents. 271*03a78d15Sespie * 272*03a78d15Sespie * @ingroup Containers 273*03a78d15Sespie * @ingroup Sequences 274*03a78d15Sespie * 275*03a78d15Sespie * This is not a true container, but an @e adaptor. It holds another 276*03a78d15Sespie * container, and provides a wrapper interface to that container. The 277*03a78d15Sespie * wrapper is what enforces sorting and first-in-first-out %queue behavior. 278*03a78d15Sespie * Very few of the standard container/sequence interface requirements are 279*03a78d15Sespie * met (e.g., iterators). 280*03a78d15Sespie * 281*03a78d15Sespie * The second template parameter defines the type of the underlying 282*03a78d15Sespie * sequence/container. It defaults to std::vector, but it can be any type 283*03a78d15Sespie * that supports @c front(), @c push_back, @c pop_back, and random-access 284*03a78d15Sespie * iterators, such as std::deque or an appropriate user-defined type. 285*03a78d15Sespie * 286*03a78d15Sespie * The third template parameter supplies the means of making priority 287*03a78d15Sespie * comparisons. It defaults to @c less<value_type> but can be anything 288*03a78d15Sespie * defining a strict weak ordering. 289*03a78d15Sespie * 290*03a78d15Sespie * Members not found in "normal" containers are @c container_type, 291*03a78d15Sespie * which is a typedef for the second Sequence parameter, and @c push, 292*03a78d15Sespie * @c pop, and @c top, which are standard %queue/FIFO operations. 293*03a78d15Sespie * 294*03a78d15Sespie * @note No equality/comparison operators are provided for %priority_queue. 295*03a78d15Sespie * 296*03a78d15Sespie * @note Sorting of the elements takes place as they are added to, and 297*03a78d15Sespie * removed from, the %priority_queue using the %priority_queue's 298*03a78d15Sespie * member functions. If you access the elements by other means, and 299*03a78d15Sespie * change their data such that the sorting order would be different, 300*03a78d15Sespie * the %priority_queue will not re-sort the elements for you. (How 301*03a78d15Sespie * could it know to do so?) 302*03a78d15Sespie */ 303*03a78d15Sespie template <typename _Tp, typename _Sequence = vector<_Tp>, 304*03a78d15Sespie typename _Compare = less<typename _Sequence::value_type> > 305*03a78d15Sespie class priority_queue 306*03a78d15Sespie { 307*03a78d15Sespie // concept requirements 308*03a78d15Sespie typedef typename _Sequence::value_type _Sequence_value_type; 309*03a78d15Sespie __glibcpp_class_requires(_Tp, _SGIAssignableConcept) 310*03a78d15Sespie __glibcpp_class_requires(_Sequence, _SequenceConcept) 311*03a78d15Sespie __glibcpp_class_requires(_Sequence, _RandomAccessContainerConcept) 312*03a78d15Sespie __glibcpp_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept) 313*03a78d15Sespie __glibcpp_class_requires4(_Compare, bool, _Tp, _Tp, _BinaryFunctionConcept) 314*03a78d15Sespie 315*03a78d15Sespie public: 316*03a78d15Sespie typedef typename _Sequence::value_type value_type; 317*03a78d15Sespie typedef typename _Sequence::reference reference; 318*03a78d15Sespie typedef typename _Sequence::const_reference const_reference; 319*03a78d15Sespie typedef typename _Sequence::size_type size_type; 320*03a78d15Sespie typedef _Sequence container_type; 321*03a78d15Sespie 322*03a78d15Sespie protected: 323*03a78d15Sespie // See queue::c for notes on these names. 324*03a78d15Sespie _Sequence c; 325*03a78d15Sespie _Compare comp; 326*03a78d15Sespie 327*03a78d15Sespie public: 328*03a78d15Sespie /** 329*03a78d15Sespie * @brief Default constructor creates no elements. 330*03a78d15Sespie */ 331*03a78d15Sespie explicit 332*03a78d15Sespie priority_queue(const _Compare& __x = _Compare(), 333*03a78d15Sespie const _Sequence& __s = _Sequence()) c(__s)334*03a78d15Sespie : c(__s), comp(__x) 335*03a78d15Sespie { make_heap(c.begin(), c.end(), comp); } 336*03a78d15Sespie 337*03a78d15Sespie /** 338*03a78d15Sespie * @brief Builds a %queue from a range. 339*03a78d15Sespie * @param first An input iterator. 340*03a78d15Sespie * @param last An input iterator. 341*03a78d15Sespie * @param x A comparison functor describing a strict weak ordering. 342*03a78d15Sespie * @param s An initial sequence with which to start. 343*03a78d15Sespie * 344*03a78d15Sespie * Begins by copying @a s, inserting a copy of the elements from 345*03a78d15Sespie * @a [first,last) into the copy of @a s, then ordering the copy 346*03a78d15Sespie * according to @a x. 347*03a78d15Sespie * 348*03a78d15Sespie * For more information on function objects, see the documentation on 349*03a78d15Sespie * @link s20_3_1_base functor base classes@endlink. 350*03a78d15Sespie */ 351*03a78d15Sespie template <typename _InputIterator> 352*03a78d15Sespie priority_queue(_InputIterator __first, _InputIterator __last, 353*03a78d15Sespie const _Compare& __x = _Compare(), 354*03a78d15Sespie const _Sequence& __s = _Sequence()) c(__s)355*03a78d15Sespie : c(__s), comp(__x) 356*03a78d15Sespie { 357*03a78d15Sespie c.insert(c.end(), __first, __last); 358*03a78d15Sespie make_heap(c.begin(), c.end(), comp); 359*03a78d15Sespie } 360*03a78d15Sespie 361*03a78d15Sespie /** 362*03a78d15Sespie * Returns true if the %queue is empty. 363*03a78d15Sespie */ 364*03a78d15Sespie bool empty()365*03a78d15Sespie empty() const { return c.empty(); } 366*03a78d15Sespie 367*03a78d15Sespie /** Returns the number of elements in the %queue. */ 368*03a78d15Sespie size_type size()369*03a78d15Sespie size() const { return c.size(); } 370*03a78d15Sespie 371*03a78d15Sespie /** 372*03a78d15Sespie * Returns a read-only (constant) reference to the data at the first 373*03a78d15Sespie * element of the %queue. 374*03a78d15Sespie */ 375*03a78d15Sespie const_reference top()376*03a78d15Sespie top() const { return c.front(); } 377*03a78d15Sespie 378*03a78d15Sespie /** 379*03a78d15Sespie * @brief Add data to the %queue. 380*03a78d15Sespie * @param x Data to be added. 381*03a78d15Sespie * 382*03a78d15Sespie * This is a typical %queue operation. 383*03a78d15Sespie * The time complexity of the operation depends on the underlying 384*03a78d15Sespie * sequence. 385*03a78d15Sespie */ 386*03a78d15Sespie void push(const value_type & __x)387*03a78d15Sespie push(const value_type& __x) 388*03a78d15Sespie { 389*03a78d15Sespie try 390*03a78d15Sespie { 391*03a78d15Sespie c.push_back(__x); 392*03a78d15Sespie push_heap(c.begin(), c.end(), comp); 393*03a78d15Sespie } 394*03a78d15Sespie catch(...) 395*03a78d15Sespie { 396*03a78d15Sespie c.clear(); 397*03a78d15Sespie __throw_exception_again; 398*03a78d15Sespie } 399*03a78d15Sespie } 400*03a78d15Sespie 401*03a78d15Sespie /** 402*03a78d15Sespie * @brief Removes first element. 403*03a78d15Sespie * 404*03a78d15Sespie * This is a typical %queue operation. It shrinks the %queue by one. 405*03a78d15Sespie * The time complexity of the operation depends on the underlying 406*03a78d15Sespie * sequence. 407*03a78d15Sespie * 408*03a78d15Sespie * Note that no data is returned, and if the first element's data is 409*03a78d15Sespie * needed, it should be retrieved before pop() is called. 410*03a78d15Sespie */ 411*03a78d15Sespie void pop()412*03a78d15Sespie pop() 413*03a78d15Sespie { 414*03a78d15Sespie try 415*03a78d15Sespie { 416*03a78d15Sespie pop_heap(c.begin(), c.end(), comp); 417*03a78d15Sespie c.pop_back(); 418*03a78d15Sespie } 419*03a78d15Sespie catch(...) 420*03a78d15Sespie { 421*03a78d15Sespie c.clear(); 422*03a78d15Sespie __throw_exception_again; 423*03a78d15Sespie } 424*03a78d15Sespie } 425*03a78d15Sespie }; 426*03a78d15Sespie 427*03a78d15Sespie // No equality/comparison operators are provided for priority_queue. 428*03a78d15Sespie } // namespace std 429*03a78d15Sespie 430*03a78d15Sespie #endif /* __GLIBCPP_INTERNAL_QUEUE_H */ 431