1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 pat_trie_/constructors_destructor_fn_imps.hpp
39 * Contains an implementation class for pat_trie.
40 */
41
42 PB_DS_CLASS_T_DEC
43 typename PB_DS_CLASS_C_DEC::head_allocator
44 PB_DS_CLASS_C_DEC::s_head_allocator;
45
46 PB_DS_CLASS_T_DEC
47 typename PB_DS_CLASS_C_DEC::inode_allocator
48 PB_DS_CLASS_C_DEC::s_inode_allocator;
49
50 PB_DS_CLASS_T_DEC
51 typename PB_DS_CLASS_C_DEC::leaf_allocator
52 PB_DS_CLASS_C_DEC::s_leaf_allocator;
53
54 PB_DS_CLASS_T_DEC
55 PB_DS_CLASS_C_DEC::
PB_DS_PAT_TRIE_NAME()56 PB_DS_PAT_TRIE_NAME() :
57 m_p_head(s_head_allocator.allocate(1)),
58 m_size(0)
59 {
60 initialize();
61 PB_DS_ASSERT_VALID((*this))
62 }
63
64 PB_DS_CLASS_T_DEC
65 PB_DS_CLASS_C_DEC::
PB_DS_PAT_TRIE_NAME(const access_traits & r_access_traits)66 PB_DS_PAT_TRIE_NAME(const access_traits& r_access_traits) :
67 synth_access_traits(r_access_traits),
68 m_p_head(s_head_allocator.allocate(1)),
69 m_size(0)
70 {
71 initialize();
72 PB_DS_ASSERT_VALID((*this))
73 }
74
75 PB_DS_CLASS_T_DEC
76 PB_DS_CLASS_C_DEC::
PB_DS_PAT_TRIE_NAME(const PB_DS_CLASS_C_DEC & other)77 PB_DS_PAT_TRIE_NAME(const PB_DS_CLASS_C_DEC& other) :
78 #ifdef _GLIBCXX_DEBUG
79 debug_base(other),
80 #endif
81 synth_access_traits(other),
82 node_update(other),
83 m_p_head(s_head_allocator.allocate(1)),
84 m_size(0)
85 {
86 initialize();
87 m_size = other.m_size;
88 PB_DS_ASSERT_VALID(other)
89 if (other.m_p_head->m_p_parent == 0)
90 {
91 PB_DS_ASSERT_VALID((*this))
92 return;
93 }
94 __try
95 {
96 m_p_head->m_p_parent = recursive_copy_node(other.m_p_head->m_p_parent);
97 }
98 __catch(...)
99 {
100 s_head_allocator.deallocate(m_p_head, 1);
101 __throw_exception_again;
102 }
103
104 m_p_head->m_p_min = leftmost_descendant(m_p_head->m_p_parent);
105 m_p_head->m_p_max = rightmost_descendant(m_p_head->m_p_parent);
106 m_p_head->m_p_parent->m_p_parent = m_p_head;
107 PB_DS_ASSERT_VALID((*this))
108 }
109
110 PB_DS_CLASS_T_DEC
111 void
112 PB_DS_CLASS_C_DEC::
swap(PB_DS_CLASS_C_DEC & other)113 swap(PB_DS_CLASS_C_DEC& other)
114 {
115 PB_DS_ASSERT_VALID((*this))
116 PB_DS_ASSERT_VALID(other)
117 value_swap(other);
118 std::swap((access_traits& )(*this), (access_traits& )other);
119 PB_DS_ASSERT_VALID((*this))
120 PB_DS_ASSERT_VALID(other)
121 }
122
123 PB_DS_CLASS_T_DEC
124 void
125 PB_DS_CLASS_C_DEC::
value_swap(PB_DS_CLASS_C_DEC & other)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::
~PB_DS_PAT_TRIE_NAME()135 ~PB_DS_PAT_TRIE_NAME()
136 {
137 clear();
138 s_head_allocator.deallocate(m_p_head, 1);
139 }
140
141 PB_DS_CLASS_T_DEC
142 void
143 PB_DS_CLASS_C_DEC::
initialize()144 initialize()
145 {
146 new (m_p_head) head();
147 m_p_head->m_p_parent = 0;
148 m_p_head->m_p_min = m_p_head;
149 m_p_head->m_p_max = m_p_head;
150 m_size = 0;
151 }
152
153 PB_DS_CLASS_T_DEC
154 template<typename It>
155 void
156 PB_DS_CLASS_C_DEC::
copy_from_range(It first_it,It last_it)157 copy_from_range(It first_it, It last_it)
158 {
159 while (first_it != last_it)
160 insert(*(first_it++));
161 }
162
163 PB_DS_CLASS_T_DEC
164 typename PB_DS_CLASS_C_DEC::node_pointer
165 PB_DS_CLASS_C_DEC::
recursive_copy_node(node_const_pointer p_ncp)166 recursive_copy_node(node_const_pointer p_ncp)
167 {
168 _GLIBCXX_DEBUG_ASSERT(p_ncp != 0);
169 if (p_ncp->m_type == leaf_node)
170 {
171 leaf_const_pointer p_other_lf = static_cast<leaf_const_pointer>(p_ncp);
172 leaf_pointer p_new_lf = s_leaf_allocator.allocate(1);
173 cond_dealtor cond(p_new_lf);
174 new (p_new_lf) leaf(p_other_lf->value());
175 apply_update(p_new_lf, (node_update*)this);
176 cond.set_no_action_dtor();
177 return (p_new_lf);
178 }
179
180 _GLIBCXX_DEBUG_ASSERT(p_ncp->m_type == i_node);
181 node_pointer a_p_children[inode::arr_size];
182 size_type child_i = 0;
183 inode_const_pointer p_icp = static_cast<inode_const_pointer>(p_ncp);
184
185 typename inode::const_iterator child_it = p_icp->begin();
186
187 inode_pointer p_ret;
188 __try
189 {
190 while (child_it != p_icp->end())
191 {
192 a_p_children[child_i] = recursive_copy_node(*(child_it));
193 child_i++;
194 child_it++;
195 }
196 p_ret = s_inode_allocator.allocate(1);
197 }
198 __catch(...)
199 {
200 while (child_i-- > 0)
201 clear_imp(a_p_children[child_i]);
202 __throw_exception_again;
203 }
204
205 new (p_ret) inode(p_icp->get_e_ind(), pref_begin(a_p_children[0]));
206
207 --child_i;
208 _GLIBCXX_DEBUG_ASSERT(child_i >= 1);
209 do
210 p_ret->add_child(a_p_children[child_i], pref_begin(a_p_children[child_i]),
211 pref_end(a_p_children[child_i]), this);
212 while (child_i-- > 0);
213 apply_update(p_ret, (node_update*)this);
214 return p_ret;
215 }
216