3 // Copyright (C) 2005, 2006 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 2, 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 // 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.
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
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
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
43 * @file erase_fn_imps.hpp
44 * Contains an implementation class for bin_search_tree_.
50 erase(const_key_reference r_key)
52 node_pointer p_nd = find_imp(r_key);
53 if (p_nd == NULL || p_nd->m_type == pat_trie_internal_node_type)
55 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(r_key));
59 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
60 if (!synth_e_access_traits::equal_keys(PB_DS_V2F(reinterpret_cast<leaf_pointer>(p_nd)->value()), r_key))
62 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_does_not_exist(r_key));
66 _GLIBCXX_DEBUG_ONLY(map_debug_base::check_key_exists(r_key));
67 erase_leaf(static_cast<leaf_pointer>(p_nd));
68 _GLIBCXX_DEBUG_ONLY(assert_valid();)
75 erase_fixup(internal_node_pointer p_nd)
77 _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) >= 1);
78 if (std::distance(p_nd->begin(), p_nd->end()) == 1)
80 node_pointer p_parent = p_nd->m_p_parent;
81 if (p_parent == m_p_head)
82 m_p_head->m_p_parent =* p_nd->begin();
85 _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
86 node_pointer p_new_child =* p_nd->begin();
87 static_cast<internal_node_pointer>(p_parent)->replace_child(
89 pref_begin(p_new_child),
90 pref_end(p_new_child),
93 (*p_nd->begin())->m_p_parent = p_nd->m_p_parent;
94 p_nd->~internal_node();
95 s_internal_node_allocator.deallocate(p_nd, 1);
97 if (p_parent == m_p_head)
100 _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
101 p_nd = static_cast<internal_node_pointer>(p_parent);
106 _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) > 1);
107 p_nd->update_prefixes(this);
108 apply_update(p_nd, (node_update* )this);
109 _GLIBCXX_DEBUG_ONLY(p_nd->assert_valid(this);)
110 if (p_nd->m_p_parent->m_type == pat_trie_head_node_type)
113 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_parent->m_type ==
114 pat_trie_internal_node_type);
116 p_nd = static_cast<internal_node_pointer>(p_nd->m_p_parent);
123 actual_erase_leaf(leaf_pointer p_l)
125 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
127 _GLIBCXX_DEBUG_ONLY(erase_existing(PB_DS_V2F(p_l->value())));
129 s_leaf_allocator.deallocate(p_l, 1);
137 _GLIBCXX_DEBUG_ONLY(assert_valid();)
141 clear_imp(m_p_head->m_p_parent);
144 _GLIBCXX_DEBUG_ONLY(map_debug_base::clear();)
145 _GLIBCXX_DEBUG_ONLY(assert_valid();)
151 clear_imp(node_pointer p_nd)
153 if (p_nd->m_type == pat_trie_internal_node_type)
155 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
156 for (typename internal_node::iterator it =
157 static_cast<internal_node_pointer>(p_nd)->begin();
158 it != static_cast<internal_node_pointer>(p_nd)->end();
161 node_pointer p_child =* it;
164 s_internal_node_allocator.deallocate(static_cast<internal_node_pointer>(p_nd), 1);
168 _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
169 static_cast<leaf_pointer>(p_nd)->~leaf();
170 s_leaf_allocator.deallocate(static_cast<leaf_pointer>(p_nd), 1);
174 inline typename PB_DS_CLASS_C_DEC::const_iterator
176 erase(const_iterator it)
178 _GLIBCXX_DEBUG_ONLY(assert_valid());
183 const_iterator ret_it = it;
185 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
186 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
187 _GLIBCXX_DEBUG_ONLY(assert_valid());
191 #ifdef PB_DS_DATA_TRUE_INDICATOR
193 inline typename PB_DS_CLASS_C_DEC::iterator
197 _GLIBCXX_DEBUG_ONLY(assert_valid());
201 iterator ret_it = it;
203 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
204 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
205 _GLIBCXX_DEBUG_ONLY(assert_valid());
208 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
211 inline typename PB_DS_CLASS_C_DEC::const_reverse_iterator
213 erase(const_reverse_iterator it)
215 _GLIBCXX_DEBUG_ONLY(assert_valid());
217 if (it.m_p_nd == m_p_head)
219 const_reverse_iterator ret_it = it;
222 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
223 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
224 _GLIBCXX_DEBUG_ONLY(assert_valid());
228 #ifdef PB_DS_DATA_TRUE_INDICATOR
230 inline typename PB_DS_CLASS_C_DEC::reverse_iterator
232 erase(reverse_iterator it)
234 _GLIBCXX_DEBUG_ONLY(assert_valid());
236 if (it.m_p_nd == m_p_head)
238 reverse_iterator ret_it = it;
241 _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
242 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
243 _GLIBCXX_DEBUG_ONLY(assert_valid());
246 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
249 template<typename Pred>
250 inline typename PB_DS_CLASS_C_DEC::size_type
254 size_type num_ersd = 0;
255 _GLIBCXX_DEBUG_ONLY(assert_valid();)
257 iterator it = begin();
260 _GLIBCXX_DEBUG_ONLY(assert_valid();)
270 _GLIBCXX_DEBUG_ONLY(assert_valid();)
277 erase_leaf(leaf_pointer p_l)
279 update_min_max_for_erased_leaf(p_l);
280 if (p_l->m_p_parent->m_type == pat_trie_head_node_type)
282 _GLIBCXX_DEBUG_ASSERT(size() == 1);
287 _GLIBCXX_DEBUG_ASSERT(size() > 1);
288 _GLIBCXX_DEBUG_ASSERT(p_l->m_p_parent->m_type ==
289 pat_trie_internal_node_type);
291 internal_node_pointer p_parent =
292 static_cast<internal_node_pointer>(p_l->m_p_parent);
294 p_parent->remove_child(p_l);
295 erase_fixup(p_parent);
296 actual_erase_leaf(p_l);
302 update_min_max_for_erased_leaf(leaf_pointer p_l)
306 m_p_head->m_p_min = m_p_head;
307 m_p_head->m_p_max = m_p_head;
311 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_min))
315 m_p_head->m_p_min = it.m_p_nd;
319 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_max))
323 m_p_head->m_p_max = it.m_p_nd;