[rtems commit] PR1994: RBTree Compare Result Change

gedare at rtems.org gedare at rtems.org
Fri Mar 30 00:41:26 UTC 2012


Module:    rtems
Branch:    master
Commit:    890358f4f7cb8bc3ac3dcbbe5805e8285c92444a
Changeset: http://git.rtems.org/rtems/commit/?id=890358f4f7cb8bc3ac3dcbbe5805e8285c92444a

Author:    Gedare Bloom <gedare at rtems.org>
Date:      Thu Mar 29 20:41:34 2012 -0400

PR1994: RBTree Compare Result Change

Change the meaning of the compare result to simplify comparison of
integer keys.

---

 cpukit/score/include/rtems/score/rbtree.h  |    4 ++--
 cpukit/score/inline/rtems/score/rbtree.inl |   23 +++++++++++++++++++++--
 cpukit/score/src/rbtreeinsert.c            |    4 ++--
 testsuites/sptests/sprbtree01/init.c       |    8 +++-----
 4 files changed, 28 insertions(+), 11 deletions(-)

diff --git a/cpukit/score/include/rtems/score/rbtree.h b/cpukit/score/include/rtems/score/rbtree.h
index f0e3b4b..7c43965 100644
--- a/cpukit/score/include/rtems/score/rbtree.h
+++ b/cpukit/score/include/rtems/score/rbtree.h
@@ -109,8 +109,8 @@ typedef enum {
  * the order in a red-black tree.
  */
 typedef int (*RBTree_Compare_function)(
-    RBTree_Node *node1,
-    RBTree_Node *node2
+  const RBTree_Node *node1,
+  const RBTree_Node *node2
 );
 
 /**
diff --git a/cpukit/score/inline/rtems/score/rbtree.inl b/cpukit/score/inline/rtems/score/rbtree.inl
index ef653e5..c74726a 100644
--- a/cpukit/score/inline/rtems/score/rbtree.inl
+++ b/cpukit/score/inline/rtems/score/rbtree.inl
@@ -324,6 +324,25 @@ RTEMS_INLINE_ROUTINE RBTree_Control *_RBTree_Find_header_unprotected(
   return (RBTree_Control*)the_node;
 }
 
+RTEMS_INLINE_ROUTINE bool _RBTree_Is_equal( int compare_result )
+{
+  return compare_result == 0;
+}
+
+RTEMS_INLINE_ROUTINE bool _RBTree_Is_greater(
+  int compare_result
+)
+{
+  return compare_result > 0;
+}
+
+RTEMS_INLINE_ROUTINE bool _RBTree_Is_lesser(
+  int compare_result
+)
+{
+  return compare_result < 0;
+}
+
 /** @brief Find the node with given key in the tree
  *
  *  This function returns a pointer to the node in @a the_rbtree 
@@ -343,13 +362,13 @@ RTEMS_INLINE_ROUTINE RBTree_Node *_RBTree_Find_unprotected(
   int compare_result;
   while (iter_node) {
     compare_result = the_rbtree->compare_function(the_node, iter_node);
-    if (compare_result == 0) {
+    if ( _RBTree_Is_equal( compare_result ) ) {
       found = iter_node;
       if ( the_rbtree->is_unique )
         break;
     }
 
-    RBTree_Direction dir = (compare_result == 1);
+    RBTree_Direction dir = _RBTree_Is_greater( compare_result );
     iter_node = iter_node->child[dir];
   } /* while(iter_node) */
 
diff --git a/cpukit/score/src/rbtreeinsert.c b/cpukit/score/src/rbtreeinsert.c
index 798bd87..3ea6b35 100644
--- a/cpukit/score/src/rbtreeinsert.c
+++ b/cpukit/score/src/rbtreeinsert.c
@@ -97,9 +97,9 @@ RBTree_Node *_RBTree_Insert_unprotected(
     /* typical binary search tree insert, descend tree to leaf and insert */
     while (iter_node) {
       compare_result = the_rbtree->compare_function(the_node, iter_node);
-      if ( the_rbtree->is_unique && !compare_result )
+      if ( the_rbtree->is_unique && _RBTree_Is_equal( compare_result ) )
         return iter_node;
-      RBTree_Direction dir = (compare_result != -1);
+      RBTree_Direction dir = !_RBTree_Is_lesser( compare_result );
       if (!iter_node->child[dir]) {
         the_node->child[RBT_LEFT] = the_node->child[RBT_RIGHT] = NULL;
         the_node->color = RBT_RED;
diff --git a/testsuites/sptests/sprbtree01/init.c b/testsuites/sptests/sprbtree01/init.c
index 38badc9..754876d 100644
--- a/testsuites/sptests/sprbtree01/init.c
+++ b/testsuites/sptests/sprbtree01/init.c
@@ -28,16 +28,14 @@ typedef struct {
 } test_node;
 
 static int test_compare_function (
-  rtems_rbtree_node* n1,
-  rtems_rbtree_node* n2
+  const rtems_rbtree_node *n1,
+  const rtems_rbtree_node *n2
 )
 {
   int key1 = rtems_rbtree_container_of( n1, test_node, Node )->key;
   int key2 = rtems_rbtree_container_of( n2, test_node, Node )->key;
 
-  if (key1 > key2) return 1;
-  else if (key1 < key2) return -1;
-  else return 0;
+  return key1 - key2;
 }
 
 /* 




More information about the vc mailing list