libstdc++
splay_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 splay_tree_/splay_fn_imps.hpp
00038  * Contains an implementation class for splay_tree_.
00039  */
00040 
00041 PB_DS_CLASS_T_DEC
00042 void
00043 PB_DS_CLASS_C_DEC::
00044 splay(node_pointer p_nd)
00045 {
00046   while (p_nd->m_p_parent != base_type::m_p_head)
00047     {
00048 #ifdef _GLIBCXX_DEBUG
00049       {
00050     node_pointer p_head = base_type::m_p_head;
00051     assert_special_imp(p_head, __FILE__, __LINE__);
00052       }
00053 #endif
00054 
00055       PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd)
00056 
00057       if (p_nd->m_p_parent->m_p_parent == base_type::m_p_head)
00058     {
00059       base_type::rotate_parent(p_nd);
00060       _GLIBCXX_DEBUG_ASSERT(p_nd == this->m_p_head->m_p_parent);
00061     }
00062       else
00063     {
00064       const node_pointer p_parent = p_nd->m_p_parent;
00065       const node_pointer p_grandparent = p_parent->m_p_parent;
00066 
00067 #ifdef _GLIBCXX_DEBUG
00068       const size_type total =
00069         base_type::recursive_count(p_grandparent);
00070       _GLIBCXX_DEBUG_ASSERT(total >= 3);
00071 #endif
00072 
00073       if (p_parent->m_p_left == p_nd &&
00074           p_grandparent->m_p_right == p_parent)
00075         splay_zig_zag_left(p_nd, p_parent, p_grandparent);
00076       else if (p_parent->m_p_right == p_nd &&
00077            p_grandparent->m_p_left == p_parent)
00078         splay_zig_zag_right(p_nd, p_parent, p_grandparent);
00079       else if (p_parent->m_p_left == p_nd &&
00080            p_grandparent->m_p_left == p_parent)
00081         splay_zig_zig_left(p_nd, p_parent, p_grandparent);
00082       else
00083         splay_zig_zig_right(p_nd, p_parent, p_grandparent);
00084       _GLIBCXX_DEBUG_ASSERT(total ==this->recursive_count(p_nd));
00085     }
00086 
00087       PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd)
00088     }
00089 }
00090 
00091 PB_DS_CLASS_T_DEC
00092 inline void
00093 PB_DS_CLASS_C_DEC::
00094 splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent,
00095            node_pointer p_grandparent)
00096 {
00097   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
00098   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
00099 
00100   PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent)
00101 
00102   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd &&
00103             p_grandparent->m_p_right == p_parent);
00104 
00105   splay_zz_start(p_nd, p_parent, p_grandparent);
00106 
00107   node_pointer p_b = p_nd->m_p_right;
00108   node_pointer p_c = p_nd->m_p_left;
00109 
00110   p_nd->m_p_right = p_parent;
00111   p_parent->m_p_parent = p_nd;
00112 
00113   p_nd->m_p_left = p_grandparent;
00114   p_grandparent->m_p_parent = p_nd;
00115 
00116   p_parent->m_p_left = p_b;
00117   if (p_b != 0)
00118     p_b->m_p_parent = p_parent;
00119 
00120   p_grandparent->m_p_right = p_c;
00121   if (p_c != 0)
00122     p_c->m_p_parent = p_grandparent;
00123 
00124   splay_zz_end(p_nd, p_parent, p_grandparent);
00125 }
00126 
00127 PB_DS_CLASS_T_DEC
00128 inline void
00129 PB_DS_CLASS_C_DEC::
00130 splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent,
00131             node_pointer p_grandparent)
00132 {
00133   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
00134   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
00135 
00136   PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent)
00137 
00138   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd &&
00139             p_grandparent->m_p_left == p_parent);
00140 
00141   splay_zz_start(p_nd, p_parent, p_grandparent);
00142 
00143   node_pointer p_b = p_nd->m_p_left;
00144   node_pointer p_c = p_nd->m_p_right;
00145 
00146   p_nd->m_p_left = p_parent;
00147   p_parent->m_p_parent = p_nd;
00148 
00149   p_nd->m_p_right = p_grandparent;
00150   p_grandparent->m_p_parent = p_nd;
00151 
00152   p_parent->m_p_right = p_b;
00153   if (p_b != 0)
00154     p_b->m_p_parent = p_parent;
00155 
00156   p_grandparent->m_p_left = p_c;
00157   if (p_c != 0)
00158     p_c->m_p_parent = p_grandparent;
00159 
00160   splay_zz_end(p_nd, p_parent, p_grandparent);
00161 }
00162 
00163 PB_DS_CLASS_T_DEC
00164 inline void
00165 PB_DS_CLASS_C_DEC::
00166 splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent,
00167            node_pointer p_grandparent)
00168 {
00169   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
00170   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
00171 
00172   PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent)
00173 
00174   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_left == p_nd &&
00175              p_nd->m_p_parent->m_p_parent->m_p_left == p_nd->m_p_parent);
00176 
00177   splay_zz_start(p_nd, p_parent, p_grandparent);
00178 
00179   node_pointer p_b = p_nd->m_p_right;
00180   node_pointer p_c = p_parent->m_p_right;
00181 
00182   p_nd->m_p_right = p_parent;
00183   p_parent->m_p_parent = p_nd;
00184 
00185   p_parent->m_p_right = p_grandparent;
00186   p_grandparent->m_p_parent = p_parent;
00187 
00188   p_parent->m_p_left = p_b;
00189   if (p_b != 0)
00190     p_b->m_p_parent = p_parent;
00191 
00192   p_grandparent->m_p_left = p_c;
00193   if (p_c != 0)
00194     p_c->m_p_parent = p_grandparent;
00195 
00196   splay_zz_end(p_nd, p_parent, p_grandparent);
00197 }
00198 
00199 PB_DS_CLASS_T_DEC
00200 inline void
00201 PB_DS_CLASS_C_DEC::
00202 splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent,
00203             node_pointer p_grandparent)
00204 {
00205   _GLIBCXX_DEBUG_ASSERT(p_parent == p_nd->m_p_parent);
00206   _GLIBCXX_DEBUG_ASSERT(p_grandparent == p_parent->m_p_parent);
00207   PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_grandparent)
00208   _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_right == p_nd &&
00209           p_nd->m_p_parent->m_p_parent->m_p_right == p_nd->m_p_parent);
00210 
00211   splay_zz_start(p_nd, p_parent, p_grandparent);
00212 
00213   node_pointer p_b = p_nd->m_p_left;
00214   node_pointer p_c = p_parent->m_p_left;
00215 
00216   p_nd->m_p_left = p_parent;
00217   p_parent->m_p_parent = p_nd;
00218 
00219   p_parent->m_p_left = p_grandparent;
00220   p_grandparent->m_p_parent = p_parent;
00221 
00222   p_parent->m_p_right = p_b;
00223   if (p_b != 0)
00224     p_b->m_p_parent = p_parent;
00225 
00226   p_grandparent->m_p_right = p_c;
00227   if (p_c != 0)
00228     p_c->m_p_parent = p_grandparent;
00229 
00230   base_type::update_to_top(p_grandparent, (node_update*)this);
00231   splay_zz_end(p_nd, p_parent, p_grandparent);
00232 }
00233 
00234 PB_DS_CLASS_T_DEC
00235 inline void
00236 PB_DS_CLASS_C_DEC::
00237 splay_zz_start(node_pointer p_nd,
00238 #ifdef _GLIBCXX_DEBUG
00239            node_pointer p_parent,
00240 #else
00241            node_pointer /*p_parent*/,
00242 #endif
00243            node_pointer p_grandparent)
00244 {
00245   _GLIBCXX_DEBUG_ASSERT(p_nd != 0);
00246   _GLIBCXX_DEBUG_ASSERT(p_parent != 0);
00247   _GLIBCXX_DEBUG_ASSERT(p_grandparent != 0);
00248 
00249   const bool grandparent_head = p_grandparent->m_p_parent == base_type::m_p_head;
00250 
00251   if (grandparent_head)
00252     {
00253       base_type::m_p_head->m_p_parent = base_type::m_p_head->m_p_parent;
00254       p_nd->m_p_parent = base_type::m_p_head;
00255       return;
00256     }
00257 
00258   node_pointer p_greatgrandparent = p_grandparent->m_p_parent;
00259 
00260   p_nd->m_p_parent = p_greatgrandparent;
00261 
00262   if (p_grandparent == p_greatgrandparent->m_p_left)
00263     p_greatgrandparent->m_p_left = p_nd;
00264   else
00265     p_greatgrandparent->m_p_right = p_nd;
00266 }
00267 
00268 PB_DS_CLASS_T_DEC
00269 inline void
00270 PB_DS_CLASS_C_DEC::
00271 splay_zz_end(node_pointer p_nd, node_pointer p_parent,
00272          node_pointer p_grandparent)
00273 {
00274   if (p_nd->m_p_parent == base_type::m_p_head)
00275     base_type::m_p_head->m_p_parent = p_nd;
00276 
00277   this->apply_update(p_grandparent, (node_update*)this);
00278   this->apply_update(p_parent, (node_update*)this);
00279   this->apply_update(p_nd, (node_update*)this);
00280   PB_DS_ASSERT_BASE_NODE_CONSISTENT(p_nd)
00281 }