Skip to content

Latest commit

 

History

History
118 lines (88 loc) · 3.63 KB

File metadata and controls

118 lines (88 loc) · 3.63 KB

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:

  • ALeft - The first array to be compared.

  • ARight - The second array to be compared.

  • AEqualityComparer - An optional function or object that is used to test the equality of two values. Used to check equality of array elements.

    If AEqualityComparer is provided it must be one of:

    If the parameter is omitted then the default equality comparer defined by Delphi's TEqualityComparer<T>.Default method is used.

Returns:

  • True if ALeft and ARight are the same or False if not.

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