[RTL/NTOSKRNL]
[reactos.git] / reactos / lib / rtl / avlsupp.c
index 50b96ce..ff89bc1 100644 (file)
@@ -12,7 +12,6 @@
 typedef struct _TABLE_ENTRY_HEADER
 {
     RTL_BALANCED_LINKS BalancedLinks;
-    LIST_ENTRY ListEntry;
     LONGLONG UserData;
 } TABLE_ENTRY_HEADER, *PTABLE_ENTRY_HEADER;
 
@@ -28,8 +27,8 @@ C_ASSERT(RtlBalancedAvlTree == 0);
 
 /* FUNCTIONS ******************************************************************/
 
-TABLE_SEARCH_RESULT
 FORCEINLINE
+TABLE_SEARCH_RESULT
 RtlpFindAvlTableNodeOrParent(IN PRTL_AVL_TABLE Table,
                              IN PVOID Buffer,
                              OUT PRTL_BALANCED_LINKS *NodeOrParent)
@@ -95,9 +94,9 @@ RtlpFindAvlTableNodeOrParent(IN PRTL_AVL_TABLE Table,
     }
 }
 
-VOID
 FORCEINLINE
-RtlPromoteAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
+VOID
+RtlpPromoteAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
 {
     PRTL_BALANCED_LINKS ParentNode, SuperParentNode;
     PRTL_BALANCED_LINKS *SwapNode1, *SwapNode2;
@@ -123,8 +122,8 @@ RtlPromoteAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
     RtlSetParent(Node, SuperParentNode);
 }
 
-BOOLEAN
 FORCEINLINE
+BOOLEAN
 RtlpRebalanceAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
 {
     PRTL_BALANCED_LINKS ChildNode, SubChildNode;
@@ -140,8 +139,8 @@ RtlpRebalanceAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
     if (RtlBalance(ChildNode) == Balance)
     {
         /* This performs the rotation described in Knuth A8-A10 for Case 1 */
-        RtlPromoteAvlTreeNode(ChildNode);
-        
+        RtlpPromoteAvlTreeNode(ChildNode);
+
         /* The nodes are now balanced */
         RtlSetBalance(ChildNode, RtlBalancedAvlTree);
         RtlSetBalance(Node, RtlBalancedAvlTree);
@@ -156,8 +155,8 @@ RtlpRebalanceAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
                         RtlLeftChildAvl(ChildNode) : RtlRightChildAvl(ChildNode);
 
         /* Do the double-rotation described in Knuth A8-A10 for Case 2 */
-        RtlPromoteAvlTreeNode(SubChildNode);
-        RtlPromoteAvlTreeNode(SubChildNode);
+        RtlpPromoteAvlTreeNode(SubChildNode);
+        RtlpPromoteAvlTreeNode(SubChildNode);
 
         /* Was the sub-child sharing the same balance as the node? */
         if (RtlBalance(SubChildNode) == Balance)
@@ -190,32 +189,33 @@ RtlpRebalanceAvlTreeNode(IN PRTL_BALANCED_LINKS Node)
         RtlSetBalance(SubChildNode, RtlBalancedAvlTree);
         return FALSE;
     }
-    
+
     /*
      * The case that remains is that the child was already balanced, so this is
      * This is the rotation required for Case 3 in Knuth A8-A10
      */
-    RtlPromoteAvlTreeNode(ChildNode);
-    
+    RtlpPromoteAvlTreeNode(ChildNode);
+
     /* Now the child has the opposite weight of the node */
     RtlSetBalance(ChildNode, -Balance);
-    
+
     /* This only happens on deletion, so we return TRUE to terminate the delete */
     return TRUE;
 }
 
-VOID
 FORCEINLINE
