This article has been dead for over three months
You
unit BinarySort; // Quite helpful and lightning fast creating of sorted indexes of your data.
interface
TYPE
DynamicIntegerArray = ARRAY OF INTEGER;
DynamicStringArray = ARRAY OF STRING;
PROCEDURE DynamicCreateIndex(VAR ArrParam,ArrParamIndex: DynamicIntegerArray); OVERLOAD;
PROCEDURE DynamicCreateIndex(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray); OVERLOAD;
PROCEDURE DynamicExtendAndStore(VAR ArrParam,ArrParamIndex: DynamicIntegerArray; CONST NewValue:INTEGER); OVERLOAD;
PROCEDURE DynamicExtendAndStore(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray; CONST NewValue:STRING); OVERLOAD;
implementation
VAR
Lo, Mid, Hi : INTEGER; // Local variables for this unit only.
FUNCTION FindMid:INTEGER; {Indented routines are not interfaced. ie: Only a local procedure}
BEGIN
FindMid:=Lo+((Hi-Lo) DIV 2);
END;
PROCEDURE BinarySortIndex(VAR ArrParam,ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
VAR
x : INTEGER;
TempVar : INTEGER;
BEGIN
Lo:=0; Hi:=HighBound; Mid:=FindMid;
TempVar := ArrParam[HighBound];
REPEAT
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
Mid:=FindMid;
UNTIL (Mid=Lo) OR (Mid=Hi);
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
END;
PROCEDURE BinarySortIndex(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray; CONST HighBound:INTEGER); OVERLOAD;
VAR
x : INTEGER;
TempVar : STRING;
BEGIN
Lo:=0; Hi:=HighBound; Mid:=FindMid;
TempVar := ArrParam[HighBound];
REPEAT
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN Lo:=Mid ELSE Hi:=Mid;
Mid:=FindMid;
UNTIL (Mid=Lo) OR (Mid=Hi);
IF TempVar>ArrParam[ArrParamIndex[Mid]] THEN INC(Mid);// We always need a last check just in case.
FOR x:=HighBound-1 DOWNTO Mid DO ArrParamIndex[x+1] := ArrParamIndex[x];// Shift the index.
ArrParamIndex[Mid]:=HighBound;// Store the pointer to index at its sorted place
END;
PROCEDURE DynamicCreateIndex(VAR ArrParam,ArrParamIndex: DynamicIntegerArray);
VAR
x,TempLo,TempHi : INTEGER;
BEGIN
Templo:=0; TempHi:=HIGH(ArrParamIndex);
FOR x:=TempLo TO TempHi DO BinarySortIndex(ArrParam,ArrParamIndex,x);
END;
PROCEDURE DynamicCreateIndex(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray);
VAR
x,TempLo,TempHi : INTEGER;
BEGIN
Templo:=0; TempHi:=HIGH(ArrParamIndex);
FOR x:=TempLo TO TempHi DO BinarySortIndex(ArrParam,ArrParamIndex,x);
END;
PROCEDURE DynamicExtendAndStore(VAR ArrParam,ArrParamIndex: DynamicIntegerArray; CONST NewValue:INTEGER);
VAR ArrSize:INTEGER;
BEGIN
ArrSize:=HIGH(ArrParam)+1;
SetLength(ArrParam,ArrSize+1);
SetLength(ArrParamIndex,ArrSize+1);
ArrParam[ArrSize]:=NewValue;
BinarySortIndex(ArrParam,ArrParamIndex,ArrSize);
END;
PROCEDURE DynamicExtendAndStore(VAR ArrParam:DynamicStringArray; VAR ArrParamIndex: DynamicIntegerArray; CONST NewValue:STRING);
VAR ArrSize:INTEGER;
BEGIN
ArrSize:=HIGH(ArrParam)+1;
SetLength(ArrParam,ArrSize+1);
SetLength(ArrParamIndex,ArrSize+1);
ArrParam[ArrSize]:=NewValue;
BinarySortIndex(ArrParam,ArrParamIndex,ArrSize);
END;
end.