libstdc++
bin_search_tree_/rotate_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_/rotate_fn_imps.hpp
00038  * Contains imps for rotating nodes.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 inline void
00043 PB_DS_CLASS_C_DEC::
00044 rotate_left(node_pointer p_x)
00045 {
00046   node_pointer p_y = p_x->m_p_right;
00047 
00048   p_x->m_p_right = p_y->m_p_left;
00049 
00050   if (p_y->m_p_left != 0)
00051     p_y->m_p_left->m_p_parent = p_x;
00052 
00053   p_y->m_p_parent = p_x->m_p_parent;
00054 
00055   if (p_x == m_p_head->m_p_parent)
00056     m_p_head->m_p_parent = p_y;
00057   else if (p_x == p_x->m_p_parent->m_p_left)
00058     p_x->m_p_parent->m_p_left = p_y;
00059   else
00060     p_x->m_p_parent->m_p_right = p_y;
00061 
00062   p_y->m_p_left = p_x;
00063   p_x->m_p_parent = p_y;
00064 
00065   PB_DS_ASSERT_NODE_CONSISTENT(p_x)
00066   PB_DS_ASSERT_NODE_CONSISTENT(p_y)
00067 
00068   apply_update(p_x, (node_update* )this);
00069   apply_update(p_x->m_p_parent, (node_update* )this);
00070 }
00071 
00072 PB_DS_CLASS_T_DEC
00073 inline void
00074 PB_DS_CLASS_C_DEC::
00075 rotate_right(node_pointer p_x)
00076 {
00077   node_pointer p_y = p_x->m_p_left;
00078 
00079   p_x->m_p_left = p_y->m_p_right;
00080 
00081   if (p_y->m_p_right != 0)
00082     p_y->m_p_right->m_p_parent = p_x;
00083 
00084   p_y->m_p_parent = p_x->m_p_parent;
00085 
00086   if (p_x == m_p_head->m_p_parent)
00087     m_p_head->m_p_parent = p_y;
00088   else if (p_x == p_x->m_p_parent->m_p_right)
00089     p_x->m_p_parent->m_p_right = p_y;
00090   else
00091     p_x->m_p_parent->m_p_left = p_y;
00092 
00093   p_y->m_p_right = p_x;
00094   p_x->m_p_parent = p_y;
00095 
00096   PB_DS_ASSERT_NODE_CONSISTENT(p_x)
00097   PB_DS_ASSERT_NODE_CONSISTENT(p_y)
00098 
00099   apply_update(p_x, (node_update* )this);
00100   apply_update(p_x->m_p_parent, (node_update* )this);
00101 }
00102 
00103 PB_DS_CLASS_T_DEC
00104 inline void
00105 PB_DS_CLASS_C_DEC::
00106 rotate_parent(node_pointer p_nd)
00107 {
00108   node_pointer p_parent = p_nd->m_p_parent;
00109 
00110   if (p_nd == p_parent->m_p_left)
00111     rotate_right(p_parent);
00112   else
00113     rotate_left(p_parent);
00114 
00115   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent = p_nd);
00116   _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == p_parent ||
00117            p_nd->m_p_right == p_parent);
00118 }
00119 
00120 PB_DS_CLASS_T_DEC
00121 inline void
00122 PB_DS_CLASS_C_DEC::
00123 apply_update(node_pointer /*p_nd*/, null_node_update_pointer /*p_update*/)
00124 { }
00125 
00126 PB_DS_CLASS_T_DEC
00127 template<typename Node_Update_>
00128 inline void
00129 PB_DS_CLASS_C_DEC::
00130 apply_update(node_pointer p_nd, Node_Update_*  /*p_update*/)
00131 {
00132   node_update::operator()(node_iterator(p_nd),
00133               node_const_iterator(static_cast<node_pointer>(0)));
00134 }
00135 
00136 PB_DS_CLASS_T_DEC
00137 template<typename Node_Update_>
00138 inline void
00139 PB_DS_CLASS_C_DEC::
00140 update_to_top(node_pointer p_nd, Node_Update_* p_update)
00141 {
00142   while (p_nd != m_p_head)
00143     {
00144       apply_update(p_nd, p_update);
00145 
00146       p_nd = p_nd->m_p_parent;
00147     }
00148 }
00149 
00150 PB_DS_CLASS_T_DEC
00151 inline void
00152 PB_DS_CLASS_C_DEC::
00153 update_to_top(node_pointer /*p_nd*/, null_node_update_pointer /*p_update*/)
00154 { }
00155