@@ -118,6 +118,38 @@ sealed class NumericRange[T](
118
118
}
119
119
}
120
120
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
+
121
153
// TODO: these private methods are straight copies from Range, duplicated
122
154
// to guard against any (most likely illusory) performance drop. They should
123
155
// be eliminated one way or another.
0 commit comments