-
Notifications
You must be signed in to change notification settings - Fork 5
/
ArrayVal.pas
94 lines (77 loc) · 2.48 KB
/
ArrayVal.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
unit ArrayVal;
////////////////////////////////////////////////////////////////////////////////
//
// Author: Jaap Baak
// https://github.com/transportmodelling/Utils
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
interface
////////////////////////////////////////////////////////////////////////////////
Uses
SysUtils;
Type
TArrayView<T> = record
// Record that provides access to the array values, but prevents the array from being changed.
private
FValues: TArray<T>;
Function GetValues(Index: Integer): T; inline;
public
Constructor Create(Values: TArray<T>);
Function Length: Integer; inline;
public
Property Values[Index: Integer]: T read GetValues; default;
end;
TArrayValues<T> = record
// Record that provides access to the array values, but prevents the array from being re-allocated.
private
FValues: TArray<T>;
Function GetValues(Index: Integer): T; inline;
Procedure SetValues(Index: Integer; Value: T); inline;
public
Constructor Create(Values: TArray<T>);
Function Length: Integer; inline;
public
Property Values[Index: Integer]: T read GetValues write SetValues; default;
end;
TStringArrayView = TArrayView<String>;
TIntArrayView = TArrayView<Integer>;
TFloat64ArrayView = TArrayView<Float64>;
TFloat32ArrayView = TArrayView<Float32>;
TStringArrayValues = TArrayValues<String>;
TIntArrayValues = TArrayValues<Integer>;
TFloat64ArrayValues = TArrayValues<Float64>;
TFloat32ArrayValues = TArrayValues<Float32>;
////////////////////////////////////////////////////////////////////////////////
implementation
////////////////////////////////////////////////////////////////////////////////
Constructor TArrayView<T>.Create(Values: TArray<T>);
begin
FValues := Values;
end;
Function TArrayView<T>.GetValues(Index: Integer): T;
begin
Result := FValues[Index];
end;
Function TArrayView<T>.Length: Integer;
begin
Result := System.Length(FValues);
end;
////////////////////////////////////////////////////////////////////////////////
Constructor TArrayValues<T>.Create(Values: TArray<T>);
begin
FValues := Values;
end;
Function TArrayValues<T>.GetValues(Index: Integer): T;
begin
Result := FValues[Index];
end;
Procedure TArrayValues<T>.SetValues(Index: Integer; Value: T);
begin
FValues[Index] := Value;
end;
Function TArrayValues<T>.Length: Integer;
begin
Result := System.Length(FValues);
end;
end.