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::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::internal_node_allocator 48 PB_DS_CLASS_C_DEC::s_internal_node_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:: 56 PB_DS_CLASS_NAME() : 57 m_p_head(s_head_allocator.allocate(1)), 58 m_size(0) 59 { 60 initialize(); 61 _GLIBCXX_DEBUG_ONLY(assert_valid();) 62 } 63 64 PB_DS_CLASS_T_DEC 65 PB_DS_CLASS_C_DEC:: 66 PB_DS_CLASS_NAME(const e_access_traits& r_e_access_traits) : 67 synth_e_access_traits(r_e_access_traits), 68 m_p_head(s_head_allocator.allocate(1)), 69 m_size(0) 70 { 71 initialize(); 72 _GLIBCXX_DEBUG_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 synth_e_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 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 89 if (other.m_p_head->m_p_parent == 0) 90 { 91 _GLIBCXX_DEBUG_ONLY(assert_valid();) 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 _GLIBCXX_DEBUG_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(assert_valid();) 116 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 117 value_swap(other); 118 std::swap((e_access_traits& )(*this), (e_access_traits& )other); 119 _GLIBCXX_DEBUG_ONLY(assert_valid();) 120 _GLIBCXX_DEBUG_ONLY(other.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_head_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 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:: 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:: 166 recursive_copy_node(const_node_pointer p_other_nd) 167 { 168 _GLIBCXX_DEBUG_ASSERT(p_other_nd != 0); 169 if (p_other_nd->m_type == pat_trie_leaf_node_type) 170 { 171 const_leaf_pointer p_other_leaf = static_cast<const_leaf_pointer>(p_other_nd); 172 173 leaf_pointer p_new_lf = s_leaf_allocator.allocate(1); 174 cond_dealtor cond(p_new_lf); 175 new (p_new_lf) leaf(p_other_leaf->value()); 176 apply_update(p_new_lf, (node_update* )this); 177 cond.set_no_action_dtor(); 178 return (p_new_lf); 179 } 180 181 _GLIBCXX_DEBUG_ASSERT(p_other_nd->m_type == pat_trie_internal_node_type); 182 node_pointer a_p_children[internal_node::arr_size]; 183 size_type child_i = 0; 184 const_internal_node_pointer p_other_internal_nd = 185 static_cast<const_internal_node_pointer>(p_other_nd); 186 187 typename internal_node::const_iterator child_it = 188 p_other_internal_nd->begin(); 189 190 internal_node_pointer p_ret; 191 __try 192 { 193 while (child_it != p_other_internal_nd->end()) 194 a_p_children[child_i++] = recursive_copy_node(*(child_it++)); 195 p_ret = s_internal_node_allocator.allocate(1); 196 } 197 __catch(...) 198 { 199 while (child_i-- > 0) 200 clear_imp(a_p_children[child_i]); 201 __throw_exception_again; 202 } 203 204 new (p_ret) internal_node(p_other_internal_nd->get_e_ind(), 205 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