-
Notifications
You must be signed in to change notification settings - Fork 1
/
adtutils.pas.mcp
303 lines (253 loc) · 6.61 KB
/
adtutils.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
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
(* 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-2006 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 adtutils;
{ This unit presents a few basic, simple utilities like Min, Max,
Exchange, ... }
interface
&# this must always be generated - needed by adtdarray
&define MCP_POINTER
&undefine MCP_NO_INTEGER
&include adtdefs.inc
&_mcp_generic_include(adtutils.i)
{ returns the smaller of two integers }
function Min(v1, v2 : Integer) : Integer;
{$ifdef OVERLOAD_DIRECTIVE }
overload;
{$endif }
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{ returns the smallest of three integers. If your compiler does not
support overloading use the second routine. @include-declarations 2 }
{$ifdef OVERLOAD_DIRECTIVE }
function Min(v1, v2, v3 : Integer) : Integer; overload;
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{$endif OVERLOAD_DIRECTIVE }
function Min3(v1, v2, v3 : Integer) : Integer;
{ returns the larger of two integers }
function Max(v1, v2 : Integer) : Integer;
{$ifdef OVERLOAD_DIRECTIVE }
overload;
{$endif OVERLOAD_DIRECTIVE }
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif INLINE_DIRECTIVE }
{ Returns the largest of three integers. @include-declarations 2 }
{$ifdef OVERLOAD_DIRECTIVE }
function Max(v1, v2, v3 : Integer) : Integer; overload;
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{$endif OVERLOAD_DIRECTIVE }
function Max3(v1, v2, v3 : Integer) : Integer;
{ exchanges the values of two pointers }
procedure ExchangePtr(var p1, p2);
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{ exchanges two one-byte values }
procedure ExchangeByte(var b1, b2);
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{ exchanges the values of two variables, each <size> bytes long }
procedure ExchangeData(var v1, v2; size : SizeType);
{ returns true if the index is valid assuming it points into a
zero-based container of size size }
function IsValidIndex(index : IndexType; size : SizeType) : Boolean;
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif }
{ returns true if the index is valid assuming it points into a
low-based container of size size }
function IsValidIndexLow(index, low : IndexType; size : SizeType) : Boolean;
{$ifdef INLINE_DIRECTIVE }
inline;
{$endif INLINE_DIRECTIVE }
{ returns the largest number smaller than the base 2 logarithm of n;
if n is 0 EInvalidArgument is raised }
function FloorLog2(n : SizeType) : SizeType;
{ returns the smallest number larger than the base 2 logarithm of n;
if n is 0 EInvalidArgument is raised }
function CeilLog2(n : SizeType) : SizeType;
implementation
uses
adtexcept, SysUtils;
&_mcp_generic_include(adtutils_impl.i)
function Min(v1, v2 : Integer) : Integer;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
if v2 < v1 then
Result := v2
else
Result := v1;
end;
function Min3(v1, v2, v3 : Integer) : Integer;
begin
if v2 < v1 then
begin
if v3 < v2 then
Result := v3
else
Result := v2;
end else
begin
if v3 < v1 then
Result := v3
else
Result := v1;
end;
end;
function Min(v1, v2, v3 : Integer) : Integer;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
Result := Min3(v1, v2, v3);
end;
function Max(v1, v2 : Integer) : Integer;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
if v2 > v1 then
Result := v2
else
Result := v1;
end;
function Max3(v1, v2, v3 : Integer) : Integer;
begin
if v2 > v1 then
begin
if v3 > v2 then
Result := v3
else
Result := v2;
end else
begin
if v3 > v1 then
Result := v3
else
Result := v1;
end;
end;
{$ifdef OVERLOAD_DIRECTIVE }
function Max(v1, v2, v3 : Integer) : Integer;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
Result := Max3(v1, v2, v3);
end;
{$endif OVERLOAD_DIRECTIVE }
procedure ExchangePtr(var p1, p2);
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
var
temp : Pointer;
begin
temp := Pointer(p1);
Pointer(p1) := Pointer(p2);
Pointer(p2) := temp;
end;
procedure ExchangeByte(var b1, b2);
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
var
tmp : Byte;
begin
tmp := Byte(b1);
Byte(b1) := Byte(b2);
Byte(b2) := tmp;
end;
procedure ExchangeData(var v1, v2; size : SizeType);
type
PByte = ^Byte;
var
i : PointerValueType;
begin
i := 0;
while i + SizeOf(Pointer) <= PointerValueType(size) do
begin
ExchangePtr(PPointer(POinterValueType(@v1) + i)^,
PPointer(PointerValueType(@v2) + i)^);
Inc(i, SizeOf(pointer));
end;
while i < PointerValueType(size) do
begin
ExchangeByte(PByte(PointerValueType(@v1) + i)^,
PByte(PointerValueType(@v2) + i)^);
Inc(i);
end;
end;
function IsValidIndex(index : IndexType; size : SizeType) : Boolean;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
Result := (index >= 0) and (SizeType(index) < size);
end;
function IsValidIndexLow(index, low : IndexType; size : SizeType) : Boolean;
{$ifdef INLINE_DIRECTIVE_REPEAT }
inline;
{$endif }
begin
Result := (index >= low) and (SizeType(index + low) < size);
end;
function FloorLog2(n : SizeType) : SizeType;
const
MSIGN_BIT = SizeOf(SizeType)*adtBitsInByte - 1;
var
mask : SizeType;
begin
if n = 0 then
raise EInvalidArgument.Create('FloorLog2');
mask := $0FF;
mask := mask shl (MSIGN_BIT - 7);
Result := MSIGN_BIT;
while (n and mask) = 0 do
begin
mask := mask shr 8;
Dec(Result, 8);
end;
mask := 1 shl Result;
while (n and mask) = 0 do
begin
mask := mask shr 1;
Dec(Result);
end;
end;
function CeilLog2(n : SizeType) : SizeType;
var
mask : SizeType;
begin
Result := FloorLog2(n);
if Result <> 0 then
begin
mask := 1 shl (Result - 1);
while ((n and mask) = 0) and (mask <> 0) do
mask := mask shr 1;
if mask <> 0 then
Inc(Result);
end;
end;
end.