3 // Copyright (C) 2005 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32 // Permission to use, copy, modify, sell, and distribute this software
33 // is hereby granted without fee, provided that the above copyright
34 // notice appears in all copies, and that both that copyright notice and
35 // this permission notice appear in supporting documentation. None of
36 // the above authors, nor IBM Haifa Research Laboratories, make any
37 // representation about the suitability of this software for any
38 // purpose. It is provided "as is" without express or implied warranty.
41 * @file rotate_fn_imps.hpp
42 * Contains imps for rotating nodes.
47 PB_ASSOC_CLASS_C_DEC::
48 rotate_left(node_pointer p_x
)
50 node_pointer p_y
= p_x
->m_p_right
;
52 p_x
->m_p_right
= p_y
->m_p_left
;
54 if (p_y
->m_p_left
!= NULL
)
55 p_y
->m_p_left
->m_p_parent
= p_x
;
57 p_y
->m_p_parent
= p_x
->m_p_parent
;
59 if (p_x
== m_p_head
->m_p_parent
)
60 m_p_head
->m_p_parent
= p_y
;
61 else if (p_x
== p_x
->m_p_parent
->m_p_left
)
62 p_x
->m_p_parent
->m_p_left
= p_y
;
64 p_x
->m_p_parent
->m_p_right
= p_y
;
67 p_x
->m_p_parent
= p_y
;
69 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_x
);)
70 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_y
);)
72 apply_update(p_x
, (Node_Updator
* )this);
73 apply_update(p_x
->m_p_parent
, (Node_Updator
* )this);
78 PB_ASSOC_CLASS_C_DEC::
79 rotate_right(node_pointer p_x
)
81 node_pointer p_y
= p_x
->m_p_left
;
83 p_x
->m_p_left
= p_y
->m_p_right
;
85 if (p_y
->m_p_right
!= NULL
)
86 p_y
->m_p_right
->m_p_parent
= p_x
;
88 p_y
->m_p_parent
= p_x
->m_p_parent
;
90 if (p_x
== m_p_head
->m_p_parent
)
91 m_p_head
->m_p_parent
= p_y
;
92 else if (p_x
== p_x
->m_p_parent
->m_p_right
)
93 p_x
->m_p_parent
->m_p_right
= p_y
;
95 p_x
->m_p_parent
->m_p_left
= p_y
;
98 p_x
->m_p_parent
= p_y
;
100 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_x
);)
101 PB_ASSOC_DBG_ONLY(assert_node_consistent(p_y
);)
103 apply_update(p_x
, (Node_Updator
* )this);
104 apply_update(p_x
->m_p_parent
, (Node_Updator
* )this);
109 PB_ASSOC_CLASS_C_DEC::
110 rotate_parent(node_pointer p_nd
)
112 node_pointer p_parent
= p_nd
->m_p_parent
;
114 if (p_nd
== p_parent
->m_p_left
)
115 rotate_right(p_parent
);
117 rotate_left(p_parent
);
119 PB_ASSOC_DBG_ASSERT(p_parent
->m_p_parent
= p_nd
);
120 PB_ASSOC_DBG_ASSERT(p_nd
->m_p_left
== p_parent
||
121 p_nd
->m_p_right
== p_parent
);
126 PB_ASSOC_CLASS_C_DEC::
127 apply_update(node_pointer
/*p_nd*/, pb_assoc::null_node_updator
* /*p_updator*/)
131 template<class Node_Updator_
>
133 PB_ASSOC_CLASS_C_DEC::
134 apply_update(node_pointer p_nd
, Node_Updator_
* p_updator
)
136 p_updator
->operator()(
137 &PB_ASSOC_V2F(p_nd
->m_value
),(p_nd
->m_p_left
== NULL
)?
139 &PB_ASSOC_V2F(p_nd
->m_p_left
->m_value
),(p_nd
->m_p_right
== NULL
)?
141 &PB_ASSOC_V2F(p_nd
->m_p_right
->m_value
));
145 template<class Node_Updator_
>
147 PB_ASSOC_CLASS_C_DEC::
148 update_to_top(node_pointer p_nd
, Node_Updator_
* p_updator
)
150 while (p_nd
!= m_p_head
)
152 apply_update(p_nd
, p_updator
);
154 p_nd
= p_nd
->m_p_parent
;
160 PB_ASSOC_CLASS_C_DEC::
161 update_to_top(node_pointer
/*p_nd*/, pb_assoc::null_node_updator
* /*p_updator*/)