3 // Copyright (C) 2005-2018 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 pairing_heap_/erase_fn_imps.hpp
38 * Contains an implementation class for a pairing heap.
46 PB_DS_ASSERT_VALID((*this))
47 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
49 node_pointer p_new_root
= join_node_children(base_type::m_p_root
);
50 PB_DS_ASSERT_NODE_CONSISTENT(p_new_root
, false)
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 PB_DS_ASSERT_VALID((*this))
62 erase(point_iterator it
)
64 PB_DS_ASSERT_VALID((*this))
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 PB_DS_ASSERT_VALID((*this))
74 remove_node(node_pointer p_nd
)
76 PB_DS_ASSERT_VALID((*this))
77 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
78 node_pointer p_new_child
= join_node_children(p_nd
);
80 PB_DS_ASSERT_NODE_CONSISTENT(p_new_child
, false)
82 if (p_nd
== base_type::m_p_root
)
85 p_new_child
->m_p_prev_or_parent
= 0;
86 base_type::m_p_root
= p_new_child
;
87 PB_DS_ASSERT_NODE_CONSISTENT(base_type::m_p_root
, false)
91 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_p_prev_or_parent
!= 0);
92 if (p_nd
->m_p_prev_or_parent
->m_p_l_child
== p_nd
)
96 p_new_child
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
97 p_new_child
->m_p_next_sibling
= p_nd
->m_p_next_sibling
;
98 if (p_new_child
->m_p_next_sibling
!= 0)
99 p_new_child
->m_p_next_sibling
->m_p_prev_or_parent
= p_new_child
;
100 p_nd
->m_p_prev_or_parent
->m_p_l_child
= p_new_child
;
101 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
->m_p_prev_or_parent
, false)
105 p_nd
->m_p_prev_or_parent
->m_p_l_child
= p_nd
->m_p_next_sibling
;
106 if (p_nd
->m_p_next_sibling
!= 0)
107 p_nd
->m_p_next_sibling
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
108 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
->m_p_prev_or_parent
, false)
112 if (p_new_child
!= 0)
114 p_new_child
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
115 p_new_child
->m_p_next_sibling
= p_nd
->m_p_next_sibling
;
116 if (p_new_child
->m_p_next_sibling
!= 0)
117 p_new_child
->m_p_next_sibling
->m_p_prev_or_parent
= p_new_child
;
118 p_new_child
->m_p_prev_or_parent
->m_p_next_sibling
= p_new_child
;
119 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
->m_p_prev_or_parent
, false)
123 p_nd
->m_p_prev_or_parent
->m_p_next_sibling
= p_nd
->m_p_next_sibling
;
124 if (p_nd
->m_p_next_sibling
!= 0)
125 p_nd
->m_p_next_sibling
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
126 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
->m_p_prev_or_parent
, false)
130 typename
PB_DS_CLASS_C_DEC::node_pointer
132 join_node_children(node_pointer p_nd
)
134 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
135 node_pointer p_ret
= p_nd
->m_p_l_child
;
138 while (p_ret
->m_p_next_sibling
!= 0)
139 p_ret
= forward_join(p_ret
, p_ret
->m_p_next_sibling
);
140 while (p_ret
->m_p_prev_or_parent
!= p_nd
)
141 p_ret
= back_join(p_ret
->m_p_prev_or_parent
, p_ret
);
142 PB_DS_ASSERT_NODE_CONSISTENT(p_ret
, false)
147 typename
PB_DS_CLASS_C_DEC::node_pointer
149 forward_join(node_pointer p_nd
, node_pointer p_next
)
151 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
152 _GLIBCXX_DEBUG_ASSERT(p_nd
->m_p_next_sibling
== p_next
);
153 if (Cmp_Fn::operator()(p_nd
->m_value
, p_next
->m_value
))
155 p_next
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
156 base_type::make_child_of(p_nd
, p_next
);
157 return p_next
->m_p_next_sibling
== 0
158 ? p_next
: p_next
->m_p_next_sibling
;
161 if (p_next
->m_p_next_sibling
!= 0)
163 p_next
->m_p_next_sibling
->m_p_prev_or_parent
= p_nd
;
164 p_nd
->m_p_next_sibling
= p_next
->m_p_next_sibling
;
165 base_type::make_child_of(p_next
, p_nd
);
166 return p_nd
->m_p_next_sibling
;
169 p_nd
->m_p_next_sibling
= 0;
170 base_type::make_child_of(p_next
, p_nd
);
171 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
, false)
176 typename
PB_DS_CLASS_C_DEC::node_pointer
178 back_join(node_pointer p_nd
, node_pointer p_next
)
180 _GLIBCXX_DEBUG_ASSERT(p_nd
!= 0);
181 _GLIBCXX_DEBUG_ASSERT(p_next
->m_p_next_sibling
== 0);
183 if (Cmp_Fn::operator()(p_nd
->m_value
, p_next
->m_value
))
185 p_next
->m_p_prev_or_parent
= p_nd
->m_p_prev_or_parent
;
186 base_type::make_child_of(p_nd
, p_next
);
187 PB_DS_ASSERT_NODE_CONSISTENT(p_next
, false)
191 p_nd
->m_p_next_sibling
= 0;
192 base_type::make_child_of(p_next
, p_nd
);
193 PB_DS_ASSERT_NODE_CONSISTENT(p_nd
, false)
198 template<typename Pred
>
199 typename
PB_DS_CLASS_C_DEC::size_type
203 PB_DS_ASSERT_VALID((*this))
204 if (base_type::empty())
206 PB_DS_ASSERT_VALID((*this))
209 base_type::to_linked_list();
210 node_pointer p_out
= base_type::prune(pred
);
215 node_pointer p_next
= p_out
->m_p_next_sibling
;
216 base_type::actual_erase_node(p_out
);
220 node_pointer p_cur
= base_type::m_p_root
;
221 base_type::m_p_root
= 0;
224 node_pointer p_next
= p_cur
->m_p_next_sibling
;
225 p_cur
->m_p_l_child
= p_cur
->m_p_next_sibling
= p_cur
->m_p_prev_or_parent
= 0;
230 PB_DS_ASSERT_VALID((*this))