This code will generate different result when RoadmapCardChain implement Hashable and Equatable protocol
struct RoadmapCardChain: Codable, Hashable, Equatable {
		let start: String
		let end: String
		let styles: String
		
		static func == (lhs: RoadmapCardChain, rhs: RoadmapCardChain) -> Bool {
				let isStyleSame = lhs.styles == rhs.styles
				let isEndSame = lhs.start == rhs.start && lhs.end == rhs.end
				let isToggleEndSame = lhs.start == rhs.end && lhs.end == rhs.start
				print(lhs)
				print(rhs)
				print("result is \(isStyleSame && ( isEndSame || isToggleEndSame ))")
				return isStyleSame && ( isEndSame || isToggleEndSame )
		}
}
let a = RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C")
let b = RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C")
var chains: Set<RoadmapCardChain> = []
print(a.hashValue)
print(b.hashValue)
chains.insert(a)
if chains.contains(b) {
		print("contains is true")
} else {
		chains.insert(b)
}
print(chains)
sometime result is :
785027920194053578
6404817261741129101
RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C")
RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C")
result is true
contains is true
[__lldb_expr_19.RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C")]
but sometime result is :
491382166321900052
7275567105868021174
[lldb_expr_21.RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C"), lldb_expr_21.RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C")]
I don't know why same codes has different performance
I still don't understand why sometimes == be called and sometimes it doesn't be called。According to my understanding Set only need hashvalue when insert or contains function, don't need to call ==
Post
Replies
Boosts
Views
Activity
struct BoxedFibonacciSequence: Sequence, IteratorProtocol
{
typealias Element = Int
var currentIndex = 0
mutating func next() -> Int? {
defer { currentIndex += 1 }
return loadFibNumber(at: currentIndex)
}
func makeIterator() -> Self {
return self
}
}
func loadFibNumber(at index: Int) -> Int {
return fibNumber(at: index)
}
var box = BoxedFibonacciSequence()
for v in box {
print("for index",box.currentIndex)
if v < 20 {
} else {
break
}
}
let fib = BoxedFibonacciSequence()
var iter = fib.makeIterator()
while let v = iter.next() {
print("while index",iter.currentIndex)
if v < 20 {
} else {
break
}
}
why use for index always 0, but while index is normal? I'm confused.
struct BoxedFibonacciSequence: Sequence, IteratorProtocol
{
typealias Element = Int
var currentIndex = 0
mutating func next() -> Int? {
defer { currentIndex += 1 }
return loadFibNumber(at: currentIndex)
}
func makeIterator() -> Self {
return self
}
}
func loadFibNumber(at index: Int) -> Int {
return fibNumber(at: index)
}
var box = BoxedFibonacciSequence()
for v in box {
print("for index",box.currentIndex)
if v < 20 {
} else {
break
}
}
let fib = BoxedFibonacciSequence()
var iter = fib.makeIterator()
while let v = iter.next() {
print("while index",iter.currentIndex)
if v < 20 {
} else {
break
}
}
//out put:
//for index 0
//for index 0
//for index 0
//for index 0
//for index 0
//for index 0
//for index 0
//for index 0
//for index 0
//while index 1
//while index 2
//while index 3
//while index 4
//while index 5
//while index 6
//while index 7
//while index 8
//while index 9