-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sorter.dpr
150 lines (128 loc) · 4.16 KB
/
Sorter.dpr
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
program Sorter;
{$APPTYPE CONSOLE}
// Èñïîëüçóåòñÿ ñîðòèðîâêà ñëèÿíèåì.
// Èäåÿ ñòàíäàðòíàÿ - ðàçáèâàåì èñõîäíûé ôàéë íà áëîêè ïðèìåðíî ðàâíîé äëèíû ïî <= SORT_BLOCK_SIZE áàéò
// Êàæäûé áëîê ñîðòèðóåì â ïàìÿòè è ñîõðàíÿåì âî âðåìåííûé ôàéë
// Äàëüøå ñëèâàåì áëîêè ñ ñîðòèðîâêîé
//
// Ìîæíî óìåíüøèòü êîëè÷åñòâî èñïîëüçóåìîãî äèñêîâîãî ïðîñòðàíñòâà åñëè çàïóñêàòü ñëèÿíèå
// íå äîæèäàÿñü îêîí÷àíèÿ ïðîöåññà ðàçáèåíèÿ, íî òàì áóäåò áîëüøå ïðîáëåì ñ óïðàâëåíèåì
// ïîòîêàìè, ïîýòîìó ïîêà òàê
//
// Óâåëè÷åíèå êîëè÷åñòâà ðàáî÷èõ ïîòîêîâ âûçûâàåò óìåíüøåíèå ñêîðîñòè èç-çà êîíêóðåíöèè çà äèñê ìåæäó ïîòîêàìè
uses
Windows,
SysUtils,
UnitSorter in 'UnitSorter.pas',
UnitBlockSorter in 'UnitBlockSorter.pas',
UnitThreadManager in 'UnitThreadManager.pas',
UnitSplitter in 'UnitSplitter.pas',
UnitMerger in 'UnitMerger.pas',
UnitBlockMerger in 'UnitBlockMerger.pas',
Common in 'Common.pas',
UnitMemoryManager in 'UnitMemoryManager.pas',
UnitBufferedFileStream in 'UnitBufferedFileStream.pas',
UnitBlockList in 'UnitBlockList.pas';
procedure ParseParams(var InFile, OutFile: string);
var
OptionsStartIndex, i: integer;
Option: string;
n: integer;
begin
InFile := ParamStr(1);
if ParamCount = 1 then
OutFile := InFile
else
begin
OutFile := ParamStr(2);
if OutFile[1] = '-' then
begin
OptionsStartIndex := 2;
OutFile := InFile;
end
else
OptionsStartIndex := 3;
for i := OptionsStartIndex to ParamCount do
begin
Option := ParamStr(i);
if (Option[1] = '-') and (Length(Option) > 1) then
begin
Delete(Option, 1, 1);
if SameText(Option, 'D') then
Debug := true
else
if SameText(Option, 'M') then
TrackMemoryUsage := true
else
if UpCase(Option[1]) = 'T' then
begin
Delete(Option, 1, 1);
if TryStrToInt(Option, n) then
MaxWorkerThreadCount := n
else
writeln('Incorrect -T option value. Using ', MaxWorkerThreadCount, ' worker treads by default');
end
else
if UpCase(Option[1]) = 'L' then
begin
Delete(Option, 1, 1);
if TryStrToInt(Option, n) then
MemoryAvailable := n
else
writeln('Incorrect -L option value. Using ', MemoryAvailable, ' by default');
end;
end;
end;
if Debug then
TrackMemoryUsage := false;
end;
end;
var
InFile, OutFile: string;
Freq, StartTime, EndTime: Int64;
InFileSize, OutFileSize: Int64;
begin
if ParamCount = 0 then
begin
writeln('Text file sorter');
writeln('Usage: sorter InFile [OutFile] [Options]');
writeln('Options:');
writeln(' -D - debug (shows log and saves all the temporary files, be careful!)');
writeln(' -M - track memory usage (doesn''t work with -D)');
writeln(' -Tn - use n worker threads (default ', MaxWorkerThreadCount, ')');
writeln(' -Ln - limit memory usage to n kilobytes (default ', MemoryAvailable, ')');
end
else
begin
ParseParams(InFile, OutFile);
if not FileExists(InFile) then
begin
writeln('File not found ', InFile);
Exit;
end;
InFileSize := FileSize(InFile);
if TrackMemoryUsage then
SetDebugMemoryManager;
write('Sorting ', InFile);
if TrackMemoryUsage then
writeln(' using ', MemoryAvailable, ' Kb of memory')
else
writeln;
InitSort;
if TrackMemoryUsage then
writeln('Buffers size: sort ', SortBufferSize, ', merge ', MergeBufferSize);
QueryPerformanceFrequency(Freq);
QueryPerformanceCounter(StartTime);
SortData(InFile, OutFile);
QueryPerformanceCounter(EndTime);
OutFileSize := FileSize(OutFile);
writeln(Format('Done in %.3f seconds ', [(EndTime - StartTime) / Freq]));
if TrackMemoryUsage then
begin
writeln('Input file size : ', InFileSize, ' bytes');
writeln('Output file size: ', OutFileSize, ' bytes');
writeln('Max heap usage: sorting ', GetMaxSortMemoryUsage div 1024, ' KBytes, merging ', GetMaxMergeMemoryUsage div 1024, ' KBytes');
RestoreMemoryManager;
end;
end;
end.