-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathThSort.pas
242 lines (210 loc) · 6.24 KB
/
ThSort.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
unit ThSort;
interface
uses
SysUtils, Classes,
Windows, Messages, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls,
PythonEngine;
type
TThreadSortForm = class(TForm)
StartBtn: TButton;
BubbleSortBox: TPaintBox;
SelectionSortBox: TPaintBox;
QuickSortBox: TPaintBox;
Label1: TLabel;
Bevel1: TBevel;
Bevel2: TBevel;
Bevel3: TBevel;
Label2: TLabel;
Label3: TLabel;
PythonMemo: TMemo;
PythonEngine1: TPythonEngine;
Start2Btn: TButton;
LoadBtn: TButton;
PythonDialog: TOpenDialog;
SaveDialog: TSaveDialog;
SaveBtn: TButton;
SortModule: TPythonModule;
procedure BubbleSortBoxPaint(Sender: TObject);
procedure SelectionSortBoxPaint(Sender: TObject);
procedure QuickSortBoxPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure StartBtnClick(Sender: TObject);
procedure Start2BtnClick(Sender: TObject);
procedure LoadBtnClick(Sender: TObject);
procedure SaveBtnClick(Sender: TObject);
procedure SortModuleInitialization(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
OwnThreadState: PPyThreadState;
ThreadsRunning: Integer;
procedure RandomizeArrays;
procedure ThreadDone(Sender: TObject);
procedure InitThreads(ThreadExecMode: TThreadExecMode; script: TStrings);
function SortModule_GetValue( pself, args : PPyObject ) : PPyObject; cdecl;
function SortModule_Swap( pself, args : PPyObject ) : PPyObject; cdecl;
public
procedure PaintArray(Box: TPaintBox; const A: array of Integer);
end;
var
ThreadSortForm: TThreadSortForm;
implementation
uses SortThds;
{$R *.dfm}
type
PSortArray = ^TSortArray;
TSortArray = array[0..176] of Integer;
var
ArraysRandom: Boolean;
BubbleSortArray, SelectionSortArray, QuickSortArray: TSortArray;
{ TThreadSortForm }
procedure TThreadSortForm.PaintArray(Box: TPaintBox; const A: array of Integer);
var
I: Integer;
begin
with Box do
begin
Canvas.Pen.Color := clRed;
for I := Low(A) to High(A) do PaintLine(Canvas, I, A[I]);
end;
end;
procedure TThreadSortForm.BubbleSortBoxPaint(Sender: TObject);
begin
PaintArray(BubbleSortBox, BubbleSortArray);
end;
procedure TThreadSortForm.SelectionSortBoxPaint(Sender: TObject);
begin
PaintArray(SelectionSortBox, SelectionSortArray);
end;
procedure TThreadSortForm.QuickSortBoxPaint(Sender: TObject);
begin
PaintArray(QuickSortBox, QuickSortArray);
end;
procedure TThreadSortForm.FormCreate(Sender: TObject);
begin
RandomizeArrays;
end;
procedure TThreadSortForm.InitThreads(ThreadExecMode: TThreadExecMode; script: TStrings);
begin
RandomizeArrays;
ThreadsRunning := 3;
with GetPythonEngine do
begin
OwnThreadState := PyEval_SaveThread;
with TSortThread.Create( ThreadExecMode, script, SortModule, 'SortFunc1',
BubbleSortBox, BubbleSortArray) do
OnTerminate := ThreadDone;
with TSortThread.Create( ThreadExecMode, script, SortModule, 'SortFunc2',
SelectionSortBox, SelectionSortArray) do
OnTerminate := ThreadDone;
with TSortThread.Create( ThreadExecMode, script, SortModule, 'SortFunc3',
QuickSortBox, QuickSortArray) do
OnTerminate := ThreadDone;
end;
StartBtn.Enabled := False;
Start2Btn.Enabled := False;
end;
procedure TThreadSortForm.Start2BtnClick(Sender: TObject);
begin
with GetPythonEngine do
begin
ExecStrings(PythonMemo.Lines);
self.InitThreads(emNewState,nil);
end;
end;
procedure TThreadSortForm.StartBtnClick(Sender: TObject);
begin
InitThreads(emNewInterpreter,PythonMemo.Lines);
//PythonEngine1.ExecStrings(PythonMemo.Lines);
end;
procedure TThreadSortForm.LoadBtnClick(Sender: TObject);
begin
with PythonDialog do if Execute then
PythonMemo.Lines.LoadFromFile(FileName);
end;
procedure TThreadSortForm.SaveBtnClick(Sender: TObject);
begin
with SaveDialog do if Execute then
PythonMemo.Lines.SaveToFile(FileName);
end;
procedure TThreadSortForm.RandomizeArrays;
var
I: Integer;
begin
if not ArraysRandom then
begin
Randomize;
for I := Low(BubbleSortArray) to High(BubbleSortArray) do
BubbleSortArray[I] := Random(170);
SelectionSortArray := BubbleSortArray;
QuickSortArray := BubbleSortArray;
ArraysRandom := True;
Repaint;
end;
end;
procedure TThreadSortForm.ThreadDone(Sender: TObject);
begin
Dec(ThreadsRunning);
if ThreadsRunning = 0 then
begin
GetPythonEngine.PyEval_RestoreThread(OwnThreadState);
StartBtn.Enabled := True;
Start2Btn.Enabled := True;
ArraysRandom := False;
end;
end;
function TThreadSortForm.SortModule_GetValue( pself, args : PPyObject ) : PPyObject; cdecl;
var
psort: NativeInt;
index: integer;
begin
with GetPythonEngine do
begin
{$IFDEF CPU64BITS}
if PyArg_ParseTuple( args, 'Li',@psort, @index) <> 0 then
{$ELSE}
if PyArg_ParseTuple( args, 'ii',@psort, @index) <> 0 then
{$ENDIF}
begin
Result := PyLong_FromLong(TSortThread(psort)[index]);
end else
Result := nil;
end;
end;
function TThreadSortForm.SortModule_Swap( pself, args : PPyObject ) : PPyObject; cdecl;
var
psort : NativeInt;
i,j: integer;
begin
with GetPythonEngine do
begin
{$IFDEF CPU64BITS}
if PyArg_ParseTuple( args, 'Lii',@psort, @i, @j) <> 0 then
{$ELSE}
if PyArg_ParseTuple( args, 'iii',@psort, @i, @j) <> 0 then
{$ENDIF}
begin
TSortThread(psort).VisualSwap(i,j);
Result := ReturnNone;
end else
Result := nil;
end;
end;
procedure TThreadSortForm.SortModuleInitialization(Sender: TObject);
begin
with Sender as TPythonModule do
begin
AddDelphiMethod( 'getvalue',
SortModule_GetValue,
'GetValue(handle,index)' );
AddDelphiMethod( 'swap',
SortModule_Swap,
'swap(handle,index1,index2)');
end;
end;
procedure TThreadSortForm.FormCloseQuery(Sender: TObject;
var CanClose: Boolean);
begin
CanClose := ThreadsRunning = 0;
end;
end.