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;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:
- A function of type TEqualityComparison<T>.
- An object that supports the IEqualityComparer<T> interface.
If the parameter is omitted then the default equality comparer defined by Delphi's TEqualityComparer<T>.Default method is used.
Returns:
Trueif ALeft and ARight are the same orFalseif not.
-
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.
-
Two empty arrays are considered equal.
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;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;