-
Notifications
You must be signed in to change notification settings - Fork 2
/
MyDelphiArrayUtils.pas
491 lines (426 loc) · 13.2 KB
/
MyDelphiArrayUtils.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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
{*
MyDelphiArrayUtils.pas - initializing and sorting arrays
----------------------
Begin: 2005/10/21
Last revision: $Date: 2010-05-20 05:03:24 $ $Author: areeves $
Version number: $Revision: 1.12 $
Code Documentation Tags: Begin 2009-08-14, Last Revision: 2009-08-14
Project: APHI General Purpose Delphi Library
Website: http://www.naadsm.org/opensource/delphi
Author: Aaron Reeves <Aaron.Reeves@colostate.edu>
--------------------------------------------------
Copyright (C) 2005 - 2009 Animal Population Health Institute, Colorado State University
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
}
(*
Documentation generation tags begin with {* or ///
Replacing these with (* or // foils the documentation generator
*)
unit MyDelphiArrayUtils;
interface
type TARDoubleArray = array of double; /// Array for double reals (5.0 x 10^-324 .. 1.7 x 10^308)
type TARIntArray = array of integer; /// Array for integers
type TARExtendedArray = array of extended; /// Array for extended reals (3.6 x 10^-4951 .. 1.1 x 10^4932)
type TARStringArray = array of string; /// Array for strings
procedure fillArray( var arr: TARIntArray; val: integer ); overload;
procedure fillArray( var arr: TARDoubleArray; val: double ); overload;
procedure fillArray( var arr: TARExtendedArray; val: extended ); overload;
procedure zeroArray( var arr: TARIntArray ); overload;
procedure zeroArray( var arr: TARDoubleArray ); overload;
procedure zeroArray( var arr: TARExtendedArray ); overload;
function arrayIsZeroed( const arr: TARIntArray ): boolean; overload;
function arrayIsZeroed( const arr: TARDoubleArray ): boolean; overload;
function arrayIsZeroed( const arr: TARExtendedArray ): boolean; overload;
{ Sort values in the array. Quicksort algorithm adapted from http://www.vb-helper.com/dart1.htm }
procedure quickSort( arr: TARDoubleArray ); overload;
procedure quickSort( arr: TARIntArray ); overload;
function arrayContains( const arr: TARStringArray; const item: string; const caseSensitive: boolean = false ): boolean; overload;
function arrayContains( const arr: array of string; const item: string; const caseSensitive: boolean = false ): boolean; overload;
// Debugging
//----------
procedure arDoubleArrayDebug( arr: TARDoubleArray );
implementation
uses
SysUtils,
DebugWindow,
MyStrUtils
;
{*
Fills every element in array arr with val
@param arr An array for integers
@param val An integer value
}
procedure fillArray( var arr: TARIntArray; val: integer );
var
i: integer;
begin
for i := low( arr ) to high( arr ) do
arr[i] := val
;
end
;
{*
Fills every element in array arr with val
@param arr An array for double real numbers
@param val A double value
}
procedure fillArray( var arr: TARDoubleArray; val: double );
var
i: integer;
begin
for i := low( arr ) to high( arr ) do
arr[i] := val
;
end
;
{*
Fills every element in array arr with val
@param arr An array for extended real numbers
@param val A double value
}
procedure fillArray( var arr: TARExtendedArray; val: extended );
var
i: integer;
begin
for i := low( arr ) to high( arr ) do
arr[i] := val
;
end
;
{*
Initializes the value of all the array elements to 0
@param arr An array for integer numbers
}
procedure zeroArray( var arr: TARIntArray );
begin
fillArray( arr, 0 );
end
;
{*
Initializes the value of all the array elements to 0.0
@param arr An array for double real numbers
}
procedure zeroArray( var arr: TARDoubleArray );
begin
fillArray( arr, 0.0 );
end
;
{*
Initializes the value of all the array elements to 0.0
@param arr An array for extended real numbers
}
procedure zeroArray( var arr: TARExtendedArray );
begin
fillArray( arr, 0.0 );
end
;
{*
Validates whether all elements of the array have a value of 0
@param arr An array for integer numbers
@return True if validation is success, else false
}
function arrayIsZeroed( const arr: TARIntArray ): boolean;
var
i: integer;
begin
result := true;
for i := low( arr ) to high( arr ) do
if( 0 <> arr[i] ) then
begin
result := false;
break;
end
;
;
end
;
{*
Validates whether all elements of the array have a value of 0.0
@param arr An array for double real numbers
@return True if validation is success, else false
}
function arrayIsZeroed( const arr: TARDoubleArray ): boolean;
var
i: integer;
begin
result := true;
for i := low( arr ) to high( arr ) do
if( 0.0 <> arr[i] ) then
begin
result := false;
break;
end
;
;
end
;
{*
Validates whether all elements of the array have a value of 0.0
@param arr An array for extended real numbers
@return True if validation is success, else false
}
function arrayIsZeroed( const arr: TARExtendedArray ): boolean;
var
i: integer;
begin
result := true;
for i := low( arr ) to high( arr ) do
if( 0.0 <> arr[i] ) then
begin
result := false;
break;
end
;
;
end
;
{*
Sorts the double real numbers in arr, ascending order
@param arr An array of integers
}
procedure quickSort( arr: TARDoubleArray );
/// Inline function that uses 0 and the length of array for min and max
procedure sort( arr: TARDoubleArray; min, max: integer );
var
med_value: double;
hi, lo, i: integer;
begin
// If the list has <= 1 element, it's sorted.
if (min >= max) then Exit;
// Pick a dividing item randomly.
i := min + Trunc(Random(max - min + 1));
med_value := arr[i];
// Swap it to the front so we can find it easily.
arr[i] := arr[min];
// Move the items smaller than this into the left
// half of the list. Move the others into the right.
lo := min;
hi := max;
while( true ) do
begin
// Look down from hi for a value < med_value.
while (arr[hi] >= med_value) do
begin
hi := hi - 1;
if (hi <= lo) then Break;
end
;
if (hi <= lo) then
begin
// We're done separating the items.
arr[lo] := med_value;
Break;
end
;
// Swap the lo and hi values.
arr[lo] := arr[hi];
// Look up from lo for a value >= med_value.
lo := lo + 1;
while (arr[lo] < med_value) do
begin
lo := lo + 1;
if (lo >= hi) then Break;
end
;
if (lo >= hi) then
begin
// We're done separating the items.
lo := hi;
arr[hi] := med_value;
Break;
end
;
// Swap the lo and hi values.
arr[hi] := arr[lo];
end
; // while (True) do
// Sort the two sublists.
sort( arr, min, lo - 1 );
sort( arr, lo + 1, max );
end
;
begin
if( 1 < length( arr ) ) then
begin
// FIX ME: See the big comment in the int quicksort function below.
dbcout( 'In quickSort (double): ' + usFloatToStr( arr[0] ), true );
sort( arr, 0, length( arr ) );
end
;
sort( arr, 0, length( arr ) );
end
;
{*
Sorts the integers in arr, ascending order
@param arr An array of integers
}
procedure quickSort( arr: TARIntArray );
/// Inline function that uses 0 and the length of array for min and max
procedure sort( arr: TARIntArray; min, max: integer );
var
med_value: integer;
hi, lo, i: integer;
begin
// If the list has <= 1 element, it's sorted.
if (min >= max) then Exit;
// Pick a dividing item randomly.
i := min + Trunc(Random(max - min + 1));
med_value := arr[i];
// Swap it to the front so we can find it easily.
arr[i] := arr[min];
// Move the items smaller than this into the left
// half of the list. Move the others into the right.
lo := min;
hi := max;
while( true ) do
begin
// Look down from hi for a value < med_value.
while (arr[hi] >= med_value) do
begin
hi := hi - 1;
if (hi <= lo) then Break;
end
;
if (hi <= lo) then
begin
// We're done separating the items.
arr[lo] := med_value;
Break;
end
;
// Swap the lo and hi values.
arr[lo] := arr[hi];
// Look up from lo for a value >= med_value.
lo := lo + 1;
while (arr[lo] < med_value) do
begin
lo := lo + 1;
if (lo >= hi) then Break;
end
;
if (lo >= hi) then
begin
// We're done separating the items.
lo := hi;
arr[hi] := med_value;
Break;
end
;
// Swap the lo and hi values.
arr[hi] := arr[lo];
end
; // while (True) do
// Sort the two sublists.
sort( arr, min, lo - 1 );
sort( arr, lo + 1, max );
end
;
begin
if( 1 < length( arr ) ) then
begin
// FIX ME:
// For reasons that are unknown and evidently quite bizarre, quickSort
// occasionally screws things up if the array hasn't been used.
// In the LibAPHI demo with default parameters and 500 iterations,
// a 0 appears from nowhere in the model output upon array sorting.
//
// This debugging statement seems to 'solve' the problem.
// The problem warrants further investigation.
dbcout( 'In quickSort (int): ' + intToStr( arr[0] ), true );
sort( arr, 0, length( arr ) );
end
;
end
;
{*
Does the string array contain the indicated string?
@param arr A string array
@param item The string to search for
@param caseSensitive is the search case-sensitive?
@return True if item is in arr, else false
}
function arrayContains( const arr: TARStringArray; const item: string; const caseSensitive: boolean = false ): boolean;
var
i: integer;
testItem, testArr: string;
begin
if( caseSensitive ) then
testItem := item
else
testItem := ansiLowerCase( item )
;
result := false; // Until shown otherwise
for i := 0 to length( arr ) - 1 do
begin
if( caseSensitive ) then
testArr := arr[i]
else
testArr := ansiLowerCase( arr[i] )
;
if( testItem = testArr ) then
begin
result := true;
break;
end
;
end
;
end
;
{*
Does the array of strings contain the indicated string?
@param arr An array of strings
@param item The string to search for
@param caseSensitive is the search case-sensitive?
@return True if item is in arr, else false
}
function arrayContains( const arr: array of string; const item: string; const caseSensitive: boolean = false ): boolean;
var
i: integer;
testItem, testArr: string;
begin
if( caseSensitive ) then
testItem := item
else
testItem := ansiLowerCase( item )
;
result := false; // Until shown otherwise
for i := 0 to length( arr ) - 1 do
begin
if( caseSensitive ) then
testArr := arr[i]
else
testArr := ansiLowerCase( arr[i] )
;
if( testItem = testArr ) then
begin
result := true;
break;
end
;
end
;
end
;
//-----------------------------------------------------------------------------
// Debugging
//-----------------------------------------------------------------------------
{*
Displays debugging output for the specified array
@param arr An array of doubles
}
procedure arDoubleArrayDebug( arr: TARDoubleArray );
var
i: integer;
begin
dbcout( endl + '---arDoubleArrayDebug:', true );
for i := 0 to length( arr ) - 1 do
dbcout( arr[i], true )
;
dbcout( '---Done.', true );
end
;
//-----------------------------------------------------------------------------
end.