]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/thin_heap_/erase_fn_imps.hpp
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / thin_heap_ / erase_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
5624e564 3// Copyright (C) 2005-2015 Free Software Foundation, Inc.
4569a895
AT
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
748086b7 8// Foundation; either version 3, or (at your option) any later
4569a895
AT
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
748086b7
JJ
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.
4569a895 19
748086b7
JJ
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/>.
4569a895
AT
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/**
a345e45d 37 * @file thin_heap_/erase_fn_imps.hpp
4569a895
AT
38 * Contains an implementation for thin_heap_.
39 */
40
41PB_DS_CLASS_T_DEC
42void
43PB_DS_CLASS_C_DEC::
44pop()
45{
f5886803
FD
46 PB_DS_ASSERT_VALID((*this))
47 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
8fc81078 48 _GLIBCXX_DEBUG_ASSERT(m_p_max != 0);
4569a895
AT
49
50 node_pointer p_nd = m_p_max;
4569a895 51 remove_max_node();
4569a895 52 base_type::actual_erase_node(p_nd);
f5886803
FD
53 PB_DS_ASSERT_VALID((*this))
54}
4569a895
AT
55
56PB_DS_CLASS_T_DEC
57inline void
58PB_DS_CLASS_C_DEC::
59remove_max_node()
60{
61 to_aux_except_max();
4569a895
AT
62 make_from_aux();
63}
64
65PB_DS_CLASS_T_DEC
66void
67PB_DS_CLASS_C_DEC::
68to_aux_except_max()
69{
70 node_pointer p_add = base_type::m_p_root;
4569a895
AT
71 while (p_add != m_p_max)
72 {
73 node_pointer p_next_add = p_add->m_p_next_sibling;
4569a895 74 add_to_aux(p_add);
4569a895
AT
75 p_add = p_next_add;
76 }
77
78 p_add = m_p_max->m_p_l_child;
8fc81078 79 while (p_add != 0)
4569a895
AT
80 {
81 node_pointer p_next_add = p_add->m_p_next_sibling;
a345e45d
BK
82 p_add->m_metadata = p_add->m_p_l_child == 0 ?
83 0 : p_add->m_p_l_child->m_metadata + 1;
4569a895
AT
84
85 add_to_aux(p_add);
4569a895
AT
86 p_add = p_next_add;
87 }
88
89 p_add = m_p_max->m_p_next_sibling;
8fc81078 90 while (p_add != 0)
4569a895
AT
91 {
92 node_pointer p_next_add = p_add->m_p_next_sibling;
4569a895 93 add_to_aux(p_add);
4569a895
AT
94 p_add = p_next_add;
95 }
96}
97
98PB_DS_CLASS_T_DEC
99inline void
100PB_DS_CLASS_C_DEC::
101add_to_aux(node_pointer p_nd)
102{
103 size_type r = p_nd->m_metadata;
8fc81078 104 while (m_a_aux[r] != 0)
4569a895 105 {
47bea7b8 106 _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata < rank_bound());
4569a895
AT
107 if (Cmp_Fn::operator()(m_a_aux[r]->m_value, p_nd->m_value))
108 make_child_of(m_a_aux[r], p_nd);
109 else
a345e45d 110 {
4569a895 111 make_child_of(p_nd, m_a_aux[r]);
4569a895 112 p_nd = m_a_aux[r];
a345e45d 113 }
4569a895 114
8fc81078 115 m_a_aux[r] = 0;
4569a895
AT
116 ++r;
117 }
118
47bea7b8 119 _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata < rank_bound());
4569a895
AT
120
121 m_a_aux[r] = p_nd;
122}
123
124PB_DS_CLASS_T_DEC
125inline void
126PB_DS_CLASS_C_DEC::
127make_child_of(node_pointer p_nd, node_pointer p_new_parent)
128{
47bea7b8
BK
129 _GLIBCXX_DEBUG_ASSERT(p_nd->m_metadata == p_new_parent->m_metadata);
130 _GLIBCXX_DEBUG_ASSERT(m_a_aux[p_nd->m_metadata] == p_nd ||
4569a895
AT
131 m_a_aux[p_nd->m_metadata] == p_new_parent);
132
133 ++p_new_parent->m_metadata;
4569a895
AT
134 base_type::make_child_of(p_nd, p_new_parent);
135}
136
137PB_DS_CLASS_T_DEC
138inline void
139PB_DS_CLASS_C_DEC::
140make_from_aux()
141{
8fc81078 142 base_type::m_p_root = m_p_max = 0;
4569a895 143 const size_type rnk_bnd = rank_bound();
4569a895 144 size_type i = 0;
4569a895
AT
145 while (i < rnk_bnd)
146 {
8fc81078 147 if (m_a_aux[i] != 0)
a345e45d 148 {
4569a895 149 make_root_and_link(m_a_aux[i]);
8fc81078 150 m_a_aux[i] = 0;
a345e45d 151 }
4569a895
AT
152 ++i;
153 }
154
f5886803
FD
155 PB_DS_ASSERT_AUX_NULL((*this))
156}
4569a895
AT
157
158PB_DS_CLASS_T_DEC
159inline void
160PB_DS_CLASS_C_DEC::
161remove_node(node_pointer p_nd)
162{
163 node_pointer p_parent = p_nd;
8fc81078 164 while (base_type::parent(p_parent) != 0)
4569a895
AT
165 p_parent = base_type::parent(p_parent);
166
167 base_type::bubble_to_top(p_nd);
4569a895
AT
168 m_p_max = p_nd;
169
170 node_pointer p_fix = base_type::m_p_root;
8fc81078 171 while (p_fix != 0&& p_fix->m_p_next_sibling != p_parent)
4569a895
AT
172 p_fix = p_fix->m_p_next_sibling;
173
8fc81078 174 if (p_fix != 0)
4569a895
AT
175 p_fix->m_p_next_sibling = p_nd;
176
177 remove_max_node();
178}
179
180PB_DS_CLASS_T_DEC
181inline void
182PB_DS_CLASS_C_DEC::
183clear()
184{
185 base_type::clear();
8fc81078 186 m_p_max = 0;
4569a895
AT
187}
188
189PB_DS_CLASS_T_DEC
190void
191PB_DS_CLASS_C_DEC::
192erase(point_iterator it)
193{
f5886803
FD
194 PB_DS_ASSERT_VALID((*this))
195 _GLIBCXX_DEBUG_ASSERT(!base_type::empty());
4569a895
AT
196
197 node_pointer p_nd = it.m_p_nd;
4569a895 198 remove_node(p_nd);
4569a895 199 base_type::actual_erase_node(p_nd);
f5886803
FD
200 PB_DS_ASSERT_VALID((*this))
201}
4569a895
AT
202
203PB_DS_CLASS_T_DEC
204template<typename Pred>
205typename PB_DS_CLASS_C_DEC::size_type
206PB_DS_CLASS_C_DEC::
207erase_if(Pred pred)
208{
f5886803 209 PB_DS_ASSERT_VALID((*this))
f5886803
FD
210 if (base_type::empty())
211 {
212 PB_DS_ASSERT_VALID((*this))
f5886803
FD
213 return 0;
214 }
4569a895
AT
215
216 base_type::to_linked_list();
4569a895 217 node_pointer p_out = base_type::prune(pred);
4569a895 218 size_type ersd = 0;
8fc81078 219 while (p_out != 0)
4569a895
AT
220 {
221 ++ersd;
4569a895 222 node_pointer p_next = p_out->m_p_next_sibling;
4569a895 223 base_type::actual_erase_node(p_out);
4569a895
AT
224 p_out = p_next;
225 }
226
227 node_pointer p_cur = base_type::m_p_root;
8fc81078 228 m_p_max = base_type::m_p_root = 0;
8fc81078 229 while (p_cur != 0)
4569a895
AT
230 {
231 node_pointer p_next = p_cur->m_p_next_sibling;
4569a895 232 make_root_and_link(p_cur);
4569a895
AT
233 p_cur = p_next;
234 }
235
f5886803 236 PB_DS_ASSERT_VALID((*this))
f5886803 237 return ersd;
4569a895
AT
238}
239
240PB_DS_CLASS_T_DEC
241inline typename PB_DS_CLASS_C_DEC::size_type
242PB_DS_CLASS_C_DEC::
243rank_bound()
244{
f5886803
FD
245 using namespace std;
246 const size_t* const p_upper =
e330aa5b
PC
247 std::upper_bound(g_a_rank_bounds,
248 g_a_rank_bounds + num_distinct_rank_bounds,
249 base_type::m_size);
4569a895
AT
250
251 if (p_upper == g_a_rank_bounds + num_distinct_rank_bounds)
252 return max_rank;
253
254 return (p_upper - g_a_rank_bounds);
255}