libstdc++
left_child_next_sibling_heap_/insert_fn_imps.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010 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 left_child_next_sibling_heap_/insert_fn_imps.hpp
00038  * Contains an implementation class for left_child_next_sibling_heap_.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 inline typename PB_DS_CLASS_C_DEC::node_pointer
00043 PB_DS_CLASS_C_DEC::
00044 get_new_node_for_insert(const_reference r_val)
00045 {
00046   return get_new_node_for_insert(r_val, s_no_throw_copies_ind);
00047 }
00048 
00049 PB_DS_CLASS_T_DEC
00050 inline typename PB_DS_CLASS_C_DEC::node_pointer
00051 PB_DS_CLASS_C_DEC::
00052 get_new_node_for_insert(const_reference r_val, false_type)
00053 {
00054   node_pointer p_new_nd = s_node_allocator.allocate(1);
00055 
00056   cond_dealtor_t cond(p_new_nd);
00057 
00058   new (const_cast<void* >(
00059               static_cast<const void* >(&p_new_nd->m_value)))
00060     typename node::value_type(r_val);
00061 
00062   cond.set_no_action();
00063 
00064   ++m_size;
00065 
00066   return (p_new_nd);
00067 }
00068 
00069 PB_DS_CLASS_T_DEC
00070 inline typename PB_DS_CLASS_C_DEC::node_pointer
00071 PB_DS_CLASS_C_DEC::
00072 get_new_node_for_insert(const_reference r_val, true_type)
00073 {
00074   node_pointer p_new_nd = s_node_allocator.allocate(1);
00075 
00076   new (const_cast<void* >(
00077               static_cast<const void* >(&p_new_nd->m_value)))
00078     typename node::value_type(r_val);
00079 
00080   ++m_size;
00081 
00082   return (p_new_nd);
00083 }
00084 
00085 PB_DS_CLASS_T_DEC
00086 inline void
00087 PB_DS_CLASS_C_DEC::
00088 make_child_of(node_pointer p_nd, node_pointer p_new_parent)
00089 {
00090   _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
00091   _GLIBCXX_DEBUG_ASSERT(p_new_parent != 0);
00092 
00093   p_nd->m_p_next_sibling = p_new_parent->m_p_l_child;
00094 
00095   if (p_new_parent->m_p_l_child != 0)
00096     p_new_parent->m_p_l_child->m_p_prev_or_parent = p_nd;
00097 
00098   p_nd->m_p_prev_or_parent = p_new_parent;
00099 
00100   p_new_parent->m_p_l_child = p_nd;
00101 }
00102 
00103 PB_DS_CLASS_T_DEC
00104 inline typename PB_DS_CLASS_C_DEC::node_pointer
00105 PB_DS_CLASS_C_DEC::
00106 parent(node_pointer p_nd)
00107 {
00108   while (true)
00109     {
00110       node_pointer p_pot = p_nd->m_p_prev_or_parent;
00111 
00112       if (p_pot == 0 || p_pot->m_p_l_child == p_nd)
00113     return p_pot;
00114 
00115       p_nd = p_pot;
00116     }
00117 }
00118 
00119 PB_DS_CLASS_T_DEC
00120 inline void
00121 PB_DS_CLASS_C_DEC::
00122 swap_with_parent(node_pointer p_nd, node_pointer p_parent)
00123 {
00124   if (p_parent == m_p_root)
00125     m_p_root = p_nd;
00126 
00127   _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
00128   _GLIBCXX_DEBUG_ASSERT(p_parent != 0);
00129   _GLIBCXX_DEBUG_ASSERT(parent(p_nd) == p_parent);
00130 
00131   const bool nd_direct_child = p_parent->m_p_l_child == p_nd;
00132   const bool parent_root = p_parent->m_p_prev_or_parent == 0;
00133   const bool parent_direct_child =
00134     !parent_root&&  p_parent->m_p_prev_or_parent->m_p_l_child == p_parent;
00135 
00136   std::swap(p_parent->m_p_prev_or_parent, p_nd->m_p_prev_or_parent);
00137   std::swap(p_parent->m_p_next_sibling, p_nd->m_p_next_sibling);
00138   std::swap(p_parent->m_p_l_child, p_nd->m_p_l_child);
00139   std::swap(p_parent->m_metadata, p_nd->m_metadata);
00140 
00141   _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child != 0);
00142   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent != 0);
00143 
00144   if (p_nd->m_p_next_sibling != 0)
00145     p_nd->m_p_next_sibling->m_p_prev_or_parent = p_nd;
00146 
00147   if (p_parent->m_p_next_sibling != 0)
00148     p_parent->m_p_next_sibling->m_p_prev_or_parent = p_parent;
00149 
00150   if (p_parent->m_p_l_child != 0)
00151     p_parent->m_p_l_child->m_p_prev_or_parent = p_parent;
00152 
00153   if (parent_direct_child)
00154     p_nd->m_p_prev_or_parent->m_p_l_child = p_nd;
00155   else if (!parent_root)
00156     p_nd->m_p_prev_or_parent->m_p_next_sibling = p_nd;
00157 
00158   if (!nd_direct_child)
00159     {
00160       p_nd->m_p_l_child->m_p_prev_or_parent = p_nd;
00161 
00162       p_parent->m_p_prev_or_parent->m_p_next_sibling = p_parent;
00163     }
00164   else
00165     {
00166       _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_l_child == p_nd);
00167       _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_prev_or_parent == p_parent);
00168 
00169       p_nd->m_p_l_child = p_parent;
00170       p_parent->m_p_prev_or_parent = p_nd;
00171     }
00172 
00173   _GLIBCXX_DEBUG_ASSERT(parent(p_parent) == p_nd);
00174 }
00175