FSharp.Core


HashIdentity Module

Common notions of value identity implementing the IEqualityComparer interface, for constructing Dictionary objects and other collections

Functions and values

Function or value Description

FromFunctions hasher equality

Full Usage: FromFunctions hasher equality

Parameters:
    hasher : 'T -> int - A function to generate a hash code from a value.
    equality : 'T -> 'T -> bool - A function to test equality of two values.

Returns: IEqualityComparer<'T> An object implementing IEqualityComparer using the given functions.
Modifiers: inline
Type parameters: 'T

Get an implementation of equality semantics using the given functions.

hasher : 'T -> int

A function to generate a hash code from a value.

equality : 'T -> 'T -> bool

A function to test equality of two values.

Returns: IEqualityComparer<'T>

An object implementing IEqualityComparer using the given functions.

Example

Create a dictionary which uses the given functions for equality and hashing:

 open System.Collections.Generic

 let modIdentity = HashIdentity.FromFunctions((fun i -> i%5), (fun i1 i2 -> i1%5 = i2%5))
 let dict = new Dictionary<int,int>(HashIdentity.FromFunctions)

 dict.[2] <- 6
 dict.[7] <- 10
In this example, only one entry is added, as the keys 2 and 7 have the same hash and are equal according to the provided functions.

LimitedStructural limit

Full Usage: LimitedStructural limit

Parameters:
    limit : int - The limit on the number of hashing operations used.

Returns: IEqualityComparer<'T> An object implementing IEqualityComparer.
Modifiers: inline
Type parameters: 'T

Get an implementation of equality semantics semantics using limited structural equality and structural hashing.

limit : int

The limit on the number of hashing operations used.

Returns: IEqualityComparer<'T>

An object implementing IEqualityComparer.

Example

Create a dictionary which uses limited structural equality and structural hashing on the key, allowing trees as efficient keys:

 open System.Collections.Generic

 type Tree = Tree of int * Tree list

 let dict = new Dictionary<Tree,int>(HashIdentity.LimitedStructural 4)

 let tree1 = Tree(0, [])
 let tree2 = Tree(0, [tree1; tree1])
 dict.Add(tree1, 6)
 dict.Add(tree2, 7)

NonStructural

Full Usage: NonStructural

Returns: IEqualityComparer<^T> An object implementing IEqualityComparer using NonStructuralComparison.op_Equality and NonStructuralComparison.hash.
Modifiers: inline
Type parameters: ^T

Get an implementation of equality semantics using non-structural equality and non-structural hashing.

Returns: IEqualityComparer<^T>

An object implementing IEqualityComparer using NonStructuralComparison.op_Equality and NonStructuralComparison.hash.

Example

Create a dictionary which uses non-structural equality and hashing on the key:

 open System.Collections.Generic

 let dict = new Dictionary<System.DateTime,int>(HashIdentity.NonStructural)

 dict.Add(System.DateTime.Now, 1)

Get an implementation of equality semantics using reference equality and reference hashing.

Returns: IEqualityComparer<'T>

An object implementing IEqualityComparer using LanguagePrimitives.PhysicalEquality and LanguagePrimitives.PhysicalHash.

Example

Create a dictionary which uses reference equality and hashing on the key, giving each key reference identity:

 open System.Collections.Generic

 let dict = new Dictionary<int[],int>(HashIdentity.Structural)

 let arr1 = [| 1;2;3 |]
 let arr2 = [| 1;2;3 |]
 dict.Add(arr1, 6)
 dict.Add(arr2, 7)
In this example, two entries are added to the dictionary, as the arrays have different object reference identity.

Structural

Full Usage: Structural

Returns: IEqualityComparer<'T> An object implementing IEqualityComparer using Operators.op_Equality and Operators.hash.
Modifiers: inline
Type parameters: 'T

Get an implementation of equality semantics using structural equality and structural hashing.

Returns: IEqualityComparer<'T>

An object implementing IEqualityComparer using Operators.op_Equality and Operators.hash.

Example

Create a dictionary which uses structural equality and structural hashing on the key, allowing an array as a key:

 open System.Collections.Generic

 let dict = new Dictionary<int[],int>(HashIdentity.Structural)

 let arr1 = [| 1;2;3 |]
 let arr2 = [| 1;2;3 |]

 dict.[arr1] <- 6
 dict.[arr2] >- 7
In this example, only one entry is added to the dictionary, as the arrays identical by structural equality.