[PATCH 2/3] rbtree: Simplify _RBTree_Rotate()

Sebastian Huber sebastian.huber at embedded-brains.de
Tue Aug 5 14:27:58 UTC 2014


---
 cpukit/score/include/rtems/score/rbtreeimpl.h | 39 ++++++++++++++++-----------
 testsuites/sptests/sprbtree01/init.c          |  1 -
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/cpukit/score/include/rtems/score/rbtreeimpl.h b/cpukit/score/include/rtems/score/rbtreeimpl.h
index e7d5630..09e4212 100644
--- a/cpukit/score/include/rtems/score/rbtreeimpl.h
+++ b/cpukit/score/include/rtems/score/rbtreeimpl.h
@@ -133,32 +133,41 @@ RTEMS_INLINE_ROUTINE bool _RBTree_Is_lesser(
 }
 
 /**
- * @brief Rotate the_node in the direction passed as second argument.
+ * @brief Rotates the node in the specified direction.
  *
  * This routine rotates @a the_node to the direction @a dir, swapping
  * @a the_node with its child\[@a dir\].
+ *
+ * @param[in] the_node The node to rotate.
+ * @param[in] dir The rotation direction.
  */
 RTEMS_INLINE_ROUTINE void _RBTree_Rotate(
-    RBTree_Node *the_node,
-    RBTree_Direction dir
-    )
+  RBTree_Node      *the_node,
+  RBTree_Direction  dir
+)
 {
-  RBTree_Node *c;
-  if (the_node == NULL) return;
-  if (the_node->child[_RBTree_Opposite_direction(dir)] == NULL) return;
+  RBTree_Direction  opp_dir = _RBTree_Opposite_direction( dir );
+  RBTree_Node      *child = the_node->child[ opp_dir ];
+  RBTree_Node      *grandchild;
+  RBTree_Node      *parent;
+
+  if ( child == NULL)
+    return;
 
-  c = the_node->child[_RBTree_Opposite_direction(dir)];
-  the_node->child[_RBTree_Opposite_direction(dir)] = c->child[dir];
+  grandchild = child->child[ dir ];
+  the_node->child[ opp_dir ] = grandchild;
 
-  if (c->child[dir])
-    c->child[dir]->parent = the_node;
+  if ( grandchild != NULL )
+    grandchild->parent = the_node;
 
-  c->child[dir] = the_node;
+  child->child[ dir ] = the_node;
 
-  the_node->parent->child[the_node != the_node->parent->child[0]] = c;
+  parent = _RBTree_Parent( the_node );
+  parent->child[ the_node == parent->child[ RBT_LEFT ] ? RBT_LEFT : RBT_RIGHT ]
+    = child;
 
-  c->parent = the_node->parent;
-  the_node->parent = c;
+  child->parent = parent;
+  the_node->parent = child;
 }
 
 /** @} */
diff --git a/testsuites/sptests/sprbtree01/init.c b/testsuites/sptests/sprbtree01/init.c
index 89abdd3..6a02a53 100644
--- a/testsuites/sptests/sprbtree01/init.c
+++ b/testsuites/sptests/sprbtree01/init.c
@@ -858,7 +858,6 @@ rtems_task Init( rtems_task_argument ignored )
 
   rtems_test_assert( !rtems_rbtree_is_node_off_tree( &node1.Node ) );
 
-  _RBTree_Rotate(NULL, RBT_LEFT);
   i = (node1.Node.parent == &node2.Node);
   _RBTree_Rotate( &node1.Node,
                   !node1.Node.child[RBT_LEFT] ? RBT_RIGHT : RBT_LEFT
-- 
1.8.1.4




More information about the devel mailing list