-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathArrayBld.pas
141 lines (120 loc) · 4.25 KB
/
ArrayBld.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
unit ArrayBld;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Type
TArrayBuilder<T> = record
private
FValues: TArray<T>;
Function GetValues(Index: Integer): T; inline;
public
Class Operator Implicit(const Values: array of T): TArrayBuilder<T>;
Class Operator Implicit(const Builder: TArrayBuilder<T>): TArray<T>;
Class Operator Add(const A: TArrayBuilder<T>; const B: array of T): TArrayBuilder<T>;
Class Operator Add(const A: TArrayBuilder<T>; const B: TArrayBuilder<T>): TArrayBuilder<T>;
Class Function Concat(const A: array of T; const B: array of T): TArray<T>; overload; static;
Class Function Concat(const A: array of T; const B: array of T; const C: array of T): TArray<T>; overload; static;
public
Constructor Create(const Values: array of T); overload;
Constructor Create(const Values: array of T; const First,count: Integer); overload;
Constructor Create(const Length: Integer; const Values: T); overload;
Constructor Create(const Length: Integer); overload;
Function Length: Integer; inline;
Procedure Append(const Values: array of T); overload;
Procedure Append(const Values: array of T; const First,Count: Integer); overload;
Procedure Clear;
public
Property AsArray: TArray<T> read FValues;
Property Values[Index: Integer]: T read GetValues; default;
end;
TIntArrayBuilder = TArrayBuilder<Integer>;
TFloatArrayBuilder = TArrayBuilder<Float64>;
TFloat32ArrayBuilder = TArrayBuilder<Float32>;
TStringArrayBuilder = TArrayBuilder<String>;
TCharArrayBuilder = TArrayBuilder<Char>;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Class Operator TArrayBuilder<T>.Implicit(const Values: array of T): TArrayBuilder<T>;
begin
Result := TArrayBuilder<T>.Create(Values);
end;
Class Operator TArrayBuilder<T>.Implicit(const Builder: TArrayBuilder<T>): TArray<T>;
begin
Result := Builder.AsArray;
end;
Class Operator TArrayBuilder<T>.Add(const A: TArrayBuilder<T>; const B: array of T): TArrayBuilder<T>;
begin
Result.Append(A.FValues);
Result.Append(B);
end;
Class Operator TArrayBuilder<T>.Add(const A: TArrayBuilder<T>; const B: TArrayBuilder<T>): TArrayBuilder<T>;
begin
Result.Append(A.FValues);
Result.Append(B.FValues);
end;
Class Function TArrayBuilder<T>.Concat(const A: array of T; const B: array of T): TArray<T>;
var
Builder: TArrayBuilder<T>;
begin
Builder.Append(A);
Builder.Append(B);
Result := Builder;
end;
Class Function TArrayBuilder<T>.Concat(const A: array of T; const B: array of T; const C: array of T): TArray<T>;
var
Builder: TArrayBuilder<T>;
begin
Builder.Append(A);
Builder.Append(B);
Builder.Append(C);
Result := Builder;
end;
Constructor TArrayBuilder<T>.Create(const Values: array of T);
begin
Finalize(FValues);
Append(Values);
end;
Constructor TArrayBuilder<T>.Create(const Values: array of T; const First,count: Integer);
begin
Finalize(FValues);
Append(Values,First,Count);
end;
Constructor TArrayBuilder<T>.Create(const Length: Integer; const Values: T);
begin
SetLength(FValues,Length);
for var Index := 0 to Length-1 do FValues[Index] := Values;
end;
Constructor TArrayBuilder<T>.Create(const Length: Integer);
begin
SetLength(FValues,Length);
end;
Function TArrayBuilder<T>.GetValues(Index: Integer): T;
begin
Result := FValues[Index];
end;
Function TArrayBuilder<T>.Length: Integer;
begin
Result := System.Length(FValues);
end;
Procedure TArrayBuilder<T>.Append(const Values: array of T);
begin
Append(Values,low(Values),System.Length(Values));
end;
Procedure TArrayBuilder<T>.Append(const Values: array of T; const First,Count: Integer);
begin
var Offset := Length;
SetLength(FValues,Offset+Count);
for var Index := 0 to Count-1 do FValues[Index+Offset] := Values[First+Index];
end;
Procedure TArrayBuilder<T>.Clear;
begin
FValues := nil;
end;
end.