libstdc++
ov_tree_map_/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 ov_tree_map_/erase_fn_imps.hpp
38 * Contains an implementation class for ov_tree_.
39 */
40
41#ifdef PB_DS_CLASS_C_DEC
42
43PB_DS_CLASS_T_DEC
44void
45PB_DS_CLASS_C_DEC::
46clear()
47{
48 PB_DS_ASSERT_VALID((*this))
49 if (m_size == 0)
50 {
51 return;
52 }
53 else
54 {
55 reallocate_metadata((node_update* )this, 0);
56 cond_dtor<size_type> cd(m_a_values, m_end_it, m_size);
57 }
58
59 _GLIBCXX_DEBUG_ONLY(debug_base::clear();)
60 m_a_values = 0;
61 m_size = 0;
62 m_end_it = m_a_values;
63 PB_DS_ASSERT_VALID((*this))
64}
65
66PB_DS_CLASS_T_DEC
67template<typename Pred>
68inline typename PB_DS_CLASS_C_DEC::size_type
69PB_DS_CLASS_C_DEC::
70erase_if(Pred pred)
71{
72 PB_DS_ASSERT_VALID((*this))
73
74#ifdef PB_DS_REGRESSION
75 typename _Alloc::group_adjustor adjust(m_size);
76#endif
77
78 size_type new_size = 0;
79 size_type num_val_ersd = 0;
80
81 for (iterator source_it = begin(); source_it != m_end_it; ++source_it)
82 if (!pred(*source_it))
83 ++new_size;
84 else
85 ++num_val_ersd;
86
87 if (new_size == 0)
88 {
89 clear();
90 return num_val_ersd;
91 }
92
93 value_vector a_new_values = s_value_alloc.allocate(new_size);
94 iterator target_it = a_new_values;
95 cond_dtor<size_type> cd(a_new_values, target_it, new_size);
96 _GLIBCXX_DEBUG_ONLY(
97 struct dbase : debug_base {
98 using debug_base::swap;
99 using debug_base::insert_new;
100 } d);
101 for (iterator source_it = begin(); source_it != m_end_it; ++source_it)
102 {
103 if (!pred(*source_it))
104 {
105 new (const_cast<void*>(static_cast<const void*>(target_it)))
106 value_type(*source_it);
107
108 _GLIBCXX_DEBUG_ONLY(d.insert_new(PB_DS_V2F(*source_it)));
109 ++target_it;
110 }
111 }
112
113 reallocate_metadata((node_update*)this, new_size);
114 _GLIBCXX_DEBUG_ONLY(d.swap(*this));
115 cd.set_no_action();
116
117 {
118 cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
119 }
120
121 m_a_values = a_new_values;
122 m_size = new_size;
123 m_end_it = target_it;
124 update(node_begin(), (node_update*)this);
125 PB_DS_ASSERT_VALID((*this))
126 return num_val_ersd;
127}
128
129PB_DS_CLASS_T_DEC
130template<typename It>
131It
132PB_DS_CLASS_C_DEC::
133erase_imp(It it)
134{
135 PB_DS_ASSERT_VALID((*this))
136 if (it == end())
137 return end();
138
139 PB_DS_CHECK_KEY_EXISTS(PB_DS_V2F(*it))
140
141#ifdef PB_DS_REGRESSION
142 typename _Alloc::group_adjustor adjust(m_size);
143#endif
144
145 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
146 value_vector a_values = 0;
147 if (m_size > 1)
148 a_values = s_value_alloc.allocate(m_size - 1);
149 iterator source_it = begin();
150 iterator source_end_it = end();
151 iterator target_it = a_values;
152 iterator ret_it = end();
153
154 cond_dtor<size_type> cd(a_values, target_it, m_size - 1);
155
156 _GLIBCXX_DEBUG_ONLY(size_type cnt = 0;)
157
158 while (source_it != source_end_it)
159 {
160 if (source_it != it)
161 {
162 _GLIBCXX_DEBUG_ONLY(++cnt;)
163 _GLIBCXX_DEBUG_ASSERT(cnt != m_size);
164 new (const_cast<void*>(static_cast<const void*>(target_it)))
165 value_type(*source_it);
166
167 ++target_it;
168 }
169 else
170 ret_it = target_it;
171 ++source_it;
172 }
173
174 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
175 reallocate_metadata((node_update*)this, m_size - 1);
176 cd.set_no_action();
177 _GLIBCXX_DEBUG_ONLY(debug_base::erase_existing(PB_DS_V2F(*it));)
178 {
179 cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
180 }
181
182 m_a_values = a_values;
183 --m_size;
184 m_end_it = m_a_values + m_size;
185 update(node_begin(), (node_update*)this);
186 PB_DS_ASSERT_VALID((*this))
187 return It(ret_it);
188}
189
190PB_DS_CLASS_T_DEC
191bool
192PB_DS_CLASS_C_DEC::
193erase(key_const_reference r_key)
194{
195 point_iterator it = find(r_key);
196 if (it == end())
197 return false;
198 erase(it);
199 return true;
200}
201#endif