Update Copyright years for files modified in 2010.
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / constructors_destructor_fn_imps.hpp
blob1aa2fd47636dae2af58140503acab6d3e5de49d2
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
5 //
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
10 // version.
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
35 // warranty.
37 /**
38 * @file constructors_destructor_fn_imps.hpp
39 * Contains an implementation class for bin_search_tree_.
42 PB_DS_CLASS_T_DEC
43 typename PB_DS_CLASS_C_DEC::node_allocator
44 PB_DS_CLASS_C_DEC::s_node_allocator;
46 PB_DS_CLASS_T_DEC
47 PB_DS_CLASS_C_DEC::
48 PB_DS_CLASS_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
50 initialize();
51 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
54 PB_DS_CLASS_T_DEC
55 PB_DS_CLASS_C_DEC::
56 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn) :
57 Cmp_Fn(r_cmp_fn), m_p_head(s_node_allocator.allocate(1)), m_size(0)
59 initialize();
60 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
63 PB_DS_CLASS_T_DEC
64 PB_DS_CLASS_C_DEC::
65 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
66 Cmp_Fn(r_cmp_fn),
67 node_update(r_node_update),
68 m_p_head(s_node_allocator.allocate(1)),
69 m_size(0)
71 initialize();
72 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
75 PB_DS_CLASS_T_DEC
76 PB_DS_CLASS_C_DEC::
77 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
78 #ifdef _GLIBCXX_DEBUG
79 debug_base(other),
80 #endif
81 #ifdef PB_DS_TREE_TRACE
82 PB_DS_TREE_TRACE_BASE_C_DEC(other),
83 #endif
84 Cmp_Fn(other),
85 node_update(other),
86 m_p_head(s_node_allocator.allocate(1)),
87 m_size(0)
89 initialize();
90 m_size = other.m_size;
91 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
93 __try
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;
99 initialize_min_max();
101 __catch(...)
103 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
104 s_node_allocator.deallocate(m_p_head, 1);
105 __throw_exception_again;
107 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
110 PB_DS_CLASS_T_DEC
111 void
112 PB_DS_CLASS_C_DEC::
113 swap(PB_DS_CLASS_C_DEC& other)
115 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
116 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
117 value_swap(other);
118 std::swap((Cmp_Fn& )(*this), (Cmp_Fn& )other);
119 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
120 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
123 PB_DS_CLASS_T_DEC
124 void
125 PB_DS_CLASS_C_DEC::
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);
133 PB_DS_CLASS_T_DEC
134 PB_DS_CLASS_C_DEC::
135 ~PB_DS_CLASS_NAME()
137 clear();
138 s_node_allocator.deallocate(m_p_head, 1);
141 PB_DS_CLASS_T_DEC
142 void
143 PB_DS_CLASS_C_DEC::
144 initialize()
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;
149 m_size = 0;
152 PB_DS_CLASS_T_DEC
153 typename PB_DS_CLASS_C_DEC::node_pointer
154 PB_DS_CLASS_C_DEC::
155 recursive_copy_node(const node_pointer p_nd)
157 if (p_nd == 0)
158 return (0);
160 node_pointer p_ret = s_node_allocator.allocate(1);
161 __try
163 new (p_ret) node(*p_nd);
165 __catch(...)
167 s_node_allocator.deallocate(p_ret, 1);
168 __throw_exception_again;
171 p_ret->m_p_left = p_ret->m_p_right = 0;
173 __try
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);
178 __catch(...)
180 clear_imp(p_ret);
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 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret);)
191 return p_ret;
194 PB_DS_CLASS_T_DEC
195 void
196 PB_DS_CLASS_C_DEC::
197 initialize_min_max()
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;
202 return;
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;