Skip to content

Commit 6cd2870

Browse files
committed
Use Indexes
1 parent f299df1 commit 6cd2870

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Sources/Algorithms/PartialSort.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Sequence where Element: Comparable {
7070
}
7171
}
7272

73-
extension MutableCollection where Self: RandomAccessCollection, Index == Int {
73+
extension MutableCollection where Self: RandomAccessCollection {
7474
/// Rearranges this collection such that the 0...k range contains the first
7575
/// k sorted elements in this collection, using the given predicate as the
7676
/// comparison between elements.
@@ -101,7 +101,7 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
101101
}
102102
}
103103

104-
extension MutableCollection where Self: RandomAccessCollection, Element: Comparable, Index == Int {
104+
extension MutableCollection where Self: RandomAccessCollection, Element: Comparable {
105105
/// Rearranges this collection such that the 0...k range contains the first
106106
/// k smallest elements in this collection.
107107
///
@@ -133,7 +133,7 @@ extension MutableCollection where Self: RandomAccessCollection, Element: Compara
133133
// __partiallySort(_:by:)
134134
//===----------------------------------------------------------------------===//
135135

136-
extension MutableCollection where Self: RandomAccessCollection, Index == Int {
136+
extension MutableCollection where Self: RandomAccessCollection {
137137
typealias Priority = (Element, Element) throws -> Bool
138138

139139
/// 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 {
154154
}
155155
var iterator = (0..<k).makeIterator()
156156
_ = iterator.next()
157-
swapAt(count - 1, heapEndIndex)
157+
swapAt(index(before: endIndex), index(startIndex, offsetBy: heapEndIndex))
158158
heapEndIndex += 1
159159
while let _ = iterator.next() {
160160
try siftDown(count - 1, by: areInIncreasingOrder, heapEndIndex: heapEndIndex)
161-
swapAt(count - 1, heapEndIndex)
161+
swapAt(index(before: endIndex), index(startIndex, offsetBy: heapEndIndex))
162162
heapEndIndex += 1
163163
}
164164
}
@@ -174,7 +174,7 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
174174
guard indexToSwap != i else {
175175
return
176176
}
177-
swapAt(i, indexToSwap)
177+
swapAt(index(startIndex, offsetBy: i), index(startIndex, offsetBy: indexToSwap))
178178
try siftDown(indexToSwap, by: priority, heapEndIndex: heapEndIndex)
179179
}
180180

@@ -199,7 +199,9 @@ extension MutableCollection where Self: RandomAccessCollection, Index == Int {
199199
guard child >= heapEndIndex else {
200200
return parent
201201
}
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 {
203205
return parent
204206
}
205207
return child

0 commit comments

Comments
 (0)