www.pudn.com > GOOGLE.rar > 2_3.pas, change:2002-01-14,size:1498b


procedure TaaBtreePage.AddKey( const aKey : string; 
	aRecLink : integer; aPageLink : integer;  
	aLeftPageLink : integer = -1 ); 
var 
	L, R, M : integer; 
	Compare : integer; 
begin 
	Assert(not IsFull,  
		'TaaBtreePage.AddKey : the page is full and shouldn''t be' ); 
	{simple case : the number of keys is zero} 
	if (FKeyCount = 0) then begin 
		FKeys.Add(aKey); 
		FRecLinks[0] := aRecLink; 
		FPageLinks[1] := aPageLink; 
		if (aLeftPAgeLink <> -1) then 
			FPAgeLinks[0] := aLeftPageLink; 
		FKeyCount := 1; 
	end 
	{otherwise, we need to binary search where to put it} 
	else begin 
		L := 0; 
		R := pred(FKeyCount); 
		while (L <= R) do begin 
			M := (L + R) div 2; 
			Compare := CompareStr(aKey , FKeys[M]); 
			if (Compare = 0 ) and (aRecLink <> -1) then 
				if (aRecLink = FRecLinks[M]) then 
					Compare := 0 
				else if (aRecLink < FRecLinks[M]) then 
					Compare := -1 
				else 
					Compare := 1; 
			if (Compare = 0) then 
				raise Exception.Create( 
					'TaaBtreePage.AddKey : key/record already exists' ); 
			if (Compare < 0) then 
				R := M - 1 
			else 
				L := m + 1; 
		end; 
		{we need to insert the key and its data at L} 
		FKeys.Insert(L , aKey); 
		Move(FRecLinks[L] , FRecLinks[L+1] , 
			(FKeyCount - L) * sizeof(longint)); 
		FRecLinks[L] := aRecLink; 
		Move(FPageLinks[L+1] , FPageLinks[L+2] , 
			(FKeyCount - L) * sizeof(longint)); 
		FPageLinks[L+1] := aPageLink; 
		inc(FKeyCount); 
	end; 
	{update the stream with the new page} 
	bpWrite; 
end;