]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
99dee823 | 3 | // Copyright (C) 2005-2021 Free Software Foundation, Inc. |
4569a895 AT |
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 | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
4569a895 AT |
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 | ||
748086b7 JJ |
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/>. | |
4569a895 AT |
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 | /** | |
a345e45d | 37 | * @file binary_heap_/erase_fn_imps.hpp |
4569a895 AT |
38 | * Contains an implementation class for a binary_heap. |
39 | */ | |
40 | ||
574dfb67 JW |
41 | #ifdef PB_DS_CLASS_C_DEC |
42 | ||
4569a895 AT |
43 | PB_DS_CLASS_T_DEC |
44 | void | |
45 | PB_DS_CLASS_C_DEC:: | |
46 | clear() | |
47 | { | |
48 | for (size_type i = 0; i < m_size; ++i) | |
49 | erase_at(m_a_entries, i, s_no_throw_copies_ind); | |
50 | ||
bc2631e0 | 51 | __try |
4569a895 | 52 | { |
a345e45d BK |
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); | |
4569a895 | 56 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); |
a345e45d BK |
57 | m_actual_size = new_size; |
58 | m_a_entries = new_entries; | |
4569a895 | 59 | } |
bc2631e0 | 60 | __catch(...) |
4569a895 AT |
61 | { } |
62 | ||
63 | m_size = 0; | |
f5886803 | 64 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 65 | } |
4569a895 AT |
66 | |
67 | PB_DS_CLASS_T_DEC | |
68 | void | |
69 | PB_DS_CLASS_C_DEC:: | |
70 | erase_at(entry_pointer a_entries, size_type i, false_type) | |
71 | { | |
72 | a_entries[i]->~value_type(); | |
4569a895 AT |
73 | s_value_allocator.deallocate(a_entries[i], 1); |
74 | } | |
75 | ||
76 | PB_DS_CLASS_T_DEC | |
77 | void | |
78 | PB_DS_CLASS_C_DEC:: | |
79 | erase_at(entry_pointer, size_type, true_type) | |
80 | { } | |
81 | ||
82 | PB_DS_CLASS_T_DEC | |
83 | inline void | |
84 | PB_DS_CLASS_C_DEC:: | |
85 | pop() | |
86 | { | |
f5886803 | 87 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 88 | _GLIBCXX_DEBUG_ASSERT(!empty()); |
4569a895 | 89 | |
a345e45d BK |
90 | pop_heap(); |
91 | erase_at(m_a_entries, m_size - 1, s_no_throw_copies_ind); | |
4569a895 | 92 | resize_for_erase_if_needed(); |
47bea7b8 | 93 | _GLIBCXX_DEBUG_ASSERT(m_size > 0); |
4569a895 AT |
94 | --m_size; |
95 | ||
f5886803 | 96 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 97 | } |
4569a895 AT |
98 | |
99 | PB_DS_CLASS_T_DEC | |
100 | template<typename Pred> | |
101 | typename PB_DS_CLASS_C_DEC::size_type | |
102 | PB_DS_CLASS_C_DEC:: | |
103 | erase_if(Pred pred) | |
104 | { | |
f5886803 | 105 | PB_DS_ASSERT_VALID((*this)) |
4569a895 | 106 | |
a345e45d | 107 | typedef typename entry_pred<value_type, Pred, _Alloc, simple_value>::type |
4569a895 AT |
108 | pred_t; |
109 | ||
110 | const size_type left = partition(pred_t(pred)); | |
47bea7b8 | 111 | _GLIBCXX_DEBUG_ASSERT(m_size >= left); |
4569a895 | 112 | const size_type ersd = m_size - left; |
4569a895 AT |
113 | for (size_type i = left; i < m_size; ++i) |
114 | erase_at(m_a_entries, i, s_no_throw_copies_ind); | |
115 | ||
bc2631e0 | 116 | __try |
4569a895 | 117 | { |
a345e45d | 118 | const size_type new_size = |
4569a895 AT |
119 | resize_policy::get_new_size_for_arbitrary(left); |
120 | ||
a345e45d BK |
121 | entry_pointer new_entries = s_entry_allocator.allocate(new_size); |
122 | std::copy(m_a_entries, m_a_entries + left, new_entries); | |
4569a895 | 123 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); |
a345e45d | 124 | m_actual_size = new_size; |
4569a895 AT |
125 | resize_policy::notify_arbitrary(m_actual_size); |
126 | } | |
bc2631e0 | 127 | __catch(...) |
4569a895 AT |
128 | { }; |
129 | ||
130 | m_size = left; | |
a345e45d | 131 | make_heap(); |
f5886803 | 132 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 133 | return ersd; |
4569a895 AT |
134 | } |
135 | ||
136 | PB_DS_CLASS_T_DEC | |
137 | inline void | |
138 | PB_DS_CLASS_C_DEC:: | |
139 | erase(point_iterator it) | |
140 | { | |
f5886803 | 141 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 142 | _GLIBCXX_DEBUG_ASSERT(!empty()); |
4569a895 AT |
143 | |
144 | const size_type fix_pos = it.m_p_e - m_a_entries; | |
4569a895 | 145 | std::swap(*it.m_p_e, m_a_entries[m_size - 1]); |
4569a895 | 146 | erase_at(m_a_entries, m_size - 1, s_no_throw_copies_ind); |
4569a895 AT |
147 | resize_for_erase_if_needed(); |
148 | ||
47bea7b8 | 149 | _GLIBCXX_DEBUG_ASSERT(m_size > 0); |
4569a895 | 150 | --m_size; |
47bea7b8 | 151 | _GLIBCXX_DEBUG_ASSERT(fix_pos <= m_size); |
4569a895 AT |
152 | |
153 | if (fix_pos != m_size) | |
154 | fix(m_a_entries + fix_pos); | |
155 | ||
f5886803 | 156 | PB_DS_ASSERT_VALID((*this)) |
191e7a30 | 157 | } |
4569a895 AT |
158 | |
159 | PB_DS_CLASS_T_DEC | |
160 | inline void | |
161 | PB_DS_CLASS_C_DEC:: | |
162 | resize_for_erase_if_needed() | |
163 | { | |
164 | if (!resize_policy::resize_needed_for_shrink(m_size)) | |
165 | return; | |
166 | ||
bc2631e0 | 167 | __try |
4569a895 | 168 | { |
a345e45d BK |
169 | const size_type new_size = resize_policy::get_new_size_for_shrink(); |
170 | entry_pointer new_entries = s_entry_allocator.allocate(new_size); | |
4569a895 AT |
171 | resize_policy::notify_shrink_resize(); |
172 | ||
47bea7b8 | 173 | _GLIBCXX_DEBUG_ASSERT(m_size > 0); |
a345e45d | 174 | std::copy(m_a_entries, m_a_entries + m_size - 1, new_entries); |
4569a895 | 175 | s_entry_allocator.deallocate(m_a_entries, m_actual_size); |
a345e45d BK |
176 | m_actual_size = new_size; |
177 | m_a_entries = new_entries; | |
4569a895 | 178 | } |
bc2631e0 | 179 | __catch(...) |
4569a895 AT |
180 | { } |
181 | } | |
182 | ||
183 | PB_DS_CLASS_T_DEC | |
184 | template<typename Pred> | |
185 | typename PB_DS_CLASS_C_DEC::size_type | |
186 | PB_DS_CLASS_C_DEC:: | |
187 | partition(Pred pred) | |
188 | { | |
189 | size_type left = 0; | |
190 | size_type right = m_size - 1; | |
191 | ||
192 | while (right + 1 != left) | |
193 | { | |
47bea7b8 | 194 | _GLIBCXX_DEBUG_ASSERT(left <= m_size); |
4569a895 AT |
195 | |
196 | if (!pred(m_a_entries[left])) | |
197 | ++left; | |
198 | else if (pred(m_a_entries[right])) | |
199 | --right; | |
200 | else | |
191e7a30 | 201 | { |
47bea7b8 | 202 | _GLIBCXX_DEBUG_ASSERT(left < right); |
4569a895 | 203 | std::swap(m_a_entries[left], m_a_entries[right]); |
4569a895 AT |
204 | ++left; |
205 | --right; | |
191e7a30 | 206 | } |
4569a895 AT |
207 | } |
208 | ||
209 | return left; | |
210 | } | |
574dfb67 | 211 | #endif |