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 a binary_heap.
52 for (size_type i = 0; i < m_size; ++i)
53 erase_at(m_a_entries, i, s_no_throw_copies_ind);
57 const size_type actual_size = resize_policy::get_new_size_for_arbitrary(0);
59 entry_pointer a_entries = s_entry_allocator.allocate(actual_size);
61 resize_policy::notify_arbitrary(actual_size);
63 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
65 m_actual_size = actual_size;
67 m_a_entries = a_entries;
74 _GLIBCXX_DEBUG_ONLY(assert_valid();)
80 erase_at(entry_pointer a_entries, size_type i, false_type)
82 a_entries[i]->~value_type();
84 s_value_allocator.deallocate(a_entries[i], 1);
90 erase_at(entry_pointer, size_type, true_type)
98 _GLIBCXX_DEBUG_ONLY(assert_valid();)
99 _GLIBCXX_DEBUG_ASSERT(!empty());
101 erase_at(m_a_entries, 0, s_no_throw_copies_ind);
103 std::pop_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
105 resize_for_erase_if_needed();
107 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
110 _GLIBCXX_DEBUG_ONLY(assert_valid();)
114 template<typename Pred>
115 typename PB_DS_CLASS_C_DEC::size_type
119 _GLIBCXX_DEBUG_ONLY(assert_valid();)
129 const size_type left = partition(pred_t(pred));
131 _GLIBCXX_DEBUG_ASSERT(m_size >= left);
133 const size_type ersd = m_size - left;
135 for (size_type i = left; i < m_size; ++i)
136 erase_at(m_a_entries, i, s_no_throw_copies_ind);
140 const size_type actual_size =
141 resize_policy::get_new_size_for_arbitrary(left);
143 entry_pointer a_entries = s_entry_allocator.allocate(actual_size);
145 std::copy(m_a_entries, m_a_entries + left, a_entries);
147 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
149 m_actual_size = actual_size;
151 resize_policy::notify_arbitrary(m_actual_size);
158 std::make_heap(m_a_entries, m_a_entries + m_size, static_cast<entry_cmp& >(*this));
160 _GLIBCXX_DEBUG_ONLY(assert_valid();)
168 erase(point_iterator it)
170 _GLIBCXX_DEBUG_ONLY(assert_valid();)
171 _GLIBCXX_DEBUG_ASSERT(!empty());
173 const size_type fix_pos = it.m_p_e - m_a_entries;
175 std::swap(*it.m_p_e, m_a_entries[m_size - 1]);
177 erase_at(m_a_entries, m_size - 1, s_no_throw_copies_ind);
179 resize_for_erase_if_needed();
181 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
184 _GLIBCXX_DEBUG_ASSERT(fix_pos <= m_size);
186 if (fix_pos != m_size)
187 fix(m_a_entries + fix_pos);
189 _GLIBCXX_DEBUG_ONLY(assert_valid();)
195 resize_for_erase_if_needed()
197 if (!resize_policy::resize_needed_for_shrink(m_size))
202 const size_type new_actual_size =
203 resize_policy::get_new_size_for_shrink();
205 entry_pointer a_new_entries = s_entry_allocator.allocate(new_actual_size);
207 resize_policy::notify_shrink_resize();
209 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
210 std::copy(m_a_entries, m_a_entries + m_size - 1, a_new_entries);
212 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
214 m_actual_size = new_actual_size;
216 m_a_entries = a_new_entries;
223 template<typename Pred>
224 typename PB_DS_CLASS_C_DEC::size_type
229 size_type right = m_size - 1;
231 while (right + 1 != left)
233 _GLIBCXX_DEBUG_ASSERT(left <= m_size);
235 if (!pred(m_a_entries[left]))
237 else if (pred(m_a_entries[right]))
241 _GLIBCXX_DEBUG_ASSERT(left < right);
243 std::swap(m_a_entries[left], m_a_entries[right]);