3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
43 * @file constructors_destructor_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
48 typename
PB_DS_CLASS_C_DEC::node_allocator
49 PB_DS_CLASS_C_DEC::s_node_allocator
;
53 PB_DS_CLASS_NAME() : m_p_head(s_node_allocator
.allocate(1)), m_size(0)
56 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
61 PB_DS_CLASS_NAME(const Cmp_Fn
& r_cmp_fn
) :
62 Cmp_Fn(r_cmp_fn
), m_p_head(s_node_allocator
.allocate(1)), m_size(0)
65 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
70 PB_DS_CLASS_NAME(const Cmp_Fn
& r_cmp_fn
, const node_update
& r_node_update
) :
72 node_update(r_node_update
),
73 m_p_head(s_node_allocator
.allocate(1)),
77 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
82 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC
& other
) :
86 #ifdef PB_DS_TREE_TRACE
87 PB_DS_TREE_TRACE_BASE_C_DEC(other
),
91 m_p_head(s_node_allocator
.allocate(1)),
95 m_size
= other
.m_size
;
96 _GLIBCXX_DEBUG_ONLY(other
.structure_only_assert_valid();)
100 m_p_head
->m_p_parent
= recursive_copy_node(other
.m_p_head
->m_p_parent
);
101 if (m_p_head
->m_p_parent
!= NULL
)
102 m_p_head
->m_p_parent
->m_p_parent
= m_p_head
;
103 m_size
= other
.m_size
;
104 initialize_min_max();
108 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
109 s_node_allocator
.deallocate(m_p_head
, 1);
110 __throw_exception_again
;
112 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
118 swap(PB_DS_CLASS_C_DEC
& other
)
120 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
121 _GLIBCXX_DEBUG_ONLY(other
.structure_only_assert_valid();)
123 std::swap((Cmp_Fn
& )(*this), (Cmp_Fn
& )other
);
124 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
125 _GLIBCXX_DEBUG_ONLY(other
.structure_only_assert_valid();)
131 value_swap(PB_DS_CLASS_C_DEC
& other
)
133 _GLIBCXX_DEBUG_ONLY(debug_base::swap(other
);)
134 std::swap(m_p_head
, other
.m_p_head
);
135 std::swap(m_size
, other
.m_size
);
143 s_node_allocator
.deallocate(m_p_head
, 1);
151 m_p_head
->m_p_parent
= NULL
;
152 m_p_head
->m_p_left
= m_p_head
;
153 m_p_head
->m_p_right
= m_p_head
;
158 typename
PB_DS_CLASS_C_DEC::node_pointer
160 recursive_copy_node(const node_pointer p_nd
)
165 node_pointer p_ret
= s_node_allocator
.allocate(1);
168 new (p_ret
) node(*p_nd
);
172 s_node_allocator
.deallocate(p_ret
, 1);
173 __throw_exception_again
;
176 p_ret
->m_p_left
= p_ret
->m_p_right
= NULL
;
180 p_ret
->m_p_left
= recursive_copy_node(p_nd
->m_p_left
);
181 p_ret
->m_p_right
= recursive_copy_node(p_nd
->m_p_right
);
186 __throw_exception_again
;
189 if (p_ret
->m_p_left
!= NULL
)
190 p_ret
->m_p_left
->m_p_parent
= p_ret
;
192 if (p_ret
->m_p_right
!= NULL
)
193 p_ret
->m_p_right
->m_p_parent
= p_ret
;
195 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret
);)
204 if (m_p_head
->m_p_parent
== NULL
)
206 m_p_head
->m_p_left
= m_p_head
->m_p_right
= m_p_head
;
211 node_pointer p_min
= m_p_head
->m_p_parent
;
212 while (p_min
->m_p_left
!= NULL
)
213 p_min
= p_min
->m_p_left
;
214 m_p_head
->m_p_left
= p_min
;
218 node_pointer p_max
= m_p_head
->m_p_parent
;
219 while (p_max
->m_p_right
!= NULL
)
220 p_max
= p_max
->m_p_right
;
221 m_p_head
->m_p_right
= p_max
;