用于二维数组的IEqualityComparer实例

时间:2012-10-31 00:18:33

标签: dictionary f# hashset iequalitycomparer structural-equality

F#支持使用=运算符和F#集合(例如Set)的二维数组的结构相等性。但是如何在.NET类HashSet中使用相同的相等比较?默认情况下,它使用引用相等性,虽然有一个构造函数接受IEqualityComparer<T>的实例,但我找不到适合二维数组的内置实例。

我看了System.Collections.StructuralComparisons.StructuralEqualityComparer,但这似乎有两个问题。首先,它不是通用的,其次,它似乎不支持二维数组:

> let xss = Array2D.create 2 2 99;;
> let yss = Array2D.create 2 2 99;;

// `=` operator does what I want
> xss = yss;;
val it : bool = true

// pre-defined StructuralEqualityComparer object doesn't work
> open System.Collections;;
> let comp = StructuralComparisons.StructuralEqualityComparer;;
val comp : IEqualityComparer
> (xss :> IStructuralEquatable).Equals(yss, comp);;
System.ArgumentException: Array was not a one-dimensional array.
   at System.Array.GetValue(Int32 index)
   at System.Array.System.Collections.IStructuralEquatable.Equals(Object other, IEqualityComparer comparer)
   at <StartupCode$FSI_0023>.$FSI_0023.main@()

最终,我想修复以下代码,使其返回1,而不是2:

> let hashset = new Generic.HashSet<int[,]>();;
> hashset.Add xss;;
> hashset.Add yss;;
> hashset.Count;;
val it : int = 2

我对使用Dictionary的解决方案感到满意,但我认为同样的问题也适用。

1 个答案:

答案 0 :(得分:2)

let a = Array2D.create 2 2 99
let b = Array2D.create 2 2 99
let set = System.Collections.Generic.HashSet(HashIdentity.Structural)
set.Add a
set.Add b
printfn "%A" set.Count // 1

<子> Online demo