Merge -r 127928:132243 from trunk
[official-gcc.git] / libstdc++-v3 / include / ext / pb_ds / detail / bin_search_tree_ / constructors_destructor_fn_imps.hpp
blob295a57aee17fb724f96287fe816686dde6d3103f
1 // -*- C++ -*-
3 // Copyright (C) 2005, 2006 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 2, 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 // 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
29 // Public License.
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
40 // warranty.
42 /**
43 * @file constructors_destructor_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
47 PB_DS_CLASS_T_DEC
48 typename PB_DS_CLASS_C_DEC::node_allocator
49 PB_DS_CLASS_C_DEC::s_node_allocator;
51 PB_DS_CLASS_T_DEC
52 PB_DS_CLASS_C_DEC::
53 PB_DS_CLASS_NAME() : m_p_head(s_node_allocator.allocate(1)), m_size(0)
55 initialize();
56 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
59 PB_DS_CLASS_T_DEC
60 PB_DS_CLASS_C_DEC::
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)
64 initialize();
65 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
68 PB_DS_CLASS_T_DEC
69 PB_DS_CLASS_C_DEC::
70 PB_DS_CLASS_NAME(const Cmp_Fn& r_cmp_fn, const node_update& r_node_update) :
71 Cmp_Fn(r_cmp_fn),
72 node_update(r_node_update),
73 m_p_head(s_node_allocator.allocate(1)),
74 m_size(0)
76 initialize();
77 _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
80 PB_DS_CLASS_T_DEC
81 PB_DS_CLASS_C_DEC::
82 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
83 #ifdef _GLIBCXX_DEBUG
84 debug_base(other),
85 #endif
86 #ifdef PB_DS_TREE_TRACE
87 PB_DS_TREE_TRACE_BASE_C_DEC(other),
88 #endif
89 Cmp_Fn(other),
90 node_update(other),
91 m_p_head(s_node_allocator.allocate(1)),
92 m_size(0)
94 initialize();
95 m_size = other.m_size;
96 _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
98 try
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();
106 catch(...)
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();)
115 PB_DS_CLASS_T_DEC
116 void
117 PB_DS_CLASS_C_DEC::
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();)
122 value_swap(other);
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();)
128 PB_DS_CLASS_T_DEC
129 void
130 PB_DS_CLASS_C_DEC::
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);
138 PB_DS_CLASS_T_DEC
139 PB_DS_CLASS_C_DEC::
140 ~PB_DS_CLASS_NAME()
142 clear();
143 s_node_allocator.deallocate(m_p_head, 1);
146 PB_DS_CLASS_T_DEC
147 void
148 PB_DS_CLASS_C_DEC::
149 initialize()
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;
154 m_size = 0;
157 PB_DS_CLASS_T_DEC
158 typename PB_DS_CLASS_C_DEC::node_pointer
159 PB_DS_CLASS_C_DEC::
160 recursive_copy_node(const node_pointer p_nd)
162 if (p_nd == NULL)
163 return (NULL);
165 node_pointer p_ret = s_node_allocator.allocate(1);
168 new (p_ret) node(*p_nd);
170 catch(...)
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);
183 catch(...)
185 clear_imp(p_ret);
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);)
196 return p_ret;
199 PB_DS_CLASS_T_DEC
200 void
201 PB_DS_CLASS_C_DEC::
202 initialize_min_max()
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;
207 return;
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;