From: <pau...@us...> - 2016-04-27 14:41:05
|
Revision: 643 http://sourceforge.net/p/tdbf/code/643 Author: paulenandrew Date: 2016-04-27 14:41:03 +0000 (Wed, 27 Apr 2016) Log Message: ----------- Move TIndexFile.Merge*() functions to private visiblity Modified Paths: -------------- trunk/src/dbf_idxfile.pas Modified: trunk/src/dbf_idxfile.pas =================================================================== --- trunk/src/dbf_idxfile.pas 2016-04-27 14:37:25 UTC (rev 642) +++ trunk/src/dbf_idxfile.pas 2016-04-27 14:41:03 UTC (rev 643) @@ -320,6 +320,10 @@ procedure ResetRange; procedure SetBracketLow; procedure SetBracketHigh; + procedure MergeSort(List: PDbfPointerList; L, R: Integer); + procedure MergeSort2(List, TempList: PDbfPointerList; L, R: Integer); + procedure MergeSort3(List, TempList: PDbfPointerList; L0, L1, R0, R1: Integer); + function MergeSortCompare(Item1, Item2: Pointer): Integer; procedure WalkFirst; procedure WalkLast; @@ -380,10 +384,6 @@ procedure CompactFile; procedure BulkLoadIndex; procedure BulkLoadIndexes; - procedure MergeSort(List: PDbfPointerList; L, R: Integer); - procedure MergeSort2(List, TempList: PDbfPointerList; L, R: Integer); - procedure MergeSort3(List, TempList: PDbfPointerList; L0, L1, R0, R1: Integer); - function MergeSortCompare(Item1, Item2: Pointer): Integer; procedure PrepareRename(NewFileName: string); procedure CalcRegenerateIndexes; @@ -3050,82 +3050,6 @@ BulkLoadIndex; end; -procedure TIndexFile.MergeSort(List: PDbfPointerList; L, R: Integer); -var - TempList: PDbfPointerList; - Size: Integer; -begin - if L < R then - begin - Size:= Succ(R-L) * SizeOf(Pointer); - GetMem(TempList, Size); - try - MergeSort2(List, TempList, L, R); - Move(TempList^, List^, Size); - finally - FreeMem(TempList); - end; - end; -end; - -procedure TIndexFile.MergeSort2(List, TempList: PDbfPointerList; L, R: Integer); -var - C: Integer; - M: Integer; - L1: Integer; - R0: Integer; -begin - if L < R then - begin - C:= Succ(R - L); - M:= L + Pred(C div 2); - L1:= M; - R0:= Succ(M); - MergeSort2(List, TempList, L, L1); - MergeSort2(List, TempList, R0, R); - MergeSort3(List, TempList, L, L1, R0, R); - Move(TempList^[L], List^[L], C * SizeOf(Pointer)); - end; -end; - -procedure TIndexFile.MergeSort3(List, TempList: PDbfPointerList; L0, L1, R0, R1: Integer); -var - I: Integer; - - procedure MergeAppend(var J: Integer); - begin - Inc(FProgressPosition); - DoProgress(FProgressPosition, FProgressMax, STRING_PROGRESS_SORTING_RECORDS); - TempList^[I] := List^[J]; - Inc(I); - Inc(J); - end; - -begin - I := L0; - while (L0 <= L1) and (R0 <= R1) do - begin - if MergeSortCompare(List^[L0], List^[R0]) <= 0 then - MergeAppend(L0) - else - MergeAppend(R0); - end; - while L0 <= L1 do - MergeAppend(L0); - while R0 <= R1 do - MergeAppend(R0); -end; - -function TIndexFile.MergeSortCompare(Item1, Item2: Pointer): Integer; -var - KeyData1: PAnsiChar; - KeyData2: PAnsiChar; -begin - KeyData1 := @PMdxEntry(Item1).KeyData; - KeyData2 := @PMdxEntry(Item2).KeyData; - Result:= CompareKeys(KeyData1, KeyData2); -end; - procedure TIndexFile.PrepareRename(NewFileName: string); begin if FIndexVersion >= xBaseIV then @@ -4255,6 +4179,82 @@ until TempPage = nil; end; +procedure TIndexFile.MergeSort(List: PDbfPointerList; L, R: Integer); +var + TempList: PDbfPointerList; + Size: Integer; +begin + if L < R then + begin + Size:= Succ(R-L) * SizeOf(Pointer); + GetMem(TempList, Size); + try + MergeSort2(List, TempList, L, R); + Move(TempList^, List^, Size); + finally + FreeMem(TempList); + end; + end; +end; + +procedure TIndexFile.MergeSort2(List, TempList: PDbfPointerList; L, R: Integer); +var + C: Integer; + M: Integer; + L1: Integer; + R0: Integer; +begin + if L < R then + begin + C:= Succ(R - L); + M:= L + Pred(C div 2); + L1:= M; + R0:= Succ(M); + MergeSort2(List, TempList, L, L1); + MergeSort2(List, TempList, R0, R); + MergeSort3(List, TempList, L, L1, R0, R); + Move(TempList^[L], List^[L], C * SizeOf(Pointer)); + end; +end; + +procedure TIndexFile.MergeSort3(List, TempList: PDbfPointerList; L0, L1, R0, R1: Integer); +var + I: Integer; + + procedure MergeAppend(var J: Integer); + begin + Inc(FProgressPosition); + DoProgress(FProgressPosition, FProgressMax, STRING_PROGRESS_SORTING_RECORDS); + TempList^[I] := List^[J]; + Inc(I); + Inc(J); + end; + +begin + I := L0; + while (L0 <= L1) and (R0 <= R1) do + begin + if MergeSortCompare(List^[L0], List^[R0]) <= 0 then + MergeAppend(L0) + else + MergeAppend(R0); + end; + while L0 <= L1 do + MergeAppend(L0); + while R0 <= R1 do + MergeAppend(R0); +end; + +function TIndexFile.MergeSortCompare(Item1, Item2: Pointer): Integer; +var + KeyData1: PAnsiChar; + KeyData2: PAnsiChar; +begin + KeyData1 := @PMdxEntry(Item1).KeyData; + KeyData2 := @PMdxEntry(Item2).KeyData; + Result:= CompareKeys(KeyData1, KeyData2); +end; + procedure TIndexFile.CancelRange; begin FRangeActive := false; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |