@@ -122,7 +122,7 @@ LayoutBam layoutBtree() //
122122 array ("stealFromRight" ); // Whether the steal from the right was successful
123123 array ("stealFromRight_parent" ); // The parent of the branch which wants to steal from the right child
124124 array ("stealFromRight_index" ); // The index of the child that wants to steal from the right sibling in its parent
125- array ("stealFromRight_parentBranchSize " ); // Parent branch size
125+ array ("stealFromRight_nP " ); // Parent branch size
126126 array ("stealFromRight_right" ); // Index of sibling on right
127127 array ("stealFromRight_l" ); // Left child index
128128 array ("stealFromRight_lk" ); // Left child key
@@ -133,6 +133,13 @@ LayoutBam layoutBtree() //
133133 array ("stealFromRight_td" ); //
134134 array ("stealFromRight_bd" ); //
135135
136+ array ("mergeRoot_nP" ); // Number in root
137+ array ("mergeRoot_l" ); // Left child of root
138+ array ("mergeRoot_r" ); // Right child of node
139+ array ("mergeRoot_nl" ); // Number in left child
140+ array ("mergeRoot_nr" ); // Number in right child
141+ array ("mergeRoot_pkn" ); //
142+
136143 array ("mergeLeftSibling" ); // Whether the merge with the left sibling was successful
137144 array ("mergeLeftSibling_parent" ); // The parent of the branch which wants to merge with its left sibling
138145 array ("mergeLeftSibling_index" ); // The index of the child that wants to steal from the child in its parent
@@ -187,6 +194,15 @@ void allocate() //
187194 L .zero ("keys" , a ); L .zero ("data" , a );
188195 }
189196
197+ void free (String f ) // Free a new node to make it available for reuse
198+ {final int n = L .get (f );
199+ if (n == 0 ) stop ("Cannot free root" ); // The root is never freed
200+ L .ones ("free" , f ); L .ones ("isLeaf" , f ); L .ones ("current_size" , f ); // Invalidate all control information
201+ L .ones ("keys" , f ); L .ones ("data" , f );
202+ L .move ("free" , "freeChainHead" , f ); // Chain this node in front of the last freed node
203+ L .move ("freeChainHead" , f ); // Freed node becomes head of the free chain
204+ }
205+
190206 void free () // Free a new node to make it available for reuse
191207 {final String f = "free_1" ; // Index of node to be freed
192208 final int n = L .get (f );
@@ -438,7 +454,7 @@ void stealFromRight() //
438454 final String $P = "stealFromRight_parent" ; // Parent node
439455 final String $index = "stealFromRight_index" ; // Index of child stealing from the right sibling
440456 final String $parent = "stealFromRight_parent" ; // The parent of the branch which wants to steal from the right child
441- final String $nP = "stealFromRight_parentBranchSize" ; // Parent branch size
457+ final String $nP = "stealFromRight_nP" ; // Parent branch size
442458 final String $right = "stealFromRight_right" ; // Index of sibling on right
443459 final String $l = "stealFromRight_l" ; // Left child index
444460 final String $lk = "stealFromRight_lk" ; // Left child key
@@ -488,58 +504,64 @@ void stealFromRight() //
488504//D2 Merge // Merge two nodes together and free the resulting free node
489505
490506 void mergeRoot () // Merge into the root
491- {rootIsLeaf ();
507+ {final String $nP = "mergeRoot_nP" ;
508+ final String $l = "mergeRoot_l" ;
509+ final String $r = "mergeRoot_r" ;
510+ final String $nl = "mergeRoot_nl" ;
511+ final String $nr = "mergeRoot_nr" ;
512+ final String $pkn = "mergeRoot_pkn" ;
513+
514+ rootIsLeaf ();
492515 if (L .get ("rootIsLeaf" ) > 0 ) return ;
493- L .set (0 , "branchSize_1" ); branchSize (); final int nP = L . get ( "branchSize" );
494- if (nP > 1 ) return ;
516+ L .set (0 , "branchSize_1" ); branchSize (); L . move ( $nP , "branchSize" );
517+ if (L . get ( $nP ) > 1 ) return ;
495518
496- int P = 0 ;
497- setStuck (P );
498- stuck_firstElement (); int l = getData ();
499- stuck_lastElement (); int r = getData ();
519+ setStuck (0 );
520+ stuck_firstElement (); getData ($l );
521+ stuck_lastElement (); getData ($r );
500522
501- L .set ( P , "hasLeavesForChildren_1 " ); // Children are leaves
523+ L .move ( "hasLeavesForChildren_1" , "root " );
502524 hasLeavesForChildren ();
503525 if (L .get ("hasLeavesForChildren" ) > 0 ) // Children are leaves
504- {L .set ( l , "leafSize_1" ); leafSize (); final int nl = L . get ( "leafSize" );
505- L .set ( r , "leafSize_1" ); leafSize (); final int nr = L . get ( "leafSize" );
526+ {L .move ( "leafSize_1" , $l ); leafSize (); L . move ( $nl , "leafSize" );
527+ L .move ( "leafSize_1" , $r ); leafSize (); L . move ( $nr , "leafSize" );
506528
507- if (nl + nr <= maxKeysPerLeaf )
508- {setStuck (P ); stuck_clear ();
509- for (int i = 0 ; i < nl ; ++i )
510- {setStuck (l ); stuck_shift ();
511- setStuck (P ); stuck_push ();
529+ if (L . get ( $nl ) + L . get ( $nr ) <= maxKeysPerLeaf )
530+ {setStuck ("root" ); stuck_clear ();
531+ for (int i = 0 ; i < L . get ( $nl ) ; ++i )
532+ {setStuck ($ l ); stuck_shift ();
533+ setStuck ("root" ); stuck_push ();
512534 }
513- for (int i = 0 ; i < nr ; ++i )
514- {setStuck (r ); stuck_shift ();
515- setStuck (P ); stuck_push ();
535+ for (int i = 0 ; i < L . get ( $nr ) ; ++i )
536+ {setStuck ($ r ); stuck_shift ();
537+ setStuck ("root" ); stuck_push ();
516538 }
517- L .set ( P , "setLeaf " ); setLeaf ();
518- L . set ( l , "free_1" ); free ();
519- L . set ( r , "free_1" ); free ();
539+ L .move ( "setLeaf" , "root " ); setLeaf ();
540+ free ($l );
541+ free ($r );
520542 }
521543 }
522544 else
523- {L .set ( l , "branchSize_1" ); branchSize (); final int nl = L . get ( "branchSize" );
524- L .set ( r , "branchSize_1" ); branchSize (); final int nr = L . get ( "branchSize" );
525- if (nl + 1 + nr > maxKeysPerBranch ) return ;
526- setStuck (P ); stuck_firstElement (); final int pkn = getKey ();
545+ {L .move ( "branchSize_1" , $l ); branchSize (); L . move ( $nl , "branchSize" );
546+ L .move ( "branchSize_1" , $r ); branchSize (); L . move ( $nr , "branchSize" );
547+ if (L . get ( $nl ) + 1 + L . get ( $nr ) > maxKeysPerBranch ) return ;
548+ setStuck ("root" ); stuck_firstElement (); getKey ($pkn );
527549 stuck_clear ();
528- for (int i = 0 ; i < nl ; ++i )
529- {setStuck (l ); stuck_shift ();
530- setStuck (P ); stuck_push ();
550+ for (int i = 0 ; i < L . get ( $nl ) ; ++i )
551+ {setStuck ($ l ); stuck_shift ();
552+ setStuck ("root" ); stuck_push ();
531553 }
532- setStuck (l ); stuck_lastElement ();
533- setStuck (P ); setKey (pkn ); stuck_push ();
554+ setStuck ($ l ); stuck_lastElement ();
555+ setStuck ("root" ); setKey ($ pkn ); stuck_push ();
534556
535- for (int i = 0 ; i < nr ; ++i )
536- {setStuck (r ); stuck_shift ();
537- setStuck (P ); stuck_push ();
557+ for (int i = 0 ; i < L . get ( $nr ) ; ++i )
558+ {setStuck ($ r ); stuck_shift ();
559+ setStuck ("root" ); stuck_push ();
538560 }
539- setStuck (r ); stuck_lastElement (); // Top next
540- setStuck (P ); setKey (0 ); stuck_push (); // Top so ignored by search ... except last
541- L . set ( l , "free_1" ); free ();
542- L . set ( r , "free_1" ); free ();
561+ setStuck ($ r ); stuck_lastElement (); // Top next
562+ setStuck ("root" ); setKey ("root" ); stuck_push (); // Top so ignored by search ... except last
563+ free ($l );
564+ free ($r );
543565 }
544566 }
545567
0 commit comments