Skip to content

Commit

Permalink
[minmax] Do nothing for empty suffix/prefix. (#202)
Browse files Browse the repository at this point in the history
It's a semantical noop but slightly more efficient for the case when max/min is requested for an empty Collection.
  • Loading branch information
ttsugriy authored Oct 31, 2023
1 parent bcd4f36 commit dd4e0e7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions Sources/Algorithms/MinMax.swift
Original file line number Diff line number Diff line change
Expand Up @@ -256,14 +256,14 @@ extension Collection {
"""
)

// Make sure we are within bounds.
let prefixCount = Swift.min(count, self.count)

// Do nothing if we're prefixing nothing.
guard count > 0 else {
guard prefixCount > 0 else {
return []
}

// Make sure we are within bounds.
let prefixCount = Swift.min(count, self.count)

// If we're attempting to prefix more than 10% of the collection, it's
// faster to sort everything.
guard prefixCount < (self.count / 10) else {
Expand Down Expand Up @@ -310,14 +310,14 @@ extension Collection {
"""
)

// Make sure we are within bounds.
let suffixCount = Swift.min(count, self.count)

// Do nothing if we're suffixing nothing.
guard count > 0 else {
guard suffixCount > 0 else {
return []
}

// Make sure we are within bounds.
let suffixCount = Swift.min(count, self.count)

// If we're attempting to prefix more than 10% of the collection, it's
// faster to sort everything.
guard suffixCount < (self.count / 10) else {
Expand Down

0 comments on commit dd4e0e7

Please sign in to comment.