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 bin_search_tree_/constructors_destructor_fn_imps.hpp
38 * Contains an implementation class for bin_search_tree_.
42 typename PB_DS_CLASS_C_DEC::node_allocator
43 PB_DS_CLASS_C_DEC::s_node_allocator;
47 PB_DS_BIN_TREE_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
50 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
55 PB_DS_BIN_TREE_NAME(const Cmp_Fn& r_cmp_fn) :
56 Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0)
59 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
64 PB_DS_BIN_TREE_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
66 node_update(r_node_update),
67 m_p_head(s_node_allocator.allocate(1)),
71 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
76 PB_DS_BIN_TREE_NAME(const PB_DS_CLASS_C_DEC& other) :
80 #ifdef PB_DS_TREE_TRACE
81 PB_DS_TREE_TRACE_BASE_C_DEC(other),
85 m_p_head(s_node_allocator.allocate(1)),
89 m_size = other.m_size;
90 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
94 m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
95 if (m_p_head->m_p_parent != 0)
96 m_p_head->m_p_parent->m_p_parent = m_p_head;
97 m_size = other.m_size;
102 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
103 s_node_allocator.deallocate(m_p_head, 1);
104 __throw_exception_again;
106 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
112 swap(PB_DS_CLASS_C_DEC& other)
114 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
115 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
117 std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
118 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
119 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
125 value_swap(PB_DS_CLASS_C_DEC& other)
127 _GLIBCXX_DEBUG_ONLY(debug_base::swap(other);)
128 std::swap(m_p_head, other.m_p_head);
129 std::swap(m_size, other.m_size);
134 ~PB_DS_BIN_TREE_NAME()
137 s_node_allocator.deallocate(m_p_head, 1);
145 m_p_head->m_p_parent = 0;
146 m_p_head->m_p_left = m_p_head;
147 m_p_head->m_p_right = m_p_head;
152 typename PB_DS_CLASS_C_DEC::node_pointer
154 recursive_copy_node(const node_pointer p_nd)
159 node_pointer p_ret = s_node_allocator.allocate(1);
162 new (p_ret) node(*p_nd);
166 s_node_allocator.deallocate(p_ret, 1);
167 __throw_exception_again;
170 p_ret->m_p_left = p_ret->m_p_right = 0;
174 p_ret->m_p_left = recursive_copy_node(p_nd->m_p_left);
175 p_ret->m_p_right = recursive_copy_node(p_nd->m_p_right);
180 __throw_exception_again;
183 if (p_ret->m_p_left != 0)
184 p_ret->m_p_left->m_p_parent = p_ret;
186 if (p_ret->m_p_right != 0)
187 p_ret->m_p_right->m_p_parent = p_ret;
189 PB_DS_ASSERT_NODE_CONSISTENT(p_ret)
198 if (m_p_head->m_p_parent == 0)
200 m_p_head->m_p_left = m_p_head->m_p_right = m_p_head;
205 node_pointer p_min = m_p_head->m_p_parent;
206 while (p_min->m_p_left != 0)
207 p_min = p_min->m_p_left;
208 m_p_head->m_p_left = p_min;
212 node_pointer p_max = m_p_head->m_p_parent;
213 while (p_max->m_p_right != 0)
214 p_max = p_max->m_p_right;
215 m_p_head->m_p_right = p_max;