+VOID
 RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
                       IN PRTL_BALANCED_LINKS NewNode,
                       IN OUT PVOID NodeOrParent,
                       IN OUT TABLE_SEARCH_RESULT SearchResult)
 {
     CHAR Balance;
-    
+
     /* Initialize the new inserted element */
     MI_ASSERT(SearchResult != TableFoundNode);
     NewNode->LeftChild = NewNode->RightChild = NULL;
+    RtlSetBalance(NewNode, RtlBalancedAvlTree);
 
     /* Increase element count */
     Table->NumberGenericTableElements++;
@@ -225,8 +225,7 @@ RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
     {
         /* This is the new root node */
         RtlInsertAsRightChildAvl(&Table->BalancedRoot, NewNode);
-        MI_ASSERT(RtlBalance(NewNode) == RtlBalancedAvlTree);
-        
+
         /* On AVL trees, we also update the depth */
         ASSERT(Table->DepthOfTree == 0);
         Table->DepthOfTree = 1;
@@ -244,7 +243,6 @@ RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
     }
 
     /* Little cheat to save on loop processing, taken from Timo */
-    MI_ASSERT(RtlBalance(NewNode) == RtlBalancedAvlTree);
     RtlSetBalance(&Table->BalancedRoot, RtlLeftHeavyAvlTree);
 
     /*
@@ -257,13 +255,13 @@ RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
     {
         /* Calculate which side to balance on */
         Balance = RtlIsLeftChildAvl(NewNode) ? RtlLeftHeavyAvlTree : RtlRightHeavyAvlTree;
-        
+
         /* Check if the parent node was balanced */
         if (RtlBalance(NodeOrParent) == RtlBalancedAvlTree)
         {
             /* It's not balanced anymore (heavy on one side) */
             RtlSetBalance(NodeOrParent, Balance);
-            
+
             /* Move up */
             NewNode = NodeOrParent;
             NodeOrParent = RtlParentAvl(NodeOrParent);
@@ -272,14 +270,14 @@ RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
         {
             /* The parent's balance is opposite, so the tree is balanced now */
             RtlSetBalance(NodeOrParent, RtlBalancedAvlTree);
-            
+
             /* Check if this is the root (the cheat applied earlier gets us here) */
             if (RtlBalance(&Table->BalancedRoot) == RtlBalancedAvlTree)
             {
                 /* The depth has thus increased */
                 Table->DepthOfTree++;
             }
-            
+
             /* We reached the root or a balanced node, so we're done */
             break;
         }
@@ -292,18 +290,18 @@ RtlpInsertAvlTreeNode(IN PRTL_AVL_TABLE Table,
     }
 }
 
-VOID
 FORCEINLINE
+VOID
 RtlpDeleteAvlTreeNode(IN PRTL_AVL_TABLE Table,
                       IN PRTL_BALANCED_LINKS Node)
 {
     PRTL_BALANCED_LINKS DeleteNode = NULL, ParentNode;
     PRTL_BALANCED_LINKS *Node1, *Node2;
     CHAR Balance;
-    
+
     /* Take one of the children if possible */
     if (!(RtlLeftChildAvl(Node)) || !(RtlRightChildAvl(Node))) DeleteNode = Node;
-    
+
     /* Otherwise, check if one side is longer */
     if (!(DeleteNode) && (RtlBalance(Node) >= RtlBalancedAvlTree))
     {
@@ -315,34 +313,34 @@ RtlpDeleteAvlTreeNode(IN PRTL_AVL_TABLE Table,
     {
         /* Pick the predecessor which will be the longest side in this case */
         DeleteNode = RtlLeftChildAvl(Node);
-        while (RtlRightChildAvl(DeleteNode)) DeleteNode = RtlRightChildAvl(DeleteNode);        
+        while (RtlRightChildAvl(DeleteNode)) DeleteNode = RtlRightChildAvl(DeleteNode);
     }
-    
+
     /* Get the parent node */
     ParentNode = RtlParentAvl(DeleteNode);
     DPRINT("Parent: %p\n", ParentNode);
-    
+
     /* Pick which now to use based on whether or not we have a left child */
     Node1 = RtlLeftChildAvl(DeleteNode) ? &DeleteNode->LeftChild : &DeleteNode->RightChild;
     DPRINT("Node 1: %p %p\n", Node1, *Node1);
-            
+
     /* Pick which node to swap based on if we're already a left child or not */
     Node2 = RtlIsLeftChildAvl(DeleteNode) ? &ParentNode->LeftChild : &ParentNode->RightChild;
     DPRINT("Node 2: %p %p\n", Node2, *Node2);
-            
+
     /* Pick the correct balance depending on which side will get heavier */
     Balance = RtlIsLeftChildAvl(DeleteNode) ? RtlLeftHeavyAvlTree : RtlRightHeavyAvlTree;
     DPRINT("Balance: %lx\n", Balance);
-    
+
     /* Swap the children nodes, making one side heavier */
     *Node2 = *Node1;
-    
+
     /* If the node has a child now, update its parent */
     if (*Node1) RtlSetParent(*Node1, ParentNode);
 
     /* Assume balanced root for loop optimization */
     RtlSetBalance(&Table->BalancedRoot, RtlBalancedAvlTree);
-    
+
     /* Loop up the tree by parents */
     while (TRUE)
     {
@@ -373,7 +371,7 @@ RtlpDeleteAvlTreeNode(IN PRTL_AVL_TABLE Table,
         /* Choose which balance factor to use based on which side we're on */
         Balance = RtlIsRightChild(ParentNode) ?
                   RtlRightHeavyAvlTree : RtlLeftHeavyAvlTree;
-        
+
         /* Iterate up the tree */
         ParentNode = RtlParentAvl(ParentNode);
     }
@@ -383,7 +381,7 @@ RtlpDeleteAvlTreeNode(IN PRTL_AVL_TABLE Table,
 
     /* Copy the deleted node itself */
     RtlpCopyAvlNodeData(DeleteNode, Node);
-    
+
     /* Pick the right node to unlink */
     Node1 = RtlIsLeftChildAvl(Node) ?
             &(RtlParentAvl(DeleteNode))->LeftChild : &(RtlParentAvl(DeleteNode))->RightChild;