libstdc++
left_child_next_sibling_heap_/erase_fn_imps.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 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_/erase_fn_imps.hpp
00038  * Contains an implementation class for left_child_next_sibling_heap_.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 void
00043 PB_DS_CLASS_C_DEC::
00044 clear()
00045 {
00046   clear_imp(m_p_root);
00047   _GLIBCXX_DEBUG_ASSERT(m_size == 0);
00048   m_p_root = 0;
00049 }
00050 
00051 PB_DS_CLASS_T_DEC
00052 inline void
00053 PB_DS_CLASS_C_DEC::
00054 actual_erase_node(node_pointer p_nd)
00055 {
00056   _GLIBCXX_DEBUG_ASSERT(m_size > 0);
00057   --m_size;
00058   p_nd->~node();
00059   s_node_allocator.deallocate(p_nd, 1);
00060 }
00061 
00062 PB_DS_CLASS_T_DEC
00063 void
00064 PB_DS_CLASS_C_DEC::
00065 clear_imp(node_pointer p_nd)
00066 {
00067   while (p_nd != 0)
00068     {
00069       clear_imp(p_nd->m_p_l_child);
00070       node_pointer p_next = p_nd->m_p_next_sibling;
00071       actual_erase_node(p_nd);
00072       p_nd = p_next;
00073     }
00074 }
00075 
00076 PB_DS_CLASS_T_DEC
00077 void
00078 PB_DS_CLASS_C_DEC::
00079 to_linked_list()
00080 {
00081   PB_DS_ASSERT_VALID((*this))
00082   node_pointer p_cur = m_p_root;
00083   while (p_cur != 0)
00084     if (p_cur->m_p_l_child != 0)
00085       {
00086     node_pointer p_child_next = p_cur->m_p_l_child->m_p_next_sibling;
00087     p_cur->m_p_l_child->m_p_next_sibling = p_cur->m_p_next_sibling;
00088     p_cur->m_p_next_sibling = p_cur->m_p_l_child;
00089     p_cur->m_p_l_child = p_child_next;
00090       }
00091     else
00092       p_cur = p_cur->m_p_next_sibling;
00093 
00094 #ifdef _GLIBCXX_DEBUG
00095   node_const_pointer p_counter = m_p_root;
00096   size_type count = 0;
00097   while (p_counter != 0)
00098     {
00099       ++count;
00100       _GLIBCXX_DEBUG_ASSERT(p_counter->m_p_l_child == 0);
00101       p_counter = p_counter->m_p_next_sibling;
00102     }
00103   _GLIBCXX_DEBUG_ASSERT(count == m_size);
00104 #endif 
00105 }
00106 
00107 PB_DS_CLASS_T_DEC
00108 template<typename Pred>
00109 typename PB_DS_CLASS_C_DEC::node_pointer
00110 PB_DS_CLASS_C_DEC::
00111 prune(Pred pred)
00112 {
00113   node_pointer p_cur = m_p_root;
00114   m_p_root = 0;
00115   node_pointer p_out = 0;
00116   while (p_cur != 0)
00117     {
00118       node_pointer p_next = p_cur->m_p_next_sibling;
00119       if (pred(p_cur->m_value))
00120         {
00121       p_cur->m_p_next_sibling = p_out;
00122       if (p_out != 0)
00123         p_out->m_p_prev_or_parent = p_cur;
00124       p_out = p_cur;
00125         }
00126       else
00127         {
00128       p_cur->m_p_next_sibling = m_p_root;
00129       if (m_p_root != 0)
00130         m_p_root->m_p_prev_or_parent = p_cur;
00131       m_p_root = p_cur;
00132         }
00133       p_cur = p_next;
00134     }
00135   return p_out;
00136 }
00137 
00138 PB_DS_CLASS_T_DEC
00139 void
00140 PB_DS_CLASS_C_DEC::
00141 bubble_to_top(node_pointer p_nd)
00142 {
00143   node_pointer p_parent = parent(p_nd);
00144   while (p_parent != 0)
00145     {
00146       swap_with_parent(p_nd, p_parent);
00147       p_parent = parent(p_nd);
00148     }
00149 }
00150