1// -*- C++ -*-
2
3// Copyright (C) 2005 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
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 2, or (at your option)
9// any later version.
10
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING.  If not, write to the Free
18// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction.  Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License.  This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
30// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
31
32// Permission to use, copy, modify, sell, and distribute this software
33// is hereby granted without fee, provided that the above copyright
34// notice appears in all copies, and that both that copyright notice and
35// this permission notice appear in supporting documentation. None of
36// the above authors, nor IBM Haifa Research Laboratories, make any
37// representation about the suitability of this software for any
38// purpose. It is provided "as is" without express or implied warranty.
39
40/**
41 * @file erase_fn_imps.hpp
42 * Contains an implementation class for ov_tree_.
43 */
44
45PB_ASSOC_CLASS_T_DEC
46inline typename PB_ASSOC_CLASS_C_DEC::size_type
47PB_ASSOC_CLASS_C_DEC::
48erase(const_key_reference r_key)
49{
50  iterator it = find(r_key);
51
52  if (it == find_end())
53    return (0);
54
55  erase(it);
56
57  return (1);
58}
59
60PB_ASSOC_CLASS_T_DEC
61void
62PB_ASSOC_CLASS_C_DEC::
63clear()
64{
65  PB_ASSOC_DBG_ONLY(assert_valid();)
66
67    if (m_size == 0)
68      {
69	PB_ASSOC_DBG_ONLY(assert_valid();)
70
71	  return;
72      }
73    else
74      {
75	cond_dtor cd(m_a_values, m_end_it, m_size);
76      }
77
78  PB_ASSOC_DBG_ONLY(my_map_debug_base::clear();)
79
80    m_a_values = NULL;
81
82  m_size = 0;
83
84  m_end_it = m_a_values;
85
86  PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
87
88    PB_ASSOC_DBG_ONLY(assert_valid();)
89    }
90
91PB_ASSOC_CLASS_T_DEC
92template<class Pred>
93inline typename PB_ASSOC_CLASS_C_DEC::size_type
94PB_ASSOC_CLASS_C_DEC::
95erase_if(Pred pred)
96{
97  PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
98
99#ifdef PB_ASSOC_BASIC_REGRESSION
100    throw_prob_adjustor adjust(m_size);
101#endif // #ifdef PB_ASSOC_BASIC_REGRESSION
102
103  size_type new_size = 0;
104
105  size_type num_val_ersd = 0;
106
107  iterator source_it = m_a_values;
108
109  for (source_it = begin(); source_it != m_end_it; ++source_it)
110    {
111      if (pred(*source_it))
112	++num_val_ersd;
113      else
114	++new_size;
115    }
116
117  if (new_size == 0)
118    {
119      clear();
120
121      return (num_val_ersd);
122    }
123
124  pointer a_new_values = s_alloc.allocate(new_size);
125
126  iterator target_it = a_new_values;
127
128  cond_dtor cd(a_new_values, target_it, new_size);
129
130  PB_ASSOC_DBG_ONLY(my_map_debug_base::clear());
131
132  for (source_it = begin(); source_it != m_end_it; ++source_it)
133    {
134      if (!pred(*source_it))
135	{
136	  new (const_cast<void* >(
137				  static_cast<const void* >(target_it)))
138	    value_type(*source_it);
139
140	  PB_ASSOC_DBG_ONLY(my_map_debug_base::insert_new(
141							  PB_ASSOC_V2F(*source_it)));
142
143	  ++target_it;
144	}
145    }
146
147  cd.set_no_action();
148
149  {
150    cond_dtor cd1(m_a_values, m_end_it, m_size);
151  }
152
153  m_a_values = a_new_values;
154
155  m_size = new_size;
156
157  m_end_it = target_it;
158
159  PB_ASSOC_DBG_ONLY(assert_valid();)
160
161    return (num_val_ersd);
162}
163
164PB_ASSOC_CLASS_T_DEC
165template<class It>
166It
167PB_ASSOC_CLASS_C_DEC::
168erase(It it)
169{
170  PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
171
172    if (it == end())
173      return end();
174
175  PB_ASSOC_DBG_ONLY(
176		    PB_ASSOC_CLASS_C_DEC::check_key_exists(PB_ASSOC_V2F(*it));)
177
178#ifdef PB_ASSOC_BASIC_REGRESSION
179    throw_prob_adjustor adjust(m_size);
180#endif // #ifdef PB_ASSOC_BASIC_REGRESSION
181
182  PB_ASSOC_DBG_ASSERT(m_size > 0);
183
184  pointer a_values = s_alloc.allocate(m_size - 1);
185
186  iterator source_it = begin();
187  iterator source_end_it = end();
188  iterator target_it = a_values;
189  iterator ret_it = end();
190
191  cond_dtor cd(a_values, target_it, m_size - 1);
192
193  PB_ASSOC_DBG_ONLY(size_type cnt = 0;)
194
195    while (source_it != source_end_it)
196      {
197	if (source_it != it)
198	  {
199	    PB_ASSOC_DBG_ONLY(++cnt;)
200	      PB_ASSOC_DBG_ASSERT(cnt != m_size);
201
202	    new (const_cast<void* >(
203				    static_cast<const void* >(target_it)))
204	      value_type(*source_it);
205
206	    ++target_it;
207	  }
208	else
209	  ret_it = target_it;
210
211	++source_it;
212      }
213
214  cd.set_no_action();
215
216  PB_ASSOC_DBG_ONLY(
217		    PB_ASSOC_CLASS_C_DEC::erase_existing(PB_ASSOC_V2F(*it));)
218    {
219      cond_dtor cd1(m_a_values, m_end_it, m_size);
220    }
221
222  m_a_values = a_values;
223
224  --m_size;
225
226  m_end_it = m_a_values + m_size;
227
228  update(node_begin(), (Node_Updator* )this);
229
230  PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
231
232    return (It(ret_it));
233}
234
235