Skip to content

Commit 36e9a39

Browse files
committed
Fix header and remove assert
1 parent e8504fd commit 36e9a39

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Sources/Algorithms/PartialSort.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
//
10+
//===----------------------------------------------------------------------===//
1011

1112
extension Collection {
1213
/// Returns the first k elements of this collection when it's sorted using
@@ -38,18 +39,17 @@ extension Collection {
3839
Cannot prefix with a negative amount of elements!
3940
"""
4041
)
41-
assert(count <= self.count, """
42-
Cannot prefix more than this Collection's size!
43-
"""
44-
)
42+
43+
// Make sure we are within bounds
44+
let prefixCount = Swift.min(count, self.count)
4545

4646
// If we're attempting to prefix more than 10% of the collection, it's faster to sort everything.
47-
guard count < (self.count / 10) else {
48-
return Array(try sorted(by: areInIncreasingOrder).prefix(count))
47+
guard prefixCount < (self.count / 10) else {
48+
return Array(try sorted(by: areInIncreasingOrder).prefix(prefixCount))
4949
}
5050

51-
var result = try self.prefix(count).sorted(by: areInIncreasingOrder)
52-
for e in self.dropFirst(count) {
51+
var result = try self.prefix(prefixCount).sorted(by: areInIncreasingOrder)
52+
for e in self.dropFirst(prefixCount) {
5353
if let last = result.last, try areInIncreasingOrder(last, e) {
5454
continue
5555
}

0 commit comments

Comments
 (0)