]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libstdc++-v3/contrib/libstdc++-v3-4.6/include/ext/pb_ds/detail/bin_search_tree_/constructors_destructor_fn_imps.hpp
update
[l4.git] / l4 / pkg / libstdc++-v3 / contrib / libstdc++-v3-4.6 / include / ext / pb_ds / detail / bin_search_tree_ / constructors_destructor_fn_imps.hpp
1 // -*- C++ -*-
2
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.
11
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.
16
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.
20
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/>.
25
26 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27
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.
36
37 /**
38  * @file constructors_destructor_fn_imps.hpp
39  * Contains an implementation class for bin_search_tree_.
40  */
41
42 PB_DS_CLASS_T_DEC
43 typename PB_DS_CLASS_C_DEC::node_allocator
44 PB_DS_CLASS_C_DEC::s_node_allocator;
45
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)
49 {
50   initialize();
51   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
52 }
53
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)
58 {
59   initialize();
60   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
61 }
62
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)
70 {
71   initialize();
72   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
73 }
74
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)
88 {
89   initialize();
90   m_size = other.m_size;
91   _GLIBCXX_DEBUG_ONLY(other.structure_only_assert_valid();)
92
93     __try
94       {
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();
100       }
101     __catch(...)
102       {
103         _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
104         s_node_allocator.deallocate(m_p_head, 1);
105         __throw_exception_again;
106       }
107   _GLIBCXX_DEBUG_ONLY(structure_only_assert_valid();)
108 }
109
110 PB_DS_CLASS_T_DEC
111 void
112 PB_DS_CLASS_C_DEC::
113 swap(PB_DS_CLASS_C_DEC& other)
114 {
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();)
121 }
122
123 PB_DS_CLASS_T_DEC
124 void
125 PB_DS_CLASS_C_DEC::
126 value_swap(PB_DS_CLASS_C_DEC& other)
127 {
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);
131 }
132
133 PB_DS_CLASS_T_DEC
134 PB_DS_CLASS_C_DEC::
135 ~PB_DS_CLASS_NAME()
136 {
137   clear();
138   s_node_allocator.deallocate(m_p_head, 1);
139 }
140
141 PB_DS_CLASS_T_DEC
142 void
143 PB_DS_CLASS_C_DEC::
144 initialize()
145 {
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;
150 }
151
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)
156 {
157   if (p_nd == 0)
158     return (0);
159
160   node_pointer p_ret = s_node_allocator.allocate(1);
161   __try
162     {
163       new (p_ret) node(*p_nd);
164     }
165   __catch(...)
166     {
167       s_node_allocator.deallocate(p_ret, 1);
168       __throw_exception_again;
169     }
170
171   p_ret->m_p_left = p_ret->m_p_right = 0;
172
173   __try
174     {
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);
177     }
178   __catch(...)
179     {
180       clear_imp(p_ret);
181       __throw_exception_again;
182     }
183
184   if (p_ret->m_p_left != 0)
185     p_ret->m_p_left->m_p_parent = p_ret;
186
187   if (p_ret->m_p_right != 0)
188     p_ret->m_p_right->m_p_parent = p_ret;
189
190   _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_ret);)
191   return p_ret;
192 }
193
194 PB_DS_CLASS_T_DEC
195 void
196 PB_DS_CLASS_C_DEC::
197 initialize_min_max()
198 {
199   if (m_p_head->m_p_parent == 0)
200     {
201       m_p_head->m_p_left = m_p_head->m_p_right = m_p_head;
202       return;
203     }
204
205   {
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;
210   }
211
212   {
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;
217   }
218 }
219