1169691Skan// -*- C++ -*-
2169691Skan
3169691Skan// Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4169691Skan//
5169691Skan// This file is part of the GNU ISO C++ Library.  This library is free
6169691Skan// software; you can redistribute it and/or modify it under the terms
7169691Skan// of the GNU General Public License as published by the Free Software
8169691Skan// Foundation; either version 2, or (at your option) any later
9169691Skan// version.
10169691Skan
11169691Skan// This library is distributed in the hope that it will be useful, but
12169691Skan// WITHOUT ANY WARRANTY; without even the implied warranty of
13169691Skan// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14169691Skan// General Public License for more details.
15169691Skan
16169691Skan// You should have received a copy of the GNU General Public License
17169691Skan// along with this library; see the file COPYING.  If not, write to
18169691Skan// the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19169691Skan// MA 02111-1307, USA.
20169691Skan
21169691Skan// As a special exception, you may use this file as part of a free
22169691Skan// software library without restriction.  Specifically, if other files
23169691Skan// instantiate templates or use macros or inline functions from this
24169691Skan// file, or you compile this file and link it with other files to
25169691Skan// produce an executable, this file does not by itself cause the
26169691Skan// resulting executable to be covered by the GNU General Public
27169691Skan// License.  This exception does not however invalidate any other
28169691Skan// reasons why the executable file might be covered by the GNU General
29169691Skan// Public License.
30169691Skan
31169691Skan// Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32169691Skan
33169691Skan// Permission to use, copy, modify, sell, and distribute this software
34169691Skan// is hereby granted without fee, provided that the above copyright
35169691Skan// notice appears in all copies, and that both that copyright notice
36169691Skan// and this permission notice appear in supporting documentation. None
37169691Skan// of the above authors, nor IBM Haifa Research Laboratories, make any
38169691Skan// representation about the suitability of this software for any
39169691Skan// purpose. It is provided "as is" without express or implied
40169691Skan// warranty.
41169691Skan
42169691Skan/**
43169691Skan * @file erase_fn_imps.hpp
44169691Skan * Contains an implementation class for ov_tree_.
45169691Skan */
46169691Skan
47169691SkanPB_DS_CLASS_T_DEC
48169691Skanvoid
49169691SkanPB_DS_CLASS_C_DEC::
50169691Skanclear()
51169691Skan{
52169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
53169691Skan  if (m_size == 0)
54169691Skan    {
55169691Skan      _GLIBCXX_DEBUG_ONLY(assert_valid();)
56169691Skan      return;
57169691Skan    }
58169691Skan  else
59169691Skan    {
60169691Skan      reallocate_metadata((node_update* )this, 0);
61169691Skan      cond_dtor<size_type> cd(m_a_values, m_end_it, m_size);
62169691Skan    }
63169691Skan
64169691Skan  _GLIBCXX_DEBUG_ONLY(map_debug_base::clear();)
65169691Skan  m_a_values = NULL;
66169691Skan  m_size = 0;
67169691Skan  m_end_it = m_a_values;
68169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
69169691Skan}
70169691Skan
71169691SkanPB_DS_CLASS_T_DEC
72169691Skantemplate<typename Pred>
73169691Skaninline typename PB_DS_CLASS_C_DEC::size_type
74169691SkanPB_DS_CLASS_C_DEC::
75169691Skanerase_if(Pred pred)
76169691Skan{
77169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
78169691Skan
79169691Skan#ifdef PB_DS_REGRESSION
80169691Skan    typename Allocator::group_throw_prob_adjustor adjust(m_size);
81169691Skan#endif
82169691Skan
83169691Skan  size_type new_size = 0;
84169691Skan  size_type num_val_ersd = 0;
85169691Skan  iterator source_it = m_a_values;
86169691Skan  for (source_it = begin(); source_it != m_end_it; ++source_it)
87169691Skan    if (!pred(*source_it))
88169691Skan      ++new_size;
89169691Skan    else
90169691Skan      ++num_val_ersd;
91169691Skan
92169691Skan  if (new_size == 0)
93169691Skan    {
94169691Skan      clear();
95169691Skan      return num_val_ersd;
96169691Skan    }
97169691Skan
98169691Skan  value_vector a_new_values = s_value_alloc.allocate(new_size);
99169691Skan  iterator target_it = a_new_values;
100169691Skan  cond_dtor<size_type> cd(a_new_values, target_it, new_size);
101169691Skan  _GLIBCXX_DEBUG_ONLY(map_debug_base::clear());
102169691Skan  for (source_it = begin(); source_it != m_end_it; ++source_it)
103169691Skan    {
104169691Skan      if (!pred(*source_it))
105169691Skan        {
106169691Skan	  new (const_cast<void*>(static_cast<const void* >(target_it)))
107169691Skan	    value_type(*source_it);
108169691Skan
109169691Skan	  _GLIBCXX_DEBUG_ONLY(map_debug_base::insert_new(PB_DS_V2F(*source_it)));
110169691Skan	  ++target_it;
111169691Skan        }
112169691Skan    }
113169691Skan
114169691Skan  reallocate_metadata((node_update* )this, new_size);
115169691Skan  cd.set_no_action();
116169691Skan
117169691Skan  {
118169691Skan    cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
119169691Skan  }
120169691Skan
121169691Skan  m_a_values = a_new_values;
122169691Skan  m_size = new_size;
123169691Skan  m_end_it = target_it;
124169691Skan  update(node_begin(), (node_update* )this);
125169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
126169691Skan  return num_val_ersd;
127169691Skan}
128169691Skan
129169691SkanPB_DS_CLASS_T_DEC
130169691Skantemplate<typename It>
131169691SkanIt
132169691SkanPB_DS_CLASS_C_DEC::
133169691Skanerase_imp(It it)
134169691Skan{
135169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
136169691Skan  if (it == end())
137169691Skan    return end();
138169691Skan
139169691Skan  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::check_key_exists(PB_DS_V2F(*it));)
140169691Skan
141169691Skan#ifdef PB_DS_REGRESSION
142169691Skan    typename Allocator::group_throw_prob_adjustor adjust(m_size);
143169691Skan#endif
144169691Skan
145169691Skan  _GLIBCXX_DEBUG_ASSERT(m_size > 0);
146169691Skan  value_vector a_values = s_value_alloc.allocate(m_size - 1);
147169691Skan  iterator source_it = begin();
148169691Skan  iterator source_end_it = end();
149169691Skan  iterator target_it = a_values;
150169691Skan  iterator ret_it = end();
151169691Skan
152169691Skan  cond_dtor<size_type> cd(a_values, target_it, m_size - 1);
153169691Skan
154169691Skan  _GLIBCXX_DEBUG_ONLY(size_type cnt = 0;)
155169691Skan
156169691Skan  while (source_it != source_end_it)
157169691Skan    {
158169691Skan      if (source_it != it)
159169691Skan	{
160169691Skan          _GLIBCXX_DEBUG_ONLY(++cnt;)
161169691Skan	  _GLIBCXX_DEBUG_ASSERT(cnt != m_size);
162169691Skan          new (const_cast<void* >(static_cast<const void* >(target_it)))
163169691Skan	      value_type(*source_it);
164169691Skan
165169691Skan          ++target_it;
166169691Skan	}
167169691Skan      else
168169691Skan	ret_it = target_it;
169169691Skan    ++source_it;
170169691Skan    }
171169691Skan
172169691Skan  _GLIBCXX_DEBUG_ASSERT(m_size > 0);
173169691Skan  reallocate_metadata((node_update* )this, m_size - 1);
174169691Skan  cd.set_no_action();
175169691Skan  _GLIBCXX_DEBUG_ONLY(PB_DS_CLASS_C_DEC::erase_existing(PB_DS_V2F(*it));)
176169691Skan  {
177169691Skan    cond_dtor<size_type> cd1(m_a_values, m_end_it, m_size);
178169691Skan  }
179169691Skan
180169691Skan  m_a_values = a_values;
181169691Skan  --m_size;
182169691Skan  m_end_it = m_a_values + m_size;
183169691Skan  update(node_begin(), (node_update* )this);
184169691Skan  _GLIBCXX_DEBUG_ONLY(assert_valid();)
185169691Skan  return It(ret_it);
186169691Skan}
187169691Skan
188169691SkanPB_DS_CLASS_T_DEC
189169691Skanbool
190169691SkanPB_DS_CLASS_C_DEC::
191169691Skanerase(const_key_reference r_key)
192169691Skan{
193169691Skan  point_iterator it = find(r_key);
194169691Skan  if (it == end())
195169691Skan    return false;
196169691Skan  erase(it);
197169691Skan  return true;
198169691Skan}
199169691Skan
200