2010-06-03 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc/alias-decl.git] / libstdc++-v3 / include / ext / pb_ds / detail / pairing_heap_ / erase_fn_imps.hpp
blob226e9531ada53606e83fd9b521a634a4a1f9968a
1 // -*- C++ -*-
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.
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.
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.
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/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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.
36 /**
37 * @file erase_fn_imps.hpp
38 * Contains an implementation class for a pairing heap.
41 PB_DS_CLASS_T_DEC
42 void
43 PB_DS_CLASS_C_DEC::
44 pop()
46 _GLIBCXX_DEBUG_ONLY(assert_valid();)
47 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
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 != 0)
52 p_new_root->m_p_prev_or_parent = 0;
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();)
59 PB_DS_CLASS_T_DEC
60 void
61 PB_DS_CLASS_C_DEC::
62 erase(point_iterator it)
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();)
71 PB_DS_CLASS_T_DEC
72 void
73 PB_DS_CLASS_C_DEC::
74 remove_node(node_pointer p_nd)
76 _GLIBCXX_DEBUG_ONLY(assert_valid();)
77 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
78 node_pointer p_new_child = join_node_children(p_nd);
80 #ifdef _GLIBCXX_DEBUG
81 if (p_new_child != 0)
82 base_type::assert_node_consistent(p_new_child, false);
83 #endif
85 if (p_nd == base_type::m_p_root)
87 if (p_new_child != 0)
88 p_new_child->m_p_prev_or_parent = 0;
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;
94 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_prev_or_parent != 0);
95 if (p_nd->m_p_prev_or_parent->m_p_l_child == p_nd)
97 if (p_new_child != 0)
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 != 0)
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;
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 != 0)
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;
115 if (p_new_child != 0)
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 != 0)
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;
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 != 0)
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);)
132 PB_DS_CLASS_T_DEC
133 typename PB_DS_CLASS_C_DEC::node_pointer
134 PB_DS_CLASS_C_DEC::
135 join_node_children(node_pointer p_nd)
137 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
138 node_pointer p_ret = p_nd->m_p_l_child;
139 if (p_ret == 0)
140 return 0;
141 while (p_ret->m_p_next_sibling != 0)
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;
149 PB_DS_CLASS_T_DEC
150 typename PB_DS_CLASS_C_DEC::node_pointer
151 PB_DS_CLASS_C_DEC::
152 forward_join(node_pointer p_nd, node_pointer p_next)
154 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
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))
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 == 0
161 ? p_next : p_next->m_p_next_sibling;
164 if (p_next->m_p_next_sibling != 0)
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;
172 p_nd->m_p_next_sibling = 0;
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;
178 PB_DS_CLASS_T_DEC
179 typename PB_DS_CLASS_C_DEC::node_pointer
180 PB_DS_CLASS_C_DEC::
181 back_join(node_pointer p_nd, node_pointer p_next)
183 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
184 _GLIBCXX_DEBUG_ASSERT(p_next->m_p_next_sibling == 0);
186 if (Cmp_Fn::operator()(p_nd->m_value, p_next->m_value))
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;
194 p_nd->m_p_next_sibling = 0;
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;
200 PB_DS_CLASS_T_DEC
201 template<typename Pred>
202 typename PB_DS_CLASS_C_DEC::size_type
203 PB_DS_CLASS_C_DEC::
204 erase_if(Pred pred)
206 _GLIBCXX_DEBUG_ONLY(assert_valid();)
207 if (base_type::empty())
209 _GLIBCXX_DEBUG_ONLY(assert_valid();)
210 return 0;
212 base_type::to_linked_list();
213 node_pointer p_out = base_type::prune(pred);
214 size_type ersd = 0;
215 while (p_out != 0)
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;
223 node_pointer p_cur = base_type::m_p_root;
224 base_type::m_p_root = 0;
225 while (p_cur != 0)
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 = 0;
230 push_imp(p_cur);
231 p_cur = p_next;
233 _GLIBCXX_DEBUG_ONLY(assert_valid();)
234 return ersd;