1 // -*- C++ -*- 2 3 // Copyright (C) 2005, 2006, 2009 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 pairing_heap_.hpp 38 * Contains an implementation class for a pairing heap. 39 */ 40 41 /* 42 * Pairing heap: 43 * Michael L. Fredman, Robert Sedgewick, Daniel Dominic Sleator, 44 * and Robert Endre Tarjan, The Pairing Heap: 45 * A New Form of Self-Adjusting Heap, Algorithmica, 1(1):111-129, 1986. 46 */ 47 48 #include <ext/pb_ds/detail/cond_dealtor.hpp> 49 #include <ext/pb_ds/detail/type_utils.hpp> 50 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/left_child_next_sibling_heap_.hpp> 51 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/null_metadata.hpp> 52 #include <debug/debug.h> 53 54 namespace __gnu_pbds 55 { 56 namespace detail 57 { 58 59 #define PB_DS_CLASS_T_DEC \ 60 template<typename Value_Type, class Cmp_Fn, class Allocator> 61 62 #define PB_DS_CLASS_C_DEC \ 63 pairing_heap_<Value_Type, Cmp_Fn, Allocator> 64 65 #ifdef _GLIBCXX_DEBUG 66 #define PB_DS_BASE_C_DEC \ 67 left_child_next_sibling_heap_< \ 68 Value_Type, \ 69 Cmp_Fn, \ 70 null_left_child_next_sibling_heap_node_metadata, \ 71 Allocator, \ 72 false> 73 #else 74 #define PB_DS_BASE_C_DEC \ 75 left_child_next_sibling_heap_< \ 76 Value_Type, \ 77 Cmp_Fn, \ 78 null_left_child_next_sibling_heap_node_metadata, \ 79 Allocator> 80 #endif 81 82 /** 83 * class description = "P4ri|\|g h3ap$"> 84 **/ 85 template<typename Value_Type, class Cmp_Fn, class Allocator> 86 class pairing_heap_ : public PB_DS_BASE_C_DEC 87 { 88 89 private: 90 typedef PB_DS_BASE_C_DEC base_type; 91 92 typedef typename base_type::node_pointer node_pointer; 93 94 public: 95 96 typedef typename Allocator::size_type size_type; 97 98 typedef typename Allocator::difference_type difference_type; 99 100 typedef Value_Type value_type; 101 102 typedef 103 typename Allocator::template rebind< 104 value_type>::other::pointer 105 pointer; 106 107 typedef 108 typename Allocator::template rebind< 109 value_type>::other::const_pointer 110 const_pointer; 111 112 typedef 113 typename Allocator::template rebind< 114 value_type>::other::reference 115 reference; 116 117 typedef 118 typename Allocator::template rebind< 119 value_type>::other::const_reference 120 const_reference; 121 122 typedef 123 typename PB_DS_BASE_C_DEC::const_point_iterator 124 const_point_iterator; 125 126 typedef typename PB_DS_BASE_C_DEC::point_iterator point_iterator; 127 128 typedef typename PB_DS_BASE_C_DEC::const_iterator const_iterator; 129 130 typedef typename PB_DS_BASE_C_DEC::iterator iterator; 131 132 typedef Cmp_Fn cmp_fn; 133 134 typedef Allocator allocator_type; 135 136 137 pairing_heap_(); 138 139 pairing_heap_(const Cmp_Fn& r_cmp_fn); 140 141 pairing_heap_(const PB_DS_CLASS_C_DEC& other); 142 143 void 144 swap(PB_DS_CLASS_C_DEC& other); 145 146 ~pairing_heap_(); 147 148 inline point_iterator 149 push(const_reference r_val); 150 151 void 152 modify(point_iterator it, const_reference r_new_val); 153 154 inline const_reference 155 top() const; 156 157 void 158 pop(); 159 160 void 161 erase(point_iterator it); 162 163 template<typename Pred> 164 size_type 165 erase_if(Pred pred); 166 167 template<typename Pred> 168 void 169 split(Pred pred, PB_DS_CLASS_C_DEC& other); 170 171 void 172 join(PB_DS_CLASS_C_DEC& other); 173 174 protected: 175 176 template<typename It> 177 void 178 copy_from_range(It first_it, It last_it); 179 180 #ifdef _GLIBCXX_DEBUG 181 void 182 assert_valid() const; 183 #endif 184 185 private: 186 187 inline void 188 push_imp(node_pointer p_nd); 189 190 node_pointer 191 join_node_children(node_pointer p_nd); 192 193 node_pointer 194 forward_join(node_pointer p_nd, node_pointer p_next); 195 196 node_pointer 197 back_join(node_pointer p_nd, node_pointer p_next); 198 199 void 200 remove_node(node_pointer p_nd); 201 202 }; 203 204 #include <ext/pb_ds/detail/pairing_heap_/constructors_destructor_fn_imps.hpp> 205 #include <ext/pb_ds/detail/pairing_heap_/debug_fn_imps.hpp> 206 #include <ext/pb_ds/detail/pairing_heap_/find_fn_imps.hpp> 207 #include <ext/pb_ds/detail/pairing_heap_/insert_fn_imps.hpp> 208 #include <ext/pb_ds/detail/pairing_heap_/erase_fn_imps.hpp> 209 #include <ext/pb_ds/detail/pairing_heap_/split_join_fn_imps.hpp> 210 211 #undef PB_DS_CLASS_C_DEC 212 #undef PB_DS_CLASS_T_DEC 213 #undef PB_DS_BASE_C_DEC 214 215 } // namespace detail 216 } // namespace __gnu_pbds 217