Search code examples
uniquejulia

equal values and uniqueness


I have a self defined type called Dyad, that consists of two variables, src, and dst. I have redefined the equal sign for dyads, such that

Dyad(1,2) == Dyad(2,1)
>true

However if I define the following Dyad vector

d = [Dyad(1,2), Dyad(2,1)]
unique(d) == d
>true

Do I need to redefine my equality so that unique() understands this?

P.S this is how I defined my equality:

==(x::Dyad, y::Dyad) = (x.src == y.src && x.dst == y.dst) || (x.src == y.dst && x.dst == y.src)

Solution

  • Probably it is even simpler to define hash for Dyad: hash(x::Dyad, h::UInt) = hash(minmax(x.src, x.dst), h) to get what you want. In this way you ensure that all functions testing for equality using hash in the process will work correctly.

    EDIT: minmax will be better than extrema here.