1 // -*- C++ -*-
2
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.
10
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.
15
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.
20
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.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
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.
41
42 /**
43 * @file constructors_destructor_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
45 */
46
47 PB_DS_CLASS_T_DEC
48 typename PB_DS_CLASS_C_DEC::head_allocator
49 PB_DS_CLASS_C_DEC::s_head_allocator;
50
51 PB_DS_CLASS_T_DEC
52 typename PB_DS_CLASS_C_DEC::internal_node_allocator
53 PB_DS_CLASS_C_DEC::s_internal_node_allocator;
54
55 PB_DS_CLASS_T_DEC
56 typename PB_DS_CLASS_C_DEC::leaf_allocator
57 PB_DS_CLASS_C_DEC::s_leaf_allocator;
58
59 PB_DS_CLASS_T_DEC
60 PB_DS_CLASS_C_DEC::
PB_DS_CLASS_NAME()61 PB_DS_CLASS_NAME() :
62 m_p_head(s_head_allocator.allocate(1)),
63 m_size(0)
64 {
65 initialize();
66 _GLIBCXX_DEBUG_ONLY(assert_valid();)
67 }
68
69 PB_DS_CLASS_T_DEC
70 PB_DS_CLASS_C_DEC::
PB_DS_CLASS_NAME(const e_access_traits & r_e_access_traits)71 PB_DS_CLASS_NAME(const e_access_traits& r_e_access_traits) :
72 synth_e_access_traits(r_e_access_traits),
73 m_p_head(s_head_allocator.allocate(1)),
74 m_size(0)
75 {
76 initialize();
77 _GLIBCXX_DEBUG_ONLY(assert_valid();)
78 }
79
80 PB_DS_CLASS_T_DEC
81 PB_DS_CLASS_C_DEC::
PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC & other)82 PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) :
83 #ifdef _GLIBCXX_DEBUG
84 map_debug_base(other),
85 #endif
86 synth_e_access_traits(other),
87 node_update(other),
88 m_p_head(s_head_allocator.allocate(1)),
89 m_size(0)
90 {
91 initialize();
92 m_size = other.m_size;
93 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
94 if (other.m_p_head->m_p_parent == NULL)
95 {
96 _GLIBCXX_DEBUG_ONLY(assert_valid();)
97 return;
98 }
99 try
100 {
101 m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
102 }
103 catch(...)
104 {
105 s_head_allocator.deallocate(m_p_head, 1);
106 __throw_exception_again;
107 }
108
109 m_p_head->m_p_min = leftmost_descendant(m_p_head->m_p_parent);
110 m_p_head->m_p_max = rightmost_descendant(m_p_head->m_p_parent);
111 m_p_head->m_p_parent->m_p_parent = m_p_head;
112 _GLIBCXX_DEBUG_ONLY(assert_valid();)
113 }
114
115 PB_DS_CLASS_T_DEC
116 void
117 PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC & other)118 swap(PB_DS_CLASS_C_DEC& other)
119 {
120 _GLIBCXX_DEBUG_ONLY(assert_valid();)
121 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
122 value_swap(other);
123 std::swap((e_access_traits& )(*this), (e_access_traits& )other);
124 _GLIBCXX_DEBUG_ONLY(assert_valid();)
125 _GLIBCXX_DEBUG_ONLY(other.assert_valid();)
126 }
127
128 PB_DS_CLASS_T_DEC
129 void
130 PB_DS_CLASS_C_DEC::
value_swap(PB_DS_CLASS_C_DEC & other)131 value_swap(PB_DS_CLASS_C_DEC& other)
132 {
133 _GLIBCXX_DEBUG_ONLY(map_debug_base::swap(other);)
134 std::swap(m_p_head, other.m_p_head);
135 std::swap(m_size, other.m_size);
136 }
137
138 PB_DS_CLASS_T_DEC
139 PB_DS_CLASS_C_DEC::
~PB_DS_CLASS_NAME()140 ~PB_DS_CLASS_NAME()
141 {
142 clear();
143 s_head_allocator.deallocate(m_p_head, 1);
144 }
145
146 PB_DS_CLASS_T_DEC
147 void
148 PB_DS_CLASS_C_DEC::
initialize()149 initialize()
150 {
151 new (m_p_head) head();
152 m_p_head->m_p_parent = NULL;
153 m_p_head->m_p_min = m_p_head;
154 m_p_head->m_p_max = m_p_head;
155 m_size = 0;
156 }
157
158 PB_DS_CLASS_T_DEC
159 template<typename It>
160 void
161 PB_DS_CLASS_C_DEC::
copy_from_range(It first_it,It last_it)162 copy_from_range(It first_it, It last_it)
163 {
164 while (first_it != last_it)
165 insert(*(first_it++));
166 }
167
168 PB_DS_CLASS_T_DEC
169 typename PB_DS_CLASS_C_DEC::node_pointer
170 PB_DS_CLASS_C_DEC::
recursive_copy_node(const_node_pointer p_other_nd)171 recursive_copy_node(const_node_pointer p_other_nd)
172 {
173 _GLIBCXX_DEBUG_ASSERT(p_other_nd != NULL);
174 if (p_other_nd->m_type == pat_trie_leaf_node_type)
175 {
176 const_leaf_pointer p_other_leaf = static_cast<const_leaf_pointer>(p_other_nd);
177
178 leaf_pointer p_new_lf = s_leaf_allocator.allocate(1);
179 cond_dealtor cond(p_new_lf);
180 new (p_new_lf) leaf(p_other_leaf->value());
181 apply_update(p_new_lf, (node_update* )this);
182 cond.set_no_action_dtor();
183 return (p_new_lf);
184 }
185
186 _GLIBCXX_DEBUG_ASSERT(p_other_nd->m_type == pat_trie_internal_node_type);
187 node_pointer a_p_children[internal_node::arr_size];
188 size_type child_i = 0;
189 const_internal_node_pointer p_other_internal_nd =
190 static_cast<const_internal_node_pointer>(p_other_nd);
191
192 typename internal_node::const_iterator child_it =
193 p_other_internal_nd->begin();
194
195 internal_node_pointer p_ret;
196 try
197 {
198 while (child_it != p_other_internal_nd->end())
199 a_p_children[child_i++] = recursive_copy_node(*(child_it++));
200 p_ret = s_internal_node_allocator.allocate(1);
201 }
202 catch(...)
203 {
204 while (child_i-- > 0)
205 clear_imp(a_p_children[child_i]);
206 __throw_exception_again;
207 }
208
209 new (p_ret) internal_node(p_other_internal_nd->get_e_ind(),
210 pref_begin(a_p_children[0]));
211
212 --child_i;
213 _GLIBCXX_DEBUG_ASSERT(child_i > 1);
214 do
215 p_ret->add_child(a_p_children[child_i], pref_begin(a_p_children[child_i]),
216 pref_end(a_p_children[child_i]), this);
217 while (child_i-- > 0);
218 apply_update(p_ret, (node_update* )this);
219 return p_ret;
220 }
221