Browse Source

+ introduce explicit implementations for the IComparer<>, IEqualityComparer<> and IExtendedEqualityComparer<> interfaces for types that should be compared using binary comparison (e.g. records, objects, etc)

Sven/Sarah Barth 2 years ago
parent
commit
2a8665ef94
1 changed files with 65 additions and 0 deletions
  1. 65 0
      packages/rtl-generics/src/generics.defaults.pas

+ 65 - 0
packages/rtl-generics/src/generics.defaults.pas

@@ -860,6 +860,28 @@ type
       const AExtendedHasher: TExtendedHasherFunc<T>); overload;
       const AExtendedHasher: TExtendedHasherFunc<T>); overload;
   end;
   end;
 
 
+  TBinaryComparer<T> = class(TInterfacedObject, IComparer<T>)
+  public
+    function Compare(const ALeft, ARight: T): Integer;
+  end;
+
+  TBinaryEqualityComparer<T> = class(TInterfacedObject, IEqualityComparer<T>)
+  private
+    FHashFactory: THashFactoryClass;
+  public
+    constructor Create(AHashFactoryClass: THashFactoryClass);
+    function Equals(const ALeft, ARight: T): Boolean;
+    function GetHashCode(const AValue: T): UInt32;
+  end;
+
+  TBinaryExtendedEqualityComparer<T> = class(TBinaryEqualityComparer<T>, IExtendedEqualityComparer<T>)
+  private
+    FExtendedHashFactory: TExtendedHashFactoryClass;
+  public
+    constructor Create(AHashFactoryClass: TExtendedHashFactoryClass);
+    procedure GetHashList(const AValue: T; AHashList: PUInt32);
+  end;
+
   { TDelphiHashFactory }
   { TDelphiHashFactory }
 
 
   TDelphiHashFactory = class(THashFactory)
   TDelphiHashFactory = class(THashFactory)
@@ -2794,6 +2816,49 @@ begin
   Result := TDelegatedExtendedEqualityComparerFunc<T>.Create(AEqualityComparison, AExtendedHasher);
   Result := TDelegatedExtendedEqualityComparerFunc<T>.Create(AEqualityComparison, AExtendedHasher);
 end;
 end;
 
 
+{ TBinaryComparer<T> }
+
+function TBinaryComparer<T>.Compare(const ALeft, ARight: T): Integer;
+begin
+  Result := BinaryCompare(@ALeft, @ARight, SizeOf(T));
+end;
+
+{ TBinaryEqualityComparer<T> }
+
+constructor TBinaryEqualityComparer<T>.Create(AHashFactoryClass: THashFactoryClass);
+begin
+  if not Assigned(AHashFactoryClass) then
+    FHashFactory := TDefaultHashFactory
+  else
+    FHashFactory := AHashFactoryClass;
+end;
+
+function TBinaryEqualityComparer<T>.Equals(const ALeft, ARight: T): Boolean;
+begin
+  Result := CompareMem(@ALeft, @ARight, SizeOf(T));
+end;
+
+function TBinaryEqualityComparer<T>.GetHashCode(const AValue: T): UInt32;
+begin
+  Result := FHashFactory.GetHashCode(@AValue, SizeOf(T), 0);
+end;
+
+{ TBinaryExtendedEqualityComparer<T> }
+
+constructor TBinaryExtendedEqualityComparer<T>.Create(AHashFactoryClass: TExtendedHashFactoryClass);
+begin
+  if not Assigned(AHashFactoryClass) then
+    FExtendedHashFactory := TDelphiDoubleHashFactory
+  else
+    FExtendedHashFactory := AHashFactoryClass;
+  inherited Create(FExtendedHashFactory);
+end;
+
+procedure TBinaryExtendedEqualityComparer<T>.GetHashList(const AValue: T; AHashList: PUInt32);
+begin
+  FExtendedHashFactory.GetHashList(@AValue, SizeOf(T), AHashList, []);
+end;
+
 { TDelphiHashFactory }
 { TDelphiHashFactory }
 
 
 class function TDelphiHashFactory.GetHashService: THashServiceClass;
 class function TDelphiHashFactory.GetHashService: THashServiceClass;