libstdc++
stl_stack.h
Go to the documentation of this file.
00001 // Stack implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /*
00026  *
00027  * Copyright (c) 1994
00028  * Hewlett-Packard Company
00029  *
00030  * Permission to use, copy, modify, distribute and sell this software
00031  * and its documentation for any purpose is hereby granted without fee,
00032  * provided that the above copyright notice appear in all copies and
00033  * that both that copyright notice and this permission notice appear
00034  * in supporting documentation.  Hewlett-Packard Company makes no
00035  * representations about the suitability of this software for any
00036  * purpose.  It is provided "as is" without express or implied warranty.
00037  *
00038  *
00039  * Copyright (c) 1996,1997
00040  * Silicon Graphics Computer Systems, Inc.
00041  *
00042  * Permission to use, copy, modify, distribute and sell this software
00043  * and its documentation for any purpose is hereby granted without fee,
00044  * provided that the above copyright notice appear in all copies and
00045  * that both that copyright notice and this permission notice appear
00046  * in supporting documentation.  Silicon Graphics makes no
00047  * representations about the suitability of this software for any
00048  * purpose.  It is provided "as is" without express or implied warranty.
00049  */
00050 
00051 /** @file bits/stl_stack.h
00052  *  This is an internal header file, included by other library headers.
00053  *  Do not attempt to use it directly. @headername{stack}
00054  */
00055 
00056 #ifndef _STL_STACK_H
00057 #define _STL_STACK_H 1
00058 
00059 #include <bits/concept_check.h>
00060 #include <debug/debug.h>
00061 #if __cplusplus >= 201103L
00062 # include <bits/uses_allocator.h>
00063 #endif
00064 
00065 namespace std _GLIBCXX_VISIBILITY(default)
00066 {
00067 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00068 
00069   /**
00070    *  @brief  A standard container giving FILO behavior.
00071    *
00072    *  @ingroup sequences
00073    *
00074    *  @tparam _Tp  Type of element.
00075    *  @tparam _Sequence  Type of underlying sequence, defaults to deque<_Tp>.
00076    *
00077    *  Meets many of the requirements of a
00078    *  <a href="tables.html#65">container</a>,
00079    *  but does not define anything to do with iterators.  Very few of the
00080    *  other standard container interfaces are defined.
00081    *
00082    *  This is not a true container, but an @e adaptor.  It holds
00083    *  another container, and provides a wrapper interface to that
00084    *  container.  The wrapper is what enforces strict
00085    *  first-in-last-out %stack behavior.
00086    *
00087    *  The second template parameter defines the type of the underlying
00088    *  sequence/container.  It defaults to std::deque, but it can be
00089    *  any type that supports @c back, @c push_back, and @c pop_front,
00090    *  such as std::list, std::vector, or an appropriate user-defined
00091    *  type.
00092    *
00093    *  Members not found in @a normal containers are @c container_type,
00094    *  which is a typedef for the second Sequence parameter, and @c
00095    *  push, @c pop, and @c top, which are standard %stack/FILO
00096    *  operations.
00097   */
00098   template<typename _Tp, typename _Sequence = deque<_Tp> >
00099     class stack
00100     {
00101       // concept requirements
00102       typedef typename _Sequence::value_type _Sequence_value_type;
00103       __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
00104       __glibcxx_class_requires(_Sequence, _BackInsertionSequenceConcept)
00105       __glibcxx_class_requires2(_Tp, _Sequence_value_type, _SameTypeConcept)
00106 
00107       template<typename _Tp1, typename _Seq1>
00108         friend bool
00109         operator==(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00110 
00111       template<typename _Tp1, typename _Seq1>
00112         friend bool
00113         operator<(const stack<_Tp1, _Seq1>&, const stack<_Tp1, _Seq1>&);
00114 
00115     public:
00116       typedef typename _Sequence::value_type                value_type;
00117       typedef typename _Sequence::reference                 reference;
00118       typedef typename _Sequence::const_reference           const_reference;
00119       typedef typename _Sequence::size_type                 size_type;
00120       typedef          _Sequence                            container_type;
00121 
00122     protected:
00123       //  See queue::c for notes on this name.
00124       _Sequence c;
00125 
00126     public:
00127       // XXX removed old def ctor, added def arg to this one to match 14882
00128       /**
00129        *  @brief  Default constructor creates no elements.
00130        */
00131 #if __cplusplus < 201103L
00132       explicit
00133       stack(const _Sequence& __c = _Sequence())
00134       : c(__c) { }
00135 #else
00136       explicit
00137       stack(const _Sequence& __c)
00138       : c(__c) { }
00139 
00140       explicit
00141       stack(_Sequence&& __c = _Sequence())
00142       : c(std::move(__c)) { }
00143 #endif
00144 
00145       /**
00146        *  Returns true if the %stack is empty.
00147        */
00148       bool
00149       empty() const
00150       { return c.empty(); }
00151 
00152       /**  Returns the number of elements in the %stack.  */
00153       size_type
00154       size() const
00155       { return c.size(); }
00156 
00157       /**
00158        *  Returns a read/write reference to the data at the first
00159        *  element of the %stack.
00160        */
00161       reference
00162       top()
00163       {
00164         __glibcxx_requires_nonempty();
00165         return c.back();
00166       }
00167 
00168       /**
00169        *  Returns a read-only (constant) reference to the data at the first
00170        *  element of the %stack.
00171        */
00172       const_reference
00173       top() const
00174       {
00175         __glibcxx_requires_nonempty();
00176         return c.back();
00177       }
00178 
00179       /**
00180        *  @brief  Add data to the top of the %stack.
00181        *  @param  __x  Data to be added.
00182        *
00183        *  This is a typical %stack operation.  The function creates an
00184        *  element at the top of the %stack and assigns the given data
00185        *  to it.  The time complexity of the operation depends on the
00186        *  underlying sequence.
00187        */
00188       void
00189       push(const value_type& __x)
00190       { c.push_back(__x); }
00191 
00192 #if __cplusplus >= 201103L
00193       void
00194       push(value_type&& __x)
00195       { c.push_back(std::move(__x)); }
00196 
00197       template<typename... _Args>
00198         void
00199         emplace(_Args&&... __args)
00200         { c.emplace_back(std::forward<_Args>(__args)...); }
00201 #endif
00202 
00203       /**
00204        *  @brief  Removes first element.
00205        *
00206        *  This is a typical %stack operation.  It shrinks the %stack
00207        *  by one.  The time complexity of the operation depends on the
00208        *  underlying sequence.
00209        *
00210        *  Note that no data is returned, and if the first element's
00211        *  data is needed, it should be retrieved before pop() is
00212        *  called.
00213        */
00214       void
00215       pop()
00216       {
00217         __glibcxx_requires_nonempty();
00218         c.pop_back();
00219       }
00220 
00221 #if __cplusplus >= 201103L
00222       void
00223       swap(stack& __s)
00224       noexcept(noexcept(swap(c, __s.c)))
00225       {
00226         using std::swap;
00227         swap(c, __s.c);
00228       }
00229 #endif
00230     };
00231 
00232   /**
00233    *  @brief  Stack equality comparison.
00234    *  @param  __x  A %stack.
00235    *  @param  __y  A %stack of the same type as @a __x.
00236    *  @return  True iff the size and elements of the stacks are equal.
00237    *
00238    *  This is an equivalence relation.  Complexity and semantics
00239    *  depend on the underlying sequence type, but the expected rules
00240    *  are: this relation is linear in the size of the sequences, and
00241    *  stacks are considered equivalent if their sequences compare
00242    *  equal.
00243   */
00244   template<typename _Tp, typename _Seq>
00245     inline bool
00246     operator==(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00247     { return __x.c == __y.c; }
00248 
00249   /**
00250    *  @brief  Stack ordering relation.
00251    *  @param  __x  A %stack.
00252    *  @param  __y  A %stack of the same type as @a x.
00253    *  @return  True iff @a x is lexicographically less than @a __y.
00254    *
00255    *  This is an total ordering relation.  Complexity and semantics
00256    *  depend on the underlying sequence type, but the expected rules
00257    *  are: this relation is linear in the size of the sequences, the
00258    *  elements must be comparable with @c <, and
00259    *  std::lexicographical_compare() is usually used to make the
00260    *  determination.
00261   */
00262   template<typename _Tp, typename _Seq>
00263     inline bool
00264     operator<(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00265     { return __x.c < __y.c; }
00266 
00267   /// Based on operator==
00268   template<typename _Tp, typename _Seq>
00269     inline bool
00270     operator!=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00271     { return !(__x == __y); }
00272 
00273   /// Based on operator<
00274   template<typename _Tp, typename _Seq>
00275     inline bool
00276     operator>(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00277     { return __y < __x; }
00278 
00279   /// Based on operator<
00280   template<typename _Tp, typename _Seq>
00281     inline bool
00282     operator<=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00283     { return !(__y < __x); }
00284 
00285   /// Based on operator<
00286   template<typename _Tp, typename _Seq>
00287     inline bool
00288     operator>=(const stack<_Tp, _Seq>& __x, const stack<_Tp, _Seq>& __y)
00289     { return !(__x < __y); }
00290 
00291 #if __cplusplus >= 201103L
00292   template<typename _Tp, typename _Seq>
00293     inline void
00294     swap(stack<_Tp, _Seq>& __x, stack<_Tp, _Seq>& __y)
00295     noexcept(noexcept(__x.swap(__y)))
00296     { __x.swap(__y); }
00297 
00298   template<typename _Tp, typename _Seq, typename _Alloc>
00299     struct uses_allocator<stack<_Tp, _Seq>, _Alloc>
00300     : public uses_allocator<_Seq, _Alloc>::type { };
00301 #endif
00302 
00303 _GLIBCXX_END_NAMESPACE_VERSION
00304 } // namespace
00305 
00306 #endif /* _STL_STACK_H */