libstdc++
priority_queue.hpp
Go to the documentation of this file.
1 // -*- C++ -*-
2 
3 // Copyright (C) 2005-2019 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 3, or (at your option) any later
9 // version.
10 
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26 
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
34 // warranty.
35 
36 /**
37  * @file priority_queue.hpp
38  * Contains priority_queues.
39  */
40 
41 #ifndef PB_DS_PRIORITY_QUEUE_HPP
42 #define PB_DS_PRIORITY_QUEUE_HPP
43 
44 #include <bits/c++config.h>
48 
49 namespace __gnu_pbds
50 {
51  /**
52  * @defgroup heap-based Heap-Based
53  * @ingroup containers-pbds
54  *
55  * @{
56  */
57 
58  /**
59  * @defgroup heap-detail Base and Policy Classes
60  * @ingroup heap-based
61  */
62 
63  /**
64  * A priority queue composed of one specific heap policy.
65  *
66  * @tparam _Tv Value type.
67  * @tparam Cmp_Fn Comparison functor.
68  * @tparam Tag Instantiating data structure type,
69  * see container_tag.
70  * @tparam _Alloc Allocator type.
71  *
72  * Base is dispatched at compile time via Tag, from the following
73  * choices: binary_heap_tag, binomial_heap_tag, pairing_heap_tag,
74  * rc_binomial_heap_tag, thin_heap_tag
75  *
76  * Base choices are: detail::binary_heap, detail::binomial_heap,
77  * detail::pairing_heap, detail::rc_binomial_heap,
78  * detail::thin_heap.
79  */
80  template<typename _Tv,
81  typename Cmp_Fn = std::less<_Tv>,
82  typename Tag = pairing_heap_tag,
83  typename _Alloc = std::allocator<char> >
85  : public detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc, Tag>::type
86  {
87  public:
88  typedef _Tv value_type;
89  typedef Cmp_Fn cmp_fn;
90  typedef Tag container_category;
91  typedef _Alloc allocator_type;
92  typedef typename allocator_type::size_type size_type;
93  typedef typename allocator_type::difference_type difference_type;
94 
95  private:
96  typedef typename detail::container_base_dispatch<_Tv, Cmp_Fn, _Alloc,
97  Tag>::type
98  base_type;
99  typedef typename _Alloc::template rebind<_Tv> __rebind_v;
100  typedef typename __rebind_v::other __rebind_va;
101 
102  public:
103  typedef typename __rebind_va::reference reference;
104  typedef typename __rebind_va::const_reference const_reference;
105  typedef typename __rebind_va::pointer pointer;
106  typedef typename __rebind_va::const_pointer const_pointer;
107 
108  typedef typename base_type::point_iterator point_iterator;
109  typedef typename base_type::point_const_iterator point_const_iterator;
110  typedef typename base_type::iterator iterator;
111  typedef typename base_type::const_iterator const_iterator;
112 
113  priority_queue() { }
114 
115  /// Constructor taking some policy objects. r_cmp_fn will be
116  /// copied by the Cmp_Fn object of the container object.
117  priority_queue(const cmp_fn& r_cmp_fn) : base_type(r_cmp_fn) { }
118 
119  /// Constructor taking __iterators to a range of value_types. The
120  /// value_types between first_it and last_it will be inserted into
121  /// the container object.
122  template<typename It>
123  priority_queue(It first_it, It last_it)
124  { base_type::copy_from_range(first_it, last_it); }
125 
126  /// Constructor taking __iterators to a range of value_types and
127  /// some policy objects The value_types between first_it and
128  /// last_it will be inserted into the container object. r_cmp_fn
129  /// will be copied by the cmp_fn object of the container object.
130  template<typename It>
131  priority_queue(It first_it, It last_it, const cmp_fn& r_cmp_fn)
132  : base_type(r_cmp_fn)
133  { base_type::copy_from_range(first_it, last_it); }
134 
135  priority_queue(const priority_queue& other)
136  : base_type((const base_type& )other) { }
137 
138  virtual
139  ~priority_queue() { }
140 
141  priority_queue&
142  operator=(const priority_queue& other)
143  {
144  if (this != &other)
145  {
146  priority_queue tmp(other);
147  swap(tmp);
148  }
149  return *this;
150  }
151 
152  void
153  swap(priority_queue& other)
154  { base_type::swap(other); }
155  };
156  ///@} heap-based
157 } // namespace __gnu_pbds
158 #endif
GNU extensions for policy-based data structures for public use.
The standard allocator, as per [20.4].
Definition: allocator.h:112
One of the comparison functors.
Definition: stl_function.h:382
priority_queue(It first_it, It last_it)
Constructor taking __iterators to a range of value_types. The value_types between first_it and last_i...
priority_queue(const cmp_fn &r_cmp_fn)
Constructor taking some policy objects. r_cmp_fn will be copied by the Cmp_Fn object of the container...
priority_queue(It first_it, It last_it, const cmp_fn &r_cmp_fn)
Constructor taking __iterators to a range of value_types and some policy objects The value_types betw...
Dispatch mechanism, primary template for associative types.