Skip to content

Commit dd4e0e7

Browse files
authored
[minmax] Do nothing for empty suffix/prefix. (#202)
It's a semantical noop but slightly more efficient for the case when max/min is requested for an empty Collection.
1 parent bcd4f36 commit dd4e0e7

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

Sources/Algorithms/MinMax.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,14 +256,14 @@ extension Collection {
256256
"""
257257
)
258258

259+
// Make sure we are within bounds.
260+
let prefixCount = Swift.min(count, self.count)
261+
259262
// Do nothing if we're prefixing nothing.
260-
guard count > 0 else {
263+
guard prefixCount > 0 else {
261264
return []
262265
}
263266

264-
// Make sure we are within bounds.
265-
let prefixCount = Swift.min(count, self.count)
266-
267267
// If we're attempting to prefix more than 10% of the collection, it's
268268
// faster to sort everything.
269269
guard prefixCount < (self.count / 10) else {
@@ -310,14 +310,14 @@ extension Collection {
310310
"""
311311
)
312312

313+
// Make sure we are within bounds.
314+
let suffixCount = Swift.min(count, self.count)
315+
313316
// Do nothing if we're suffixing nothing.
314-
guard count > 0 else {
317+
guard suffixCount > 0 else {
315318
return []
316319
}
317320

318-
// Make sure we are within bounds.
319-
let suffixCount = Swift.min(count, self.count)
320-
321321
// If we're attempting to prefix more than 10% of the collection, it's
322322
// faster to sort everything.
323323
guard suffixCount < (self.count / 10) else {

0 commit comments

Comments
 (0)