@@ -70,7 +70,7 @@ extension Sequence where Element: Comparable {
70
70
}
71
71
}
72
72
73
- extension MutableCollection where Self: RandomAccessCollection , Index == Int {
73
+ extension MutableCollection where Self: RandomAccessCollection {
74
74
/// Rearranges this collection such that the 0...k range contains the first
75
75
/// k sorted elements in this collection, using the given predicate as the
76
76
/// comparison between elements.
@@ -101,7 +101,7 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
101
101
}
102
102
}
103
103
104
- extension MutableCollection where Self: RandomAccessCollection , Element: Comparable , Index == Int {
104
+ extension MutableCollection where Self: RandomAccessCollection , Element: Comparable {
105
105
/// Rearranges this collection such that the 0...k range contains the first
106
106
/// k smallest elements in this collection.
107
107
///
@@ -133,7 +133,7 @@ extension MutableCollection where Self: RandomAccessCollection, Element: Compara
133
133
// __partiallySort(_:by:)
134
134
//===----------------------------------------------------------------------===//
135
135
136
- extension MutableCollection where Self: RandomAccessCollection , Index == Int {
136
+ extension MutableCollection where Self: RandomAccessCollection {
137
137
typealias Priority = ( Element , Element ) throws -> Bool
138
138
139
139
/// Partially sorts this collection by using an in place heapsort that stops after we find the desired k amount
@@ -154,11 +154,11 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
154
154
}
155
155
var iterator = ( 0 ..< k) . makeIterator ( )
156
156
_ = iterator. next ( )
157
- swapAt ( count - 1 , heapEndIndex)
157
+ swapAt ( index ( before : endIndex ) , index ( startIndex , offsetBy : heapEndIndex) )
158
158
heapEndIndex += 1
159
159
while let _ = iterator. next ( ) {
160
160
try siftDown ( count - 1 , by: areInIncreasingOrder, heapEndIndex: heapEndIndex)
161
- swapAt ( count - 1 , heapEndIndex)
161
+ swapAt ( index ( before : endIndex ) , index ( startIndex , offsetBy : heapEndIndex) )
162
162
heapEndIndex += 1
163
163
}
164
164
}
@@ -174,7 +174,7 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
174
174
guard indexToSwap != i else {
175
175
return
176
176
}
177
- swapAt ( i , indexToSwap)
177
+ swapAt ( index ( startIndex , offsetBy : i ) , index ( startIndex , offsetBy : indexToSwap) )
178
178
try siftDown ( indexToSwap, by: priority, heapEndIndex: heapEndIndex)
179
179
}
180
180
@@ -199,7 +199,9 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
199
199
guard child >= heapEndIndex else {
200
200
return parent
201
201
}
202
- guard try priority ( self [ child] , self [ parent] ) else {
202
+ let childElement = self [ index ( startIndex, offsetBy: child) ]
203
+ let parentElement = self [ index ( startIndex, offsetBy: parent) ]
204
+ guard try priority ( childElement, parentElement) else {
203
205
return parent
204
206
}
205
207
return child
0 commit comments