为什么push!()会向一个Set添加重复的元素?

时间:2016-04-30 16:17:59

标签: julia

在Julia中使用复合类型的push!时,isequal函数似乎会向集合中添加重复项。阅读Julia标准文档,我假设t2函数将用于测试重复项。我想我误解了,所以也许有人可以帮助我。

作为示例,请参阅下面的代码。特别是,我想知道为什么t1被添加到集合中,尽管与t相同。

非常感谢任何帮助。注意:在我的情况下,如果字段x1x2相等,则type t x1::Float64 x2::Float64 b1::Bool b2::Bool end isequal( tx::t, ty::t) = (tx.x1 == ty.x1) && (tx.x2 == ty.x2) ==(tx::t, ty::t) = (tx.x1 == ty.x1) && (tx.x2 == ty.x2) t1 = t( 1, 2, true, true) t2 = t( 1, 2, true, true) tc = t1 tdc = deepcopy( t1) [ t1 == t2 isequal( t1, t2)] # ---> [ true true ] [ t1 == tc isequal( t1, tc)] # ---> [ true true ] [ t1 == tdc isequal( t1, tdc)] # ---> [ true true ] s = Set{t}() push!( s, t1) push!( s, t2) # adds t2 to the set although t2 and t1 are identical ... push!( s, tc) # does not add ... push!( s, tdc) # adds tdc although tdc and t1 are identical 类型的两个变量被视为相同;其余字段的值无关紧要。

{{1}}

1 个答案:

答案 0 :(得分:7)

正如DSM所示,您只需为您的类型添加(%esp)方法,即:

hash
相关问题