split_join_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 void
00047 PB_ASSOC_CLASS_C_DEC::
00048 split(const_key_reference r_key, PB_ASSOC_CLASS_C_DEC& r_other)
00049 {
00050   PB_ASSOC_DBG_ONLY(assert_valid();)
00051     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00052 
00053     if (m_size == 0)
00054       {
00055     r_other.clear();
00056 
00057     PB_ASSOC_DBG_ONLY(assert_valid();)
00058       PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00059 
00060       return;
00061       }
00062 
00063   if (Cmp_Fn::operator()(r_key, PB_ASSOC_V2F(*begin())))
00064     {
00065       swap(r_other);
00066 
00067       PB_ASSOC_DBG_ONLY(assert_valid();)
00068     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00069 
00070     return;
00071     }
00072 
00073   if (!Cmp_Fn::operator()(
00074               r_key,
00075               PB_ASSOC_V2F(*(end() - 1))))
00076     {
00077       PB_ASSOC_DBG_ONLY(assert_valid();)
00078     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00079 
00080     return;
00081     }
00082 
00083   if (m_size == 1)
00084     {
00085       swap(r_other);
00086 
00087       PB_ASSOC_DBG_ONLY(assert_valid();)
00088     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00089 
00090     return;
00091     }
00092 
00093 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
00094   for (const_iterator it = begin(); it != end(); ++it)
00095     if (Cmp_Fn::operator()(
00096                r_key,
00097                PB_ASSOC_V2F(*it)))
00098       {
00099     my_map_debug_base::erase_existing(PB_ASSOC_V2F(*it));
00100     r_other.my_map_debug_base::insert_new(PB_ASSOC_V2F(*it));
00101       }
00102 #endif // PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
00103 
00104   iterator it = upper_bound(r_key);
00105 
00106   PB_ASSOC_CLASS_C_DEC new_other(r_other, r_other);
00107 
00108   new_other.copy_from_ordered_range(it, end());
00109 
00110   PB_ASSOC_CLASS_C_DEC new_this(*this, * this);
00111 
00112   new_this.copy_from_ordered_range(begin(), it);
00113 
00114   // No exceptions from this point.
00115 
00116   r_other.update(r_other.node_begin(), (Node_Updator* )(&r_other));
00117 
00118   update(node_begin(), (Node_Updator* )this);
00119 
00120   r_other.swap(new_other);
00121 
00122   swap(new_this);
00123 
00124   PB_ASSOC_DBG_ONLY(assert_valid();)
00125     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00126     }
00127 
00128 PB_ASSOC_CLASS_T_DEC
00129 void
00130 PB_ASSOC_CLASS_C_DEC::
00131 join(PB_ASSOC_CLASS_C_DEC& r_other)
00132 {
00133   PB_ASSOC_DBG_ONLY(assert_valid();)
00134     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00135 
00136     if (r_other.m_size == 0)
00137       return;
00138 
00139   if (m_size == 0)
00140     {
00141       swap(r_other);
00142 
00143       return;
00144     }
00145 
00146   const bool greater = Cmp_Fn::operator()(
00147                       PB_ASSOC_V2F(*(end() - 1)),
00148                       PB_ASSOC_V2F(*r_other.begin()));
00149 
00150   const bool lesser = Cmp_Fn::operator()(
00151                      PB_ASSOC_V2F(*(r_other.end() - 1)),
00152                      PB_ASSOC_V2F(*begin()));
00153 
00154   if (!greater&&  !lesser)
00155     throw cannot_join();
00156 
00157 #ifdef PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
00158   for (const_iterator other_it = r_other.begin(); other_it != r_other.end();
00159        ++other_it)
00160     {
00161       my_map_debug_base::insert_new(PB_ASSOC_V2F(*other_it));
00162       r_other.my_map_debug_base::erase_existing(PB_ASSOC_V2F(*other_it));
00163     }
00164 #endif // PB_ASSOC_BIN_SEARCH_TREE_DEBUG_
00165 
00166   PB_ASSOC_CLASS_C_DEC new_this(*this, * this);
00167 
00168   if (greater)
00169     new_this.copy_from_ordered_range(
00170                      begin(),
00171                      end(),
00172                      r_other.begin(),
00173                      r_other.end());
00174   else
00175     new_this.copy_from_ordered_range(
00176                      r_other.begin(),
00177                      r_other.end(),
00178                      begin(),
00179                      end());
00180 
00181   // No exceptions from this point.
00182 
00183   swap(new_this);
00184 
00185   r_other.clear();
00186 
00187   PB_ASSOC_DBG_ONLY(assert_valid();)
00188     PB_ASSOC_DBG_ONLY(r_other.assert_valid();)
00189     }

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