Skip to content

Commit

Permalink
Add IntRange.size
Browse files Browse the repository at this point in the history
Add an extension function to the IntRange to calculate its size, which ends up being faster than calling IntRange.count
  • Loading branch information
GeistInDerSH committed Dec 9, 2024
1 parent 6cc6b21 commit 68fba61
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,11 @@ fun IntRange.isFullyContained(other: IntRange): Boolean = this.toSet().containsA
* @return A new range, that contains both ranges
*/
fun IntRange.intersection(other: IntRange): IntRange = maxOf(this.first, other.first)..minOf(this.last, other.last)

/**
* Count the number of elements in the [IntRange]. It's faster than calling IntRange::count
* because it doesn't need to loop over the whole range
*
* @return The size of the [IntRange]
*/
fun IntRange.size(): Int = this.last - this.first + 1

0 comments on commit 68fba61

Please sign in to comment.