Table of Contents

Class StructuralEqualityComparerProvider

Namespace
ShapeShift.Equality
Assembly
ShapeShift.dll

Creates deep, structural IEqualityComparer<T> implementations from PolyType shapes.

public sealed record StructuralEqualityComparerProvider : IEquatable<StructuralEqualityComparerProvider>
Inheritance
StructuralEqualityComparerProvider
Implements
Inherited Members

Remarks

This type is an immutable record. Each mutation returns a new instance, allowing an instance to be safely shared and cached. Comparers are memoized per instance, so reusing an instance avoids rebuilding the comparer graph.

See the structural equality documentation for the precise semantics that the generated comparers implement.

Constructors

StructuralEqualityComparerProvider()

Initializes a new instance of the StructuralEqualityComparerProvider class.

public StructuralEqualityComparerProvider()

Properties

CollisionResistant

Gets a provider that produces comparers with process-randomized, collision resistant hash codes.

public static StructuralEqualityComparerProvider CollisionResistant { get; }

Property Value

StructuralEqualityComparerProvider

Remarks

Hash codes produced by comparers from this provider must never be persisted or transmitted, as they vary from one process to the next.

Default

Gets a provider that produces comparers with deterministic (non-randomized) hash codes.

public static StructuralEqualityComparerProvider Default { get; }

Property Value

StructuralEqualityComparerProvider

UseCollisionResistantHashing

Gets a value indicating whether the comparers hash the full content of values using a process-randomized, keyed hash function.

public bool UseCollisionResistantHashing { get; init; }

Property Value

bool

The default value is false.

Remarks

When false (the default), hash codes are computed by combining the hash codes that the leaf values themselves report, except for string and byte arrays, which are hashed with a deterministic content hash so that the result is stable across processes. Such hash codes are cheap but offer no protection against an adversary who chooses inputs that deliberately collide.

When true, every well-known leaf value is hashed by content with SipHash-2-4 under a key that is randomly generated once per process. This dramatically raises the cost of finding collisions, at the cost of speed and of any cross-process stability.

Methods

GetComparer<T>()

Gets a structural comparer for a self-describing type.

public IEqualityComparer<T> GetComparer<T>() where T : IShapeable<T>

Returns

IEqualityComparer<T>

A structural comparer.

Type Parameters

T

The type to compare.

GetComparer<T>(ITypeShapeProvider)

Gets a structural comparer for a type with a shape provided by another type.

public IEqualityComparer<T> GetComparer<T>(ITypeShapeProvider provider)

Parameters

provider ITypeShapeProvider

The shape provider.

Returns

IEqualityComparer<T>

A structural comparer.

Type Parameters

T

The type to compare.

GetComparer<T>(ITypeShape<T>)

Gets a structural comparer for a type with a known shape.

public IEqualityComparer<T> GetComparer<T>(ITypeShape<T> shape)

Parameters

shape ITypeShape<T>

The shape of the type to compare.

Returns

IEqualityComparer<T>

A structural comparer.

Type Parameters

T

The type to compare.

GetComparer<T, TProvider>()

Gets a structural comparer for a type whose shape is provided by a witness type.

public IEqualityComparer<T> GetComparer<T, TProvider>() where TProvider : IShapeable<T>

Returns

IEqualityComparer<T>

A structural comparer.

Type Parameters

T

The type to compare.

TProvider

The witness type that describes T.

WithComparer<T>(IEqualityComparer<T>)

Returns a provider that uses a caller supplied comparer for a particular type instead of comparing values of that type structurally.

public StructuralEqualityComparerProvider WithComparer<T>(IEqualityComparer<T> comparer)

Parameters

comparer IEqualityComparer<T>

The comparer to use. Its GetHashCode(T) must be consistent with its Equals(T, T) method.

Returns

StructuralEqualityComparerProvider

A new provider with the override applied.

Type Parameters

T

The type whose comparison should be delegated to comparer.

Remarks

The override applies wherever a value of type T appears in a graph, including as an element of a collection, a dictionary key, or a member of an object.