-
Notifications
You must be signed in to change notification settings - Fork 0
/
adtfunct.pas.mcp
266 lines (216 loc) · 7.58 KB
/
adtfunct.pas.mcp
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
(* This file is a part of the PascalAdt library, which provides
commonly used algorithms and data structures for the FPC and Delphi
compilers.
Copyright (C) 2004 by Lukasz Czajka
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation; either version 2.1 of
the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
02110-1301 USA *)
unit adtfunct;
{ this unit provides abstract functor interfaces as well as some
commonly used concrete functors; functors are just object which are
primarily used as an enhanced kind of functions; there are several
types of functors: 1) ordinary functors (IUnaryFunctor,
IBinaryFunctor), which just transform their pointer argument(s) into
a pointer or only perform some operation on their argument(s); 2)
predicates, which return either true or false basing on the values
of their arguments; used primarily for testing certain attributes of
objects; 3) comparers, which compare two objects and return the
result 4) subtractors, which subtract the values of two objects 5)
hashers, which compute 32-bit unsigned hash values of their
argument. The concrete classes in this unit are either descendants
of the above or function/procedure adaptors, which adapt their
arguments to abstract functor interfaces so as to make it possible
to use an ordinary function/procedure pointer where a functor is
required. }
interface
&# we need the following specializations even if not chosen by the user
&define MCP_POINTER
&undefine MCP_NO_INTEGER
&include adtdefs.inc
type
{ a base for all functors }
IFunctor = interface (IInterface)
{ returns the object implementing the interface }
function GetObject : TObject;
end;
{ every class implementing functor interfaces should inherit from
this one }
TFunctor = class (TInterfacedObject)
public
{$ifdef DEBUG_PASCAL_ADT }
constructor Create;
destructor Destroy; override;
{$endif }
{ Returns self. }
function GetObject : TObject;
end;
&_mcp_generic_include(adtfunct.i)
type
{ a functor for disposing objects; }
TObjectDisposer = class (TFunctor, IUnaryFunctor)
public
{ assumes the given argument is a pointer to an object; destroys
it and returns nil; invokes TObject(aitem).Free; }
function Perform(aitem : TObject) : TObject;
end;
{ a functor comparing the items by the values of the pointer
addresses exclusively }
TPointerValueComparer = class (TFunctor, IBinaryComparer, ISubtractor)
public
{ compares pointers according to their values interpreted as
numbers }
function Compare(aitem1, aitem2 : TObject) : Integer;
end;
TNoCaseStringComparer = class (TFunctor, IStringBinaryComparer)
public
{ compares the two strings case-insensitively }
function Compare(aitem1, aitem2 : String) : Integer;
end;
TTextComparer = TNoCaseStringComparer;
&ifdef MCP_POINTER
{ a functor used to dispose PChars }
TPCharDisposer = class (TFunctor, IPointerUnaryFunctor)
public
{ assumes the given argument is a pointer to a PChar; destroys
it and returns nil }
function Perform(aitem : Pointer) : Pointer;
end;
{ a functor for comparing PChars case-sensitively }
TPCharComparer = class (TFunctor, IPointerBinaryComparer)
public
{ interprets its arguments as PChar's and calls StrComp to
compare them }
function Compare(aitem1, aitem2 : Pointer) : Integer;
end;
{ a functor for comparing PChars case-insensitively }
TNoCasePCharComparer = class (TFunctor, IPointerBinaryComparer)
public
function Compare(aitem1, aitem2 : Pointer) : Integer;
end;
&endif
{ returns a TObjectDisposer; it is more efficient to call this
function instead of creating TObjectDisposer directly, because here
an existing object is reused intead of creating a new one }
function ObjectDisposer : IUnaryFunctor;
function PointerValueComparer : IBinaryComparer;
function NoCaseStringComparer : IStringBinaryComparer;
implementation
uses
{$ifdef DEBUG_PASCAL_ADT }
adtlog,
{$endif }
SysUtils;
{$ifdef DEBUG_PASCAL_ADT }
const
existingFunctors : Cardinal = 0;
{$endif }
var
varObjectDisposer : IUnaryFunctor;
varPointerValueComparer : IBinaryComparer;
varNoCaseStringComparer : IStringBinaryComparer;
&_mcp_generic_include(adtfunct_impl.i)
{ ------------------------ TFunctor ---------------------------- }
{$ifdef DEBUG_PASCAL_ADT }
constructor TFunctor.Create;
begin
Inc(existingFunctors);
end;
destructor TFunctor.Destroy;
begin
Dec(existingFunctors);
end;
{$endif }
function TFunctor.GetObject : TObject;
begin
Result := self;
end;
{ ------------------------- TObjectDisposer --------------------------------- }
function TObjectDisposer.Perform(aitem : TObject) : TObject;
begin
aitem.Free;
Result := nil;
end;
{ -------------------------- TPointerValueComparer -------------------------- }
function TPointerValueComparer.Compare(aitem1, aitem2 : TObject) : Integer;
begin
if PointerValueType(aitem1) = PointerValueType(aitem2) then begin
Result := 0;
end else if PointerValueType(aitem1) > PointerValueType(aitem2) then begin
Result := 1;
end else begin
Result := -1;
end
end;
function TNoCaseStringComparer.Compare(aitem1, aitem2 : String) : Integer;
begin
Result := CompareText(aitem1, aitem2);
end;
&ifdef MCP_POINTER
{ ------------------------- TPCharDisposer --------------------------------- }
function TPCharDisposer.Perform(aitem : Pointer) : Pointer;
begin
StrDispose(PChar(aitem));
Result := nil;
end;
{ ---------------------------- TPCharComparer ----------------------------- }
function TPCharComparer.Compare(aitem1, aitem2 : Pointer) : Integer;
begin
Result := StrComp(PChar(aitem1), PChar(aitem2));
end;
{ ---------------------------- TNoCasePCharComparer ------------------------- }
function TNoCasePCharComparer.Compare(aitem1, aitem2 : Pointer) : Integer;
var
pch1, pch2 : PChar;
begin
pch1 := PChar(aitem1);
pch2 := PCHar(aitem2);
while (pch1^ <> #0) and (pch2^ <> #0) and
(Lowercase(pch1^) = Lowercase(pch2^)) do
begin
Inc(pch1);
Inc(pch2);
end;
Result := Ord(pch1^) - Ord(pch2^);
end;
&endif &# end MCP_POINTER
function ObjectDisposer : IUnaryFunctor;
begin
Result := varObjectDisposer;
end;
function PointerValueComparer : IBinaryComparer;
begin
Result := varPointerValueComparer;
end;
function NoCaseStringComparer : IStringBinaryComparer;
begin
Result := varNoCaseStringComparer;
end;
initialization
varIdentity := TIdentity.Create;
varStringIdentity := TStringIdentity.Create;
&ifdef MCP_POINTER
varPointerIdentity := TPointerIdentity.Create;
&endif
&ifndef MCP_NO_INTEGER
varIntegerIdentity := TIntegerIdentity.Create;
&endif
&ifdef MCP_CARDINAL
varCardinalIdentity := TCardinalIdentity.Create;
&endif
&ifdef MCP_REAL
varRealIdentity := TRealIdentity.Create;
&endif
varObjectDisposer := TObjectDisposer.Create;
varPointerValueComparer := TPointerValueComparer.Create;
varNoCaseStringComparer := TNoCaseStringComparer.Create;
finalization
end.