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 split_fn_imps.hpp 44 * Contains an implementation class for bin_search_tree_. 45 */ 46 47PB_DS_CLASS_T_DEC 48void 49PB_DS_CLASS_C_DEC:: 50split(const_key_reference r_key, PB_DS_CLASS_C_DEC& other) 51{ 52 _GLIBCXX_DEBUG_ONLY(assert_valid();); 53 _GLIBCXX_DEBUG_ONLY(other.assert_valid();); 54 split_join_branch_bag bag; 55 leaf_pointer p_split_lf = split_prep(r_key, other, bag); 56 if (p_split_lf == NULL) 57 { 58 _GLIBCXX_DEBUG_ASSERT(bag.empty()); 59 _GLIBCXX_DEBUG_ONLY(assert_valid();) 60 _GLIBCXX_DEBUG_ONLY(other.assert_valid();) 61 return; 62 } 63 64 _GLIBCXX_DEBUG_ASSERT(!bag.empty()); 65 other.clear(); 66 m_p_head->m_p_parent = rec_split(m_p_head->m_p_parent, 67 pref_begin(p_split_lf), 68 pref_end(p_split_lf), 69 other, 70 bag); 71 72 m_p_head->m_p_parent->m_p_parent = m_p_head; 73 74 other.m_p_head->m_p_max = m_p_head->m_p_max; 75 m_p_head->m_p_max = rightmost_descendant(m_p_head->m_p_parent); 76 other.m_p_head->m_p_min = 77 other.leftmost_descendant(other.m_p_head->m_p_parent); 78 79 other.m_size = std::distance(other.PB_DS_CLASS_C_DEC::begin(), 80 other.PB_DS_CLASS_C_DEC::end()); 81 m_size -= other.m_size; 82 _GLIBCXX_DEBUG_ONLY(assert_valid();); 83 _GLIBCXX_DEBUG_ONLY(other.assert_valid();); 84} 85 86PB_DS_CLASS_T_DEC 87typename PB_DS_CLASS_C_DEC::leaf_pointer 88PB_DS_CLASS_C_DEC:: 89split_prep(const_key_reference r_key, PB_DS_CLASS_C_DEC& other, split_join_branch_bag& r_bag) 90{ 91 _GLIBCXX_DEBUG_ASSERT(r_bag.empty()); 92 if (m_size == 0) 93 { 94 other.clear(); 95 _GLIBCXX_DEBUG_ONLY(assert_valid();); 96 _GLIBCXX_DEBUG_ONLY(other.assert_valid();); 97 return (NULL); 98 } 99 100 if (synth_e_access_traits::cmp_keys(r_key, 101 PB_DS_V2F(static_cast<const_leaf_pointer>(m_p_head->m_p_min)->value()))) 102 { 103 other.clear(); 104 value_swap(other); 105 _GLIBCXX_DEBUG_ONLY(assert_valid();); 106 _GLIBCXX_DEBUG_ONLY(other.assert_valid();); 107 return (NULL); 108 } 109 110 if (!synth_e_access_traits::cmp_keys(r_key, 111 PB_DS_V2F(static_cast<const_leaf_pointer>(m_p_head->m_p_max)->value()))) 112 { 113 _GLIBCXX_DEBUG_ONLY(assert_valid();); 114 _GLIBCXX_DEBUG_ONLY(other.assert_valid();); 115 return (NULL); 116 } 117 118 iterator it = lower_bound(r_key); 119 120 if (!synth_e_access_traits::equal_keys(PB_DS_V2F(*it), r_key)) 121 --it; 122 123 node_pointer p_nd = it.m_p_nd; 124 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type); 125 leaf_pointer p_ret_l = static_cast<leaf_pointer>(p_nd); 126 while (p_nd->m_type != pat_trie_head_node_type) 127 { 128 r_bag.add_branch(); 129 p_nd = p_nd->m_p_parent; 130 } 131 _GLIBCXX_DEBUG_ONLY(map_debug_base::split(r_key,(synth_e_access_traits& )(*this), other);) 132 133 return (p_ret_l); 134} 135 136PB_DS_CLASS_T_DEC 137typename PB_DS_CLASS_C_DEC::node_pointer 138PB_DS_CLASS_C_DEC:: 139rec_split(node_pointer p_nd, const_e_iterator b_it, const_e_iterator e_it, PB_DS_CLASS_C_DEC& other, split_join_branch_bag& r_bag) 140{ 141 if (p_nd->m_type == pat_trie_leaf_node_type) 142 { 143 _GLIBCXX_DEBUG_ASSERT(other.m_p_head->m_p_parent == NULL); 144 return (p_nd); 145 } 146 147 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type); 148 internal_node_pointer p_internal_nd = static_cast<internal_node_pointer>(p_nd); 149 150 node_pointer p_child_ret = rec_split(p_internal_nd->get_child_node(b_it, e_it, this), b_it, e_it, other, r_bag); 151 152 _GLIBCXX_DEBUG_ONLY(p_child_ret->assert_valid(this);) 153 p_internal_nd->replace_child(p_child_ret, b_it, e_it, this); 154 apply_update(p_internal_nd, (node_update* )this); 155 156 typename internal_node::iterator child_it = 157 p_internal_nd->get_child_it(b_it, e_it, this); 158 159 const size_type lhs_num_children = 160 std::distance(p_internal_nd->begin(), child_it) + 1; 161 162 _GLIBCXX_DEBUG_ASSERT(lhs_num_children > 0); 163 164 size_type rhs_num_children = 165 std::distance(p_internal_nd->begin(), p_internal_nd->end()) - 166 lhs_num_children; 167 168 if (rhs_num_children == 0) 169 { 170 apply_update(p_internal_nd, (node_update* )this); 171 return (p_internal_nd); 172 } 173 174 ++child_it; 175 other.split_insert_branch(p_internal_nd->get_e_ind(), 176 b_it, child_it, rhs_num_children, r_bag); 177 178 child_it = p_internal_nd->get_child_it(b_it, e_it, this); 179 ++child_it; 180 while (rhs_num_children != 0) 181 { 182 child_it = p_internal_nd->remove_child(child_it); 183 --rhs_num_children; 184 } 185 186 apply_update(p_internal_nd, (node_update* )this); 187 _GLIBCXX_DEBUG_ASSERT(std::distance(p_internal_nd->begin(), 188 p_internal_nd->end()) >= 1); 189 190 if (std::distance(p_internal_nd->begin(), p_internal_nd->end()) > 1) 191 { 192 p_internal_nd->update_prefixes(this); 193 _GLIBCXX_DEBUG_ONLY(p_internal_nd->assert_valid(this);) 194 apply_update(p_internal_nd, (node_update* )this); 195 return (p_internal_nd); 196 } 197 198 node_pointer p_ret =* p_internal_nd->begin(); 199 p_internal_nd->~internal_node(); 200 s_internal_node_allocator.deallocate(p_internal_nd, 1); 201 apply_update(p_ret, (node_update* )this); 202 return (p_ret); 203} 204 205PB_DS_CLASS_T_DEC 206void 207PB_DS_CLASS_C_DEC:: 208split_insert_branch(size_type e_ind, const_e_iterator b_it, typename internal_node::iterator child_b_it, size_type num_children, split_join_branch_bag& r_bag) 209{ 210#ifdef _GLIBCXX_DEBUG 211 if (m_p_head->m_p_parent != NULL) 212 m_p_head->m_p_parent->assert_valid(this); 213#endif 214 215 const size_type total_num_children =((m_p_head->m_p_parent == NULL)? 0 : 1) + num_children; 216 217 if (total_num_children == 0) 218 { 219 _GLIBCXX_DEBUG_ASSERT(m_p_head->m_p_parent == NULL); 220 return; 221 } 222 223 if (total_num_children == 1) 224 { 225 if (m_p_head->m_p_parent != NULL) 226 { 227 _GLIBCXX_DEBUG_ONLY(m_p_head->m_p_parent->assert_valid(this);) 228 return; 229 } 230 231 _GLIBCXX_DEBUG_ASSERT(m_p_head->m_p_parent == NULL); 232 m_p_head->m_p_parent =* child_b_it; 233 m_p_head->m_p_parent->m_p_parent = m_p_head; 234 apply_update(m_p_head->m_p_parent, (node_update* )this); 235 _GLIBCXX_DEBUG_ONLY(m_p_head->m_p_parent->assert_valid(this);) 236 return; 237 } 238 239 _GLIBCXX_DEBUG_ASSERT(total_num_children > 1); 240 internal_node_pointer p_new_root = r_bag.get_branch(); 241 new (p_new_root) internal_node(e_ind, b_it); 242 size_type num_inserted = 0; 243 while (num_inserted++ < num_children) 244 { 245 _GLIBCXX_DEBUG_ONLY((*child_b_it)->assert_valid(this);) 246 p_new_root->add_child(*child_b_it, pref_begin(*child_b_it), 247 pref_end(*child_b_it), this); 248 ++child_b_it; 249 } 250 251 if (m_p_head->m_p_parent != NULL) 252 p_new_root->add_child(m_p_head->m_p_parent, 253 pref_begin(m_p_head->m_p_parent), 254 pref_end(m_p_head->m_p_parent), this); 255 256 m_p_head->m_p_parent = p_new_root; 257 p_new_root->m_p_parent = m_p_head; 258 apply_update(m_p_head->m_p_parent, (node_update* )this); 259 _GLIBCXX_DEBUG_ONLY(m_p_head->m_p_parent->assert_valid(this);) 260} 261