libstdc++
pairing_heap_.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2011 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 pairing_heap_/pairing_heap_.hpp
00038  * Contains an implementation class for a pairing heap.
00039  */
00040 
00041 /*
00042  * Pairing heap:
00043  * Michael L. Fredman, Robert Sedgewick, Daniel Dominic Sleator,
00044  *    and Robert Endre Tarjan, The Pairing Heap:
00045  *    A New Form of Self-Adjusting Heap, Algorithmica, 1(1):111-129, 1986.
00046  */
00047 
00048 #include <ext/pb_ds/detail/cond_dealtor.hpp>
00049 #include <ext/pb_ds/detail/type_utils.hpp>
00050 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/left_child_next_sibling_heap_.hpp>
00051 #include <debug/debug.h>
00052 
00053 namespace __gnu_pbds
00054 {
00055   namespace detail
00056   {
00057 #define PB_DS_CLASS_T_DEC \
00058   template<typename Value_Type, typename Cmp_Fn, typename _Alloc>
00059 
00060 #define PB_DS_CLASS_C_DEC \
00061   pairing_heap<Value_Type, Cmp_Fn, _Alloc>
00062 
00063 #ifdef _GLIBCXX_DEBUG
00064 #define PB_DS_P_HEAP_BASE \
00065   left_child_next_sibling_heap<Value_Type, Cmp_Fn, null_type, _Alloc, false>
00066 #else
00067 #define PB_DS_P_HEAP_BASE \
00068   left_child_next_sibling_heap<Value_Type, Cmp_Fn, null_type, _Alloc>
00069 #endif
00070 
00071     /**
00072      *  Pairing heap.
00073      *
00074      *  @ingroup heap-detail
00075      */
00076     template<typename Value_Type, typename Cmp_Fn, typename _Alloc>
00077     class pairing_heap : public PB_DS_P_HEAP_BASE
00078     {
00079     private:
00080       typedef PB_DS_P_HEAP_BASE             base_type;
00081       typedef typename base_type::node_pointer      node_pointer;
00082 
00083       typedef typename _Alloc::template rebind<Value_Type>::other __rebind_a;
00084 
00085     public:
00086       typedef Value_Type                value_type;
00087       typedef Cmp_Fn                    cmp_fn;
00088       typedef _Alloc                    allocator_type;
00089       typedef typename _Alloc::size_type        size_type;
00090       typedef typename _Alloc::difference_type      difference_type;
00091 
00092       typedef typename __rebind_a::pointer      pointer;
00093       typedef typename __rebind_a::const_pointer    const_pointer;
00094       typedef typename __rebind_a::reference        reference;
00095       typedef typename __rebind_a::const_reference  const_reference;
00096 
00097       typedef typename base_type::point_const_iterator  point_const_iterator;
00098       typedef typename base_type::point_iterator    point_iterator;
00099       typedef typename base_type::const_iterator    const_iterator;
00100       typedef typename base_type::iterator      iterator;
00101 
00102       pairing_heap();
00103 
00104       pairing_heap(const Cmp_Fn&);
00105 
00106       pairing_heap(const pairing_heap&);
00107 
00108       void
00109       swap(pairing_heap&);
00110 
00111       ~pairing_heap();
00112 
00113       inline point_iterator
00114       push(const_reference);
00115 
00116       void
00117       modify(point_iterator, const_reference);
00118 
00119       inline const_reference
00120       top() const;
00121 
00122       void
00123       pop();
00124 
00125       void
00126       erase(point_iterator);
00127 
00128       template<typename Pred>
00129       size_type
00130       erase_if(Pred);
00131 
00132       template<typename Pred>
00133       void
00134       split(Pred, pairing_heap&);
00135 
00136       void
00137       join(pairing_heap&);
00138 
00139     protected:
00140 
00141       template<typename It>
00142       void
00143       copy_from_range(It, It);
00144 
00145 #ifdef _GLIBCXX_DEBUG
00146       void
00147       assert_valid(const char*, int) const;
00148 #endif
00149 
00150     private:
00151 
00152       inline void
00153       push_imp(node_pointer);
00154 
00155       node_pointer
00156       join_node_children(node_pointer);
00157 
00158       node_pointer
00159       forward_join(node_pointer, node_pointer);
00160 
00161       node_pointer
00162       back_join(node_pointer, node_pointer);
00163 
00164       void
00165       remove_node(node_pointer);
00166     };
00167 
00168 #define PB_DS_ASSERT_NODE_CONSISTENT(_Node, _Bool) \
00169  _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(_Node, _Bool,    \
00170                                __FILE__, __LINE__);)
00171 
00172 #include <ext/pb_ds/detail/pairing_heap_/constructors_destructor_fn_imps.hpp>
00173 #include <ext/pb_ds/detail/pairing_heap_/debug_fn_imps.hpp>
00174 #include <ext/pb_ds/detail/pairing_heap_/find_fn_imps.hpp>
00175 #include <ext/pb_ds/detail/pairing_heap_/insert_fn_imps.hpp>
00176 #include <ext/pb_ds/detail/pairing_heap_/erase_fn_imps.hpp>
00177 #include <ext/pb_ds/detail/pairing_heap_/split_join_fn_imps.hpp>
00178 
00179 #undef PB_DS_ASSERT_NODE_CONSISTENT
00180 #undef PB_DS_CLASS_C_DEC
00181 #undef PB_DS_CLASS_T_DEC
00182 #undef PB_DS_P_HEAP_BASE
00183 
00184   } // namespace detail
00185 } // namespace __gnu_pbds