libstdc++
bin_search_tree_/insert_fn_imps.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 2011 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 terms
00007 // of the GNU General Public License as published by the Free Software
00008 // Foundation; either version 3, or (at your option) any later
00009 // version.
00010 
00011 // This library is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file bin_search_tree_/insert_fn_imps.hpp
00038  * Contains an implementation class for bin_search_tree_.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 inline std::pair<typename PB_DS_CLASS_C_DEC::point_iterator, bool>
00043 PB_DS_CLASS_C_DEC::
00044 insert_leaf(const_reference r_value)
00045 {
00046   PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
00047 
00048   if (m_size == 0)
00049     return std::make_pair(insert_imp_empty(r_value),
00050               true);
00051 
00052   node_pointer p_nd = m_p_head->m_p_parent;
00053   node_pointer p_pot = m_p_head;
00054 
00055   while (p_nd != 0)
00056     if (!Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
00057                 PB_DS_V2F(r_value)))
00058       {
00059     p_pot = p_nd;
00060 
00061     p_nd = p_nd->m_p_left;
00062       }
00063     else
00064       p_nd = p_nd->m_p_right;
00065 
00066   if (p_pot == m_p_head)
00067     return std::make_pair(insert_leaf_new(r_value, m_p_head->m_p_right, false),
00068               true);
00069 
00070   if (!Cmp_Fn::operator()(PB_DS_V2F(r_value),
00071               PB_DS_V2F(p_pot->m_value)))
00072     {
00073       PB_DS_STRUCT_ONLY_ASSERT_VALID((*this))
00074       PB_DS_CHECK_KEY_EXISTS(PB_DS_V2F(r_value))
00075       return std::make_pair(p_pot, false);
00076     }
00077 
00078   PB_DS_CHECK_KEY_DOES_NOT_EXIST(PB_DS_V2F(r_value))
00079 
00080   p_nd = p_pot->m_p_left;
00081   if (p_nd == 0)
00082     return std::make_pair(insert_leaf_new(r_value, p_pot, true),
00083               true);
00084 
00085   while (p_nd->m_p_right != 0)
00086     p_nd = p_nd->m_p_right;
00087 
00088   return std::make_pair(insert_leaf_new(r_value, p_nd, false),
00089             true);
00090 }
00091 
00092 PB_DS_CLASS_T_DEC
00093 inline typename PB_DS_CLASS_C_DEC::iterator
00094 PB_DS_CLASS_C_DEC::
00095 insert_leaf_new(const_reference r_value, node_pointer p_nd, bool left_nd)
00096 {
00097   node_pointer p_new_nd =
00098     get_new_node_for_leaf_insert(r_value,
00099                  traits_base::m_no_throw_copies_indicator);
00100 
00101   if (left_nd)
00102     {
00103       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == 0);
00104       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(PB_DS_V2F(r_value),
00105                            PB_DS_V2F(p_nd->m_value)));
00106 
00107       p_nd->m_p_left = p_new_nd;
00108       if (m_p_head->m_p_left == p_nd)
00109     m_p_head->m_p_left = p_new_nd;
00110     }
00111   else
00112     {
00113       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_right == 0);
00114       _GLIBCXX_DEBUG_ASSERT(Cmp_Fn::operator()(PB_DS_V2F(p_nd->m_value),
00115                            PB_DS_V2F(r_value)));
00116 
00117       p_nd->m_p_right = p_new_nd;
00118       if (m_p_head->m_p_right == p_nd)
00119     m_p_head->m_p_right = p_new_nd;
00120     }
00121 
00122   p_new_nd->m_p_parent = p_nd;
00123   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
00124   PB_DS_ASSERT_NODE_CONSISTENT(p_nd)
00125 
00126   update_to_top(p_new_nd, (node_update* )this);
00127   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value));)
00128   return iterator(p_new_nd);
00129 }
00130 
00131 PB_DS_CLASS_T_DEC
00132 inline typename PB_DS_CLASS_C_DEC::iterator
00133 PB_DS_CLASS_C_DEC::
00134 insert_imp_empty(const_reference r_value)
00135 {
00136   node_pointer p_new_node =
00137     get_new_node_for_leaf_insert(r_value, traits_base::m_no_throw_copies_indicator);
00138 
00139   m_p_head->m_p_left = m_p_head->m_p_right =
00140     m_p_head->m_p_parent = p_new_node;
00141 
00142   p_new_node->m_p_parent = m_p_head;
00143   p_new_node->m_p_left = p_new_node->m_p_right = 0;
00144   _GLIBCXX_DEBUG_ONLY(debug_base::insert_new(PB_DS_V2F(r_value));)
00145 
00146   update_to_top(m_p_head->m_p_parent, (node_update*)this);
00147   return iterator(p_new_node);
00148 }
00149 
00150 PB_DS_CLASS_T_DEC
00151 inline typename PB_DS_CLASS_C_DEC::node_pointer
00152 PB_DS_CLASS_C_DEC::
00153 get_new_node_for_leaf_insert(const_reference r_val, false_type)
00154 {
00155   node_pointer p_new_nd = s_node_allocator.allocate(1);
00156   cond_dealtor_t cond(p_new_nd);
00157 
00158   new (const_cast<void* >(static_cast<const void* >(&p_new_nd->m_value)))
00159     typename node::value_type(r_val);
00160 
00161   cond.set_no_action();
00162   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
00163   ++m_size;
00164   return p_new_nd;
00165 }
00166 
00167 PB_DS_CLASS_T_DEC
00168 inline typename PB_DS_CLASS_C_DEC::node_pointer
00169 PB_DS_CLASS_C_DEC::
00170 get_new_node_for_leaf_insert(const_reference r_val, true_type)
00171 {
00172   node_pointer p_new_nd = s_node_allocator.allocate(1);
00173 
00174   new (const_cast<void* >(static_cast<const void* >(&p_new_nd->m_value)))
00175     typename node::value_type(r_val);
00176 
00177   p_new_nd->m_p_left = p_new_nd->m_p_right = 0;
00178   ++m_size;
00179   return p_new_nd;
00180 }