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);
54 if (p_nd == NULL || p_nd->m_type == pat_trie_internal_node_type)
56 PB_DS_DBG_ONLY(map_debug_base::check_key_does_not_exist(
62 PB_DS_DBG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
64 if (!synth_e_access_traits::equal_keys(
65 PB_DS_V2F(reinterpret_cast<leaf_pointer>(p_nd)->value()),
68 PB_DS_DBG_ONLY(map_debug_base::check_key_does_not_exist(
74 PB_DS_DBG_ONLY(map_debug_base::check_key_exists(r_key));
76 erase_leaf(static_cast<leaf_pointer>(p_nd));
78 PB_DS_DBG_ONLY(assert_valid();)
86 erase_fixup(internal_node_pointer p_nd)
88 PB_DS_DBG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) >= 1);
90 if (std::distance(p_nd->begin(), p_nd->end()) == 1)
92 node_pointer p_parent = p_nd->m_p_parent;
94 if (p_parent == m_p_head)
95 m_p_head->m_p_parent =* p_nd->begin();
98 PB_DS_DBG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
100 node_pointer p_new_child =* p_nd->begin();
102 static_cast<internal_node_pointer>(p_parent)->replace_child(
104 pref_begin(p_new_child),
105 pref_end(p_new_child),
108 (*p_nd->begin())->m_p_parent = p_nd->m_p_parent;
110 p_nd->~internal_node();
112 s_internal_node_allocator.deallocate(p_nd, 1);
114 if (p_parent == m_p_head)
117 PB_DS_DBG_ASSERT(p_parent->m_type == pat_trie_internal_node_type);
119 p_nd = static_cast<internal_node_pointer>(p_parent);
124 PB_DS_DBG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) > 1);
126 p_nd->update_prefixes(this);
128 apply_update(p_nd, (node_update* )this);
130 PB_DS_DBG_ONLY(p_nd->assert_valid(this);)
132 if (p_nd->m_p_parent->m_type == pat_trie_head_node_type)
135 PB_DS_DBG_ASSERT(p_nd->m_p_parent->m_type ==
136 pat_trie_internal_node_type);
138 p_nd = static_cast<internal_node_pointer>(p_nd->m_p_parent);
145 actual_erase_leaf(leaf_pointer p_l)
147 PB_DS_DBG_ASSERT(m_size > 0);
150 PB_DS_DBG_ONLY(erase_existing(PB_DS_V2F(p_l->value())));
154 s_leaf_allocator.deallocate(p_l, 1);
162 PB_DS_DBG_ONLY(assert_valid();)
167 clear_imp(m_p_head->m_p_parent);
173 PB_DS_DBG_ONLY(map_debug_base::clear();)
175 PB_DS_DBG_ONLY(assert_valid();)
181 clear_imp(node_pointer p_nd)
183 if (p_nd->m_type == pat_trie_internal_node_type)
185 PB_DS_DBG_ASSERT(p_nd->m_type == pat_trie_internal_node_type);
187 for (typename internal_node::iterator it =
188 static_cast<internal_node_pointer>(p_nd)->begin();
189 it != static_cast<internal_node_pointer>(p_nd)->end();
192 node_pointer p_child =* it;
197 s_internal_node_allocator.deallocate(
198 static_cast<internal_node_pointer>(p_nd), 1);
203 PB_DS_DBG_ASSERT(p_nd->m_type == pat_trie_leaf_node_type);
205 static_cast<leaf_pointer>(p_nd)->~leaf();
207 s_leaf_allocator.deallocate(
208 static_cast<leaf_pointer>(p_nd), 1);
212 inline typename PB_DS_CLASS_C_DEC::const_iterator
214 erase(const_iterator it)
216 PB_DS_DBG_ONLY(assert_valid());
221 const_iterator ret_it = it;
225 PB_DS_DBG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
227 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
229 PB_DS_DBG_ONLY(assert_valid());
234 #ifdef PB_DS_DATA_TRUE_INDICATOR
236 inline typename PB_DS_CLASS_C_DEC::iterator
240 PB_DS_DBG_ONLY(assert_valid());
245 iterator ret_it = it;
249 PB_DS_DBG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
251 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
253 PB_DS_DBG_ONLY(assert_valid());
257 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
260 inline typename PB_DS_CLASS_C_DEC::const_reverse_iterator
262 erase(const_reverse_iterator it)
264 PB_DS_DBG_ONLY(assert_valid());
266 if (it.m_p_nd == m_p_head)
269 const_reverse_iterator ret_it = it;
273 PB_DS_DBG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
275 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
277 PB_DS_DBG_ONLY(assert_valid());
282 #ifdef PB_DS_DATA_TRUE_INDICATOR
284 inline typename PB_DS_CLASS_C_DEC::reverse_iterator
286 erase(reverse_iterator it)
288 PB_DS_DBG_ONLY(assert_valid());
290 if (it.m_p_nd == m_p_head)
293 reverse_iterator ret_it = it;
297 PB_DS_DBG_ASSERT(it.m_p_nd->m_type == pat_trie_leaf_node_type);
299 erase_leaf(static_cast<leaf_pointer>(it.m_p_nd));
301 PB_DS_DBG_ONLY(assert_valid());
305 #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR
308 template<typename Pred>
309 inline typename PB_DS_CLASS_C_DEC::size_type
313 size_type num_ersd = 0;
315 PB_DS_DBG_ONLY(assert_valid();)
317 iterator it = begin();
321 PB_DS_DBG_ONLY(assert_valid();)
333 PB_DS_DBG_ONLY(assert_valid();)
341 erase_leaf(leaf_pointer p_l)
343 update_min_max_for_erased_leaf(p_l);
345 if (p_l->m_p_parent->m_type == pat_trie_head_node_type)
347 PB_DS_DBG_ASSERT(size() == 1);
354 PB_DS_DBG_ASSERT(size() > 1);
356 PB_DS_DBG_ASSERT(p_l->m_p_parent->m_type ==
357 pat_trie_internal_node_type);
359 internal_node_pointer p_parent =
360 static_cast<internal_node_pointer>(p_l->m_p_parent);
362 p_parent->remove_child(p_l);
364 erase_fixup(p_parent);
366 actual_erase_leaf(p_l);
372 update_min_max_for_erased_leaf(leaf_pointer p_l)
376 m_p_head->m_p_min = m_p_head;
377 m_p_head->m_p_max = m_p_head;
382 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_min))
388 m_p_head->m_p_min = it.m_p_nd;
393 if (p_l == static_cast<const_leaf_pointer>(m_p_head->m_p_max))
399 m_p_head->m_p_max = it.m_p_nd;