splay_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 splay(node_pointer p_nd)
00049 {
00050   while (p_nd->m_p_parent != PB_ASSOC_BASE_C_DEC::m_p_head)
00051     {
00052       PB_ASSOC_DBG_ONLY(PB_ASSOC_BASE_C_DEC::assert_node_consistent(p_nd);)
00053 
00054     if (p_nd->m_p_parent->m_p_parent ==
00055         PB_ASSOC_BASE_C_DEC::m_p_head)
00056       {
00057         PB_ASSOC_BASE_C_DEC::rotate_parent(p_nd);
00058 
00059         PB_ASSOC_DBG_ASSERT(p_nd ==
00060                 PB_ASSOC_BASE_C_DEC::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 PB_ASSOC_SPLAY_TREE_DEBUG_
00068         const size_type total =
00069           PB_ASSOC_BASE_C_DEC::recursive_count(p_grandparent);
00070 
00071         PB_ASSOC_DBG_ASSERT(total >= 3);
00072 #endif // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
00073 
00074         if (p_parent->m_p_left == p_nd&& 
00075         p_grandparent->m_p_right == p_parent)
00076           splay_zig_zag_left(p_nd, p_parent, p_grandparent);
00077         else if (p_parent->m_p_right == p_nd&& 
00078              p_grandparent->m_p_left == p_parent)
00079           splay_zig_zag_right(p_nd, p_parent, p_grandparent);
00080         else if (p_parent->m_p_left == p_nd&& 
00081              p_grandparent->m_p_left == p_parent)
00082           splay_zig_zig_left(p_nd, p_parent, p_grandparent);
00083         else
00084           splay_zig_zig_right(p_nd, p_parent, p_grandparent);
00085 
00086         PB_ASSOC_DBG_ASSERT(total ==
00087                 PB_ASSOC_BASE_C_DEC::recursive_count(p_nd));
00088       }
00089 
00090       PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd);)
00091     }
00092 }
00093 
00094 PB_ASSOC_CLASS_T_DEC
00095 inline void
00096 PB_ASSOC_CLASS_C_DEC::
00097 splay_zig_zag_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
00098 {
00099   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
00100   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
00101 
00102   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
00103 
00104     PB_ASSOC_DBG_ASSERT(p_parent->m_p_left == p_nd&& 
00105             p_grandparent->m_p_right == p_parent);
00106 
00107   splay_zz_start(p_nd, p_parent, p_grandparent);
00108 
00109   node_pointer p_b = p_nd->m_p_right;
00110   node_pointer p_c = p_nd->m_p_left;
00111 
00112   p_nd->m_p_right = p_parent;
00113   p_parent->m_p_parent = p_nd;
00114 
00115   p_nd->m_p_left = p_grandparent;
00116   p_grandparent->m_p_parent = p_nd;
00117 
00118   p_parent->m_p_left = p_b;
00119   if (p_b != NULL)
00120     p_b->m_p_parent = p_parent;
00121 
00122   p_grandparent->m_p_right = p_c;
00123   if (p_c != NULL)
00124     p_c->m_p_parent = p_grandparent;
00125 
00126   splay_zz_end(p_nd, p_parent, p_grandparent);
00127 }
00128 
00129 PB_ASSOC_CLASS_T_DEC
00130 inline void
00131 PB_ASSOC_CLASS_C_DEC::
00132 splay_zig_zag_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
00133 {
00134   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
00135   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
00136 
00137   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
00138 
00139     PB_ASSOC_DBG_ASSERT(p_parent->m_p_right == p_nd&& 
00140             p_grandparent->m_p_left == p_parent);
00141 
00142   splay_zz_start(p_nd, p_parent, p_grandparent);
00143 
00144   node_pointer p_b = p_nd->m_p_left;
00145   node_pointer p_c = p_nd->m_p_right;
00146 
00147   p_nd->m_p_left = p_parent;
00148   p_parent->m_p_parent = p_nd;
00149 
00150   p_nd->m_p_right = p_grandparent;
00151   p_grandparent->m_p_parent = p_nd;
00152 
00153   p_parent->m_p_right = p_b;
00154   if (p_b != NULL)
00155     p_b->m_p_parent = p_parent;
00156 
00157   p_grandparent->m_p_left = p_c;
00158   if (p_c != NULL)
00159     p_c->m_p_parent = p_grandparent;
00160 
00161   splay_zz_end(p_nd, p_parent, p_grandparent);
00162 }
00163 
00164 PB_ASSOC_CLASS_T_DEC
00165 inline void
00166 PB_ASSOC_CLASS_C_DEC::
00167 splay_zig_zig_left(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
00168 {
00169   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
00170   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
00171 
00172   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
00173 
00174     PB_ASSOC_DBG_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 != NULL)
00190     p_b->m_p_parent = p_parent;
00191 
00192   p_grandparent->m_p_left = p_c;
00193   if (p_c != NULL)
00194     p_c->m_p_parent = p_grandparent;
00195 
00196   splay_zz_end(p_nd, p_parent, p_grandparent);
00197 }
00198 
00199 PB_ASSOC_CLASS_T_DEC
00200 inline void
00201 PB_ASSOC_CLASS_C_DEC::
00202 splay_zig_zig_right(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
00203 {
00204   PB_ASSOC_DBG_ASSERT(p_parent == p_nd->m_p_parent);
00205   PB_ASSOC_DBG_ASSERT(p_grandparent == p_parent->m_p_parent);
00206 
00207   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_grandparent);)
00208 
00209     PB_ASSOC_DBG_ASSERT(p_parent->m_p_right == p_nd&& 
00210             p_nd->m_p_parent->m_p_parent->m_p_right == p_nd->m_p_parent);
00211 
00212   splay_zz_start(p_nd, p_parent, p_grandparent);
00213 
00214   node_pointer p_b = p_nd->m_p_left;
00215   node_pointer p_c = p_parent->m_p_left;
00216 
00217   p_nd->m_p_left = p_parent;
00218   p_parent->m_p_parent = p_nd;
00219 
00220   p_parent->m_p_left = p_grandparent;
00221   p_grandparent->m_p_parent = p_parent;
00222 
00223   p_parent->m_p_right = p_b;
00224   if (p_b != NULL)
00225     p_b->m_p_parent = p_parent;
00226 
00227   p_grandparent->m_p_right = p_c;
00228   if (p_c != NULL)
00229     p_c->m_p_parent = p_grandparent;
00230 
00231   PB_ASSOC_BASE_C_DEC::update_to_top(
00232                      p_grandparent, (Node_Updator* )this);
00233 
00234   splay_zz_end(p_nd, p_parent, p_grandparent);
00235 }
00236 
00237 PB_ASSOC_CLASS_T_DEC
00238 inline void
00239 PB_ASSOC_CLASS_C_DEC::
00240 splay_zz_start(node_pointer p_nd,
00241 #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
00242            node_pointer p_parent,
00243 #else // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
00244            node_pointer /*p_parent*/,
00245 #endif // #ifdef PB_ASSOC_SPLAY_TREE_DEBUG_
00246            node_pointer p_grandparent)
00247 {
00248   PB_ASSOC_DBG_ASSERT(p_nd != NULL);
00249   PB_ASSOC_DBG_ASSERT(p_parent != NULL);
00250   PB_ASSOC_DBG_ASSERT(p_grandparent != NULL);
00251 
00252   const bool grandparent_head =
00253     p_grandparent->m_p_parent == PB_ASSOC_BASE_C_DEC::m_p_head;
00254 
00255   if (grandparent_head)
00256     {
00257       PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent =
00258     PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent;
00259 
00260       p_nd->m_p_parent = PB_ASSOC_BASE_C_DEC::m_p_head;
00261 
00262       return;
00263     }
00264 
00265   node_pointer p_greatgrandparent = p_grandparent->m_p_parent;
00266 
00267   p_nd->m_p_parent = p_greatgrandparent;
00268 
00269   if (p_grandparent == p_greatgrandparent->m_p_left)
00270     p_greatgrandparent->m_p_left = p_nd;
00271   else
00272     p_greatgrandparent->m_p_right = p_nd;
00273 }
00274 
00275 PB_ASSOC_CLASS_T_DEC
00276 inline void
00277 PB_ASSOC_CLASS_C_DEC::
00278 splay_zz_end(node_pointer p_nd, node_pointer p_parent, node_pointer p_grandparent)
00279 {
00280   if (p_nd->m_p_parent == PB_ASSOC_BASE_C_DEC::m_p_head)
00281     PB_ASSOC_BASE_C_DEC::m_p_head->m_p_parent = p_nd;
00282 
00283   apply_update(p_grandparent, (Node_Updator* )this);
00284   apply_update(p_parent, (Node_Updator* )this);
00285   apply_update(p_nd, (Node_Updator* )this);
00286 
00287   PB_ASSOC_DBG_ONLY(assert_node_consistent(p_nd);)
00288     }
00289 

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