hashable

What makes a user-defined class unhashable?

放肆的年华 提交于 2019-12-09 07:28:38
问题 The docs say that a class is hashable as long as it defines __hash__ method and __eq__ method. However: class X(list): # read-only interface of `tuple` and `list` should be the same, so reuse tuple.__hash__ __hash__ = tuple.__hash__ x1 = X() s = {x1} # TypeError: unhashable type: 'X' What makes X unhashable? Note that I must have identical lists (in terms of regular equality) to be hashed to the same value; otherwise, I will violate this requirement on hash functions: The only required

Why does Data.HashTable use hashing with salt (from Data.Hashable)?

℡╲_俬逩灬. 提交于 2019-12-05 09:11:58
I do not understand why Data.HashTable is using Data.Hashable , which has hashWithSalt as the (only/basic) method. This does not fit with the natural optimization of computing the hash value once, and storing it in the object (natural, because Haskell objects are immutable). If I want to use HashTables with that, then I'm forced to implement hashWithSalt . (Going 1.2.0.* to 1.2.1.*, hashable re-introduced hash as a class method, but this does not help?) The actual Table implementations don't seem to make use of hashWithSalt ( HashTable.ST.Linear does not at all, HashTable.ST.Cuckoo uses two

What makes a user-defined class unhashable?

拈花ヽ惹草 提交于 2019-12-03 09:22:42
The docs say that a class is hashable as long as it defines __hash__ method and __eq__ method. However: class X(list): # read-only interface of `tuple` and `list` should be the same, so reuse tuple.__hash__ __hash__ = tuple.__hash__ x1 = X() s = {x1} # TypeError: unhashable type: 'X' What makes X unhashable? Note that I must have identical lists (in terms of regular equality) to be hashed to the same value; otherwise, I will violate this requirement on hash functions: The only required property is that objects which compare equal have the same hash value The docs do warn that a hashable object

Set's contains method returns different value at different time

那年仲夏 提交于 2019-12-02 08:27:27
问题 I was thinking about how Swift ensures uniqueness for Set because I have turned one of my obj from Equatable to Hashable for free and so I came up with this simple Playground struct SimpleStruct: Hashable { let string: String let number: Int static func == (lhs: SimpleStruct, rhs: SimpleStruct) -> Bool { let areEqual = lhs.string == rhs.string print(lhs, rhs, areEqual) return areEqual } } var set = Set<SimpleStruct>() let first = SimpleStruct(string: "a", number: 2) set.insert(first) So my

Set's contains method returns different value at different time

房东的猫 提交于 2019-12-02 04:33:36
I was thinking about how Swift ensures uniqueness for Set because I have turned one of my obj from Equatable to Hashable for free and so I came up with this simple Playground struct SimpleStruct: Hashable { let string: String let number: Int static func == (lhs: SimpleStruct, rhs: SimpleStruct) -> Bool { let areEqual = lhs.string == rhs.string print(lhs, rhs, areEqual) return areEqual } } var set = Set<SimpleStruct>() let first = SimpleStruct(string: "a", number: 2) set.insert(first) So my first question was: Will the static func == method be called anytime I insert a new obj inside the set?

How to handle hash collisions for Dictionaries in Swift

删除回忆录丶 提交于 2019-11-30 08:16:37
问题 TLDR My custom structure implements the Hashable Protocol. However, when hash collisions occur while inserting keys in a Dictionary , they are not automatically handled. How do I overcome this problem? Background I had previously asked this question How to implement the Hashable Protocol in Swift for an Int array (a custom string struct). Later I added my own answer, which seemed to be working. However, recently I have detected a subtle problem with hashValue collisions when using a

Check for mutability in Python?

本小妞迷上赌 提交于 2019-11-30 04:23:34
Consider this code : a = {...} # a is an dict with arbitrary contents b = a.copy() What role does mutability play in the keys and values of the dicts? How do I ensure changes to keys or values of one dict are not reflected in the other? How does this relate to the hashable constraint of the dict keys? Are there any differences in behaviour between Python 2.x and Python 3.x? How do I check if a type is mutable in Python? 1) Keys must not be mutable, unless you have a user-defined class that is hashable but also mutable. That's all that's forced upon you. However, using a hashable, mutable

How can I use a Swift enum as a Dictionary key? (Conforming to Equatable)

天大地大妈咪最大 提交于 2019-11-29 10:47:55
问题 I've defined an enum to represent a selection of a "station"; stations are defined by a unique positive integer, so I've created the following enum to allow negative values to represent special selections: enum StationSelector : Printable { case Nearest case LastShown case List case Specific(Int) func toInt() -> Int { switch self { case .Nearest: return -1 case .LastShown: return -2 case .List: return -3 case .Specific(let stationNum): return stationNum } } static func fromInt(value:Int) ->

Swift: 'Hashable.hashValue' is deprecated as a protocol requirement;

南笙酒味 提交于 2019-11-29 03:22:56
I've been facing following issue (it's just a warning) with my iOS project. 'Hashable.hashValue' is deprecated as a protocol requirement; conform type 'ActiveType' to 'Hashable' by implementing 'hash(into:)' instead Xcode 10.2 Swift 5 Source Code: public enum ActiveType { case mention case hashtag case url case custom(pattern: String) var pattern: String { switch self { case .mention: return RegexParser.mentionPattern case .hashtag: return RegexParser.hashtagPattern case .url: return RegexParser.urlPattern case .custom(let regex): return regex } } } extension ActiveType: Hashable, Equatable {

Check for mutability in Python?

不问归期 提交于 2019-11-29 01:39:03
问题 Consider this code: a = {...} # a is an dict with arbitrary contents b = a.copy() What role does mutability play in the keys and values of the dicts? How do I ensure changes to keys or values of one dict are not reflected in the other? How does this relate to the hashable constraint of the dict keys? Are there any differences in behaviour between Python 2.x and Python 3.x? How do I check if a type is mutable in Python? 回答1: 1) Keys must not be mutable, unless you have a user-defined class