libstdc++: insert_fn_imps.hpp Source File

libstdc++

left_child_next_sibling_heap_/insert_fn_imps.hpp
Go to the documentation of this file.
1// -*- C++ -*-
2
3// Copyright (C) 2005-2026 Free Software Foundation, Inc.
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
8// Foundation; either version 3, or (at your option) any later
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
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.
19
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/>.
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/**
37 * @file left_child_next_sibling_heap_/insert_fn_imps.hpp
38 * Contains an implementation class for left_child_next_sibling_heap_.
39 */
40
41#ifdef PB_DS_CLASS_C_DEC
42
43PB_DS_CLASS_T_DEC
44inline typename PB_DS_CLASS_C_DEC::node_pointer
45PB_DS_CLASS_C_DEC::
46get_new_node_for_insert(const_reference r_val)
47{
48 return get_new_node_for_insert(r_val, s_no_throw_copies_ind);
49}
50
51PB_DS_CLASS_T_DEC
52inline typename PB_DS_CLASS_C_DEC::node_pointer
53PB_DS_CLASS_C_DEC::
54get_new_node_for_insert(const_reference r_val, false_type)
55{
56 node_pointer p_new_nd = s_node_allocator.allocate(1);
57
58 cond_dealtor_t cond(p_new_nd);
59
60 new (const_cast<void* >(
61 static_cast<const void* >(&p_new_nd->m_value)))
62 typename node::value_type(r_val);
63
64 cond.set_no_action();
65
66 ++m_size;
67
68 return (p_new_nd);
69}
70
71PB_DS_CLASS_T_DEC
72inline typename PB_DS_CLASS_C_DEC::node_pointer
73PB_DS_CLASS_C_DEC::
74get_new_node_for_insert(const_reference r_val, true_type)
75{
76 node_pointer p_new_nd = s_node_allocator.allocate(1);
77
78 new (const_cast<void* >(
79 static_cast<const void* >(&p_new_nd->m_value)))
80 typename node::value_type(r_val);
81
82 ++m_size;
83
84 return (p_new_nd);
85}
86
87PB_DS_CLASS_T_DEC
88inline void
89PB_DS_CLASS_C_DEC::
90make_child_of(node_pointer p_nd, node_pointer p_new_parent)
91{
92 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
93 _GLIBCXX_DEBUG_ASSERT(p_new_parent != 0);
94
95 p_nd->m_p_next_sibling = p_new_parent->m_p_l_child;
96
97 if (p_new_parent->m_p_l_child != 0)
98 p_new_parent->m_p_l_child->m_p_prev_or_parent = p_nd;
99
100 p_nd->m_p_prev_or_parent = p_new_parent;
101
102 p_new_parent->m_p_l_child = p_nd;
103}
104
105PB_DS_CLASS_T_DEC
106inline typename PB_DS_CLASS_C_DEC::node_pointer
107PB_DS_CLASS_C_DEC::
108parent(node_pointer p_nd)
109{
110 while (true)
111 {
112 node_pointer p_pot = p_nd->m_p_prev_or_parent;
113
114 if (p_pot == 0 || p_pot->m_p_l_child == p_nd)
115 return p_pot;
116
117 p_nd = p_pot;
118 }
119}
120
121PB_DS_CLASS_T_DEC
122inline void
123PB_DS_CLASS_C_DEC::
124swap_with_parent(node_pointer p_nd, node_pointer p_parent)
125{
126 if (p_parent == m_p_root)
127 m_p_root = p_nd;
128
129 _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
130 _GLIBCXX_DEBUG_ASSERT(p_parent != 0);
131 _GLIBCXX_DEBUG_ASSERT(parent(p_nd) == p_parent);
132
133 const bool nd_direct_child = p_parent->m_p_l_child == p_nd;
134 const bool parent_root = p_parent->m_p_prev_or_parent == 0;
135 const bool parent_direct_child =
136 !parent_root&& p_parent->m_p_prev_or_parent->m_p_l_child == p_parent;
137
138 std::swap(p_parent->m_p_prev_or_parent, p_nd->m_p_prev_or_parent);
139 std::swap(p_parent->m_p_next_sibling, p_nd->m_p_next_sibling);
140 std::swap(p_parent->m_p_l_child, p_nd->m_p_l_child);
141 std::swap(p_parent->m_metadata, p_nd->m_metadata);
142
143 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child != 0);
144 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent != 0);
145
146 if (p_nd->m_p_next_sibling != 0)
147 p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd;
148
149 if (p_parent->m_p_next_sibling != 0)
150 p_parent->m_p_next_sibling->m_p_prev_or_parent = p_parent;
151
152 if (p_parent->m_p_l_child != 0)
153 p_parent->m_p_l_child->m_p_prev_or_parent = p_parent;
154
155 if (parent_direct_child)
156 p_nd->m_p_prev_or_parent->m_p_l_child = p_nd;
157 else if (!parent_root)
158 p_nd->m_p_prev_or_parent->m_p_next_sibling = p_nd;
159
160 if (!nd_direct_child)
161 {
162 p_nd->m_p_l_child->m_p_prev_or_parent = p_nd;
163
164 p_parent->m_p_prev_or_parent->m_p_next_sibling = p_parent;
165 }
166 else
167 {
168 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child == p_nd);
169 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent == p_parent);
170
171 p_nd->m_p_l_child = p_parent;
172 p_parent->m_p_prev_or_parent = p_nd;
173 }
174
175 _GLIBCXX_DEBUG_ASSERT(parent(p_parent) == p_nd);
176}
177
178#endif