]>
Commit | Line | Data |
---|---|---|
4569a895 AT |
1 | // -*- C++ -*- |
2 | ||
83ffe9cd | 3 | // Copyright (C) 2005-2023 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. | |
4569a895 | 19 | |
748086b7 JJ |
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 BK |
37 | * @file pat_trie_/erase_fn_imps.hpp |
38 | * Contains an implementation class for pat_trie. | |
4569a895 AT |
39 | */ |
40 | ||
574dfb67 JW |
41 | #ifdef PB_DS_CLASS_C_DEC |
42 | ||
4569a895 AT |
43 | PB_DS_CLASS_T_DEC |
44 | inline bool | |
45 | PB_DS_CLASS_C_DEC:: | |
a345e45d | 46 | erase(key_const_reference r_key) |
4569a895 AT |
47 | { |
48 | node_pointer p_nd = find_imp(r_key); | |
a345e45d | 49 | if (p_nd == 0 || p_nd->m_type == i_node) |
4569a895 | 50 | { |
f5886803 | 51 | PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key) |
1b24692f | 52 | return false; |
4569a895 AT |
53 | } |
54 | ||
a345e45d BK |
55 | _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == leaf_node); |
56 | if (!synth_access_traits::equal_keys(PB_DS_V2F(reinterpret_cast<leaf_pointer>(p_nd)->value()), r_key)) | |
4569a895 | 57 | { |
f5886803 | 58 | PB_DS_CHECK_KEY_DOES_NOT_EXIST(r_key) |
1b24692f | 59 | return false; |
4569a895 AT |
60 | } |
61 | ||
f5886803 | 62 | PB_DS_CHECK_KEY_EXISTS(r_key) |
4569a895 | 63 | erase_leaf(static_cast<leaf_pointer>(p_nd)); |
f5886803 | 64 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 65 | return true; |
4569a895 AT |
66 | } |
67 | ||
68 | PB_DS_CLASS_T_DEC | |
69 | void | |
70 | PB_DS_CLASS_C_DEC:: | |
a345e45d | 71 | erase_fixup(inode_pointer p_nd) |
4569a895 | 72 | { |
47bea7b8 | 73 | _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) >= 1); |
4569a895 AT |
74 | if (std::distance(p_nd->begin(), p_nd->end()) == 1) |
75 | { | |
76 | node_pointer p_parent = p_nd->m_p_parent; | |
4569a895 | 77 | if (p_parent == m_p_head) |
a345e45d | 78 | m_p_head->m_p_parent = *p_nd->begin(); |
4569a895 | 79 | else |
a345e45d BK |
80 | { |
81 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == i_node); | |
82 | node_pointer p_new_child = *p_nd->begin(); | |
83 | ||
84 | typedef inode_pointer inode_ptr; | |
85 | inode_ptr p_internal = static_cast<inode_ptr>(p_parent); | |
86 | p_internal->replace_child(p_new_child, pref_begin(p_new_child), | |
87 | pref_end(p_new_child), this); | |
88 | } | |
4569a895 | 89 | (*p_nd->begin())->m_p_parent = p_nd->m_p_parent; |
a345e45d BK |
90 | p_nd->~inode(); |
91 | s_inode_allocator.deallocate(p_nd, 1); | |
4569a895 AT |
92 | |
93 | if (p_parent == m_p_head) | |
94 | return; | |
95 | ||
a345e45d BK |
96 | _GLIBCXX_DEBUG_ASSERT(p_parent->m_type == i_node); |
97 | p_nd = static_cast<inode_pointer>(p_parent); | |
4569a895 AT |
98 | } |
99 | ||
100 | while (true) | |
101 | { | |
47bea7b8 | 102 | _GLIBCXX_DEBUG_ASSERT(std::distance(p_nd->begin(), p_nd->end()) > 1); |
4569a895 | 103 | p_nd->update_prefixes(this); |
a345e45d | 104 | apply_update(p_nd, (node_update*)this); |
f5886803 | 105 | PB_DS_ASSERT_NODE_VALID(p_nd) |
a345e45d BK |
106 | if (p_nd->m_p_parent->m_type == head_node) |
107 | return; | |
4569a895 | 108 | |
a345e45d | 109 | _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_parent->m_type == i_node); |
4569a895 | 110 | |
a345e45d | 111 | p_nd = static_cast<inode_pointer>(p_nd->m_p_parent); |
4569a895 AT |
112 | } |
113 | } | |
114 | ||
115 | PB_DS_CLASS_T_DEC | |
116 | inline void | |
117 | PB_DS_CLASS_C_DEC:: | |
118 | actual_erase_leaf(leaf_pointer p_l) | |
119 | { | |
47bea7b8 | 120 | _GLIBCXX_DEBUG_ASSERT(m_size > 0); |
4569a895 | 121 | --m_size; |
a345e45d | 122 | _GLIBCXX_DEBUG_ONLY(debug_base::erase_existing(PB_DS_V2F(p_l->value()))); |
4569a895 | 123 | p_l->~leaf(); |
4569a895 AT |
124 | s_leaf_allocator.deallocate(p_l, 1); |
125 | } | |
126 | ||
127 | PB_DS_CLASS_T_DEC | |
128 | void | |
129 | PB_DS_CLASS_C_DEC:: | |
130 | clear() | |
131 | { | |
a345e45d BK |
132 | if (!empty()) |
133 | { | |
134 | clear_imp(m_p_head->m_p_parent); | |
135 | m_size = 0; | |
136 | initialize(); | |
137 | _GLIBCXX_DEBUG_ONLY(debug_base::clear();) | |
138 | PB_DS_ASSERT_VALID((*this)) | |
139 | } | |
1b24692f | 140 | } |
4569a895 AT |
141 | |
142 | PB_DS_CLASS_T_DEC | |
143 | void | |
144 | PB_DS_CLASS_C_DEC:: | |
145 | clear_imp(node_pointer p_nd) | |
146 | { | |
a345e45d | 147 | if (p_nd->m_type == i_node) |
4569a895 | 148 | { |
a345e45d BK |
149 | _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == i_node); |
150 | for (typename inode::iterator it = | |
151 | static_cast<inode_pointer>(p_nd)->begin(); | |
152 | it != static_cast<inode_pointer>(p_nd)->end(); | |
4569a895 | 153 | ++it) |
a345e45d | 154 | { |
4569a895 | 155 | node_pointer p_child =* it; |
4569a895 | 156 | clear_imp(p_child); |
a345e45d BK |
157 | } |
158 | s_inode_allocator.deallocate(static_cast<inode_pointer>(p_nd), 1); | |
4569a895 AT |
159 | return; |
160 | } | |
161 | ||
a345e45d | 162 | _GLIBCXX_DEBUG_ASSERT(p_nd->m_type == leaf_node); |
4569a895 | 163 | static_cast<leaf_pointer>(p_nd)->~leaf(); |
1b24692f | 164 | s_leaf_allocator.deallocate(static_cast<leaf_pointer>(p_nd), 1); |
4569a895 AT |
165 | } |
166 | ||
167 | PB_DS_CLASS_T_DEC | |
168 | inline typename PB_DS_CLASS_C_DEC::const_iterator | |
169 | PB_DS_CLASS_C_DEC:: | |
170 | erase(const_iterator it) | |
171 | { | |
f5886803 | 172 | PB_DS_ASSERT_VALID((*this)) |
4569a895 AT |
173 | |
174 | if (it == end()) | |
1b24692f | 175 | return it; |
4569a895 AT |
176 | |
177 | const_iterator ret_it = it; | |
4569a895 | 178 | ++ret_it; |
a345e45d | 179 | _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == leaf_node); |
4569a895 | 180 | erase_leaf(static_cast<leaf_pointer>(it.m_p_nd)); |
f5886803 | 181 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 182 | return ret_it; |
4569a895 AT |
183 | } |
184 | ||
185 | #ifdef PB_DS_DATA_TRUE_INDICATOR | |
186 | PB_DS_CLASS_T_DEC | |
187 | inline typename PB_DS_CLASS_C_DEC::iterator | |
188 | PB_DS_CLASS_C_DEC:: | |
189 | erase(iterator it) | |
190 | { | |
f5886803 | 191 | PB_DS_ASSERT_VALID((*this)) |
4569a895 AT |
192 | |
193 | if (it == end()) | |
1b24692f | 194 | return it; |
4569a895 | 195 | iterator ret_it = it; |
4569a895 | 196 | ++ret_it; |
a345e45d | 197 | _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == leaf_node); |
4569a895 | 198 | erase_leaf(static_cast<leaf_pointer>(it.m_p_nd)); |
f5886803 | 199 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 200 | return ret_it; |
4569a895 AT |
201 | } |
202 | #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR | |
203 | ||
204 | PB_DS_CLASS_T_DEC | |
205 | inline typename PB_DS_CLASS_C_DEC::const_reverse_iterator | |
206 | PB_DS_CLASS_C_DEC:: | |
207 | erase(const_reverse_iterator it) | |
208 | { | |
f5886803 | 209 | PB_DS_ASSERT_VALID((*this)) |
4569a895 AT |
210 | |
211 | if (it.m_p_nd == m_p_head) | |
1b24692f | 212 | return it; |
4569a895 | 213 | const_reverse_iterator ret_it = it; |
4569a895 AT |
214 | ++ret_it; |
215 | ||
a345e45d | 216 | _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == leaf_node); |
4569a895 | 217 | erase_leaf(static_cast<leaf_pointer>(it.m_p_nd)); |
f5886803 | 218 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 219 | return ret_it; |
4569a895 AT |
220 | } |
221 | ||
222 | #ifdef PB_DS_DATA_TRUE_INDICATOR | |
223 | PB_DS_CLASS_T_DEC | |
224 | inline typename PB_DS_CLASS_C_DEC::reverse_iterator | |
225 | PB_DS_CLASS_C_DEC:: | |
226 | erase(reverse_iterator it) | |
227 | { | |
f5886803 | 228 | PB_DS_ASSERT_VALID((*this)) |
4569a895 AT |
229 | |
230 | if (it.m_p_nd == m_p_head) | |
1b24692f | 231 | return it; |
4569a895 | 232 | reverse_iterator ret_it = it; |
4569a895 AT |
233 | ++ret_it; |
234 | ||
a345e45d | 235 | _GLIBCXX_DEBUG_ASSERT(it.m_p_nd->m_type == leaf_node); |
4569a895 | 236 | erase_leaf(static_cast<leaf_pointer>(it.m_p_nd)); |
f5886803 | 237 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 238 | return ret_it; |
4569a895 AT |
239 | } |
240 | #endif // #ifdef PB_DS_DATA_TRUE_INDICATOR | |
241 | ||
242 | PB_DS_CLASS_T_DEC | |
243 | template<typename Pred> | |
244 | inline typename PB_DS_CLASS_C_DEC::size_type | |
245 | PB_DS_CLASS_C_DEC:: | |
246 | erase_if(Pred pred) | |
247 | { | |
248 | size_type num_ersd = 0; | |
f5886803 | 249 | PB_DS_ASSERT_VALID((*this)) |
4569a895 | 250 | |
1b24692f | 251 | iterator it = begin(); |
4569a895 AT |
252 | while (it != end()) |
253 | { | |
f5886803 FD |
254 | PB_DS_ASSERT_VALID((*this)) |
255 | if (pred(*it)) | |
256 | { | |
a345e45d BK |
257 | ++num_ersd; |
258 | it = erase(it); | |
f5886803 FD |
259 | } |
260 | else | |
261 | ++it; | |
4569a895 AT |
262 | } |
263 | ||
f5886803 | 264 | PB_DS_ASSERT_VALID((*this)) |
1b24692f | 265 | return num_ersd; |
4569a895 AT |
266 | } |
267 | ||
268 | PB_DS_CLASS_T_DEC | |
269 | void | |
270 | PB_DS_CLASS_C_DEC:: | |
271 | erase_leaf(leaf_pointer p_l) | |
272 | { | |
273 | update_min_max_for_erased_leaf(p_l); | |
a345e45d | 274 | if (p_l->m_p_parent->m_type == head_node) |
4569a895 | 275 | { |
47bea7b8 | 276 | _GLIBCXX_DEBUG_ASSERT(size() == 1); |
4569a895 | 277 | clear(); |
4569a895 AT |
278 | return; |
279 | } | |
280 | ||
47bea7b8 | 281 | _GLIBCXX_DEBUG_ASSERT(size() > 1); |
a345e45d | 282 | _GLIBCXX_DEBUG_ASSERT(p_l->m_p_parent->m_type == i_node); |
4569a895 | 283 | |
a345e45d | 284 | inode_pointer p_parent = static_cast<inode_pointer>(p_l->m_p_parent); |
4569a895 AT |
285 | |
286 | p_parent->remove_child(p_l); | |
4569a895 | 287 | erase_fixup(p_parent); |
4569a895 AT |
288 | actual_erase_leaf(p_l); |
289 | } | |
290 | ||
291 | PB_DS_CLASS_T_DEC | |
292 | void | |
293 | PB_DS_CLASS_C_DEC:: | |
294 | update_min_max_for_erased_leaf(leaf_pointer p_l) | |
295 | { | |
296 | if (m_size == 1) | |
297 | { | |
298 | m_p_head->m_p_min = m_p_head; | |
299 | m_p_head->m_p_max = m_p_head; | |
4569a895 AT |
300 | return; |
301 | } | |
302 | ||
a345e45d | 303 | if (p_l == static_cast<leaf_const_pointer>(m_p_head->m_p_min)) |
4569a895 AT |
304 | { |
305 | iterator it(p_l); | |
4569a895 | 306 | ++it; |
4569a895 | 307 | m_p_head->m_p_min = it.m_p_nd; |
4569a895 AT |
308 | return; |
309 | } | |
310 | ||
a345e45d | 311 | if (p_l == static_cast<leaf_const_pointer>(m_p_head->m_p_max)) |
4569a895 AT |
312 | { |
313 | iterator it(p_l); | |
4569a895 | 314 | --it; |
4569a895 AT |
315 | m_p_head->m_p_max = it.m_p_nd; |
316 | } | |
317 | } | |
574dfb67 | 318 | #endif |