Skip to content

Latest commit

 

History

History
69 lines (46 loc) · 1.73 KB

File metadata and controls

69 lines (46 loc) · 1.73 KB

Shift<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function Shift<T>(var A: TArray<T>): T;
  static;

Description

Removes the first element from a non-empty array.

The length of the array is reduced by one.

Parameters:

  • A - Array from which the element is to be removed. The array is updated in place when the element is deleted.

Returns:

  • The element that was removed from A.

Preconditions:

  • A must not be empty. An EAssertionFailed exception is raised otherwise.

Example

This example shifts elements from an array until the array is empty.

procedure Shift_Eg;
var
  A, Expected: TArray<string>;
begin
  A := TArray<string>.Create('a', 'stitch', 'in', 'time');

  Expected := TArray<string>.Create('stitch', 'in', 'time');
  Assert(TArrayUtils.Shift<string>(A) = 'a');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create('in', 'time');
  Assert(TArrayUtils.Shift<string>(A) = 'stitch');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create('time');
  Assert(TArrayUtils.Shift<string>(A) = 'in');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create();
  Assert(TArrayUtils.Shift<string>(A) = 'time');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));
end;

See Also