3 // Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
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
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
37 * @file erase_fn_imps.hpp
38 * Contains an implementation class for a pairing heap.
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
!= NULL
)
52 p_new_root
->m_p_prev_or_parent
= NULL
;
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();)
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();)
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
);
81 if (p_new_child
!= NULL
)
82 base_type::assert_node_consistent(p_new_child
, false);
85 if (p_nd
== base_type::m_p_root
)
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);)
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
)
97 if (p_new_child
!= NULL
)
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);)
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);)
115 if (p_new_child
!= NULL
)
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);)
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);)
133 typename
PB_DS_CLASS_C_DEC::node_pointer
135 join_node_children(node_pointer p_nd
)
137 _GLIBCXX_DEBUG_ASSERT(p_nd
!= NULL
);
138 node_pointer p_ret
= p_nd
->m_p_l_child
;
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);)
150 typename
PB_DS_CLASS_C_DEC::node_pointer
152 forward_join(node_pointer p_nd
, node_pointer p_next
)
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
))
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
;
164 if (p_next
->m_p_next_sibling
!= NULL
)
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
= NULL
;
173 base_type::make_child_of(p_next
, p_nd
);
174 _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(p_nd
, false));
179 typename
PB_DS_CLASS_C_DEC::node_pointer
181 back_join(node_pointer p_nd
, node_pointer p_next
)
183 _GLIBCXX_DEBUG_ASSERT(p_nd
!= NULL
);
184 _GLIBCXX_DEBUG_ASSERT(p_next
->m_p_next_sibling
== NULL
);
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));
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));
201 template<typename Pred
>
202 typename
PB_DS_CLASS_C_DEC::size_type
206 _GLIBCXX_DEBUG_ONLY(assert_valid();)
207 if (base_type::empty())
209 _GLIBCXX_DEBUG_ONLY(assert_valid();)
212 base_type::to_linked_list();
213 node_pointer p_out
= base_type::prune(pred
);
215 while (p_out
!= NULL
)
218 node_pointer p_next
= p_out
->m_p_next_sibling
;
219 base_type::actual_erase_node(p_out
);
223 node_pointer p_cur
= base_type::m_p_root
;
224 base_type::m_p_root
= NULL
;
225 while (p_cur
!= NULL
)
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
;
233 _GLIBCXX_DEBUG_ONLY(assert_valid();)