1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
4 //
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 terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction. Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License. This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43 * @file rotate_fn_imps.hpp
44 * Contains imps for rotating nodes.
45 */
46
47 PB_DS_CLASS_T_DEC
48 inline void
49 PB_DS_CLASS_C_DEC::
rotate_left(node_pointer p_x)50 rotate_left(node_pointer p_x)
51 {
52 node_pointer p_y = p_x->m_p_right;
53 p_x->m_p_right = p_y->m_p_left;
54
55 if (p_y->m_p_left != NULL)
56 p_y->m_p_left->m_p_parent = p_x;
57
58 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;
63 else
64 p_x->m_p_parent->m_p_right = p_y;
65
66 p_y->m_p_left = p_x;
67 p_x->m_p_parent = p_y;
68
69 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_x);)
70 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_y);)
71
72 apply_update(p_x, (Node_Update* )this);
73 apply_update(p_x->m_p_parent, (Node_Update* )this);
74 }
75
76 PB_DS_CLASS_T_DEC
77 inline void
78 PB_DS_CLASS_C_DEC::
rotate_right(node_pointer p_x)79 rotate_right(node_pointer p_x)
80 {
81 node_pointer p_y = p_x->m_p_left;
82 p_x->m_p_left = p_y->m_p_right;
83
84 if (p_y->m_p_right != NULL)
85 p_y->m_p_right->m_p_parent = p_x;
86
87 p_y->m_p_parent = p_x->m_p_parent;
88 if (p_x == m_p_head->m_p_parent)
89 m_p_head->m_p_parent = p_y;
90 else if (p_x == p_x->m_p_parent->m_p_right)
91 p_x->m_p_parent->m_p_right = p_y;
92 else
93 p_x->m_p_parent->m_p_left = p_y;
94
95 p_y->m_p_right = p_x;
96 p_x->m_p_parent = p_y;
97
98 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_x);)
99 _GLIBCXX_DEBUG_ONLY(assert_node_consistent(p_y);)
100
101 apply_update(p_x, (Node_Update* )this);
102 apply_update(p_x->m_p_parent, (Node_Update* )this);
103 }
104
105 PB_DS_CLASS_T_DEC
106 inline void
107 PB_DS_CLASS_C_DEC::
rotate_parent(node_pointer p_nd)108 rotate_parent(node_pointer p_nd)
109 {
110 node_pointer p_parent = p_nd->m_p_parent;
111 if (p_nd == p_parent->m_p_left)
112 rotate_right(p_parent);
113 else
114 rotate_left(p_parent);
115 _GLIBCXX_DEBUG_ASSERT(p_parent->m_p_parent = p_nd);
116 _GLIBCXX_DEBUG_ASSERT(p_nd->m_p_left == p_parent || p_nd->m_p_right == p_parent);
117 }
118
119 PB_DS_CLASS_T_DEC
120 inline void
121 PB_DS_CLASS_C_DEC::
apply_update(node_pointer,pb_ds::null_node_update *)122 apply_update(node_pointer /*p_nd*/, pb_ds::null_node_update* /*p_update*/)
123 { }
124
125 PB_DS_CLASS_T_DEC
126 template<typename Node_Update_>
127 inline void
128 PB_DS_CLASS_C_DEC::
apply_update(node_pointer p_nd,Node_Update_ * p_update)129 apply_update(node_pointer p_nd, Node_Update_* p_update)
130 {
131 p_update->operator()(& PB_DS_V2F(p_nd->m_value),(p_nd->m_p_left == NULL) ?
132 NULL :
133 & PB_DS_V2F(p_nd->m_p_left->m_value),(p_nd->m_p_right == NULL) ?
134 NULL :
135 & PB_DS_V2F(p_nd->m_p_right->m_value));
136 }
137
138 PB_DS_CLASS_T_DEC
139 template<typename Node_Update_>
140 inline void
141 PB_DS_CLASS_C_DEC::
update_to_top(node_pointer p_nd,Node_Update_ * p_update)142 update_to_top(node_pointer p_nd, Node_Update_* p_update)
143 {
144 while (p_nd != m_p_head)
145 {
146 apply_update(p_nd, p_update);
147 p_nd = p_nd->m_p_parent;
148 }
149 }
150
151 PB_DS_CLASS_T_DEC
152 inline void
153 PB_DS_CLASS_C_DEC::
update_to_top(node_pointer,pb_ds::null_node_update *)154 update_to_top(node_pointer /*p_nd*/, pb_ds::null_node_update* /*p_update*/)
155 { }
156
157