Algorithmic-Problems icon indicating copy to clipboard operation
Algorithmic-Problems copied to clipboard

It seems you can use NSHashTable instead of Set for LinkedLists/LoopDetection/Loop.swift

Open kyzmitch opened this issue 7 years ago • 0 comments

func hasLoopNodeHash() -> Node? {
        var set = Set<Node>() // **Right here**
        var temp: Node? = self
        while let t = temp {
            if set.contains(t) {
                return t
            } else {
                set.insert(t)
            }
            temp = temp?.next
        }
        return nil
    }

because Its members may use pointer identity for equality and hashing. So, you don't have to implement Comparable methods like:

public static func ==(lhs: Node, rhs: Node) -> Bool {
        return lhs === rhs
    }
    
    var hashValue: Int {
        return ObjectIdentifier(self).hashValue
    }

kyzmitch avatar Dec 23 '17 20:12 kyzmitch