]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/ext/pb_ds/detail/thin_heap_/insert_fn_imps.hpp
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / thin_heap_ / insert_fn_imps.hpp
CommitLineData
4569a895
AT
1// -*- C++ -*-
2
818ab71a 3// Copyright (C) 2005-2016 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_/insert_fn_imps.hpp
4569a895
AT
38 * Contains an implementation for thin_heap_.
39 */
40
41PB_DS_CLASS_T_DEC
42inline typename PB_DS_CLASS_C_DEC::point_iterator
43PB_DS_CLASS_C_DEC::
44push(const_reference r_val)
45{
f5886803 46 PB_DS_ASSERT_VALID((*this))
f5886803 47 node_pointer p_nd = base_type::get_new_node_for_insert(r_val);
4569a895 48 p_nd->m_metadata = 0;
8fc81078 49 p_nd->m_p_prev_or_parent = p_nd->m_p_l_child = 0;
8fc81078 50 if (base_type::m_p_root == 0)
4569a895 51 {
8fc81078 52 p_nd->m_p_next_sibling = 0;
4569a895 53 m_p_max = base_type::m_p_root = p_nd;
f5886803 54 PB_DS_ASSERT_VALID((*this))
f5886803 55 return point_iterator(p_nd);
4569a895
AT
56 }
57
58 p_nd->m_p_next_sibling = base_type::m_p_root;
8fc81078 59 base_type::m_p_root->m_p_prev_or_parent = 0;
4569a895 60 base_type::m_p_root = p_nd;
4569a895 61 update_max(p_nd);
f5886803 62 PB_DS_ASSERT_VALID((*this))
f5886803 63 return point_iterator(p_nd);
4569a895
AT
64}
65
66PB_DS_CLASS_T_DEC
67inline void
68PB_DS_CLASS_C_DEC::
69make_root(node_pointer p_nd)
70{
a345e45d
BK
71 p_nd->m_metadata = p_nd->m_p_l_child == 0
72 ? 0 : 1 + p_nd->m_p_l_child->m_metadata;
4569a895
AT
73}
74
75PB_DS_CLASS_T_DEC
76inline void
77PB_DS_CLASS_C_DEC::
78make_root_and_link(node_pointer p_nd)
79{
80 make_root(p_nd);
8fc81078 81 p_nd->m_p_prev_or_parent = 0;
4569a895 82 p_nd->m_p_next_sibling = base_type::m_p_root;
8fc81078
PC
83 if (base_type::m_p_root != 0)
84 base_type::m_p_root->m_p_prev_or_parent = 0;
4569a895
AT
85
86 base_type::m_p_root = p_nd;
4569a895
AT
87 update_max(p_nd);
88}
89
90PB_DS_CLASS_T_DEC
91inline void
92PB_DS_CLASS_C_DEC::
93fix(node_pointer p_y)
94{
95 while (true)
96 {
8fc81078 97 if (p_y->m_p_prev_or_parent == 0)
4569a895
AT
98 {
99 fix_root(p_y);
4569a895
AT
100 return;
101 }
8fc81078 102 else if (p_y->m_metadata == 1&& p_y->m_p_next_sibling == 0)
4569a895 103 {
8fc81078 104 if (p_y->m_p_l_child != 0)
4569a895
AT
105 {
106 fix_sibling_rank_1_unmarked(p_y);
4569a895
AT
107 return;
108 }
109
110 fix_sibling_rank_1_marked(p_y);
4569a895
AT
111 p_y = p_y->m_p_prev_or_parent;
112 }
113 else if (p_y->m_metadata > p_y->m_p_next_sibling->m_metadata + 1)
114 {
8fc81078 115 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_l_child != 0);
4569a895
AT
116 if (p_y->m_metadata != p_y->m_p_l_child->m_metadata + 2)
117 {
118 fix_sibling_general_unmarked(p_y);
4569a895
AT
119 return;
120 }
121
122 fix_sibling_general_marked(p_y);
4569a895
AT
123 p_y = p_y->m_p_prev_or_parent;
124 }
8fc81078
PC
125 else if ((p_y->m_p_l_child == 0&&
126 p_y->m_metadata == 2) ||(p_y->m_p_l_child != 0&&
4569a895
AT
127 p_y->m_metadata == p_y->m_p_l_child->m_metadata + 3))
128 {
129 node_pointer p_z = p_y->m_p_prev_or_parent;
4569a895 130 fix_child(p_y);
4569a895
AT
131 p_y = p_z;
132 }
133 else
134 return;
135 }
136}
137
138PB_DS_CLASS_T_DEC
139inline void
140PB_DS_CLASS_C_DEC::
141fix_root(node_pointer p_y)
142{
8fc81078 143 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent == 0);
4569a895 144 make_root(p_y);
f5886803
FD
145 PB_DS_ASSERT_NODE_CONSISTENT(p_y, true)
146}
4569a895
AT
147
148PB_DS_CLASS_T_DEC
149inline void
150PB_DS_CLASS_C_DEC::
151fix_sibling_rank_1_unmarked(node_pointer p_y)
152{
8fc81078 153 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
4569a895 154
47bea7b8 155 _GLIBCXX_DEBUG_ONLY(node_pointer p_w = p_y->m_p_l_child;)
f5886803 156 _GLIBCXX_DEBUG_ASSERT(p_w != 0);
8fc81078
PC
157 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling == 0);
158 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_next_sibling == 0);
4569a895
AT
159
160 p_y->m_p_next_sibling = p_y->m_p_l_child;
4569a895 161 p_y->m_p_next_sibling->m_p_prev_or_parent = p_y;
8fc81078 162 p_y->m_p_l_child = 0;
f5886803
FD
163 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
164}
4569a895
AT
165
166PB_DS_CLASS_T_DEC
167inline void
168PB_DS_CLASS_C_DEC::
169fix_sibling_rank_1_marked(node_pointer p_y)
170{
8fc81078
PC
171 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
172 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_l_child == 0);
4569a895 173 p_y->m_metadata = 0;
f5886803
FD
174 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
175}
4569a895
AT
176
177PB_DS_CLASS_T_DEC
178inline void
179PB_DS_CLASS_C_DEC::
180fix_sibling_general_unmarked(node_pointer p_y)
181{
8fc81078 182 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
4569a895
AT
183
184 node_pointer p_w = p_y->m_p_l_child;
8fc81078
PC
185 _GLIBCXX_DEBUG_ASSERT(p_w != 0);
186 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling != 0);
4569a895
AT
187
188 p_y->m_p_l_child = p_w->m_p_next_sibling;
189 p_w->m_p_next_sibling->m_p_prev_or_parent = p_y;
190
191 p_w->m_p_next_sibling = p_y->m_p_next_sibling;
8fc81078 192 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling != 0);
4569a895
AT
193 p_w->m_p_next_sibling->m_p_prev_or_parent = p_w;
194
195 p_y->m_p_next_sibling = p_w;
196 p_w->m_p_prev_or_parent = p_y;
197
f5886803
FD
198 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
199}
4569a895
AT
200
201PB_DS_CLASS_T_DEC
202inline void
203PB_DS_CLASS_C_DEC::
204fix_sibling_general_marked(node_pointer p_y)
205{
8fc81078 206 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
4569a895 207 --p_y->m_metadata;
f5886803
FD
208 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
209}
4569a895
AT
210
211PB_DS_CLASS_T_DEC
212inline void
213PB_DS_CLASS_C_DEC::
214fix_child(node_pointer p_y)
215{
8fc81078 216 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
4569a895 217
8fc81078 218 if (p_y->m_p_next_sibling != 0)
4569a895
AT
219 p_y->m_p_next_sibling->m_p_prev_or_parent = p_y->m_p_prev_or_parent;
220
221 if (p_y->m_p_prev_or_parent->m_p_l_child == p_y)
222 p_y->m_p_prev_or_parent->m_p_l_child = p_y->m_p_next_sibling;
223 else
224 p_y->m_p_prev_or_parent->m_p_next_sibling = p_y->m_p_next_sibling;
225
226 make_root_and_link(p_y);
227}
228
229PB_DS_CLASS_T_DEC
230void
231PB_DS_CLASS_C_DEC::
232modify(point_iterator it, const_reference r_new_val)
233{
f5886803 234 PB_DS_ASSERT_VALID((*this))
a345e45d 235 node_pointer p_nd = it.m_p_nd;
8fc81078 236 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
4569a895
AT
237
238 const bool smaller = Cmp_Fn::operator()(r_new_val, p_nd->m_value);
4569a895 239 p_nd->m_value = r_new_val;
4569a895
AT
240 if (smaller)
241 {
242 remove_node(p_nd);
8fc81078 243 p_nd->m_p_l_child = 0;
4569a895 244 make_root_and_link(p_nd);
f5886803 245 PB_DS_ASSERT_VALID((*this))
f5886803 246 return;
4569a895
AT
247 }
248
8fc81078 249 if (p_nd->m_p_prev_or_parent == 0)
4569a895
AT
250 {
251 update_max(p_nd);
f5886803 252 PB_DS_ASSERT_VALID((*this))
f5886803 253 return;
4569a895
AT
254 }
255
256 node_pointer p_y = p_nd->m_p_prev_or_parent;
8fc81078 257 _GLIBCXX_DEBUG_ASSERT(p_y != 0);
4569a895 258
8fc81078 259 if (p_nd->m_p_next_sibling != 0)
4569a895
AT
260 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_y;
261
262 if (p_y->m_p_l_child == p_nd)
263 p_y->m_p_l_child = p_nd->m_p_next_sibling;
264 else
265 p_y->m_p_next_sibling = p_nd->m_p_next_sibling;
266
267 fix(p_y);
4569a895 268 make_root_and_link(p_nd);
f5886803
FD
269 PB_DS_ASSERT_VALID((*this))
270}
4569a895
AT
271
272PB_DS_CLASS_T_DEC
273inline void
274PB_DS_CLASS_C_DEC::
275update_max(node_pointer p_nd)
276{
8fc81078 277 if (m_p_max == 0 || Cmp_Fn::operator()(m_p_max->m_value, p_nd->m_value))
4569a895
AT
278 m_p_max = p_nd;
279}
280