-
Notifications
You must be signed in to change notification settings - Fork 2
/
uUtil.pas
471 lines (401 loc) · 10.3 KB
/
uUtil.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
unit uUtil;
interface
uses
System.Generics.Collections;
type
TPermutationItems = TArray<Integer>;
TPermutations = class(TEnumerable<TPermutationItems>)
public
FItemCount: Integer;
FStartIndex: Integer;
FCount: Integer;
protected
function DoGetEnumerator: TEnumerator<TPermutationItems>; reintroduce;
public
constructor Create(const AItemCount: Integer; const AStartIndex: Integer = 0);
type
TEnumerator = class(TEnumerator<TPermutationItems>)
private
FParent: TPermutations;
FCurrent: TPermutationItems;
FInitial: TPermutationItems;
FCurrentIndex: Integer;
function GetCurrent: TPermutationItems;
protected
function DoGetCurrent: TPermutationItems; override;
function DoMoveNext: Boolean; override;
public
constructor Create(const AParent: TPermutations);
property Current: TPermutationItems read GetCurrent;
function MoveNext: Boolean;
end;
function GetEnumerator: TEnumerator; reintroduce; inline;
end;
TSubSequences<T> = class(TEnumerable<TArray<T>>)
private
FSequence: TArray<T>;
FLength: Integer;
FMinLength, FMaxLength: Integer;
protected
function DoGetEnumerator: TEnumerator<TArray<T>>; reintroduce;
public
constructor Create(const ASequence: TArray<T>; const AMinLength: Integer = -1; const AMaxLength: Integer = -1);
type
TMoveNextResult = ( mnrOk, mnrWrongSize, mnrEof );
TEnumerator = class(TEnumerator<TArray<T>>)
private
FParent: TSubSequences<T>;
FCurrent: TArray<T>;
FIndex: Integer;
FCount: Integer;
function GetCurrent: TArray<T>;
function TryMoveNext: TMoveNextResult;
function CheckSubsequenceSize(const Index: Integer): Boolean;
protected
function DoGetCurrent: TArray<T>; override;
function DoMoveNext: Boolean; override;
public
constructor Create(const AParent: TSubSequences<T>);
property Current: TArray<T> read GetCurrent;
function MoveNext: Boolean;
end;
function GetEnumerator: TEnumerator; reintroduce; inline;
end;
// Simple circular double linked list implementation
PNode = ^TNode;
TNode = record
private
constructor Create(const AData: Integer);
public
Data: Integer;
Next, Prev: PNode;
class function Pointer(const AData: Integer): PNode; static;
function Push(const Value: Integer): PNode;
function Pop: PNode;
function Rotate(const Value: Integer): PNode;
procedure Free;
function ToString: String;
end;
// Simplified implementation of Knuth-Morris-Pratt alhorithm
// Search Needle in Haystack, return array of found positions (empty array of nothibg found)
function KMP(const Needle, Haystack: String): TArray<Integer>;
// Count 1 bits in number
function BitCount(X: Integer): Integer;
procedure SortString(var S: String);
// Greatest common divisor
function GCD(A, B: Int64): Int64;
// Least common multiple
function LCM(const A, B: Int64): Int64;
// Swap A and B
procedure Swap(var A, B: Integer); overload;
procedure Swap(var A, B: Int64); overload;
// Convert a String with binary representation of a number to Integer
// e.g. '100101010'
function BinToInt(const Bin: String): Integer;
implementation
uses
System.SysUtils, System.Math;
function KMP(const Needle, Haystack: String): TArray<Integer>;
var
NL, HL: Integer;
I, J: Integer;
begin
SetLength(Result, 0);
NL := Needle.Length;
HL := Haystack.Length;
J := 1;
for I := 1 to HL do
begin
while (J > 1) and (Haystack[I] <> Needle[J]) do
Dec(J);
if Haystack[I] = Needle[J] then
Inc(J);
if J = NL + 1 then
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := I - NL + 1;
end;
end;
end;
function BitCount(X: Integer): Integer;
begin
// 01010101010101010101010101010101...
X := (X and $55555555) + ((X shr 1) and $55555555);
// 00110011001100110011001100110011...
X := (X and $33333333) + ((X shr 2) and $33333333);
// 00001111000011110000111100001111...
X := (X and $0f0f0f0f) + ((X shr 4) and $0f0f0f0f);
// 00000000111111110000000011111111...
X := (X and $00ff00ff) + ((X shr 8) and $00ff00ff);
// 00000000000000001111111111111111...
X := (X and $0000ffff) + ((X shr 16) and $0000ffff);
Result := X;
end;
procedure SortString(var S: String);
var
A: TArray<Char>;
begin
A := S.ToCharArray;
TArray.Sort<Char>(A);
S := String.Create(A);
end;
function GCD(A, B: Int64): Int64;
var
T: Int64;
begin
Result := A;
while B <> 0 do
begin
T := B;
B := Result mod B;
Result := T;
end;
end;
function LCM(const A, B: Int64): Int64;
begin
Result := (A div GCD(A, B)) * B;
end;
procedure Swap(var A, B: Integer);
var
Tmp: Integer;
begin
Tmp := A;
A := B;
B := Tmp;
end;
procedure Swap(var A, B: Int64);
var
Tmp: Int64;
begin
Tmp := A;
A := B;
B := Tmp;
end;
function BinToInt(const Bin: String): Integer;
var
I, L: Integer;
begin
Result := 0;
L := Bin.Length;
for I := 1 to L do
if Bin[I] = '1' then
Inc(Result, 1 shl (L - I));
end;
{ TPermutation.TEnumerator }
constructor TPermutations.TEnumerator.Create(const AParent: TPermutations);
var
I: Integer;
begin
FParent := AParent;
SetLength(FInitial, FParent.FItemCount);
for I := 0 to FParent.FItemCount - 1 do
FInitial[I] := FParent.FStartIndex + I;
FCurrent := Copy(FInitial, 0, FParent.FItemCount);
FCurrentIndex := 0;
end;
function TPermutations.TEnumerator.DoGetCurrent: TPermutationItems;
begin
Result := GetCurrent
end;
function TPermutations.TEnumerator.DoMoveNext: Boolean;
begin
Result := MoveNext;
end;
function TPermutations.TEnumerator.GetCurrent: TPermutationItems;
begin
Result := FCurrent;
end;
function TPermutations.TEnumerator.MoveNext: Boolean;
var
I, J, K: Integer;
begin
if FCurrentIndex >= FParent.FCount then
Exit(False);
Result := True;
Inc(FCurrentIndex);
FCurrent := Copy(FInitial, 0, FParent.FItemCount);
K := FCurrentIndex;
for I := Low(FCurrent) + 1 to High(FCurrent) + 1 do
begin
J := K mod I;
Swap(FCurrent[J], FCurrent[I - 1]);
K := K div I;
end;
end;
{ TPermutation }
constructor TPermutations.Create(const AItemCount: Integer; const AStartIndex: Integer);
var
I: Integer;
begin
FItemCount := AItemCount;
FStartIndex := AStartIndex;
FCount := 1;
for I := 2 to FItemCount do
FCount := FCount * I;
end;
function TPermutations.DoGetEnumerator: TEnumerator<TPermutationItems>;
begin
Result := GetEnumerator;
end;
function TPermutations.GetEnumerator: TEnumerator;
begin
Result := TEnumerator.Create(Self);
end;
{ TSubSequences<T>.TEnumerator }
constructor TSubSequences<T>.TEnumerator.Create(const AParent: TSubSequences<T>);
begin
FParent := AParent;
SetLength(FCurrent, 0);
FIndex := -1;
FCount := Round(Power(2, FParent.FLength)) - 1;
end;
function TSubSequences<T>.TEnumerator.DoGetCurrent: TArray<T>;
begin
Result := GetCurrent;
end;
function TSubSequences<T>.TEnumerator.DoMoveNext: Boolean;
begin
Result := MoveNext;
end;
function TSubSequences<T>.TEnumerator.GetCurrent: TArray<T>;
begin
Result := FCurrent;
end;
function TSubSequences<T>.TEnumerator.MoveNext: Boolean;
var
MoveNextResult: TMoveNextResult;
begin
repeat
MoveNextResult := TryMoveNext;
until MoveNextResult <> mnrWrongSize;
Result := MoveNextResult = mnrOk;
end;
function TSubSequences<T>.TEnumerator.CheckSubsequenceSize(const Index: Integer): Boolean;
var
Bits: Integer;
begin
Bits := BitCount(Index);
Result := (FParent.FMinLength <= Bits) and (FParent.FMaxLength >= Bits);
end;
function TSubSequences<T>.TEnumerator.TryMoveNext: TMoveNextResult;
var
Bit: Integer;
I, L: Integer;
begin
if FIndex > FCount then
Exit(mnrEof);
Inc(FIndex);
if not CheckSubsequenceSize(FIndex) then
Exit(mnrWrongSize);
Result := mnrOk;
Bit := FIndex;
I := FParent.FLength - 1;
L := 0;
SetLength(FCurrent, FParent.FLength);
while (Bit <> 0) and (I >= 0) do
begin
if (Bit and 1) = 1 then
begin
FCurrent[L] := FParent.FSequence[I];
Inc(L);
end;
Bit := Bit shr 1;
Dec(I);
end;
SetLength(FCurrent, L);
end;
{ TSubSequences<T> }
constructor TSubSequences<T>.Create(const ASequence: TArray<T>; const AMinLength: Integer = -1; const AMaxLength: Integer = -1);
begin
FSequence := ASequence;
TArray.Sort<T>(FSequence);
FLength := Length(FSequence);
FMinLength := AMinLength;
FMaxLength := AMaxLength;
if FMaxLength = -1 then
FMaxLength := MaxInt;
end;
function TSubSequences<T>.DoGetEnumerator: TEnumerator<TArray<T>>;
begin
Result := GetEnumerator;
end;
function TSubSequences<T>.GetEnumerator: TEnumerator;
begin
Result := TEnumerator.Create(Self);
end;
{ TNode }
constructor TNode.Create(const AData: Integer);
begin
Data := AData;
Prev := nil;
Next := nil;
end;
procedure TNode.Free;
var
Node, NextNode: PNode;
begin
Node := @Self;
try
while Node <> nil do
begin
NextNode := Node.Next;
if NextNode <> nil then
begin
Node.Next := nil;
Dispose(Node);
end;
Node := NextNode;
end;
except
// Yes, I know. Something gone wrong and I don't want to fix it
end;
end;
class function TNode.Pointer(const AData: Integer): PNode;
begin
New(Result);
Result^ := TNode.Create(AData);
Result.Next := Result;
Result.Prev := Result;
end;
function TNode.Pop: PNode;
begin
Result := Prev;
// Adjust links
Next.Prev := Prev;
Prev.Next := Next;
Next := nil;
Prev := nil;
Dispose(@Self);
end;
function TNode.Push(const Value: Integer): PNode;
begin
Result := TNode.Pointer(Value);
Result.Prev := @Self;
Result.Next := Next;
Next.Prev := Result;
Next := Result;
end;
function TNode.Rotate(const Value: Integer): PNode;
var
I, L: Integer;
begin
Result := @Self;
L := Abs(Value);
case Sign(Value) of
-1: for I := 1 to L do Result := Result.Prev;
1: for I := 1 to L do Result := Result.Next;
end;
end;
function TNode.ToString: String;
var
Node: PNode;
begin
Result := Data.ToString;
Node := Self.Next;
while Node <> @Self do
begin
Result := Result + ', ' + Node.Data.ToString;
Node := Node.Next;
end;
end;
end.