3 // Copyright (C) 2005, 2006, 2009 Free Software Foundation, Inc.
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 3, or (at your option) any later
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.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
27 // Permission to use, copy, modify, sell, and distribute this software
28 // is hereby granted without fee, provided that the above copyright
29 // notice appears in all copies, and that both that copyright notice
30 // and this permission notice appear in supporting documentation. None
31 // of the above authors, nor IBM Haifa Research Laboratories, make any
32 // representation about the suitability of this software for any
33 // purpose. It is provided "as is" without express or implied
37 * @file erase_fn_imps.hpp
38 * Contains an implementation class for bin_search_tree_.
44 erase(const_key_reference r_key)
46 node_pointer p_nd = find_imp(r_key);
47 if (p_nd == NULL || p_nd->m_type == pat_trie_internal_node_type)
49 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key));
53 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
54 if (!synth_e_access_traits::equal_keys(PB_DS_V2F(reinterpret_cast<leaf_pointer>(p_nd)->value()), r_key))
56 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_does_not_exist(r_key));
60 _GLIBCXX_DEBUG_ONLY(debug_base::check_key_exists(r_key));
61 erase_leaf(static_cast<leaf_pointer>(p_nd));
62 _GLIBCXX_DEBUG_ONLY(assert_valid();)
69 erase_fixup(internal_node_pointer p_nd)
71 _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) >= 1);
72 if (std::distance(p_nd->begin(), p_nd->end()) == 1)
74 node_pointer p_parent = p_nd->m_p_parent;
75 if (p_parent == m_p_head)
76 m_p_head->m_p_parent =* p_nd->begin();
79 _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
80 node_pointer p_new_child =* p_nd->begin();
81 static_cast<internal_node_pointer>(p_parent)->replace_child(
83 pref_begin(p_new_child),
84 pref_end(p_new_child),
87 (*p_nd->begin())->m_p_parent = p_nd->m_p_parent;
88 p_nd->~internal_node();
89 s_internal_node_allocator.deallocate(p_nd, 1);
91 if (p_parent == m_p_head)
94 _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
95 p_nd = static_cast<internal_node_pointer>(p_parent);
100 _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) > 1);
101 p_nd->update_prefixes(this);
102 apply_update(p_nd, (node_update* )this);
103 _GLIBCXX_DEBUG_ONLY(p_nd->assert_valid(this);)
104 if (p_nd->m_p_parent->m_type == pat_trie_head_node_type)
107 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_parent->m_type ==
108 pat_trie_internal_node_type);
110 p_nd = static_cast<internal_node_pointer>(p_nd->m_p_parent);
117 actual_erase_leaf(leaf_pointer p_l)
119 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
121 _GLIBCXX_DEBUG_ONLY(erase_existing(PB_DS_V2F(p_l->value())));
123 s_leaf_allocator.deallocate(p_l, 1);
131 _GLIBCXX_DEBUG_ONLY(assert_valid();)
135 clear_imp(m_p_head->m_p_parent);
138 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
139 _GLIBCXX_DEBUG_ONLY(assert_valid();)
145 clear_imp(node_pointer p_nd)
147 if (p_nd->m_type == pat_trie_internal_node_type)
149 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
150 for (typename internal_node::iterator it =
151 static_cast<internal_node_pointer>(p_nd)->begin();
152 it != static_cast<internal_node_pointer>(p_nd)->end();
155 node_pointer p_child =* it;
158 s_internal_node_allocator.deallocate(static_cast<internal_node_pointer>(p_nd), 1);
162 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
163 static_cast<leaf_pointer>(p_nd)->~leaf();
164 s_leaf_allocator.deallocate(static_cast<leaf_pointer>(p_nd), 1);
168 inline typename PB_DS_CLASS_C_DEC::const_iterator
170 erase(const_iterator it)
172 _GLIBCXX_DEBUG_ONLY(assert_valid());
177 const_iterator ret_it = it;
179 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
180 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
181 _GLIBCXX_DEBUG_ONLY(assert_valid());
185 #ifdef PB_DS_DATA_TRUE_INDICATOR
187 inline typename PB_DS_CLASS_C_DEC::iterator
191 _GLIBCXX_DEBUG_ONLY(assert_valid());
195 iterator ret_it = it;
197 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
198 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
199 _GLIBCXX_DEBUG_ONLY(assert_valid());
202 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
205 inline typename PB_DS_CLASS_C_DEC::const_reverse_iterator
207 erase(const_reverse_iterator it)
209 _GLIBCXX_DEBUG_ONLY(assert_valid());
211 if (it.m_p_nd == m_p_head)
213 const_reverse_iterator ret_it = it;
216 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
217 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
218 _GLIBCXX_DEBUG_ONLY(assert_valid());
222 #ifdef PB_DS_DATA_TRUE_INDICATOR
224 inline typename PB_DS_CLASS_C_DEC::reverse_iterator
226 erase(reverse_iterator it)
228 _GLIBCXX_DEBUG_ONLY(assert_valid());
230 if (it.m_p_nd == m_p_head)
232 reverse_iterator ret_it = it;
235 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
236 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
237 _GLIBCXX_DEBUG_ONLY(assert_valid());
240 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
243 template<typename Pred>
244 inline typename PB_DS_CLASS_C_DEC::size_type
248 size_type num_ersd = 0;
249 _GLIBCXX_DEBUG_ONLY(assert_valid();)
251 iterator it = begin();
254 _GLIBCXX_DEBUG_ONLY(assert_valid();)
264 _GLIBCXX_DEBUG_ONLY(assert_valid();)
271 erase_leaf(leaf_pointer p_l)
273 update_min_max_for_erased_leaf(p_l);
274 if (p_l->m_p_parent->m_type == pat_trie_head_node_type)
276 _GLIBCXX_DEBUG_ASSERT(size() == 1);
281 _GLIBCXX_DEBUG_ASSERT(size() > 1);
282 _GLIBCXX_DEBUG_ASSERT(p_l->m_p_parent->m_type ==
283 pat_trie_internal_node_type);
285 internal_node_pointer p_parent =
286 static_cast<internal_node_pointer>(p_l->m_p_parent);
288 p_parent->remove_child(p_l);
289 erase_fixup(p_parent);
290 actual_erase_leaf(p_l);
296 update_min_max_for_erased_leaf(leaf_pointer p_l)
300 m_p_head->m_p_min = m_p_head;
301 m_p_head->m_p_max = m_p_head;
305 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_min))
309 m_p_head->m_p_min = it.m_p_nd;
313 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_max))
317 m_p_head->m_p_max = it.m_p_nd;