www.pudn.com > GOOGLE.rar > 4.pas, change:2002-02-05,size:2621b


function TaaBtreePage.Fill( aParent : TaaBtreePage; var 
	aMidKey : string; var aMidLink : integer ) : TaaBtreePage; 
var 
	i	   : integer; 
	ParentInx  : integer; 
	SibLink	   : integer; 
	IsRightSib : boolean; 
	Sibling    : TaaBtreePage; 
begin 
	{find our sibling} 
	SibLink := -1; 
	for i := 0 to pred( aParent.FKeyCount ) do 
		if ( aParent.FPageLinks[i] = PageNumber ) then begin 
			ParentInx := i; 
			SibLink := aParent.FPageLinks[i + 1]; 
			IsRightSib := true; 
			Break; 
		end; 
	if( SibLink = -1 ) then begin 
		ParentInx := pred( aParent.FKeyCount ); 
		SibLink := aParent.FPageLinks[ParentInx]; 
		IsRightSib := false; 
	end; 
	{get the sibling} 
	Sibling := TaaBtreePage.Ctreate( FRecStrm , FKeyLen , 
		SibLink ); 
	{if the sibling is half full , merge} 
	if Sibling.IsHalfFull then begin 
		aMidKey := aParent.FKeys[ParentInx]; 
		aMidLink := aParent.FRecLinks[ParentInx]; 
		if IsRightSib then begin 
			Result := Self; 
			Merge( Sibling , aMidKey , aMidLink ); 
			aParent.FPageLinks[ParentInx + 1] := Self.PageNumber; 
			Sibling.Recycle; 
			Sibling.Free; 
		end 
	else begin 
		Result := Sibling; 
		Sibling.Merge( Self , aMidKey , aMidLink ); 
		aParent.FPageLinks[ ParentInx + 1] := Sibling.PageNumber; 
		Recycle; 
		Free; 
		end 
	end 
	{otherwise rotate a key from the sibling, through the parent,  
		into ourselves} 
	else begin 
		if IsRightSib then begin 
			FKeys.Add(aParent.FKeys[ParentInx]); 
			FRecLinks[FKeyCount] := aParent.FRecLinks[ParentInx]; 
			FPageLinks[FKeyCount + 1] := Sibling.FPageLinks[0]; 
			inc(FKeyCount); 
			aParent.FKeys[ParentInx] := Sibling.FKeys[0]; 
			Sibling.FKeys(0); 
			aParent.FRecLinks[ParentInx] := Sibling.FRecLinks[0]; 
			dec(Sibling.FKeyCount); 
			Move(Sibling.FRecLinks[1] , Sibling.FRecLinks[0] , 
				Sibling.FKeyCout * sizeof(integer)); 
			Move(Sibling.FPageLinks[1] , Sibling.FRecLinks[0] , 
				((Sibling.FKeyCount + 1) * sizeof(integer))); 
		end 
		else begin 
			FKeys.Insert( 0 , aParent.FKeys[ParentInx] ); 
			Move( FRecLinks[0] , FrecLinks[1] , 
				(FKeyCount * sizeof(integer))); 
			FRecLinks[0] := aParent.FRecLinks[ParentInx]; 
			Move( FPageLinks[0] , FPageLinks[1] , 
				((FKeuCount + 1) * sizeof(integer))); 
			FPageLinks[0] := Sibling.FPageLinks[Sibling.KeyCount]; 
			inc(FKeyCount); 
			dec(Sibling.FKeyCount); 
			aParent.FKeys[ParentInx] := 
				Sibling.FKeys[Sibling.FKeyCount]; 
			Sibling.FKeys.Delete(Sibling.FKeyCount); 
			aPArent.FRecLinks[ParentInx] := 
				Sibling.FRecLinks[Sibling.FKeyCount]; 
		end; 
		bpWrite; 
		aParent.bpWrite; 
		Sibling.bpWrite; 
		sibling.Free; 
		Result := nil; 
	end; 
end;