File tree Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Expand file tree Collapse file tree 1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change 7
7
//
8
8
// See https://swift.org/LICENSE.txt for license information
9
9
//
10
+ //===----------------------------------------------------------------------===//
10
11
11
12
extension Collection {
12
13
/// Returns the first k elements of this collection when it's sorted using
@@ -38,18 +39,17 @@ extension Collection {
38
39
Cannot prefix with a negative amount of elements!
39
40
"""
40
41
)
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)
45
45
46
46
// 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 ) )
49
49
}
50
50
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 ) {
53
53
if let last = result. last, try areInIncreasingOrder ( last, e) {
54
54
continue
55
55
}
You can’t perform that action at this time.
0 commit comments