]> 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
36fed23c 1// -*- C++ -*-
2
fbd26352 3// Copyright (C) 2005-2019 Free Software Foundation, Inc.
36fed23c 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
6bc9506f 8// Foundation; either version 3, or (at your option) any later
36fed23c 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
6bc9506f 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.
36fed23c 19
6bc9506f 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/>.
36fed23c 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/**
4f4a327e 37 * @file thin_heap_/insert_fn_imps.hpp
36fed23c 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{
2548203e 46 PB_DS_ASSERT_VALID((*this))
2548203e 47 node_pointer p_nd = base_type::get_new_node_for_insert(r_val);
36fed23c 48 p_nd->m_metadata = 0;
e4bb1925 49 p_nd->m_p_prev_or_parent = p_nd->m_p_l_child = 0;
e4bb1925 50 if (base_type::m_p_root == 0)
36fed23c 51 {
e4bb1925 52 p_nd->m_p_next_sibling = 0;
36fed23c 53 m_p_max = base_type::m_p_root = p_nd;
2548203e 54 PB_DS_ASSERT_VALID((*this))
2548203e 55 return point_iterator(p_nd);
36fed23c 56 }
57
58 p_nd->m_p_next_sibling = base_type::m_p_root;
e4bb1925 59 base_type::m_p_root->m_p_prev_or_parent = 0;
36fed23c 60 base_type::m_p_root = p_nd;
36fed23c 61 update_max(p_nd);
2548203e 62 PB_DS_ASSERT_VALID((*this))
2548203e 63 return point_iterator(p_nd);
36fed23c 64}
65
66PB_DS_CLASS_T_DEC
67inline void
68PB_DS_CLASS_C_DEC::
69make_root(node_pointer p_nd)
70{
4f4a327e 71 p_nd->m_metadata = p_nd->m_p_l_child == 0
72 ? 0 : 1 + p_nd->m_p_l_child->m_metadata;
36fed23c 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);
e4bb1925 81 p_nd->m_p_prev_or_parent = 0;
36fed23c 82 p_nd->m_p_next_sibling = base_type::m_p_root;
e4bb1925 83 if (base_type::m_p_root != 0)
84 base_type::m_p_root->m_p_prev_or_parent = 0;
36fed23c 85
86 base_type::m_p_root = p_nd;
36fed23c 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 {
e4bb1925 97 if (p_y->m_p_prev_or_parent == 0)
36fed23c 98 {
99 fix_root(p_y);
36fed23c 100 return;
101 }
e4bb1925 102 else if (p_y->m_metadata == 1&& p_y->m_p_next_sibling == 0)
36fed23c 103 {
e4bb1925 104 if (p_y->m_p_l_child != 0)
36fed23c 105 {
106 fix_sibling_rank_1_unmarked(p_y);
36fed23c 107 return;
108 }
109
110 fix_sibling_rank_1_marked(p_y);
36fed23c 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 {
e4bb1925 115 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_l_child != 0);
36fed23c 116 if (p_y->m_metadata != p_y->m_p_l_child->m_metadata + 2)
117 {
118 fix_sibling_general_unmarked(p_y);
36fed23c 119 return;
120 }
121
122 fix_sibling_general_marked(p_y);
36fed23c 123 p_y = p_y->m_p_prev_or_parent;
124 }
e4bb1925 125 else if ((p_y->m_p_l_child == 0&&
126 p_y->m_metadata == 2) ||(p_y->m_p_l_child != 0&&
36fed23c 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;
36fed23c 130 fix_child(p_y);
36fed23c 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{
e4bb1925 143 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent == 0);
36fed23c 144 make_root(p_y);
2548203e 145 PB_DS_ASSERT_NODE_CONSISTENT(p_y, true)
146}
36fed23c 147
148PB_DS_CLASS_T_DEC
149inline void
150PB_DS_CLASS_C_DEC::
151fix_sibling_rank_1_unmarked(node_pointer p_y)
152{
e4bb1925 153 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
36fed23c 154
2b31bec4 155 _GLIBCXX_DEBUG_ONLY(node_pointer p_w = p_y->m_p_l_child;)
2548203e 156 _GLIBCXX_DEBUG_ASSERT(p_w != 0);
e4bb1925 157 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling == 0);
158 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_next_sibling == 0);
36fed23c 159
160 p_y->m_p_next_sibling = p_y->m_p_l_child;
36fed23c 161 p_y->m_p_next_sibling->m_p_prev_or_parent = p_y;
e4bb1925 162 p_y->m_p_l_child = 0;
2548203e 163 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
164}
36fed23c 165
166PB_DS_CLASS_T_DEC
167inline void
168PB_DS_CLASS_C_DEC::
169fix_sibling_rank_1_marked(node_pointer p_y)
170{
e4bb1925 171 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
172 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_l_child == 0);
36fed23c 173 p_y->m_metadata = 0;
2548203e 174 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
175}
36fed23c 176
177PB_DS_CLASS_T_DEC
178inline void
179PB_DS_CLASS_C_DEC::
180fix_sibling_general_unmarked(node_pointer p_y)
181{
e4bb1925 182 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
36fed23c 183
184 node_pointer p_w = p_y->m_p_l_child;
e4bb1925 185 _GLIBCXX_DEBUG_ASSERT(p_w != 0);
186 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling != 0);
36fed23c 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;
e4bb1925 192 _GLIBCXX_DEBUG_ASSERT(p_w->m_p_next_sibling != 0);
36fed23c 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
2548203e 198 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
199}
36fed23c 200
201PB_DS_CLASS_T_DEC
202inline void
203PB_DS_CLASS_C_DEC::
204fix_sibling_general_marked(node_pointer p_y)
205{
e4bb1925 206 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
36fed23c 207 --p_y->m_metadata;
2548203e 208 PB_DS_ASSERT_NODE_CONSISTENT(p_y, false)
209}
36fed23c 210
211PB_DS_CLASS_T_DEC
212inline void
213PB_DS_CLASS_C_DEC::
214fix_child(node_pointer p_y)
215{
e4bb1925 216 _GLIBCXX_DEBUG_ASSERT(p_y->m_p_prev_or_parent != 0);
36fed23c 217
e4bb1925 218 if (p_y->m_p_next_sibling != 0)
36fed23c 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{
2548203e 234 PB_DS_ASSERT_VALID((*this))
4f4a327e 235 node_pointer p_nd = it.m_p_nd;
e4bb1925 236 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
36fed23c 237
238 const bool smaller = Cmp_Fn::operator()(r_new_val, p_nd->m_value);
36fed23c 239 p_nd->m_value = r_new_val;
36fed23c 240 if (smaller)
241 {
242 remove_node(p_nd);
e4bb1925 243 p_nd->m_p_l_child = 0;
36fed23c 244 make_root_and_link(p_nd);
2548203e 245 PB_DS_ASSERT_VALID((*this))
2548203e 246 return;
36fed23c 247 }
248
e4bb1925 249 if (p_nd->m_p_prev_or_parent == 0)
36fed23c 250 {
251 update_max(p_nd);
2548203e 252 PB_DS_ASSERT_VALID((*this))
2548203e 253 return;
36fed23c 254 }
255
256 node_pointer p_y = p_nd->m_p_prev_or_parent;
e4bb1925 257 _GLIBCXX_DEBUG_ASSERT(p_y != 0);
36fed23c 258
e4bb1925 259 if (p_nd->m_p_next_sibling != 0)
36fed23c 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);
36fed23c 268 make_root_and_link(p_nd);
2548203e 269 PB_DS_ASSERT_VALID((*this))
270}
36fed23c 271
272PB_DS_CLASS_T_DEC
273inline void
274PB_DS_CLASS_C_DEC::
275update_max(node_pointer p_nd)
276{
e4bb1925 277 if (m_p_max == 0 || Cmp_Fn::operator()(m_p_max->m_value, p_nd->m_value))
36fed23c 278 m_p_max = p_nd;
279}
280