But they are tedious to use even with the additional syntax support in Delphi XE7. There are other implementations that improve dynamic array usage that work even for older versions.
Spring4D will introduce the new DynamicArray<T> type which is a record type with several methods and operator overloads (take a look at it in our develop branch on Bitbucket where we are busy implementing a lot of awesome things for Spring4D 1.2 release this spring).
Let's take a quick look at some of the code you can write using the new DynamicArray<T>.
var
arr, arr2: DynamicArray<Integer>;
i: Integer;
begin
{$IFDEF DELPHIXE7_UP}
arr := [1, 2, 3];
arr := arr + arr;
arr := arr + [4, 5];
{$ELSE}
arr.Assign([1, 2, 3]);
arr.Add(arr);
arr.Add([4, 5]);
{$ENDIF}
for i in arr do
Write(i, ' '); // 1 2 3 1 2 3 4 5
Writeln;
arr2.Assign([2, 4]);
if arr2 in arr then
begin
arr := arr - arr2;
for i in arr do
Write(i, ' '); // 1 3 1 2 3 5
Writeln;
end;
arr.Assign([1, 2, 3, 4, 5, 6]);
arr2 := arr.Splice(2, 2, [7, 8, 9]);
for i in arr2 do
Write(i, ' '); // 3 4
Writeln;
for i in arr do
Write(i, ' '); // 1 2 7 8 9 5 6
Writeln;
It will be available in all versions supported (Delphi 2010 and higher) - only the new syntax to initialize a dynamic array (with the square brackets) is limited to XE7 and higher. But DynamicArray<T> has the add operator and even some more that you don't have with native XE7 dynamic arrays. All methods are continuously optimized for performance as far as possible using pure pascal. DynamicArray<T> does not perform any worse than using the native operations - in some situations even better. Of course it is assignment compatible to TArray<T> by implicit operator overload and will run on all platforms.
Until next time with more about Spring4D 1.2 or something different but not any less interesting!
Edit (01.02.2015): renamed TDynArray to DynamicArray<T>
No comments:
Post a Comment