Header menu logo 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
namespace System
namespace System.Collections
namespace System.Collections.Generic
val modIdentity: ((obj -> obj -> bool) -> IEqualityComparer<obj>)
module HashIdentity from Microsoft.FSharp.Collections
val FromFunctions: hasher: ('T -> int) -> equality: ('T -> 'T -> bool) -> IEqualityComparer<'T>
val i: 'a (requires member (%))
val i1: 'a (requires member (%) and equality and member (%))
val i2: 'a (requires member (%) and equality and member (%))
val dict: Dictionary<int,int>
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>


--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
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 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)
namespace System
namespace System.Collections
namespace System.Collections.Generic
Multiple items
union case Tree.Tree: int * Tree list -> Tree

--------------------
type Tree = | Tree of int * Tree list
type Tree = | Tree of int * Tree list
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type 'T list = List<'T>
val dict: Dictionary<Tree,int>
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>


--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
module HashIdentity from Microsoft.FSharp.Collections
val LimitedStructural: limit: int -> IEqualityComparer<'T> (requires equality)
val tree1: Tree
val tree2: Tree
Dictionary.Add(key: Tree, value: int) : unit

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)
namespace System
namespace System.Collections
namespace System.Collections.Generic
val dict: Dictionary<System.DateTime,int>
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>


--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Multiple items
[<Struct>] type DateTime = new: date: DateOnly * time: TimeOnly -> unit + 16 overloads member Add: value: TimeSpan -> DateTime member AddDays: value: float -> DateTime member AddHours: value: float -> DateTime member AddMicroseconds: value: float -> DateTime member AddMilliseconds: value: float -> DateTime member AddMinutes: value: float -> DateTime member AddMonths: months: int -> DateTime member AddSeconds: value: float -> DateTime member AddTicks: value: int64 -> DateTime ...
<summary>Represents an instant in time, typically expressed as a date and time of day.</summary>

--------------------
System.DateTime ()
   (+0 other overloads)
System.DateTime(ticks: int64) : System.DateTime
   (+0 other overloads)
System.DateTime(date: System.DateOnly, time: System.TimeOnly) : System.DateTime
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(date: System.DateOnly, time: System.TimeOnly, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : System.DateTime
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : System.DateTime
   (+0 other overloads)
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
module HashIdentity from Microsoft.FSharp.Collections
val NonStructural<'T (requires equality and member (=))> : IEqualityComparer<'T> (requires equality and member (=))
Dictionary.Add(key: System.DateTime, value: int) : unit
property System.DateTime.Now: System.DateTime with get
<summary>Gets a <see cref="T:System.DateTime" /> object that is set to the current date and time on this computer, expressed as the local time.</summary>
<returns>An object whose value is the current local date and time.</returns>

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 array,int>(HashIdentity.Structural)

 let arr1 = [| 1;2;3 |]
 let arr2 = [| 1;2;3 |]
 dict.Add(arr1, 6)
 dict.Add(arr2, 7)
namespace System
namespace System.Collections
namespace System.Collections.Generic
val dict: Dictionary<int array,int>
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>


--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type 'T array = 'T array
module HashIdentity from Microsoft.FSharp.Collections
val Structural<'T (requires equality)> : IEqualityComparer<'T> (requires equality)
val arr1: int array
val arr2: int array
Dictionary.Add(key: int array, value: int) : unit
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 array,int>(HashIdentity.Structural)

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

 dict.[arr1] <- 6
 dict.[arr2] >- 7
namespace System
namespace System.Collections
namespace System.Collections.Generic
val dict: Dictionary<int array,int>
Multiple items
type Dictionary<'TKey,'TValue> = interface ICollection<KeyValuePair<'TKey,'TValue>> interface IEnumerable<KeyValuePair<'TKey,'TValue>> interface IEnumerable interface IDictionary<'TKey,'TValue> interface IReadOnlyCollection<KeyValuePair<'TKey,'TValue>> interface IReadOnlyDictionary<'TKey,'TValue> interface ICollection interface IDictionary interface IDeserializationCallback interface ISerializable ...
<summary>Represents a collection of keys and values.</summary>
<typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
<typeparam name="TValue">The type of the values in the dictionary.</typeparam>


--------------------
Dictionary() : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>) : Dictionary<'TKey,'TValue>
Dictionary(comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int) : Dictionary<'TKey,'TValue>
Dictionary(dictionary: IDictionary<'TKey,'TValue>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(collection: IEnumerable<KeyValuePair<'TKey,'TValue>>, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Dictionary(capacity: int, comparer: IEqualityComparer<'TKey>) : Dictionary<'TKey,'TValue>
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
type 'T array = 'T array
module HashIdentity from Microsoft.FSharp.Collections
val Structural<'T (requires equality)> : IEqualityComparer<'T> (requires equality)
val arr1: int array
val arr2: int array
In this example, only one entry is added to the dictionary, as the arrays identical by structural equality.

Type something to start searching.