-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmwFixedRecSort.pas
857 lines (781 loc) · 20.8 KB
/
mwFixedRecSort.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
{+--------------------------------------------------------------------------+
| Unit: mwFixedRecSort
| Created: 11.97
| Author: Martin Waldenburg
| Copyright 1997, all rights reserved.
| Description: A buffered sorter for an unlimmited amount of records with a fixed
| length using a three-way merge for memory and a buffered
| three-way merge for files.
| Version: 1.2
| Status FreeWare
| It's provided as is, without a warranty of any kind.
| You use it at your own risc.
| E-Mail me at [email protected]}
{--------------------------------------------------------------------}
{ Martin Waldenburg
Landaeckerstrasse 27
71642 Ludwigsburg
Germany
Share your Code
+--------------------------------------------------------------------------+}
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
unit mwFixedRecSort;
{$R-}
interface
uses
{$IFNDEF FPC}
Windows,
{$ENDIF}
SysUtils, Classes;
type
{** DObject are TVarRecs, and can store any kind of atomic value. }
DObject = TVarRec;
{** A pointer to an individual DObject. }
PDObject = ^DObject;
{** A closure that can compare two objects and returns less than zero if
obj1 is less than obj2, 0 if obj1 equals obj2, and greater than zero if
obj1 is greater than obj2;
@param obj1 The first object (left hand side).
@param obj2 The second object (right hand side).}
DComparator = reference to function (const obj1, obj2 : DObject) : Integer;
PMergeArray = ^TMergeArray;
TMergeArray = array of PDObject;
{ TSub3Array defines the boundaries of a SubArray and determines if
the SubArray is full or not.
The MergeSort Algorithm is easier readable with this class.}
TSub3Array = class(TObject)
private
FMax: LongInt;
public
FLeft: LongInt; { - Initialized to 0. }
FRight: LongInt; { - Initialized to 0. }
Full: Boolean;
constructor Create(MaxValue: LongInt);
destructor Destroy; override;
procedure Init(LeftEnd, RightEnd: LongInt);
procedure Next;
end; { TSub3Array }
{ TM3Array class }
TM3Array = class(TObject)
private
FLeftArray, FMidArray, FRightArray: TSub3Array;
FM3Array, TempArray, SwapArray: TMergeArray;
FCount: Integer;
fCapacity:Integer;
procedure SetCapacity(NewCapacity:Integer);
procedure Expand;
protected
function Get(Index: Integer): PDObject;
procedure Put(Index: Integer; Item: PDObject);
procedure Merge(SorCompare: DComparator);
public
destructor Destroy; override;
function Add(Item: PDObject): Integer;
procedure Clear;
function Last: PDObject;
procedure MergeSort(SorCompare: DComparator);
procedure QuickSort(SorCompare: DComparator);
property Count: Integer read FCount write FCount;
property Items[Index: Integer]: PDObject read Get write Put; default;
property M3Array: TMergeArray read FM3Array;
property Capacity:Integer read fCapacity write SetCapacity;
end; { TM3Array }
TmIOBuffer = class(TObject)
private
fBuffFile: File;
fFileName: String;
fFilledSize:Longint;
fBufferSize: LongInt;
fBufferPos: LongInt;
fBuffer: Pointer;
fNeedFill: Boolean;
fEof:Boolean;
fFileEof: Boolean;
FRecCount: Cardinal;
fSize:Longint;
fDataLen:Longint;
procedure AllocBuffer;
public
constructor Create(const FileName: String; DataLen, BuffSize: Integer);
destructor Destroy; override;
procedure FillBuffer;
function ReadData:Pointer;
procedure WriteData(Var NewData);
procedure FlushBuffer;
procedure CloseBuffFile;
procedure DeleteBuffFile;
property Eof:Boolean read fEof;
property RecCount: Cardinal read FRecCount;
property Size:Longint read fSize;
property DataLen:Longint read fDataLen;
end; { TmIOBuffer }
TTempFile = class(TObject)
private
fFileName: String;
Reader: TmIOBuffer;
fFull:Boolean;
protected
public
FLeft: PDObject;
constructor Create;
destructor Destroy; override;
procedure Next;
procedure Init(const FileName: String);
property Full:Boolean read fFull;
end; { TTempFile }
TMergeFile = class(TObject)
private
FFileOne, FFileTwo, FFileThree: TTempFile;
Writer: TmIOBuffer;
fInList, fOutList, TempList: TStringList;
fFileName: String;
public
constructor Create(InList: TStringList);
destructor Destroy; override;
procedure FileMerge(MergeCompare: DComparator);
procedure MergeSort(MergeCompare: DComparator);
property FileName: String read fFileName;
end; { TMergeFile }
TFixRecSort = class(TObject)
private
Reader, Writer: TmIOBuffer;
FMaxLines: LongInt;
fMerArray: TM3Array;
MergeFile: TMergeFile;
fFileName: String;
fTempFileList: TStringList;
fCompare: DComparator;
fMaxMem:LongInt;
fUseMergesort:Boolean;
function GetMaxMem:LongInt;
procedure SetMaxMem(value:LongInt);
public
constructor Create(RecLen: LongInt);
destructor Destroy; override;
procedure Start(Compare: DComparator);
procedure Init(const FileName: String);
property MaxLines: LongInt read FMaxLines write FMaxLines default 60000;
property MaxMem:LongInt read GetMaxMem write SetMaxMem;
property UseMergesort:Boolean read fUseMergesort write fUseMergesort;
end; { TFixRecSort }
var
FRecLen, fBuffersSize: Integer;
implementation
uses
System.IOUtils;
constructor TSub3Array.Create(MaxValue: LongInt);
begin
inherited Create;
FLeft := 0;
FRight := 0;
Full := False;
FMax := MaxValue;
end; { Create }
procedure TSub3Array.Init(LeftEnd, RightEnd: LongInt); { public }
begin
FLeft:= LeftEnd;
FRight:= RightEnd;
if FLeft > FMax then Full:= False else Full:= True;
end; { Init }
procedure TSub3Array.Next;
begin
inc(FLeft);
if (FLeft > FRight) or (FLeft > FMax) then Full:= False;
end; { Next }
destructor TSub3Array.Destroy;
begin
inherited Destroy;
end; { Destroy }
{ TM3Array }
destructor TM3Array.Destroy;
begin
Clear;
inherited Destroy;
end;
function TM3Array.Add(Item: PDObject): Integer;
begin
Result := FCount;
if Result = FCapacity then Expand;
FM3Array[Result] := Item;
Inc(FCount);
end;
procedure TM3Array.Expand;
begin
SetCapacity(FCapacity + 8192);
end;
procedure TM3Array.SetCapacity(NewCapacity:Integer);
begin
FCapacity:= NewCapacity;
SetLength(FM3Array, FCapacity * 4);
end;
procedure TM3Array.Clear;
begin
FCount:= 0;
SetLength(TempArray, 0);
SetLength(FM3Array, 0);
FCapacity:= 0;
end;
function TM3Array.Get(Index: Integer): PDObject;
begin
Result := FM3Array[Index];
end;
function TM3Array.Last: PDObject;
begin
Result := Get(FCount - 1);
end;
procedure TM3Array.Put(Index: Integer; Item: PDObject);
begin
FM3Array[Index] := Item;
end;
{ Based on a non-recursive QuickSort from the SWAG-Archive.
( TV Sorting Unit by Brad Williams ) }
procedure TM3Array.QuickSort(SorCompare: DComparator);
var Left, Right, SubArray, SubLeft, SubRight:LongInt;
Temp, Pivot: PDObject;
Stack : array[1..128] of record First, Last : LongInt; end;
begin
SubArray := 1;
Stack[SubArray].First := 0;
Stack[SubArray].Last := Count - 1;
repeat
Left := Stack[SubArray].First;
Right := Stack[SubArray].Last;
Dec(SubArray);
repeat
SubLeft := Left;
SubRight := Right;
Pivot := FM3Array[(Left + Right) shr 1];
repeat
while SorCompare(FM3Array[SubLeft]^, Pivot^) < 0 do Inc(SubLeft);
while SorCompare(FM3Array[SubRight]^, Pivot^) > 0 do Dec(SubRight);
IF SubLeft <= SubRight then
begin
Temp := FM3Array[SubLeft];
FM3Array[SubLeft] := FM3Array[SubRight];
FM3Array[SubRight] := Temp;
Inc(SubLeft);
Dec(SubRight);
end;
until SubLeft > SubRight;
IF SubLeft < Right then
begin
Inc(SubArray);
Stack[SubArray].First := SubLeft;
Stack[SubArray].Last := Right;
end;
Right := SubRight;
until Left >= Right;
until SubArray = 0;
end; { QuickSort }
{This is a three way merge routine.
Unfortunately the " Merge " routine needs additional memory}
procedure TM3Array.Merge(SorCompare: DComparator);
var
TempPos : integer;
begin
TempPos := FLeftArray.FLeft;
while ( FLeftArray.Full ) and ( FMidArray.Full ) and ( FRightArray.Full ) do {Main Loop}
begin
if SorCompare(FM3Array[FLeftArray.FLeft]^, FM3Array[FMidArray.FLeft]^) <= 0 then
begin
if SorCompare(FM3Array[FLeftArray.FLeft]^, FM3Array[FRightArray.FLeft]^) <= 0 then
begin
TempArray[ TempPos ] := FM3Array[ FLeftArray.FLeft ];
FLeftArray.Next;
end
else
begin
TempArray[ TempPos ] := FM3Array[ FRightArray.FLeft ];
FRightArray.Next;
end;
end
else
begin
if SorCompare(FM3Array[FMidArray.FLeft]^, FM3Array[FRightArray.FLeft]^) <= 0 then
begin
TempArray[ TempPos ] := FM3Array[ FMidArray.FLeft ];
FMidArray.Next;
end
else
begin
TempArray[ TempPos ] := FM3Array[ FRightArray.FLeft ];
FRightArray.Next;
end;
end;
inc(TempPos);
end;
while ( FLeftArray.Full ) and ( FMidArray.Full ) do
begin
if SorCompare(FM3Array[FLeftArray.FLeft]^, FM3Array[FMidArray.FLeft]^) <= 0 then
begin
TempArray[ TempPos ] := FM3Array[ FLeftArray.FLeft ];
FLeftArray.Next;
end
else
begin
TempArray[ TempPos ] := FM3Array[ FMidArray.FLeft ];
FMidArray.Next;
end;
inc(TempPos);
end;
while ( FMidArray.Full ) and ( FRightArray.Full ) do
begin
if SorCompare(FM3Array[FMidArray.FLeft]^, FM3Array[FRightArray.FLeft]^) <= 0 then
begin
TempArray[ TempPos ] := FM3Array[ FMidArray.FLeft ];
FMidArray.Next;
end
else
begin
TempArray[ TempPos ] := FM3Array[ FRightArray.FLeft ];
FRightArray.Next;
end;
inc(TempPos);
end;
while ( FLeftArray.Full ) and ( FRightArray.Full ) do
begin
if SorCompare(FM3Array[FLeftArray.FLeft]^, FM3Array[FRightArray.FLeft]^) <= 0 then
begin
TempArray[ TempPos ] := FM3Array[ FLeftArray.FLeft ];
FLeftArray.Next;
end
else
begin
TempArray[ TempPos ] := FM3Array[ FRightArray.FLeft ];
FRightArray.Next;
end;
inc(TempPos);
end;
while FLeftArray.Full do { Copy Rest of First Sub3Array }
begin
TempArray[ TempPos ] := FM3Array[ FLeftArray.FLeft ];
inc(TempPos); FLeftArray.Next;
end;
while FMidArray.Full do { Copy Rest of Second Sub3Array }
begin
TempArray[ TempPos ] := FM3Array[ FMidArray.FLeft ];
inc(TempPos); FMidArray.Next;
end;
while FRightArray.Full do { Copy Rest of Third Sub3Array }
begin
TempArray[ TempPos ] := FM3Array[ FRightArray.FLeft ];
inc(TempPos); FRightArray.Next;
end;
end; { Merge }
{Non-recursive Mergesort.
Very fast, if enough memory available.
The number of comparisions used is nearly optimal, about 3/4 of QuickSort.
If comparision plays a very more important role than exchangement,
it outperforms QuickSort in any case.
( Large keys in pointer arrays, for example text with few short lines. )
From all Algoritms with O(N lg N) it's the only stable, meaning it lefts
equal keys in the order of input. This may be important in some cases. }
procedure TM3Array.MergeSort(SorCompare: DComparator);
var
a, b, c, N, todo: LongInt;
begin
SetLength(TempArray, FCount * 4);
FLeftArray:= TSub3Array.Create(FCount -1);
FMidArray:= TSub3Array.Create(FCount -1);
FRightArray:= TSub3Array.Create(FCount -1);
N:= 1;
repeat
todo:= 0;
repeat
a:= todo;
b:= a +N;
c:= b +N;
todo:= C +N;
FLeftArray.Init(a, b -1);
FMidArray.Init(b, c -1);
FRightArray.Init(c, todo -1);
Merge(SorCompare);
until todo >= Fcount;
SwapArray:= FM3Array; {Alternating use of the arrays.}
FM3Array:= TempArray;
TempArray:= SwapArray;
N:= N+ N +N;
until N >= Fcount;
FLeftArray.Free;
FMidArray.Free;
FRightArray.Free;
SetLength(TempArray, 0);
end; { MergeSort }
constructor TmIOBuffer.Create(const FileName: String; DataLen, BuffSize: Integer);
var
fHandle: Integer;
begin
inherited create;
FDataLen:= DataLen;
fFileName:= FileName;
if not FileExists(FileName) then
begin
fHandle:= FileCreate(FileName);
FileClose(fHandle);
end;
fBufferSize:= BuffSize;
FRecCount:= BuffSize Div DataLen;
fBufferSize:= DWORD(DataLen) * FRecCount;
AssignFile(fBuffFile, FileName);
Reset(fBuffFile, 1);
fSize:= FileSize(fBuffFile);
fNeedFill:= True;
fEof:= False;
fFileEof:= False;
AllocBuffer;
fBufferPos:= 0;
end; { create }
destructor TmIOBuffer.destroy;
begin
ReallocMem(fBuffer, 0);
CloseBuffFile;
inherited destroy;
end; { destroy }
procedure TmIOBuffer.AllocBuffer;
begin
fFilledSize:= fBufferSize;
ReallocMem(fBuffer, fBufferSize);
end; { SetBufferSize }
procedure TmIOBuffer.FillBuffer;
var
Readed: LongInt;
begin
Readed := 0;
BlockRead(fBuffFile, fBuffer^, fBufferSize, Readed);
if FilePos(FBuffFile) = FSize then fFileEof:= True;
fBufferPos:= 0;
fFilledSize:= Readed;
fNeedFill:= False;
end; { FillBuffer }
function TmIOBuffer.ReadData:Pointer;
begin
fEof:= False;
if fNeedFill then FillBuffer;
{$IFDEF FPC}
Result:= Pointer(fBuffer + fBufferPos);
{$ELSE}
Result:= Pointer(Integer(fBuffer) + fBufferPos);
{$ENDIF}
inc(fBufferPos, fDataLen);
if fBufferPos >= fFilledSize then
begin
fNeedFill:= True;
if FFileEof then FEof:= True;
end;
end; { ReadData }
procedure TmIOBuffer.WriteData(Var NewData);
var
Pos: LongInt;
begin
if (fBufferPos >= 0) and (Pointer(NewData) <> nil) then
begin
Pos := fBufferPos + fDataLen;
if Pos > 0 then
begin
if Pos >= FBufferSize then
begin
FlushBuffer;
end;
{$IFDEF FPC}
Move(NewData, Pointer(fBuffer + fBufferPos)^, fDataLen);
{$ELSE}
Move(NewData, Pointer(LongInt(fBuffer) + fBufferPos)^, fDataLen);
{$ENDIF}
inc(fBufferPos, fDataLen);
end;
end;
end; { WriteData }
procedure TmIOBuffer.FlushBuffer;
var
Written: LongInt;
begin
Written := 0;
BlockWrite(fBuffFile, fBuffer^, fBufferPos, Written);
fBufferPos:= 0;
end; { FlushBuffer }
procedure TmIOBuffer.CloseBuffFile;
begin
CloseFile(fBuffFile);
end; { CloseBuffFile }
procedure TmIOBuffer.DeleteBuffFile;
begin
SysUtils.DeleteFile(fFileName);
end; { DeleteBuffFile }
constructor TTempFile.Create;
begin
inherited Create;
fFull:= False;
end; { Create }
procedure TTempFile.Init(const FileName: String);
begin
fFull:= False;
fFileName:= FileName;
if fFileName <> '' then
begin
Reader:= TmIOBuffer.Create(fFileName, FRecLen, fBuffersSize);
if not Reader.Eof then
begin
fLeft:= Reader.ReadData;
fFull:= True;
end
else
begin
Reader.Free;
SysUtils.DeleteFile(fFileName);
fFileName:= '';
end;
end;
end; { Init }
procedure TTempFile.Next;
begin
if not Reader.Eof then
begin
fLeft:= Reader.ReadData;
fFull:= True;
end
else
begin
fFull:= False;
if fFileName <> '' then
begin
Reader.Free;
SysUtils.DeleteFile(fFileName);
fFileName:= '';
end;
end
end; { Next }
destructor TTempFile.Destroy;
begin
if fFileName <> '' then
begin
Reader.Free;
SysUtils.DeleteFile(fFileName);
end;
inherited Destroy;
end; { Destroy }
constructor TMergeFile.Create(InList: TStringList);
begin
inherited Create;
fInList:= InList;
end; { Create }
destructor TMergeFile.Destroy;
begin
inherited Destroy;
end; { Destroy }
procedure TMergeFile.FileMerge(MergeCompare: DComparator);
begin
while ( FFileOne.Full ) and ( FFileTwo.Full ) and ( FFileThree.Full ) do
begin
if MergeCompare(FFileOne.FLeft^, FFileTwo.FLeft^) <= 0 then
begin
if MergeCompare(FFileOne.FLeft^, FFileThree.FLeft^) <= 0 then
begin
Writer.WriteData(FFileOne.fLeft^);
FFileOne.Next;
end
else
begin
Writer.WriteData(FFileThree.fLeft^);
FFileThree.Next;
end;
end
else
begin
if MergeCompare(FFileTwo.FLeft^, FFileThree.FLeft^) <= 0 then
begin
Writer.WriteData(FFileTwo.fLeft^);
FFileTwo.Next;
end
else
begin
Writer.WriteData(FFileThree.fLeft^);
FFileThree.Next;
end;
end;
end;
while ( FFileOne.Full ) and ( FFileTwo.Full ) do
begin
if MergeCompare(FFileOne.FLeft^, FFileTwo.FLeft^) <= 0 then
begin
Writer.WriteData(FFileOne.fLeft^);
FFileOne.Next;
end
else
begin
Writer.WriteData(FFileTwo.fLeft^);
FFileTwo.Next;
end;
end;
while ( FFileOne.Full ) and ( FFileThree.Full ) do
begin
if MergeCompare(FFileOne.FLeft^, FFileThree.FLeft^) <= 0 then
begin
Writer.WriteData(FFileOne.fLeft^);
FFileOne.Next;
end
else
begin
Writer.WriteData(FFileThree.fLeft^);
FFileThree.Next;
end;
end;
while ( FFileTwo.Full ) and ( FFileThree.Full ) do
begin
if MergeCompare(FFileTwo.FLeft^, FFileThree.FLeft^) <= 0 then
begin
Writer.WriteData(FFileTwo.fLeft^);
FFileTwo.Next;
end
else
begin
Writer.WriteData(FFileThree.fLeft^);
FFileThree.Next;
end;
end;
while FFileOne.Full do { Write Rest of First SubFile }
begin
Writer.WriteData(FFileOne.fLeft^);
FFileOne.Next;
end;
while FFileTwo.Full do { Write Rest of Second SubFile }
begin
Writer.WriteData(FFileTwo.fLeft^);
FFileTwo.Next;
end;
while FFileThree.Full do { Write Rest of Third SubFile }
begin
Writer.WriteData(FFileThree.fLeft^);
FFileThree.Next;
end;
end; { FileMerge }
procedure TMergeFile.MergeSort(MergeCompare: DComparator);
var
a, b, c: String;
N, todo: LongInt;
LTempDirectory: string;
begin
LTempDirectory := IncludeTrailingPathDelimiter(TPath.GetTempPath);
fOutList:= TStringList.Create;
fOutList.Clear;
todo:= 0;
N:= fInList.Count;
fFileOne:= TTempFile.Create;
fFileTwo:= TTempFile.Create;
fFileThree:= TTempFile.Create;
while fInList.Count > 1 do
begin
while todo < fInList.Count do
begin
fFileName:= LTempDirectory + 'Temp' + IntToStr(N);
inc(N);
Writer:= TmIOBuffer.Create(fFileName, fRecLen, fBuffersSize*3);
fOutList.Add(fFileName);
a:= fInList.Strings[todo]; inc(todo);
if todo < fInList.Count then begin b:= fInList.Strings[todo]; inc(todo) end else b:= '';
if todo < fInList.Count then begin c:= fInList.Strings[todo]; inc(todo) end else c:= '';
FFileOne.Init(a);
FFileTwo.Init(b);
FFileThree.Init(c);
FileMerge(MergeCompare);
Writer.FlushBuffer;
Writer.Free;
if todo = fInList.Count -1 then
begin
fOutList.Add(fInList.Strings[todo]);
inc(todo);
end;
end;
todo:= 0;
TempList:= fInList;
fInList:= fOutList;
fOutList:= TempList;
fOutList.Clear;
end;
fFileOne.Free;
fFileTwo.Free;
fFileThree.Free;
fOutList.Free
end; { MergeSort }
constructor TFixRecSort.Create(RecLen: LongInt);
begin
inherited Create;
FRecLen:= RecLen;
fFileName:= '';
FMaxLines := 60000;
FUseMergesort:= True;
end; { Create }
procedure TFixRecSort.Init(const FileName: String);
begin
fFileName:= FileName;
fTempFileList:= TStringList.Create;
end;
function TFixRecSort.GetMaxMem:LongInt;
begin
Result:= fMaxMem;
end; { GetMaxMem }
procedure TFixRecSort.SetMaxMem(value:LongInt);
var
RecLenPlus, CountRec: Integer;
begin
if Value < 100000 then Value:= 100000;
fBuffersSize:= value div 6;
RecLenPlus:= FRecLen +8;
CountRec:= fBuffersSize div RecLenPlus;
fBuffersSize:= CountRec *FRecLen;
fMaxMem:= Value;
end; { SetMaxMem }
procedure TFixRecSort.Start(Compare: DComparator);
var
TempFileName, BackFileName, InFileName: String;
I, K: Integer;
LTempDirectory: string;
begin
LTempDirectory := IncludeTrailingPathDelimiter(TPath.GetTempPath);
FCompare:= Compare;
I:= 0;
InFileName:= fFileName;
BackFileName:= ChangeFileExt(fFileName, '.bak');
if FileExists(BackFileName) then DeleteFile(PChar(BackFileName));
Reader:= TmIOBuffer.Create(FFileName, fRecLen, fBuffersSize *5);
while not Reader.Eof do
begin
fMerArray:= TM3Array.Create;
TempFileName:= LTempDirectory + 'Temp' + IntToStr(I);
fTempFileList.Add(TempFileName);
Writer:= TmIOBuffer.Create(TempFileName, fRecLen, fBuffersSize);
inc(I);
while (fMerArray.Count < fMaxLines) and (DWORD(fMerArray.Count) <= Reader.RecCount) and (not Reader.Eof) do
begin
fMerArray.Add(Reader.ReadData);
end; { while }
if UseMergesort then fMerArray.MergeSort(fCompare)
else fMerArray.QuickSort(fCompare);
for K := 0 to fMerArray.Count -1 do { Iterate }
begin
Writer.WriteData(fMerArray[K]^);
end; { for }
Writer.FlushBuffer;
Writer.Free;
fMerArray.Free;
end; { while }
Reader.Free;
if fTempFileList.Count > 1 then
begin
MergeFile:= TMergeFile.Create(fTempFileList);
MergeFile.MergeSort(fCompare);
RenameFile(InFileName, BackFileName);
RenameFile(MergeFile.FileName, FFileName);
MergeFile.Free;
end else
begin
RenameFile(InFileName, BackFileName);
RenameFile(TempFileName, FFileName);
end;
end; { Start }
destructor TFixRecSort.Destroy;
begin
inherited Destroy;
end; { Destroy }
end.