@@ -32,8 +32,9 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
32
32
}
33
33
34
34
/// Initialize with a sequence of integers.
35
- public init < ElementSequence : Sequence > ( indexes: ElementSequence ) where ElementSequence. Iterator. Element == Element {
36
- _indexes = indexes. map { $0 }
35
+ public init < ElementSequence : Sequence > ( indexes: ElementSequence )
36
+ where ElementSequence. Iterator. Element == Element {
37
+ _indexes = indexes. map { $0 }
37
38
}
38
39
39
40
/// Initialize with an array literal.
@@ -42,7 +43,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
42
43
}
43
44
44
45
/// Initialize with an array of elements.
45
- public init ( indexes: Array < Element > ) {
46
+ public init ( indexes: [ Element ] ) {
46
47
_indexes = indexes
47
48
}
48
49
@@ -67,7 +68,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
67
68
}
68
69
69
70
/// Append an array of elements to `self`.
70
- public mutating func append( _ other: Array < Element > ) {
71
+ public mutating func append( _ other: [ Element ] ) {
71
72
_indexes. append ( contentsOf: other)
72
73
}
73
74
@@ -84,7 +85,7 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
84
85
}
85
86
86
87
/// Return a new `IndexPath` containing the elements in self and the elements in `other`.
87
- public func appending( _ other: Array < Element > ) -> IndexPath {
88
+ public func appending( _ other: [ Element ] ) -> IndexPath {
88
89
return IndexPath ( indexes: _indexes + other)
89
90
}
90
91
@@ -131,10 +132,11 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
131
132
}
132
133
133
134
/// Sorting an array of `IndexPath` using this comparison results in an array representing nodes in depth-first traversal order.
134
- public func compare( _ other: IndexPath ) -> ComparisonResult {
135
+ public func compare( _ other: IndexPath ) -> ComparisonResult {
135
136
// This is temporary
136
137
let me = self . makeReference ( )
137
- return me. compare ( other)
138
+ let other = other. makeReference ( )
139
+ return me. compare ( other as IndexPath )
138
140
}
139
141
140
142
public var hashValue : Int {
@@ -143,74 +145,76 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
143
145
return me. hash
144
146
}
145
147
146
- public var description : String {
147
- return _indexes. description
148
- }
149
-
150
- public var debugDescription : String {
151
- return _indexes. debugDescription
152
- }
153
-
154
148
// MARK: - Bridging Helpers
155
149
156
150
fileprivate init ( nsIndexPath: ReferenceType ) {
157
151
let count = nsIndexPath. length
158
152
if count == 0 {
159
153
_indexes = [ ]
160
154
} else {
161
- var ptr = malloc ( count * MemoryLayout< Element> . size) ? . bindMemory ( to : Element . self , capacity : count * MemoryLayout < Element > . size )
155
+ var ptr = malloc ( count * MemoryLayout< Element> . size)
162
156
defer { free ( ptr) }
163
157
164
- nsIndexPath. getIndexes ( ptr!, range: NSMakeRange ( 0 , count) )
158
+ let elementPtr = ptr!. bindMemory ( to: Element . self, capacity: count)
159
+ nsIndexPath. getIndexes ( elementPtr, range: NSMakeRange ( 0 , count) )
165
160
166
- let buffer = UnsafeBufferPointer ( start: ptr , count: count)
161
+ let buffer = UnsafeBufferPointer ( start: elementPtr , count: count)
167
162
_indexes = buffer. map { $0 }
168
163
}
169
164
}
170
165
171
166
fileprivate func makeReference( ) -> ReferenceType {
172
167
return _indexes. withUnsafeBufferPointer {
173
- return ReferenceType ( indexes: $0. baseAddress! , length: $0. count)
168
+ return ReferenceType ( indexes: $0. baseAddress, length: $0. count)
174
169
}
175
170
}
176
171
177
- }
172
+ public static func == ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
173
+ return lhs. _indexes == rhs. _indexes
174
+ }
178
175
179
- public func == ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
180
- return lhs. _indexes == rhs. _indexes
181
- }
176
+ public static func + ( lhs: IndexPath , rhs: IndexPath ) -> IndexPath {
177
+ return lhs. appending ( rhs)
178
+ }
182
179
183
- public func + ( lhs: IndexPath , rhs: IndexPath ) -> IndexPath {
184
- return lhs. appending ( rhs)
185
- }
180
+ public static func += ( lhs: inout IndexPath , rhs: IndexPath ) {
181
+ lhs. append ( rhs)
182
+ }
186
183
187
- public func += ( lhs: inout IndexPath , rhs: IndexPath ) {
188
- lhs. append ( rhs)
189
- }
184
+ public static func < ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
185
+ return lhs. compare ( rhs) == ComparisonResult . orderedAscending
186
+ }
190
187
191
- public func < ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
192
- return lhs. compare ( rhs) == ComparisonResult . orderedAscending
193
- }
188
+ public static func <= ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
189
+ let order = lhs. compare ( rhs)
190
+ return order == ComparisonResult . orderedAscending || order == ComparisonResult . orderedSame
191
+ }
194
192
195
- public func <= ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
196
- let order = lhs. compare ( rhs)
197
- return order == ComparisonResult . orderedAscending || order == ComparisonResult . orderedSame
198
- }
193
+ public static func > ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
194
+ return lhs. compare ( rhs) == ComparisonResult . orderedDescending
195
+ }
199
196
200
- public func > ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
201
- return lhs. compare ( rhs) == ComparisonResult . orderedDescending
197
+ public static func >= ( lhs: IndexPath , rhs: IndexPath ) -> Bool {
198
+ let order = lhs. compare ( rhs)
199
+ return order == ComparisonResult . orderedDescending || order == ComparisonResult . orderedSame
200
+ }
202
201
}
203
202
204
- public func >= ( lhs : IndexPath , rhs : IndexPath ) -> Bool {
205
- let order = lhs . compare ( rhs )
206
- return order == ComparisonResult . orderedDescending || order == ComparisonResult . orderedSame
207
- }
203
+ extension IndexPath : CustomStringConvertible , CustomDebugStringConvertible , CustomReflectable {
204
+ public var description : String {
205
+ return _indexes . description
206
+ }
208
207
209
- extension IndexPath {
210
- public static func _isBridgedToObjectiveC( ) -> Bool {
211
- return true
208
+ public var debugDescription : String {
209
+ return _indexes. debugDescription
212
210
}
213
211
212
+ public var customMirror : Mirror {
213
+ return _indexes. customMirror
214
+ }
215
+ }
216
+
217
+ extension IndexPath : _ObjectiveCBridgeable {
214
218
public static func _getObjectiveCType( ) -> Any . Type {
215
219
return NSIndexPath . self
216
220
}
@@ -231,5 +235,13 @@ extension IndexPath {
231
235
232
236
public static func _unconditionallyBridgeFromObjectiveC( _ source: NSIndexPath ? ) -> IndexPath {
233
237
return IndexPath ( nsIndexPath: source!)
234
- }
238
+ }
239
+ }
240
+
241
+ extension NSIndexPath : _HasCustomAnyHashableRepresentation {
242
+ // Must be @nonobjc to avoid infinite recursion during bridging.
243
+ @nonobjc
244
+ public func _toCustomAnyHashable( ) -> AnyHashable ? {
245
+ return AnyHashable ( self as IndexPath )
246
+ }
235
247
}
0 commit comments