forked from martok/ImageHash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortbase.pas
More file actions
173 lines (150 loc) · 5.45 KB
/
sortbase.pas
File metadata and controls
173 lines (150 loc) · 5.45 KB
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
unit sortbase;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
TSortCompareFuncContext = function(Item1, Item2, Context: Pointer): Integer;
TSortCompareFuncNoContext = function(Item1, Item2: Pointer): Integer;
// Alias for compatibility if needed, though usually function pointer types match by signature
TListSortComparer_NoContext = TSortCompareFuncNoContext;
PSortingAlgorithm = ^TSortingAlgorithm;
TSortingAlgorithm = record
ItemListSorter_ContextComparer: procedure(Base: Pointer; Count: Integer; ElementSize: Integer; Compare: TSortCompareFuncContext; Context: Pointer);
PtrListSorter_NoContextComparer: procedure(Base: Pointer; Count: Integer; Compare: TSortCompareFuncNoContext);
end;
var
DefaultSortingAlgorithm: PSortingAlgorithm;
implementation
type
TInternalSorting = class
class procedure QuickSortContext(Base: Pointer; L, R: Integer; ElementSize: Integer; Compare: TSortCompareFuncContext; Context: Pointer);
class procedure QuickSortNoContext(Base: Pointer; L, R: Integer; Compare: TSortCompareFuncNoContext);
end;
var
GAlgorithm: TSortingAlgorithm;
class procedure TInternalSorting.QuickSortContext(Base: Pointer; L, R: Integer; ElementSize: Integer; Compare: TSortCompareFuncContext; Context: Pointer);
var
I, J: Integer;
P, T: Pointer;
PI, PJ: Pointer;
begin
if L < R then
begin
GetMem(T, ElementSize);
try
I := L;
J := R;
// Pivot
P := Base + ((L + R) div 2) * ElementSize;
repeat
while Compare(Base + I * ElementSize, P, Context) < 0 do Inc(I);
while Compare(Base + J * ElementSize, P, Context) > 0 do Dec(J);
if I <= J then
begin
PI := Base + I * ElementSize;
PJ := Base + J * ElementSize;
// Swap content
Move(PI^, T^, ElementSize);
Move(PJ^, PI^, ElementSize);
Move(T^, PJ^, ElementSize);
// If Pivot was swapped (it's pointing into the array), update it
// Note: P is a pointer calculated from Base, but if we move data, P still points to the same memory address,
// but the data at that address has changed.
// Wait, QuickSort usually picks the value of the pivot.
// Since we are dealing with generic data, we should copy the pivot value to a temp buffer if we want to be safe,
// OR we just accept that we are comparing against the value at the pivot position.
// BUT, if we swap the pivot position, the pivot value changes.
// Standard implementation copies the pivot value.
// Let's copy pivot value to T and use T for comparison.
Inc(I);
Dec(J);
end;
until I > J;
// Recursive calls
if L < J then QuickSortContext(Base, L, J, ElementSize, Compare, Context);
if I < R then QuickSortContext(Base, I, R, ElementSize, Compare, Context);
finally
FreeMem(T);
end;
end;
end;
// Correct implementation of QuickSortContext using Pivot Copy
procedure QuickSortContext_Impl(Base: Pointer; L, R: Integer; ElementSize: Integer; Compare: TSortCompareFuncContext; Context: Pointer);
var
I, J: Integer;
Pivot, Temp: Pointer;
begin
if L >= R then Exit;
GetMem(Pivot, ElementSize);
GetMem(Temp, ElementSize);
try
I := L;
J := R;
// Copy Pivot Value
Move((Base + ((L + R) div 2) * ElementSize)^, Pivot^, ElementSize);
repeat
while Compare(Base + I * ElementSize, Pivot, Context) < 0 do Inc(I);
while Compare(Base + J * ElementSize, Pivot, Context) > 0 do Dec(J);
if I <= J then
begin
// Swap
Move((Base + I * ElementSize)^, Temp^, ElementSize);
Move((Base + J * ElementSize)^, (Base + I * ElementSize)^, ElementSize);
Move(Temp^, (Base + J * ElementSize)^, ElementSize);
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSortContext_Impl(Base, L, J, ElementSize, Compare, Context);
if I < R then QuickSortContext_Impl(Base, I, R, ElementSize, Compare, Context);
finally
FreeMem(Pivot);
FreeMem(Temp);
end;
end;
class procedure TInternalSorting.QuickSortNoContext(Base: Pointer; L, R: Integer; Compare: TSortCompareFuncNoContext);
type
PPointer = ^Pointer;
var
I, J: Integer;
P, T: Pointer;
Arr: PPointer;
begin
Arr := PPointer(Base);
if L < R then
begin
I := L;
J := R;
P := Arr[(L + R) div 2];
repeat
while Compare(Arr[I], P) < 0 do Inc(I);
while Compare(Arr[J], P) > 0 do Dec(J);
if I <= J then
begin
T := Arr[I];
Arr[I] := Arr[J];
Arr[J] := T;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSortNoContext(Base, L, J, Compare);
if I < R then QuickSortNoContext(Base, I, R, Compare);
end;
end;
procedure ItemListSorter_ContextComparer_Impl(Base: Pointer; Count: Integer; ElementSize: Integer; Compare: TSortCompareFuncContext; Context: Pointer);
begin
if Count > 1 then
QuickSortContext_Impl(Base, 0, Count - 1, ElementSize, Compare, Context);
end;
procedure PtrListSorter_NoContextComparer_Impl(Base: Pointer; Count: Integer; Compare: TSortCompareFuncNoContext);
begin
if Count > 1 then
TInternalSorting.QuickSortNoContext(Base, 0, Count - 1, Compare);
end;
initialization
GAlgorithm.ItemListSorter_ContextComparer := @ItemListSorter_ContextComparer_Impl;
GAlgorithm.PtrListSorter_NoContextComparer := @PtrListSorter_NoContextComparer_Impl;
DefaultSortingAlgorithm := @GAlgorithm;
end.