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 erase_fn_imps.hpp 38 * Contains an implementation class for a pairing heap. 39 */ 40 41PB_DS_CLASS_T_DEC 42void 43PB_DS_CLASS_C_DEC:: 44pop() 45{ 46 _GLIBCXX_DEBUG_ONLY(assert_valid();) 47 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 48 49 node_pointer p_new_root = join_node_children(base_type::m_p_root); 50 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_new_root, false);) 51 if (p_new_root != NULL) 52 p_new_root->m_p_prev_or_parent = NULL; 53 54 base_type::actual_erase_node(base_type::m_p_root); 55 base_type::m_p_root = p_new_root; 56 _GLIBCXX_DEBUG_ONLY(assert_valid();) 57} 58 59PB_DS_CLASS_T_DEC 60void 61PB_DS_CLASS_C_DEC:: 62erase(point_iterator it) 63{ 64 _GLIBCXX_DEBUG_ONLY(assert_valid();) 65 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 66 remove_node(it.m_p_nd); 67 base_type::actual_erase_node(it.m_p_nd); 68 _GLIBCXX_DEBUG_ONLY(assert_valid();) 69} 70 71PB_DS_CLASS_T_DEC 72void 73PB_DS_CLASS_C_DEC:: 74remove_node(node_pointer p_nd) 75{ 76 _GLIBCXX_DEBUG_ONLY(assert_valid();) 77 _GLIBCXX_DEBUG_ASSERT(!base_type::empty()); 78 node_pointer p_new_child = join_node_children(p_nd); 79 80#ifdef _GLIBCXX_DEBUG 81 if (p_new_child != NULL) 82 base_type::assert_node_consistent(p_new_child, false); 83#endif 84 85 if (p_nd == base_type::m_p_root) 86 { 87 if (p_new_child != NULL) 88 p_new_child->m_p_prev_or_parent = NULL; 89 base_type::m_p_root = p_new_child; 90 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(base_type::m_p_root, false);) 91 return; 92 } 93 94 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_prev_or_parent != NULL); 95 if (p_nd->m_p_prev_or_parent->m_p_l_child == p_nd) 96 { 97 if (p_new_child != NULL) 98 { 99 p_new_child->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 100 p_new_child->m_p_next_sibling = p_nd->m_p_next_sibling; 101 if (p_new_child->m_p_next_sibling != NULL) 102 p_new_child->m_p_next_sibling->m_p_prev_or_parent = p_new_child; 103 p_nd->m_p_prev_or_parent->m_p_l_child = p_new_child; 104 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd->m_p_prev_or_parent, false);) 105 return; 106 } 107 108 p_nd->m_p_prev_or_parent->m_p_l_child = p_nd->m_p_next_sibling; 109 if (p_nd->m_p_next_sibling != NULL) 110 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 111 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd->m_p_prev_or_parent, false);) 112 return; 113 } 114 115 if (p_new_child != NULL) 116 { 117 p_new_child->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 118 p_new_child->m_p_next_sibling = p_nd->m_p_next_sibling; 119 if (p_new_child->m_p_next_sibling != NULL) 120 p_new_child->m_p_next_sibling->m_p_prev_or_parent = p_new_child; 121 p_new_child->m_p_prev_or_parent->m_p_next_sibling = p_new_child; 122 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd->m_p_prev_or_parent, false);) 123 return; 124 } 125 126 p_nd->m_p_prev_or_parent->m_p_next_sibling = p_nd->m_p_next_sibling; 127 if (p_nd->m_p_next_sibling != NULL) 128 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 129 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd->m_p_prev_or_parent, false);) 130} 131 132PB_DS_CLASS_T_DEC 133typename PB_DS_CLASS_C_DEC::node_pointer 134PB_DS_CLASS_C_DEC:: 135join_node_children(node_pointer p_nd) 136{ 137 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL); 138 node_pointer p_ret = p_nd->m_p_l_child; 139 if (p_ret == NULL) 140 return NULL; 141 while (p_ret->m_p_next_sibling != NULL) 142 p_ret = forward_join(p_ret, p_ret->m_p_next_sibling); 143 while (p_ret->m_p_prev_or_parent != p_nd) 144 p_ret = back_join(p_ret->m_p_prev_or_parent, p_ret); 145 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret, false);) 146 return p_ret; 147} 148 149PB_DS_CLASS_T_DEC 150typename PB_DS_CLASS_C_DEC::node_pointer 151PB_DS_CLASS_C_DEC:: 152forward_join(node_pointer p_nd, node_pointer p_next) 153{ 154 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL); 155 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_next_sibling == p_next); 156 if (Cmp_Fn::operator()(p_nd->m_value, p_next->m_value)) 157 { 158 p_next->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 159 base_type::make_child_of(p_nd, p_next); 160 return p_next->m_p_next_sibling == NULL 161 ? p_next : p_next->m_p_next_sibling; 162 } 163 164 if (p_next->m_p_next_sibling != NULL) 165 { 166 p_next->m_p_next_sibling->m_p_prev_or_parent = p_nd; 167 p_nd->m_p_next_sibling = p_next->m_p_next_sibling; 168 base_type::make_child_of(p_next, p_nd); 169 return p_nd->m_p_next_sibling; 170 } 171 172 p_nd->m_p_next_sibling = NULL; 173 base_type::make_child_of(p_next, p_nd); 174 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd, false)); 175 return p_nd; 176} 177 178PB_DS_CLASS_T_DEC 179typename PB_DS_CLASS_C_DEC::node_pointer 180PB_DS_CLASS_C_DEC:: 181back_join(node_pointer p_nd, node_pointer p_next) 182{ 183 _GLIBCXX_DEBUG_ASSERT(p_nd != NULL); 184 _GLIBCXX_DEBUG_ASSERT(p_next->m_p_next_sibling == NULL); 185 186 if (Cmp_Fn::operator()(p_nd->m_value, p_next->m_value)) 187 { 188 p_next->m_p_prev_or_parent = p_nd->m_p_prev_or_parent; 189 base_type::make_child_of(p_nd, p_next); 190 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_next, false)); 191 return p_next; 192 } 193 194 p_nd->m_p_next_sibling = NULL; 195 base_type::make_child_of(p_next, p_nd); 196 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd, false)); 197 return p_nd; 198} 199 200PB_DS_CLASS_T_DEC 201template<typename Pred> 202typename PB_DS_CLASS_C_DEC::size_type 203PB_DS_CLASS_C_DEC:: 204erase_if(Pred pred) 205{ 206 _GLIBCXX_DEBUG_ONLY(assert_valid();) 207 if (base_type::empty()) 208 { 209 _GLIBCXX_DEBUG_ONLY(assert_valid();) 210 return 0; 211 } 212 base_type::to_linked_list(); 213 node_pointer p_out = base_type::prune(pred); 214 size_type ersd = 0; 215 while (p_out != NULL) 216 { 217 ++ersd; 218 node_pointer p_next = p_out->m_p_next_sibling; 219 base_type::actual_erase_node(p_out); 220 p_out = p_next; 221 } 222 223 node_pointer p_cur = base_type::m_p_root; 224 base_type::m_p_root = NULL; 225 while (p_cur != NULL) 226 { 227 node_pointer p_next = p_cur->m_p_next_sibling; 228 p_cur->m_p_l_child = p_cur->m_p_next_sibling = p_cur->m_p_prev_or_parent = NULL; 229 230 push_imp(p_cur); 231 p_cur = p_next; 232 } 233 _GLIBCXX_DEBUG_ONLY(assert_valid();) 234 return ersd; 235} 236 237