1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 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 2, 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 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43 * @file erase_fn_imps.hpp
44 * Contains an implementation class for ov_tree_.
45 */
46
47 PB_DS_CLASS_T_DEC
48 void
49 PB_DS_CLASS_C_DEC::
clear()50 clear()
51 {
52 _GLIBCXX_DEBUG_ONLY(assert_valid();)
53 if (m_size == 0)
54 {
55 _GLIBCXX_DEBUG_ONLY(assert_valid();)
56 return;
57 }
58 else
59 {
60 reallocate_metadata((node_update* )this, 0);
61 cond_dtor<size_type> cd(m_a_values, m_end_it, m_size);
62 }
63
64 _GLIBCXX_DEBUG_ONLY(map_debug_base::clear();)
65 m_a_values = NULL;
66 m_size = 0;
67 m_end_it = m_a_values;
68 _GLIBCXX_DEBUG_ONLY(assert_valid();)
69 }
70
71 PB_DS_CLASS_T_DEC
72 template<typename Pred>
73 inline typename PB_DS_CLASS_C_DEC::size_type
74 PB_DS_CLASS_C_DEC::
erase_if(Pred pred)75 erase_if(Pred pred)
76 {
77 _GLIBCXX_DEBUG_ONLY(assert_valid();)
78
79 #ifdef PB_DS_REGRESSION
80 typename Allocator::group_throw_prob_adjustor adjust(m_size);
81 #endif
82
83 size_type new_size = 0;
84 size_type num_val_ersd = 0;
85 iterator source_it = m_a_values;
86 for (source_it = begin(); source_it != m_end_it; ++source_it)
87 if (!pred(*source_it))
88 ++new_size;
89 else
90 ++num_val_ersd;
91
92 if (new_size == 0)
93 {
94 clear();
95 return num_val_ersd;
96 }
97
98 value_vector a_new_values = s_value_alloc.allocate(new_size);
99 iterator target_it = a_new_values;
100 cond_dtor<size_type> cd(a_new_values, target_it, new_size);
101 _GLIBCXX_DEBUG_ONLY(map_debug_base::clear());
102 for (source_it = begin(); source_it != m_end_it; ++source_it)
103 {
104 if (!pred(*source_it))
105 {
106 new (const_cast<void*>(static_cast<const void* >(target_it)))
107 value_type(*source_it);
108
109 _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(*source_it)));
110 ++target_it;
111 }
112 }
113
114 reallocate_metadata((node_update* )this, new_size);
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 _GLIBCXX_DEBUG_ONLY(assert_valid();)
126 return num_val_ersd;
127 }
128
129 PB_DS_CLASS_T_DEC
130 template<typename It>
131 It
132 PB_DS_CLASS_C_DEC::
erase_imp(It it)133 erase_imp(It it)
134 {
135 _GLIBCXX_DEBUG_ONLY(assert_valid();)
136 if (it == end())
137 return end();
138
139 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::check_key_exists(PB_DS_V2F(*it));)
140
141 #ifdef PB_DS_REGRESSION
142 typename Allocator::group_throw_prob_adjustor adjust(m_size);
143 #endif
144
145 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
146 value_vector a_values = s_value_alloc.allocate(m_size - 1);
147 iterator source_it = begin();
148 iterator source_end_it = end();
149 iterator target_it = a_values;
150 iterator ret_it = end();
151
152 cond_dtor<size_type> cd(a_values, target_it, m_size - 1);
153
154 _GLIBCXX_DEBUG_ONLY(size_type cnt = 0;)
155
156 while (source_it != source_end_it)
157 {
158 if (source_it != it)
159 {
160 _GLIBCXX_DEBUG_ONLY(++cnt;)
161 _GLIBCXX_DEBUG_ASSERT(cnt != m_size);
162 new (const_cast<void* >(static_cast<const void* >(target_it)))
163 value_type(*source_it);
164
165 ++target_it;
166 }
167 else
168 ret_it = target_it;
169 ++source_it;
170 }
171
172 _GLIBCXX_DEBUG_ASSERT(m_size > 0);
173 reallocate_metadata((node_update* )this, m_size - 1);
174 cd.set_no_action();
175 _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::erase_existing(PB_DS_V2F(*it));)
176 {
177 cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
178 }
179
180 m_a_values = a_values;
181 --m_size;
182 m_end_it = m_a_values + m_size;
183 update(node_begin(), (node_update* )this);
184 _GLIBCXX_DEBUG_ONLY(assert_valid();)
185 return It(ret_it);
186 }
187
188 PB_DS_CLASS_T_DEC
189 bool
190 PB_DS_CLASS_C_DEC::
erase(const_key_reference r_key)191 erase(const_key_reference r_key)
192 {
193 point_iterator it = find(r_key);
194 if (it == end())
195 return false;
196 erase(it);
197 return true;
198 }
199
200