3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the terms
8 // of the GNU General Public License as published by the Free Software
9 // Foundation; either version 3, or (at your option) any later
12 // This library is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
28 // Permission to use, copy, modify, sell, and distribute this software
29 // is hereby granted without fee, provided that the above copyright
30 // notice appears in all copies, and that both that copyright notice
31 // and this permission notice appear in supporting documentation. None
32 // of the above authors, nor IBM Haifa Research Laboratories, make any
33 // representation about the suitability of this software for any
34 // purpose. It is provided "as is" without express or implied
38 * @file bin_search_tree_/constructors_destructor_fn_imps.hpp
39 * Contains an implementation class for bin_search_tree_.
43 typename PB_DS_CLASS_C_DEC::node_allocator
44 PB_DS_CLASS_C_DEC::s_node_allocator;
48 PB_DS_BIN_TREE_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
51 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
56 PB_DS_BIN_TREE_NAME(const Cmp_Fn& r_cmp_fn) :
57 Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0)
60 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
65 PB_DS_BIN_TREE_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
67 node_update(r_node_update),
68 m_p_head(s_node_allocator.allocate(1)),
72 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
77 PB_DS_BIN_TREE_NAME(const PB_DS_CLASS_C_DEC& other) :
81 #ifdef PB_DS_TREE_TRACE
82 PB_DS_TREE_TRACE_BASE_C_DEC(other),
86 m_p_head(s_node_allocator.allocate(1)),
90 m_size = other.m_size;
91 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
95 m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
96 if (m_p_head->m_p_parent != 0)
97 m_p_head->m_p_parent->m_p_parent = m_p_head;
98 m_size = other.m_size;
103 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
104 s_node_allocator.deallocate(m_p_head, 1);
105 __throw_exception_again;
107 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
113 swap(PB_DS_CLASS_C_DEC& other)
115 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
116 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
118 std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
119 PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
120 PB_DS_STRUCT_ONLY_ASSERT_VALID(other)
126 value_swap(PB_DS_CLASS_C_DEC& other)
128 _GLIBCXX_DEBUG_ONLY(debug_base::swap(other);)
129 std::swap(m_p_head, other.m_p_head);
130 std::swap(m_size, other.m_size);
135 ~PB_DS_BIN_TREE_NAME()
138 s_node_allocator.deallocate(m_p_head, 1);
146 m_p_head->m_p_parent = 0;
147 m_p_head->m_p_left = m_p_head;
148 m_p_head->m_p_right = m_p_head;
153 typename PB_DS_CLASS_C_DEC::node_pointer
155 recursive_copy_node(const node_pointer p_nd)
160 node_pointer p_ret = s_node_allocator.allocate(1);
163 new (p_ret) node(*p_nd);
167 s_node_allocator.deallocate(p_ret, 1);
168 __throw_exception_again;
171 p_ret->m_p_left = p_ret->m_p_right = 0;
175 p_ret->m_p_left = recursive_copy_node(p_nd->m_p_left);
176 p_ret->m_p_right = recursive_copy_node(p_nd->m_p_right);
181 __throw_exception_again;
184 if (p_ret->m_p_left != 0)
185 p_ret->m_p_left->m_p_parent = p_ret;
187 if (p_ret->m_p_right != 0)
188 p_ret->m_p_right->m_p_parent = p_ret;
190 PB_DS_ASSERT_NODE_CONSISTENT(p_ret)
199 if (m_p_head->m_p_parent == 0)
201 m_p_head->m_p_left = m_p_head->m_p_right = m_p_head;
206 node_pointer p_min = m_p_head->m_p_parent;
207 while (p_min->m_p_left != 0)
208 p_min = p_min->m_p_left;
209 m_p_head->m_p_left = p_min;
213 node_pointer p_max = m_p_head->m_p_parent;
214 while (p_max->m_p_right != 0)
215 p_max = p_max->m_p_right;
216 m_p_head->m_p_right = p_max;