libstdc++
|
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 pairing_heap_/erase_fn_imps.hpp 00038 * Contains an implementation class for a pairing heap. 00039 */ 00040 00041 PB_DS_CLASS_T_DEC 00042 void 00043 PB_DS_CLASS_C_DEC:: 00044 pop() 00045 { 00046 PB_DS_ASSERT_VALID((*this)) 00047 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 00048 00049 node_pointer p_new_root = join_node_children(base_type::m_p_root); 00050 PB_DS_ASSERT_NODE_CONSISTENT(p_new_root, false) 00051 if (p_new_root != 0) 00052 p_new_root->m_p_prev_or_parent = 0; 00053 00054 base_type::actual_erase_node(base_type::m_p_root); 00055 base_type::m_p_root = p_new_root; 00056 PB_DS_ASSERT_VALID((*this)) 00057 } 00058 00059 PB_DS_CLASS_T_DEC 00060 void 00061 PB_DS_CLASS_C_DEC:: 00062 erase(point_iterator it) 00063 { 00064 PB_DS_ASSERT_VALID((*this)) 00065 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 00066 remove_node(it.m_p_nd); 00067 base_type::actual_erase_node(it.m_p_nd); 00068 PB_DS_ASSERT_VALID((*this)) 00069 } 00070 00071 PB_DS_CLASS_T_DEC 00072 void 00073 PB_DS_CLASS_C_DEC:: 00074 remove_node(node_pointer p_nd) 00075 { 00076 PB_DS_ASSERT_VALID((*this)) 00077 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 00078 node_pointer p_new_child = join_node_children(p_nd); 00079 00080 PB_DS_ASSERT_NODE_CONSISTENT(p_new_child, false) 00081 00082 if (p_nd == base_type::m_p_root) 00083 { 00084 if (p_new_child != 0) 00085 p_new_child->m_p_prev_or_parent = 0; 00086 base_type::m_p_root = p_new_child; 00087 PB_DS_ASSERT_NODE_CONSISTENT(base_type::m_p_root, false) 00088 return; 00089 } 00090 00091 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_prev_or_parent != 0); 00092 if (p_nd->m_p_prev_or_parent->m_p_l_child == p_nd) 00093 { 00094 if (p_new_child != 0) 00095 { 00096 p_new_child->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00097 p_new_child->m_p_next_sibling = p_nd->m_p_next_sibling; 00098 if (p_new_child->m_p_next_sibling != 0) 00099 p_new_child->m_p_next_sibling->m_p_prev_or_parent = p_new_child; 00100 p_nd->m_p_prev_or_parent->m_p_l_child = p_new_child; 00101 PB_DS_ASSERT_NODE_CONSISTENT(p_nd->m_p_prev_or_parent, false) 00102 return; 00103 } 00104 00105 p_nd->m_p_prev_or_parent->m_p_l_child = p_nd->m_p_next_sibling; 00106 if (p_nd->m_p_next_sibling != 0) 00107 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00108 PB_DS_ASSERT_NODE_CONSISTENT(p_nd->m_p_prev_or_parent, false) 00109 return; 00110 } 00111 00112 if (p_new_child != 0) 00113 { 00114 p_new_child->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00115 p_new_child->m_p_next_sibling = p_nd->m_p_next_sibling; 00116 if (p_new_child->m_p_next_sibling != 0) 00117 p_new_child->m_p_next_sibling->m_p_prev_or_parent = p_new_child; 00118 p_new_child->m_p_prev_or_parent->m_p_next_sibling = p_new_child; 00119 PB_DS_ASSERT_NODE_CONSISTENT(p_nd->m_p_prev_or_parent, false) 00120 return; 00121 } 00122 00123 p_nd->m_p_prev_or_parent->m_p_next_sibling = p_nd->m_p_next_sibling; 00124 if (p_nd->m_p_next_sibling != 0) 00125 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00126 PB_DS_ASSERT_NODE_CONSISTENT(p_nd->m_p_prev_or_parent, false) 00127 } 00128 00129 PB_DS_CLASS_T_DEC 00130 typename PB_DS_CLASS_C_DEC::node_pointer 00131 PB_DS_CLASS_C_DEC:: 00132 join_node_children(node_pointer p_nd) 00133 { 00134 _GLIBCXX_DEBUG_ASSERT(p_nd != 0); 00135 node_pointer p_ret = p_nd->m_p_l_child; 00136 if (p_ret == 0) 00137 return 0; 00138 while (p_ret->m_p_next_sibling != 0) 00139 p_ret = forward_join(p_ret, p_ret->m_p_next_sibling); 00140 while (p_ret->m_p_prev_or_parent != p_nd) 00141 p_ret = back_join(p_ret->m_p_prev_or_parent, p_ret); 00142 PB_DS_ASSERT_NODE_CONSISTENT(p_ret, false) 00143 return p_ret; 00144 } 00145 00146 PB_DS_CLASS_T_DEC 00147 typename PB_DS_CLASS_C_DEC::node_pointer 00148 PB_DS_CLASS_C_DEC:: 00149 forward_join(node_pointer p_nd, node_pointer p_next) 00150 { 00151 _GLIBCXX_DEBUG_ASSERT(p_nd != 0); 00152 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_next_sibling == p_next); 00153 if (Cmp_Fn::operator()(p_nd->m_value, p_next->m_value)) 00154 { 00155 p_next->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00156 base_type::make_child_of(p_nd, p_next); 00157 return p_next->m_p_next_sibling == 0 00158 ? p_next : p_next->m_p_next_sibling; 00159 } 00160 00161 if (p_next->m_p_next_sibling != 0) 00162 { 00163 p_next->m_p_next_sibling->m_p_prev_or_parent = p_nd; 00164 p_nd->m_p_next_sibling = p_next->m_p_next_sibling; 00165 base_type::make_child_of(p_next, p_nd); 00166 return p_nd->m_p_next_sibling; 00167 } 00168 00169 p_nd->m_p_next_sibling = 0; 00170 base_type::make_child_of(p_next, p_nd); 00171 PB_DS_ASSERT_NODE_CONSISTENT(p_nd, false) 00172 return p_nd; 00173 } 00174 00175 PB_DS_CLASS_T_DEC 00176 typename PB_DS_CLASS_C_DEC::node_pointer 00177 PB_DS_CLASS_C_DEC:: 00178 back_join(node_pointer p_nd, node_pointer p_next) 00179 { 00180 _GLIBCXX_DEBUG_ASSERT(p_nd != 0); 00181 _GLIBCXX_DEBUG_ASSERT(p_next->m_p_next_sibling == 0); 00182 00183 if (Cmp_Fn::operator()(p_nd->m_value, p_next->m_value)) 00184 { 00185 p_next->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 00186 base_type::make_child_of(p_nd, p_next); 00187 PB_DS_ASSERT_NODE_CONSISTENT(p_next, false) 00188 return p_next; 00189 } 00190 00191 p_nd->m_p_next_sibling = 0; 00192 base_type::make_child_of(p_next, p_nd); 00193 PB_DS_ASSERT_NODE_CONSISTENT(p_nd, false) 00194 return p_nd; 00195 } 00196 00197 PB_DS_CLASS_T_DEC 00198 template<typename Pred> 00199 typename PB_DS_CLASS_C_DEC::size_type 00200 PB_DS_CLASS_C_DEC:: 00201 erase_if(Pred pred) 00202 { 00203 PB_DS_ASSERT_VALID((*this)) 00204 if (base_type::empty()) 00205 { 00206 PB_DS_ASSERT_VALID((*this)) 00207 return 0; 00208 } 00209 base_type::to_linked_list(); 00210 node_pointer p_out = base_type::prune(pred); 00211 size_type ersd = 0; 00212 while (p_out != 0) 00213 { 00214 ++ersd; 00215 node_pointer p_next = p_out->m_p_next_sibling; 00216 base_type::actual_erase_node(p_out); 00217 p_out = p_next; 00218 } 00219 00220 node_pointer p_cur = base_type::m_p_root; 00221 base_type::m_p_root = 0; 00222 while (p_cur != 0) 00223 { 00224 node_pointer p_next = p_cur->m_p_next_sibling; 00225 p_cur->m_p_l_child = p_cur->m_p_next_sibling = p_cur->m_p_prev_or_parent = 0; 00226 00227 push_imp(p_cur); 00228 p_cur = p_next; 00229 } 00230 PB_DS_ASSERT_VALID((*this)) 00231 return ersd; 00232 } 00233