libstdc++
priority_queue.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010 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 terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // 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 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file priority_queue.hpp
00038  * Contains priority_queues.
00039  */
00040 
00041 #ifndef PB_DS_PRIORITY_QUEUE_HPP
00042 #define PB_DS_PRIORITY_QUEUE_HPP
00043 
00044 #include <bits/c++config.h>
00045 #include <ext/pb_ds/tag_and_trait.hpp>
00046 #include <ext/pb_ds/detail/priority_queue_base_dispatch.hpp>
00047 #include <ext/pb_ds/detail/standard_policies.hpp>
00048 
00049 namespace __gnu_pbds
00050 {
00051   /**
00052    *  @defgroup heap-based
00053    *  @ingroup containers-pbds
00054    *  @{
00055    */
00056 
00057   /**
00058    *  @defgroup heap-detail Base and Policy Classes
00059    *  @ingroup heap-based
00060    */
00061 
00062   /**
00063    *  A priority queue composed of one specific heap policy.
00064    *
00065    *  @tparam _Tv           Value type.
00066    *  @tparam Cmp_Fn            Comparison functor.
00067    *  @tparam Tag           Instantiating data structure type,
00068    *                    see container_tag.
00069    *  @tparam _Alloc            Allocator type.
00070    *
00071    *  Base is dispatched at compile time via Tag, from the following
00072    *  choices: binary_heap_tag, binomial_heap_tag, pairing_heap_tag,
00073    *           rc_binomial_heap_tag, thin_heap_tag
00074    *
00075    *  Base choices are: detail::binary_heap, detail::binomial_heap,
00076    *                    detail::pairing_heap, detail::rc_binomial_heap,
00077    *                    detail::thin_heap.
00078    */
00079    template<typename _Tv,
00080        typename Cmp_Fn = std::less<_Tv>,
00081        typename Tag = pairing_heap_tag,
00082        typename _Alloc = std::allocator<char> >
00083   class priority_queue
00084   : public detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc, Tag>::type
00085   {
00086   public:
00087     typedef _Tv                     value_type;
00088     typedef Cmp_Fn                  cmp_fn;
00089     typedef Tag                     container_category;
00090     typedef _Alloc                  allocator_type;
00091     typedef typename allocator_type::size_type      size_type;
00092     typedef typename allocator_type::difference_type    difference_type;
00093 
00094   private:
00095     typedef typename detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc,
00096                              Tag>::type
00097                             base_type;
00098     typedef typename _Alloc::template rebind<_Tv>       __rebind_v;
00099     typedef typename __rebind_v::other          __rebind_va;
00100 
00101  public:
00102     typedef typename __rebind_va::reference         reference;
00103     typedef typename __rebind_va::const_reference   const_reference;
00104     typedef typename __rebind_va::pointer       pointer;
00105     typedef typename __rebind_va::const_pointer     const_pointer;
00106 
00107     typedef typename base_type::point_iterator      point_iterator;
00108     typedef typename base_type::point_const_iterator    point_const_iterator;
00109     typedef typename base_type::iterator        iterator;
00110     typedef typename base_type::const_iterator      const_iterator;
00111 
00112     priority_queue() { }
00113 
00114     /// Constructor taking some policy objects. r_cmp_fn will be
00115     /// copied by the Cmp_Fn object of the container object.
00116     priority_queue(const cmp_fn& r_cmp_fn) : base_type(r_cmp_fn) { }
00117 
00118     /// Constructor taking __iterators to a range of value_types. The
00119     /// value_types between first_it and last_it will be inserted into
00120     /// the container object.
00121     template<typename It>
00122     priority_queue(It first_it, It last_it)
00123     { base_type::copy_from_range(first_it, last_it); }
00124 
00125     /// Constructor taking __iterators to a range of value_types and
00126     /// some policy objects The value_types between first_it and
00127     /// last_it will be inserted into the container object. r_cmp_fn
00128     /// will be copied by the cmp_fn object of the container object.
00129     template<typename It>
00130     priority_queue(It first_it, It last_it, const cmp_fn& r_cmp_fn)
00131     : base_type(r_cmp_fn)
00132     { base_type::copy_from_range(first_it, last_it); }
00133 
00134     priority_queue(const priority_queue& other)
00135     : base_type((const base_type& )other) { }
00136 
00137     virtual
00138     ~priority_queue() { }
00139 
00140     priority_queue&
00141     operator=(const priority_queue& other)
00142     {
00143       if (this != &other)
00144     {
00145       priority_queue tmp(other);
00146       swap(tmp);
00147     }
00148       return *this;
00149     }
00150 
00151     void
00152     swap(priority_queue& other)
00153     { base_type::swap(other); }
00154   };
00155 } // namespace __gnu_pbds
00156  //@} heap-based
00157 #endif