Skip to content

Latest commit

 

History

History
108 lines (78 loc) · 3.09 KB

File metadata and controls

108 lines (78 loc) · 3.09 KB

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:

  • ALeft - The first array to be compared.

  • ARight - The second array to be compared.

  • ACount - The number of bytes to be compared. Must be a positive number.

  • 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 begin with the same ACount elements or False if not.

Precondition:

  • ACount must be ≥ 1. An EAssertionFailed exception is raised otherwise.

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