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.

Equal<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

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

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

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

Description

Checks if two arrays, both with elements of of the same type, have the same contents, in the same order.

Parameters:

Returns:

Notes

  1. Two non-empty arrays are considered equal if, and only if, both arrays have the same length and the elements at the same index in each array are equal.

  2. Two empty arrays are considered equal.

Examples

Example #1

Using a comparer function:

procedure Equal_Eg1;
var
  A, B, C, D, E1, E2: TArray<Integer>;
  EqComparerFn: TEqualityComparison<Integer>;
begin
  A := TArray<Integer>.Create(1, 2, 3);
  B := TArray<Integer>.Create(1, 2, 3);
  C := TArray<Integer>.Create(1, 2);
  D := TArray<Integer>.Create(1, 5, 7);
  E1 := TArray<Integer>.Create();        // empty
  E2 := TArray<Integer>.Create();        // empty
  EqComparerFn := function(const Left, Right: Integer): Boolean
    begin
      Result := Left = Right;
    end;
  Assert(TArrayUtils.Equal<Integer>(A, B, EqComparerFn) = True);
  Assert(TArrayUtils.Equal<Integer>(A, C, EqComparerFn) = False);
  Assert(TArrayUtils.Equal<Integer>(A, D, EqComparerFn) = False);
  Assert(TArrayUtils.Equal<Integer>(A, E1, EqComparerFn) = False);
  Assert(TArrayUtils.Equal<Integer>(E1, E2, EqComparerFn) = True);
end;

Example #2

Using a comparer object:

procedure Equal_Eg2;
var
  A, B, C, D, E1, E2: TArray<string>;
  EqComparerObj: IEqualityComparer<string>;
begin
  A := TArray<string>.Create('a', 'b', 'c');
  B := TArray<string>.Create('a', 'b', 'c');
  C := TArray<string>.Create('a', 'b');
  D := TArray<string>.Create('a', 'd', 'f');
  E1 := TArray<string>.Create();        // empty
  E2 := TArray<string>.Create();        // empty
  EqComparerObj := TDelegatedEqualityComparer<string>.Create(
    SameStr,
    function (const Value: string): Integer
    begin
      // This is only safe because the hash function isn't called by Equal
      Result := 0;
    end
  );
  Assert(TArrayUtils.Equal<string>(A, B, EqComparerObj) = True);
  Assert(TArrayUtils.Equal<string>(A, C, EqComparerObj) = False);
  Assert(TArrayUtils.Equal<string>(A, D, EqComparerObj) = False);
  Assert(TArrayUtils.Equal<string>(A, E1, EqComparerObj) = False);
  Assert(TArrayUtils.Equal<string>(E1, E2, EqComparerObj) = True);
end;

See Also