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.

EqualStart<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function EqualStart<T>(const ALeft, ARight: array of T;
  const ACount: Integer; const AEqualityComparer: TEqualityComparison<T>):
  Boolean;
  overload; static;

class function EqualStart<T>(const ALeft, ARight: array of T;
  const ACount: Integer; const AEqualityComparer: IEqualityComparer<T>):
  Boolean;
  overload; static;

class function EqualStart<T>(const ALeft, ARight: array of T;
  const ACount: Integer): Boolean;
  overload; static;

Description

Checks if a given number of elements at the start two arrays, both with elements of the same type, have the same contents, in the same order.

Parameters:

Returns:

Precondition:

Examples

Example #1

Using a comparer function:

procedure EqualStart_Eg1;
var
  A, B: TArray<Integer>;
  EqComparerFn: TEqualityComparison<Integer>;
begin
  A := TArray<Integer>.Create(1, 2, 3);
  B := TArray<Integer>.Create(1, 2, 5, 6);
  EqComparerFn := function(const Left, Right: Integer): Boolean
    begin
      Result := Left = Right;
    end;
  Assert(TArrayUtils.EqualStart<Integer>(A, B, 1, EqComparerFn) = True);
  Assert(TArrayUtils.EqualStart<Integer>(A, B, 2, EqComparerFn) = True);
  Assert(TArrayUtils.EqualStart<Integer>(A, B, 3, EqComparerFn) = False);
end;

Example #2

Using a comparer object:

procedure EqualStart_Eg2;
var
  A, B: TArray<string>;
  EqComparerObj: IEqualityComparer<string>;
begin
  A := TArray<string>.Create('a', 'b', 'c');
  B := TArray<string>.Create('a', 'b');
  EqComparerObj := TDelegatedEqualityComparer<string>.Create(
    SameStr,
    function (const Value: string): Integer
    begin
      // This is only safe because the hash function isn't called by EqualStart
      Result := 0;
    end
  );
  Assert(TArrayUtils.EqualStart<string>(A, B, 2, EqComparerObj) = True);
  Assert(TArrayUtils.EqualStart<string>(A, B, 3, EqComparerObj) = False);
end;

See Also