]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
bc2631e0 | 3 | // Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc. |
4569a895 AT |
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:: | |
61 | PB_DS_CLASS_NAME() : | |
62 | m_p_head(s_head_allocator.allocate(1)), | |
63 | m_size(0) | |
64 | { | |
65 | initialize(); | |
47bea7b8 BK |
66 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
67 | } | |
4569a895 AT |
68 | |
69 | PB_DS_CLASS_T_DEC | |
70 | PB_DS_CLASS_C_DEC:: | |
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(); | |
47bea7b8 BK |
77 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
78 | } | |
4569a895 AT |
79 | |
80 | PB_DS_CLASS_T_DEC | |
81 | PB_DS_CLASS_C_DEC:: | |
82 | PB_DS_CLASS_NAME(const PB_DS_CLASS_C_DEC& other) : | |
47bea7b8 | 83 | #ifdef _GLIBCXX_DEBUG |
551fe1a2 | 84 | debug_base(other), |
47bea7b8 | 85 | #endif |
4569a895 AT |
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(); | |
4569a895 | 92 | m_size = other.m_size; |
47bea7b8 | 93 | _GLIBCXX_DEBUG_ONLY(other.assert_valid();) |
4569a895 AT |
94 | if (other.m_p_head->m_p_parent == NULL) |
95 | { | |
47bea7b8 BK |
96 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
97 | return; | |
4569a895 | 98 | } |
bc2631e0 | 99 | __try |
4569a895 | 100 | { |
47bea7b8 | 101 | m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent); |
4569a895 | 102 | } |
bc2631e0 | 103 | __catch(...) |
4569a895 AT |
104 | { |
105 | s_head_allocator.deallocate(m_p_head, 1); | |
8fafc2d3 | 106 | __throw_exception_again; |
4569a895 AT |
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); | |
4569a895 | 111 | m_p_head->m_p_parent->m_p_parent = m_p_head; |
47bea7b8 BK |
112 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
113 | } | |
4569a895 AT |
114 | |
115 | PB_DS_CLASS_T_DEC | |
116 | void | |
117 | PB_DS_CLASS_C_DEC:: | |
118 | swap(PB_DS_CLASS_C_DEC& other) | |
119 | { | |
47bea7b8 BK |
120 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
121 | _GLIBCXX_DEBUG_ONLY(other.assert_valid();) | |
122 | value_swap(other); | |
4569a895 | 123 | std::swap((e_access_traits& )(*this), (e_access_traits& )other); |
47bea7b8 BK |
124 | _GLIBCXX_DEBUG_ONLY(assert_valid();) |
125 | _GLIBCXX_DEBUG_ONLY(other.assert_valid();) | |
126 | } | |
4569a895 AT |
127 | |
128 | PB_DS_CLASS_T_DEC | |
129 | void | |
130 | PB_DS_CLASS_C_DEC:: | |
131 | value_swap(PB_DS_CLASS_C_DEC& other) | |
132 | { | |
551fe1a2 | 133 | _GLIBCXX_DEBUG_ONLY(debug_base::swap(other);) |
47bea7b8 | 134 | std::swap(m_p_head, other.m_p_head); |
4569a895 AT |
135 | std::swap(m_size, other.m_size); |
136 | } | |
137 | ||
138 | PB_DS_CLASS_T_DEC | |
139 | PB_DS_CLASS_C_DEC:: | |
140 | ~PB_DS_CLASS_NAME() | |
141 | { | |
142 | clear(); | |
4569a895 AT |
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:: | |
149 | initialize() | |
150 | { | |
151 | new (m_p_head) head(); | |
4569a895 | 152 | m_p_head->m_p_parent = NULL; |
4569a895 AT |
153 | m_p_head->m_p_min = m_p_head; |
154 | m_p_head->m_p_max = m_p_head; | |
4569a895 AT |
155 | m_size = 0; |
156 | } | |
157 | ||
158 | PB_DS_CLASS_T_DEC | |
159 | template<typename It> | |
160 | void | |
161 | PB_DS_CLASS_C_DEC:: | |
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:: | |
171 | recursive_copy_node(const_node_pointer p_other_nd) | |
172 | { | |
47bea7b8 | 173 | _GLIBCXX_DEBUG_ASSERT(p_other_nd != NULL); |
4569a895 AT |
174 | if (p_other_nd->m_type == pat_trie_leaf_node_type) |
175 | { | |
47bea7b8 | 176 | const_leaf_pointer p_other_leaf = static_cast<const_leaf_pointer>(p_other_nd); |
4569a895 AT |
177 | |
178 | leaf_pointer p_new_lf = s_leaf_allocator.allocate(1); | |
4569a895 | 179 | cond_dealtor cond(p_new_lf); |
4569a895 | 180 | new (p_new_lf) leaf(p_other_leaf->value()); |
4569a895 | 181 | apply_update(p_new_lf, (node_update* )this); |
4569a895 | 182 | cond.set_no_action_dtor(); |
4569a895 AT |
183 | return (p_new_lf); |
184 | } | |
185 | ||
47bea7b8 | 186 | _GLIBCXX_DEBUG_ASSERT(p_other_nd->m_type == pat_trie_internal_node_type); |
4569a895 | 187 | node_pointer a_p_children[internal_node::arr_size]; |
4569a895 | 188 | size_type child_i = 0; |
4569a895 AT |
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; | |
bc2631e0 | 196 | __try |
4569a895 AT |
197 | { |
198 | while (child_it != p_other_internal_nd->end()) | |
199 | a_p_children[child_i++] = recursive_copy_node(*(child_it++)); | |
4569a895 AT |
200 | p_ret = s_internal_node_allocator.allocate(1); |
201 | } | |
bc2631e0 | 202 | __catch(...) |
4569a895 AT |
203 | { |
204 | while (child_i-- > 0) | |
205 | clear_imp(a_p_children[child_i]); | |
8fafc2d3 | 206 | __throw_exception_again; |
4569a895 AT |
207 | } |
208 | ||
47bea7b8 | 209 | new (p_ret) internal_node(p_other_internal_nd->get_e_ind(), |
4569a895 AT |
210 | pref_begin(a_p_children[0])); |
211 | ||
212 | --child_i; | |
47bea7b8 | 213 | _GLIBCXX_DEBUG_ASSERT(child_i > 1); |
4569a895 | 214 | do |
47bea7b8 BK |
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); | |
4569a895 | 217 | while (child_i-- > 0); |
4569a895 | 218 | apply_update(p_ret, (node_update* )this); |
47bea7b8 | 219 | return p_ret; |
4569a895 | 220 | } |