Skip to content

Commit a6bfe5c

Browse files
authored
Merge pull request scala/scala#10627 from GerretS/Add-indexOf-to-NumericRange
Add indexOf() and lastIndexOf() to NumericRange.
2 parents b5da916 + 3cd2dfe commit a6bfe5c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

library/src/scala/collection/immutable/NumericRange.scala

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@ sealed class NumericRange[T](
118118
}
119119
}
120120

121+
private[this] def indexOfTyped(elem: T, from: Int): Int =
122+
posOf(elem) match {
123+
case pos if pos >= from => pos
124+
case _ => -1
125+
}
126+
127+
final override def indexOf[B >: T](elem: B, from: Int): Int =
128+
try indexOfTyped(elem.asInstanceOf[T], from)
129+
catch { case _: ClassCastException => super.indexOf(elem, from) }
130+
131+
private[this] def lastIndexOfTyped(elem: T, end: Int): Int =
132+
posOf(elem) match {
133+
case pos if pos <= end => pos
134+
case _ => -1
135+
}
136+
137+
final override def lastIndexOf[B >: T](elem: B, end: Int = length - 1): Int =
138+
try lastIndexOfTyped(elem.asInstanceOf[T], end)
139+
catch { case _: ClassCastException => super.lastIndexOf(elem, end) }
140+
141+
private[this] def posOf(i: T): Int =
142+
/*
143+
If i is in this NumericRange, its position can simply be calculated by taking the amount of values up till i.
144+
NumericRange.count does this in an most efficient manner.
145+
Note that the contains() method throws an exception if the range has more than Int.MaxValue elements, but so did
146+
the original indexOf / lastIndexOf functions, so no functionality changed. */
147+
if (contains(i)) {
148+
/* Because of zero indexing, the count is always one higher than the index. This can be simply solved by setting
149+
isInclusive = false. */
150+
NumericRange.count(this.start, i, this.step, isInclusive = false)
151+
} else -1
152+
121153
// TODO: these private methods are straight copies from Range, duplicated
122154
// to guard against any (most likely illusory) performance drop. They should
123155
// be eliminated one way or another.

0 commit comments

Comments
 (0)