libstdc++
left_child_next_sibling_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 left_child_next_sibling_heap_/left_child_next_sibling_heap_.hpp
00038  * Contains an implementation class for a basic heap.
00039  */
00040 
00041 #ifndef PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
00042 #define PB_DS_LEFT_CHILD_NEXT_SIBLING_HEAP_HPP
00043 
00044 /*
00045  * Based on CLRS.
00046  */
00047 
00048 #include <iterator>
00049 #include <ext/pb_ds/detail/cond_dealtor.hpp>
00050 #include <ext/pb_ds/detail/type_utils.hpp>
00051 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/node.hpp>
00052 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/point_const_iterator.hpp>
00053 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/const_iterator.hpp>
00054 #ifdef PB_DS_LC_NS_HEAP_TRACE_
00055 #include <iostream>
00056 #endif
00057 #include <debug/debug.h>
00058 
00059 namespace __gnu_pbds
00060 {
00061   namespace detail
00062   {
00063 #ifdef _GLIBCXX_DEBUG
00064 #define PB_DS_CLASS_T_DEC \
00065     template<typename Value_Type, typename Cmp_Fn, typename Node_Metadata, \
00066          typename _Alloc, bool Single_Link_Roots>
00067 
00068 #define PB_DS_CLASS_C_DEC \
00069     left_child_next_sibling_heap<Value_Type, Cmp_Fn, Node_Metadata, \
00070                   _Alloc, Single_Link_Roots>
00071 #else
00072 #define PB_DS_CLASS_T_DEC \
00073     template<typename Value_Type, typename Cmp_Fn, typename Node_Metadata, \
00074          typename _Alloc>
00075 
00076 #define PB_DS_CLASS_C_DEC \
00077     left_child_next_sibling_heap<Value_Type, Cmp_Fn, Node_Metadata, _Alloc>
00078 #endif
00079 
00080     /// Base class for a basic heap.
00081     template<typename Value_Type,
00082          typename Cmp_Fn,
00083          typename Node_Metadata,
00084          typename _Alloc
00085 #ifdef _GLIBCXX_DEBUG
00086          ,bool Single_Link_Roots>
00087 #else
00088          >
00089 #endif
00090     class left_child_next_sibling_heap : public Cmp_Fn
00091     {
00092     protected:
00093       typedef
00094       typename _Alloc::template rebind<
00095       left_child_next_sibling_heap_node_<Value_Type, Node_Metadata,
00096                      _Alloc> >::other
00097       node_allocator;
00098 
00099       typedef typename node_allocator::value_type       node;
00100       typedef typename node_allocator::pointer      node_pointer;
00101       typedef typename node_allocator::const_pointer    node_const_pointer;
00102       typedef Node_Metadata node_metadata;
00103       typedef std::pair< node_pointer, node_pointer>    node_pointer_pair;
00104 
00105     private:
00106       typedef cond_dealtor< node, _Alloc>       cond_dealtor_t;
00107 
00108       enum
00109     {
00110       simple_value = is_simple<Value_Type>::value
00111     };
00112 
00113       typedef integral_constant<int, simple_value>  no_throw_copies_t;
00114       typedef typename _Alloc::template rebind<Value_Type>  __rebind_v;
00115 
00116     public:
00117       typedef typename _Alloc::size_type        size_type;
00118       typedef typename _Alloc::difference_type  difference_type;
00119       typedef Value_Type                value_type;
00120 
00121       typedef typename __rebind_v::other::pointer   pointer;
00122       typedef typename __rebind_v::other::const_pointer const_pointer;
00123       typedef typename __rebind_v::other::reference reference;
00124       typedef typename __rebind_v::other::const_reference const_reference;
00125 
00126       typedef left_child_next_sibling_heap_node_point_const_iterator_<node, _Alloc>
00127       point_const_iterator;
00128 
00129       typedef point_const_iterator          point_iterator;
00130 
00131       typedef left_child_next_sibling_heap_const_iterator_<node, _Alloc>
00132       const_iterator;
00133 
00134       typedef const_iterator                iterator;
00135       typedef Cmp_Fn                    cmp_fn;
00136       typedef _Alloc                    allocator_type;
00137 
00138       left_child_next_sibling_heap();
00139       left_child_next_sibling_heap(const Cmp_Fn&);
00140       left_child_next_sibling_heap(const left_child_next_sibling_heap&);
00141 
00142       void
00143       swap(PB_DS_CLASS_C_DEC&);
00144 
00145       ~left_child_next_sibling_heap();
00146 
00147       inline bool
00148       empty() const;
00149 
00150       inline size_type
00151       size() const;
00152 
00153       inline size_type
00154       max_size() const;
00155 
00156       Cmp_Fn&
00157       get_cmp_fn();
00158 
00159       const Cmp_Fn&
00160       get_cmp_fn() const;
00161 
00162       inline iterator
00163       begin();
00164 
00165       inline const_iterator
00166       begin() const;
00167 
00168       inline iterator
00169       end();
00170 
00171       inline const_iterator
00172       end() const;
00173 
00174       void
00175       clear();
00176 
00177 #ifdef PB_DS_LC_NS_HEAP_TRACE_
00178       void
00179       trace() const;
00180 #endif
00181 
00182     protected:
00183       inline node_pointer
00184       get_new_node_for_insert(const_reference);
00185 
00186       inline static void
00187       make_child_of(node_pointer, node_pointer);
00188 
00189       void
00190       value_swap(left_child_next_sibling_heap&);
00191 
00192       inline static node_pointer
00193       parent(node_pointer);
00194 
00195       inline void
00196       swap_with_parent(node_pointer, node_pointer);
00197 
00198       void
00199       bubble_to_top(node_pointer);
00200 
00201       inline void
00202       actual_erase_node(node_pointer);
00203 
00204       void
00205       clear_imp(node_pointer);
00206 
00207       void
00208       to_linked_list();
00209 
00210       template<typename Pred>
00211       node_pointer
00212       prune(Pred);
00213 
00214 #ifdef _GLIBCXX_DEBUG
00215       void
00216       assert_valid(const char*, int) const;
00217 
00218       void
00219       assert_node_consistent(node_const_pointer, bool, const char*, int) const;
00220 
00221       static size_type
00222       size_under_node(node_const_pointer);
00223 
00224       static size_type
00225       degree(node_const_pointer);
00226 #endif
00227 
00228 #ifdef PB_DS_LC_NS_HEAP_TRACE_
00229       static void
00230       trace_node(node_const_pointer, size_type);
00231 #endif
00232 
00233     private:
00234 #ifdef _GLIBCXX_DEBUG
00235       void
00236       assert_iterators(const char*, int) const;
00237 
00238       void
00239       assert_size(const char*, int) const;
00240 
00241       static size_type
00242       size_from_node(node_const_pointer);
00243 #endif
00244 
00245       node_pointer
00246       recursive_copy_node(node_const_pointer);
00247 
00248       inline node_pointer
00249       get_new_node_for_insert(const_reference, false_type);
00250 
00251       inline node_pointer
00252       get_new_node_for_insert(const_reference, true_type);
00253 
00254 #ifdef PB_DS_LC_NS_HEAP_TRACE_
00255       template<typename Metadata_>
00256       static void
00257       trace_node_metadata(node_const_pointer, type_to_type<Metadata_>);
00258 
00259       static void
00260       trace_node_metadata(node_const_pointer, type_to_type<null_type>);
00261 #endif
00262 
00263       static node_allocator     s_node_allocator;
00264       static no_throw_copies_t  s_no_throw_copies_ind;
00265 
00266     protected:
00267       node_pointer      m_p_root;
00268       size_type         m_size;
00269     };
00270 
00271 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/constructors_destructor_fn_imps.hpp>
00272 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/iterators_fn_imps.hpp>
00273 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/debug_fn_imps.hpp>
00274 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/trace_fn_imps.hpp>
00275 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/insert_fn_imps.hpp>
00276 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/erase_fn_imps.hpp>
00277 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/info_fn_imps.hpp>
00278 #include <ext/pb_ds/detail/left_child_next_sibling_heap_/policy_access_fn_imps.hpp>
00279 
00280 #undef PB_DS_CLASS_C_DEC
00281 #undef PB_DS_CLASS_T_DEC
00282 
00283   } // namespace detail
00284 } // namespace __gnu_pbds
00285 
00286 #endif