libstdc++
binary_heap_/erase_fn_imps.hpp
Go to the documentation of this file.
1// -*- C++ -*-
2
3// Copyright (C) 2005-2026 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 3, 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// 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.
19
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/>.
24
25// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
26
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
34// warranty.
35
36/**
37 * @file binary_heap_/erase_fn_imps.hpp
38 * Contains an implementation class for a binary_heap.
39 */
40
41#ifdef PB_DS_CLASS_C_DEC
42
43PB_DS_CLASS_T_DEC
44void
45PB_DS_CLASS_C_DEC::
46clear()
47{
48 for (size_type i = 0; i < m_size; ++i)
49 erase_at(m_a_entries, i, s_no_throw_copies_ind);
50
51 __try
52 {
53 const size_type new_size = resize_policy::get_new_size_for_arbitrary(0);
54 entry_pointer new_entries = s_entry_allocator.allocate(new_size);
55 resize_policy::notify_arbitrary(new_size);
56 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
57 m_actual_size = new_size;
58 m_a_entries = new_entries;
59 }
60 __catch(...)
61 { }
62
63 m_size = 0;
64 PB_DS_ASSERT_VALID((*this))
65}
66
67PB_DS_CLASS_T_DEC
68void
69PB_DS_CLASS_C_DEC::
70erase_at(entry_pointer a_entries, size_type i, false_type)
71{
72 a_entries[i]->~value_type();
73 s_value_allocator.deallocate(a_entries[i], 1);
74}
75
76PB_DS_CLASS_T_DEC
77void
78PB_DS_CLASS_C_DEC::
79erase_at(entry_pointer, size_type, true_type)
80{ }
81
82PB_DS_CLASS_T_DEC
83inline void
84PB_DS_CLASS_C_DEC::
85pop()
86{
87 PB_DS_ASSERT_VALID((*this))
88 _GLIBCXX_DEBUG_ASSERT(!empty());
89
90 pop_heap();
91 erase_at(m_a_entries, m_size - 1, s_no_throw_copies_ind);
92 resize_for_erase_if_needed();
93 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
94 --m_size;
95
96 PB_DS_ASSERT_VALID((*this))
97}
98
99PB_DS_CLASS_T_DEC
100template<typename Pred>
101typename PB_DS_CLASS_C_DEC::size_type
102PB_DS_CLASS_C_DEC::
103erase_if(Pred pred)
104{
105 PB_DS_ASSERT_VALID((*this))
106
107 typedef typename entry_pred<value_type, Pred, _Alloc, simple_value>::type
108 pred_t;
109
110 const size_type left = partition(pred_t(pred));
111 _GLIBCXX_DEBUG_ASSERT(m_size >= left);
112 const size_type ersd = m_size - left;
113 for (size_type i = left; i < m_size; ++i)
114 erase_at(m_a_entries, i, s_no_throw_copies_ind);
115
116 __try
117 {
118 const size_type new_size =
119 resize_policy::get_new_size_for_arbitrary(left);
120
121 entry_pointer new_entries = s_entry_allocator.allocate(new_size);
122 std::copy(m_a_entries, m_a_entries + left, new_entries);
123 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
124 m_a_entries = new_entries;
125 m_actual_size = new_size;
126 resize_policy::notify_arbitrary(m_actual_size);
127 }
128 __catch(...)
129 { };
130
131 m_size = left;
132 make_heap();
133 PB_DS_ASSERT_VALID((*this))
134 return ersd;
135}
136
137PB_DS_CLASS_T_DEC
138inline void
139PB_DS_CLASS_C_DEC::
140erase(point_iterator it)
141{
142 PB_DS_ASSERT_VALID((*this))
143 _GLIBCXX_DEBUG_ASSERT(!empty());
144
145 const size_type fix_pos = it.m_p_e - m_a_entries;
146 std::swap(*it.m_p_e, m_a_entries[m_size - 1]);
147 erase_at(m_a_entries, m_size - 1, s_no_throw_copies_ind);
148 resize_for_erase_if_needed();
149
150 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
151 --m_size;
152 _GLIBCXX_DEBUG_ASSERT(fix_pos <= m_size);
153
154 if (fix_pos != m_size)
155 fix(m_a_entries + fix_pos);
156
157 PB_DS_ASSERT_VALID((*this))
158}
159
160PB_DS_CLASS_T_DEC
161inline void
162PB_DS_CLASS_C_DEC::
163resize_for_erase_if_needed()
164{
165 if (!resize_policy::resize_needed_for_shrink(m_size))
166 return;
167
168 __try
169 {
170 const size_type new_size = resize_policy::get_new_size_for_shrink();
171 entry_pointer new_entries = s_entry_allocator.allocate(new_size);
172 resize_policy::notify_shrink_resize();
173
174 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
175 std::copy(m_a_entries, m_a_entries + m_size - 1, new_entries);
176 s_entry_allocator.deallocate(m_a_entries, m_actual_size);
177 m_actual_size = new_size;
178 m_a_entries = new_entries;
179 }
180 __catch(...)
181 { }
182}
183
184PB_DS_CLASS_T_DEC
185template<typename Pred>
186typename PB_DS_CLASS_C_DEC::size_type
187PB_DS_CLASS_C_DEC::
188partition(Pred pred)
189{
190 size_type left = 0;
191 size_type right = m_size - 1;
192
193 while (right + 1 != left)
194 {
195 _GLIBCXX_DEBUG_ASSERT(left <= m_size);
196
197 if (!pred(m_a_entries[left]))
198 ++left;
199 else if (pred(m_a_entries[right]))
200 --right;
201 else
202 {
203 _GLIBCXX_DEBUG_ASSERT(left < right);
204 std::swap(m_a_entries[left], m_a_entries[right]);
205 ++left;
206 --right;
207 }
208 }
209
210 return left;
211}
212#endif
ISO C++ entities toplevel namespace is std.
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition ios_base.h:1077
ios_base & right(ios_base &__base)
Calls base.setf(ios_base::right, ios_base::adjustfield).
Definition ios_base.h:1085