erase_fn_imps.hpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00031 
00032 // Permission to use, copy, modify, sell, and distribute this software
00033 // is hereby granted without fee, provided that the above copyright
00034 // notice appears in all copies, and that both that copyright notice and
00035 // this permission notice appear in supporting documentation. None of
00036 // the above authors, nor IBM Haifa Research Laboratories, make any
00037 // representation about the suitability of this software for any
00038 // purpose. It is provided "as is" without express or implied warranty.
00039 
00045 PB_ASSOC_CLASS_T_DEC
00046 inline typename PB_ASSOC_CLASS_C_DEC::size_type
00047 PB_ASSOC_CLASS_C_DEC::
00048 erase(const_key_reference r_key)
00049 {
00050   iterator it = find(r_key);
00051 
00052   if (it == find_end())
00053     return (0);
00054 
00055   erase(it);
00056 
00057   return (1);
00058 }
00059 
00060 PB_ASSOC_CLASS_T_DEC
00061 void
00062 PB_ASSOC_CLASS_C_DEC::
00063 clear()
00064 {
00065   PB_ASSOC_DBG_ONLY(assert_valid();)
00066 
00067     if (m_size == 0)
00068       {
00069     PB_ASSOC_DBG_ONLY(assert_valid();)
00070 
00071       return;
00072       }
00073     else
00074       {
00075     cond_dtor cd(m_a_values, m_end_it, m_size);
00076       }
00077 
00078   PB_ASSOC_DBG_ONLY(my_map_debug_base::clear();)
00079 
00080     m_a_values = NULL;
00081 
00082   m_size = 0;
00083 
00084   m_end_it = m_a_values;
00085 
00086   PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
00087 
00088     PB_ASSOC_DBG_ONLY(assert_valid();)
00089     }
00090 
00091 PB_ASSOC_CLASS_T_DEC
00092 template<class Pred>
00093 inline typename PB_ASSOC_CLASS_C_DEC::size_type
00094 PB_ASSOC_CLASS_C_DEC::
00095 erase_if(Pred pred)
00096 {
00097   PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
00098 
00099 #ifdef PB_ASSOC_BASIC_REGRESSION
00100     throw_prob_adjustor adjust(m_size);
00101 #endif // #ifdef PB_ASSOC_BASIC_REGRESSION
00102 
00103   size_type new_size = 0;
00104 
00105   size_type num_val_ersd = 0;
00106 
00107   iterator source_it = m_a_values;
00108 
00109   for (source_it = begin(); source_it != m_end_it; ++source_it)
00110     {
00111       if (pred(*source_it))
00112     ++num_val_ersd;
00113       else
00114     ++new_size;
00115     }
00116 
00117   if (new_size == 0)
00118     {
00119       clear();
00120 
00121       return (num_val_ersd);
00122     }
00123 
00124   pointer a_new_values = s_alloc.allocate(new_size);
00125 
00126   iterator target_it = a_new_values;
00127 
00128   cond_dtor cd(a_new_values, target_it, new_size);
00129 
00130   PB_ASSOC_DBG_ONLY(my_map_debug_base::clear());
00131 
00132   for (source_it = begin(); source_it != m_end_it; ++source_it)
00133     {
00134       if (!pred(*source_it))
00135     {
00136       new (const_cast<void* >(
00137                   static_cast<const void* >(target_it)))
00138         value_type(*source_it);
00139 
00140       PB_ASSOC_DBG_ONLY(my_map_debug_base::insert_new(
00141                               PB_ASSOC_V2F(*source_it)));
00142 
00143       ++target_it;
00144     }
00145     }
00146 
00147   cd.set_no_action();
00148 
00149   {
00150     cond_dtor cd1(m_a_values, m_end_it, m_size);
00151   }
00152 
00153   m_a_values = a_new_values;
00154 
00155   m_size = new_size;
00156 
00157   m_end_it = target_it;
00158 
00159   PB_ASSOC_DBG_ONLY(assert_valid();)
00160 
00161     return (num_val_ersd);
00162 }
00163 
00164 PB_ASSOC_CLASS_T_DEC
00165 template<class It>
00166 It
00167 PB_ASSOC_CLASS_C_DEC::
00168 erase(It it)
00169 {
00170   PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
00171 
00172     if (it == end())
00173       return end();
00174 
00175   PB_ASSOC_DBG_ONLY(
00176             PB_ASSOC_CLASS_C_DEC::check_key_exists(PB_ASSOC_V2F(*it));)
00177 
00178 #ifdef PB_ASSOC_BASIC_REGRESSION
00179     throw_prob_adjustor adjust(m_size);
00180 #endif // #ifdef PB_ASSOC_BASIC_REGRESSION
00181 
00182   PB_ASSOC_DBG_ASSERT(m_size > 0);
00183 
00184   pointer a_values = s_alloc.allocate(m_size - 1);
00185 
00186   iterator source_it = begin();
00187   iterator source_end_it = end();
00188   iterator target_it = a_values;
00189   iterator ret_it = end();
00190 
00191   cond_dtor cd(a_values, target_it, m_size - 1);
00192 
00193   PB_ASSOC_DBG_ONLY(size_type cnt = 0;)
00194 
00195     while (source_it != source_end_it)
00196       {
00197     if (source_it != it)
00198       {
00199         PB_ASSOC_DBG_ONLY(++cnt;)
00200           PB_ASSOC_DBG_ASSERT(cnt != m_size);
00201 
00202         new (const_cast<void* >(
00203                     static_cast<const void* >(target_it)))
00204           value_type(*source_it);
00205 
00206         ++target_it;
00207       }
00208     else
00209       ret_it = target_it;
00210 
00211     ++source_it;
00212       }
00213 
00214   cd.set_no_action();
00215 
00216   PB_ASSOC_DBG_ONLY(
00217             PB_ASSOC_CLASS_C_DEC::erase_existing(PB_ASSOC_V2F(*it));)
00218     {
00219       cond_dtor cd1(m_a_values, m_end_it, m_size);
00220     }
00221 
00222   m_a_values = a_values;
00223 
00224   --m_size;
00225 
00226   m_end_it = m_a_values + m_size;
00227 
00228   update(node_begin(), (Node_Updator* )this);
00229 
00230   PB_ASSOC_DBG_ONLY(PB_ASSOC_CLASS_C_DEC::assert_valid();)
00231 
00232     return (It(ret_it));
00233 }
00234 

Generated on Tue Feb 2 16:55:55 2010 for GNU C++ STL by  doxygen 1.4.7