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