Post

Replies

Boosts

Views

Activity

Custom Sequence, use `for` index don't change, but `while` is normal. why?
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
1
0
263
Aug ’23
For a custom Sequence protocol, but index don't change, but use while index change. why?
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.
0
0
227
Aug ’23
Swift Set type performance inconsistently when inserting data
This code will generate different result when RoadmapCardChain  implement Hashable  and Equatable  protocol struct RoadmapCardChain: Codable, Hashable, Equatable { &#9;&#9;let start: String &#9;&#9;let end: String &#9;&#9;let styles: String &#9;&#9; &#9;&#9;static func == (lhs: RoadmapCardChain, rhs: RoadmapCardChain) -> Bool { &#9;&#9;&#9;&#9;let isStyleSame = lhs.styles == rhs.styles &#9;&#9;&#9;&#9;let isEndSame = lhs.start == rhs.start &amp;&amp; lhs.end == rhs.end &#9;&#9;&#9;&#9;let isToggleEndSame = lhs.start == rhs.end &amp;&amp; lhs.end == rhs.start &#9;&#9;&#9;&#9;print(lhs) &#9;&#9;&#9;&#9;print(rhs) &#9;&#9;&#9;&#9;print("result is \(isStyleSame &amp;&amp; ( isEndSame || isToggleEndSame ))") &#9;&#9;&#9;&#9;return isStyleSame &amp;&amp; ( isEndSame || isToggleEndSame ) &#9;&#9;} } let a = RoadmapCardChain(start: "Cafeteria", end: "Payment BIZ", styles: "AECC5C") let b = RoadmapCardChain(start: "Payment BIZ", end: "Cafeteria", styles: "AECC5C") var chains: Set&lt;RoadmapCardChain&gt; = [] print(a.hashValue) print(b.hashValue) chains.insert(a) if chains.contains(b) { &#9;&#9;print("contains is true") } else { &#9;&#9;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 ==
1
0
401
Aug ’20