Created
May 24, 2017 06:27
-
-
Save takasek/bca2a1d719634e69d68925b1f65656eb to your computer and use it in GitHub Desktop.
hash値ってそのまま集合のkeyとして使われるんだと思ってた…! 実際はあくまで検索を効率化するための第一キーで、同一性をあらわすものではなかったのね…! #CodePiece
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Hoge: Equatable, Hashable { | |
let a: Int | |
let b: Int | |
static func == (lhs: Hoge, rhs: Hoge) -> Bool { | |
return lhs.a == rhs.a | |
&& lhs.b == rhs.b | |
} | |
var hashValue: Int { | |
return a | |
} | |
} | |
var s: Set<Hoge> = [] | |
s.insert(Hoge(a: 1, b: 1)) | |
s.insert(Hoge(a: 1, b: 2)) | |
s.insert(Hoge(a: 1, b: 2)) | |
print(s) // [Hoge(a: 1, b: 1), Hoge(a: 1, b: 2)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment