在Julia中添加到集合时重载对象比较? [英] Overload object comparison when adding to a set in Julia?

查看:95
本文介绍了在Julia中添加到集合时重载对象比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以重载Base.Set如何在Julia中进行对象比较?

Is there a way to overload how Base.Set does its object comparisons in Julia?

我尝试重载isequal==,但是当我的对象应该相同时,它们仍被标记为不同.

I tried overloading isequal and ==, but my objects are still marked as different when they should be the same.

例如

type Test
    x
    y
end

function ==(a::Test, b::Test)
    return a.x == b.x && a.y == b.y
end

Set([Test(2,3), Test(2,3)])

给予

Set([Test(2,3),Test(2,3)])

推荐答案

Set是一种Dict,其值的类型为Void:

A Set is a kind of Dict with values of type Void: ref

type Set{T} <: AbstractSet{T}
    dict::Dict{T,Void}

    Set() = new(Dict{T,Void}())
    Set(itr) = union!(new(Dict{T,Void}()), itr)
end  

Julia-lang文档描述了Dic键入如下:

Dict是标准的关联集合.它的实现用途 hash()作为密钥的哈希函数,而isequal()确定 平等.为自定义类型定义这两个函数,以覆盖如何 它们存储在哈希表中.

Dict is the standard associative collection. Its implementation uses hash() as the hashing function for the key, and isequal() to determine equality. Define these two functions for custom types to override how they are stored in a hash table.

检查, dict.jl ,然后查找ht_keyindex2()ht_keyindex()功能.仅当这两个条件为true时,两者都将返回index:

Check, dict.jl, and look for ht_keyindex2() and ht_keyindex() functions. Both will return an index only if these two conditions are true:

if !isslotmissing(h,index) && isequal(key,keys[index])
    return index
end

上面的位置:index = hashindex(key, sz)
朱莉娅使用hash()函数执行哈希任务:

Where in the above: index = hashindex(key, sz)
Julia uses hash() function do the task of hashing:

hash(x[, h])
计算整数哈希码,使等值(x,y) 意味着hash(x)== hash(y).可选的第二个参数h是哈希 代码与结果混合.新类型应实施 2参数形式,通常通过调用2参数哈希方法 递归地将内容的哈希值相互混合 (并带有h).通常,任何实现哈希的类型也应 实现自己的==(因此等于)以保证该属性 上面提到过.

hash(x[, h])
Compute an integer hash code such that isequal(x,y) implies hash(x)==hash(y). The optional second argument h is a hash code to be mixed with the result. New types should implement the 2-argument form, typically by calling the 2-argument hash method recursively in order to mix hashes of the contents with each other (and with h). Typically, any type that implements hash should also implement its own == (hence isequal) to guarantee the property mentioned above.

因此,通过这些准备工作,很明显,覆盖Base.==不是正确的&完成任务的完整方法,但是

So with these preparations it is obvious that overriding Base.==, is not the right & complete way to do the task, but

  1. 我们需要重写hash()函数以返回相同的hashindex
  2. 代替Base.==足以覆盖Base.isequal
  1. We need to override hash() function to return the same hashindex and
  2. Instead of Base.== it is sufficient to override Base.isequal

代码:

type Test
    x
    y
end

Base.hash(a::Test, h::UInt) = hash(a.y, hash(a.x, hash(:Test, h)))
Base.isequal(a::Test, b::Test) = Base.isequal(hash(a), hash(b))
Set([Test(2,3), Test(2,3)])

问题是,尽管Test(2,3) == Test(2,3) #=> false它可以按预期工作.

The point is, although Test(2,3) == Test(2,3) #=> false it works as expected.

这篇关于在Julia中添加到集合时重载对象比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