Welcome to the new DelphiDabbler Code Library Documentation.

This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.

Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.

Chop<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function Chop<T>(var A: TArray<T>;
  AStartIndex, AEndIndex: Integer): TArray<T>;
  overload; static;

class function Chop<T>(var A: TArray<T>; AStartIndex: Integer): TArray<T>;
  overload; static;

Description

Removes a contiguous range of elements from an array and returns an array containing the removed elements.

The elements of the returned array are shallow copies of the corresponding elements of the original array, so any references within the resulting array are the same as those in the initial array.

The original array is updated in place.

Parameters:

Returns:

Note

If the returned array needs to have deep copies of the deleted array elements then pass the return value of Chop<T> to the deep copying overload of Copy<T>, along with a suitable TCloner<T> implementation.

Examples

Example #1

This example shows the most flexible use of Chop<T> where both the start and end indexes are provided:

procedure Chop_Eg1;
var
  A, ATest, ASlice, AExpectedSlice, AExpectedRemainder: TArray<string>;
begin
  A := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
  // slice from the start of A
  ATest := Copy(A);
  ASlice := TArrayUtils.Chop<string>(ATest, 0, 2);
  AExpectedRemainder := TArray<string>.Create('time', 'saves', 'nine');
  AExpectedSlice := TArray<string>.Create('a', 'stitch', 'in');
  Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
  Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
  // Chop in the middle of ATest
  ATest := Copy(A);
  ASlice := TArrayUtils.Chop<string>(ATest, 2, 4);
  AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'nine');
  AExpectedSlice := TArray<string>.Create('in', 'time', 'saves');
  Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
  Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
  // Chop at the end of ATest
  ATest := Copy(A);
  ASlice := TArrayUtils.Chop<string>(ATest, 4, 5);
  AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'in', 'time');
  AExpectedSlice := TArray<string>.Create('saves', 'nine');
  Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
  Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
end;

Example #2

This second example show the use of the basic version of Chop<T> where only the start index is provided:

procedure Chop_Eg2;
var
  A, ATest, ASlice, AExpectedSlice, AExpectedRemainder: TArray<string>;
begin
  A := TArray<string>.Create('a', 'stitch', 'in', 'time', 'saves', 'nine');
  // slice from mid to end of A
  ATest := Copy(A);
  ASlice := TArrayUtils.Chop<string>(ATest, 3);
  AExpectedRemainder := TArray<string>.Create('a', 'stitch', 'in');
  AExpectedSlice := TArray<string>.Create('time', 'saves', 'nine');
  Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
  Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
  // Chop of all of ATest
  ATest := Copy(A);
  ASlice := TArrayUtils.Chop<string>(ATest, 0);
  AExpectedRemainder := TArray<string>.Create();
  AExpectedSlice := Copy(A);
  Assert(TArrayUtils.Equal<string>(AExpectedRemainder, ATest, SameStr));
  Assert(TArrayUtils.Equal<string>(AExpectedSlice, ASlice, SameStr));
end;

See Also