erase_fn_imps.hpp revision 169691
1209513Simp// -*- C++ -*- 2209513Simp 3209552Simp// Copyright (C) 2005, 2006 Free Software Foundation, Inc. 4209513Simp// 5209513Simp// This file is part of the GNU ISO C++ Library. This library is free 6209513Simp// software; you can redistribute it and/or modify it under the terms 7209513Simp// of the GNU General Public License as published by the Free Software 8209513Simp// Foundation; either version 2, or (at your option) any later 9209513Simp// version. 10209513Simp 11209513Simp// This library is distributed in the hope that it will be useful, but 12209513Simp// WITHOUT ANY WARRANTY; without even the implied warranty of 13209513Simp// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14209513Simp// General Public License for more details. 15209513Simp 16209513Simp// You should have received a copy of the GNU General Public License 17209513Simp// along with this library; see the file COPYING. If not, write to 18209513Simp// the Free Software Foundation, 59 Temple Place - Suite 330, Boston, 19209513Simp// MA 02111-1307, USA. 20209513Simp 21209513Simp// As a special exception, you may use this file as part of a free 22209513Simp// software library without restriction. Specifically, if other files 23209513Simp// instantiate templates or use macros or inline functions from this 24209513Simp// file, or you compile this file and link it with other files to 25209513Simp// produce an executable, this file does not by itself cause the 26209513Simp// resulting executable to be covered by the GNU General Public 27209513Simp// License. This exception does not however invalidate any other 28209513Simp// reasons why the executable file might be covered by the GNU General 29209513Simp// Public License. 30209513Simp 31209513Simp// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL. 32209513Simp 33209513Simp// Permission to use, copy, modify, sell, and distribute this software 34209513Simp// is hereby granted without fee, provided that the above copyright 35209513Simp// notice appears in all copies, and that both that copyright notice 36209513Simp// and this permission notice appear in supporting documentation. None 37209513Simp// of the above authors, nor IBM Haifa Research Laboratories, make any 38209513Simp// representation about the suitability of this software for any 39209513Simp// purpose. It is provided "as is" without express or implied 40209513Simp// warranty. 41209513Simp 42209513Simp/** 43209513Simp * @file erase_fn_imps.hpp 44209513Simp * Contains an implementation class for left_child_next_sibling_heap_. 45209513Simp */ 46209513Simp 47209513SimpPB_DS_CLASS_T_DEC 48209513Simpvoid 49209513SimpPB_DS_CLASS_C_DEC:: 50209513Simpclear() 51209513Simp{ 52220059Sjpaetzel clear_imp(m_p_root); 53220059Sjpaetzel _GLIBCXX_DEBUG_ASSERT(m_size == 0); 54209513Simp m_p_root = NULL; 55209513Simp} 56209513Simp 57209513SimpPB_DS_CLASS_T_DEC 58209513Simpinline void 59209513SimpPB_DS_CLASS_C_DEC:: 60209513Simpactual_erase_node(node_pointer p_nd) 61209513Simp{ 62209513Simp _GLIBCXX_DEBUG_ASSERT(m_size > 0); 63209513Simp --m_size; 64209513Simp p_nd->~node(); 65209513Simp s_node_allocator.deallocate(p_nd, 1); 66209513Simp} 67209513Simp 68209513SimpPB_DS_CLASS_T_DEC 69209513Simpvoid 70209513SimpPB_DS_CLASS_C_DEC:: 71209513Simpclear_imp(node_pointer p_nd) 72209513Simp{ 73209513Simp while (p_nd != NULL) 74209513Simp { 75209513Simp clear_imp(p_nd->m_p_l_child); 76209513Simp node_pointer p_next = p_nd->m_p_next_sibling; 77209513Simp actual_erase_node(p_nd); 78209513Simp p_nd = p_next; 79209513Simp } 80209513Simp} 81209513Simp 82209513SimpPB_DS_CLASS_T_DEC 83209513Simpvoid 84209513SimpPB_DS_CLASS_C_DEC:: 85209513Simpto_linked_list() 86209513Simp{ 87209513Simp _GLIBCXX_DEBUG_ONLY(assert_valid();) 88209513Simp node_pointer p_cur = m_p_root; 89209513Simp while (p_cur != NULL) 90209513Simp if (p_cur->m_p_l_child != NULL) 91209513Simp { 92209513Simp node_pointer p_child_next = p_cur->m_p_l_child->m_p_next_sibling; 93209513Simp p_cur->m_p_l_child->m_p_next_sibling = p_cur->m_p_next_sibling; 94209513Simp p_cur->m_p_next_sibling = p_cur->m_p_l_child; 95220059Sjpaetzel p_cur->m_p_l_child = p_child_next; 96209513Simp } 97209513Simp else 98209513Simp p_cur = p_cur->m_p_next_sibling; 99209513Simp 100209513Simp#ifdef _GLIBCXX_DEBUG 101209513Simp const_node_pointer p_counter = m_p_root; 102209513Simp size_type count = 0; 103209513Simp while (p_counter != NULL) 104209513Simp { 105209513Simp ++count; 106209513Simp _GLIBCXX_DEBUG_ASSERT(p_counter->m_p_l_child == NULL); 107209513Simp p_counter = p_counter->m_p_next_sibling; 108209513Simp } 109209513Simp _GLIBCXX_DEBUG_ASSERT(count == m_size); 110209513Simp#endif 111209513Simp} 112209513Simp 113209513SimpPB_DS_CLASS_T_DEC 114209513Simptemplate<typename Pred> 115209513Simptypename PB_DS_CLASS_C_DEC::node_pointer 116209513SimpPB_DS_CLASS_C_DEC:: 117232880Sjpaetzelprune(Pred pred) 118220059Sjpaetzel{ 119209513Simp node_pointer p_cur = m_p_root; 120220059Sjpaetzel m_p_root = NULL; 121209513Simp node_pointer p_out = NULL; 122209513Simp while (p_cur != NULL) 123209513Simp { 124209513Simp node_pointer p_next = p_cur->m_p_next_sibling; 125209513Simp if (pred(p_cur->m_value)) 126209513Simp { 127209513Simp p_cur->m_p_next_sibling = p_out; 128209513Simp if (p_out != NULL) 129209513Simp p_out->m_p_prev_or_parent = p_cur; 130209513Simp p_out = p_cur; 131209513Simp } 132209513Simp else 133209513Simp { 134209513Simp p_cur->m_p_next_sibling = m_p_root; 135209513Simp if (m_p_root != NULL) 136209513Simp m_p_root->m_p_prev_or_parent = p_cur; 137 m_p_root = p_cur; 138 } 139 p_cur = p_next; 140 } 141 return p_out; 142} 143 144PB_DS_CLASS_T_DEC 145void 146PB_DS_CLASS_C_DEC:: 147bubble_to_top(node_pointer p_nd) 148{ 149 node_pointer p_parent = parent(p_nd); 150 while (p_parent != NULL) 151 { 152 swap_with_parent(p_nd, p_parent); 153 p_parent = parent(p_nd); 154 } 155} 156 157