Merged trunk at revision 161680 into branch.
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / constructors_destructor_fn_imps.hpp
blobb9f80e960aa6e5d243aef915b72405164f47ca89
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009 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.
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
34 // warranty.
36 /**
37 * @file constructors_destructor_fn_imps.hpp
38 * Contains an implementation class for bin_search_tree_.
41 PB_DS_CLASS_T_DEC
42 typename PB_DS_CLASS_C_DEC::node_allocator
43 PB_DS_CLASS_C_DEC::s_node_allocator;
45 PB_DS_CLASS_T_DEC
46 PB_DS_CLASS_C_DEC::
47 PB_DS_CLASS_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
49 initialize();
50 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
53 PB_DS_CLASS_T_DEC
54 PB_DS_CLASS_C_DEC::
55 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn) :
56 Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0)
58 initialize();
59 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
62 PB_DS_CLASS_T_DEC
63 PB_DS_CLASS_C_DEC::
64 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
65 Cmp_Fn(r_cmp_fn),
66 node_update(r_node_update),
67 m_p_head(s_node_allocator.allocate(1)),
68 m_size(0)
70 initialize();
71 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
74 PB_DS_CLASS_T_DEC
75 PB_DS_CLASS_C_DEC::
76 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
77 #ifdef _GLIBCXX_DEBUG
78 debug_base(other),
79 #endif
80 #ifdef PB_DS_TREE_TRACE
81 PB_DS_TREE_TRACE_BASE_C_DEC(other),
82 #endif
83 Cmp_Fn(other),
84 node_update(other),
85 m_p_head(s_node_allocator.allocate(1)),
86 m_size(0)
88 initialize();
89 m_size = other.m_size;
90 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
92 __try
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;
98 initialize_min_max();
100 __catch(...)
102 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
103 s_node_allocator.deallocate(m_p_head, 1);
104 __throw_exception_again;
106 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
109 PB_DS_CLASS_T_DEC
110 void
111 PB_DS_CLASS_C_DEC::
112 swap(PB_DS_CLASS_C_DEC& other)
114 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
115 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
116 value_swap(other);
117 std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
118 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
119 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
122 PB_DS_CLASS_T_DEC
123 void
124 PB_DS_CLASS_C_DEC::
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);
132 PB_DS_CLASS_T_DEC
133 PB_DS_CLASS_C_DEC::
134 ~PB_DS_CLASS_NAME()
136 clear();
137 s_node_allocator.deallocate(m_p_head, 1);
140 PB_DS_CLASS_T_DEC
141 void
142 PB_DS_CLASS_C_DEC::
143 initialize()
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;
148 m_size = 0;
151 PB_DS_CLASS_T_DEC
152 typename PB_DS_CLASS_C_DEC::node_pointer
153 PB_DS_CLASS_C_DEC::
154 recursive_copy_node(const node_pointer p_nd)
156 if (p_nd == 0)
157 return (0);
159 node_pointer p_ret = s_node_allocator.allocate(1);
160 __try
162 new (p_ret) node(*p_nd);
164 __catch(...)
166 s_node_allocator.deallocate(p_ret, 1);
167 __throw_exception_again;
170 p_ret->m_p_left = p_ret->m_p_right = 0;
172 __try
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);
177 __catch(...)
179 clear_imp(p_ret);
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 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret);)
190 return p_ret;
193 PB_DS_CLASS_T_DEC
194 void
195 PB_DS_CLASS_C_DEC::
196 initialize_min_max()
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;
201 return;
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;