Skip to content

Commit 7870283

Browse files
committed
Merge commit 'c919d61373' into merge-2.12-into-2.13.x
* commit 'c919d61373': reduce the complexity of Enumeration lookups Optimise sameLength intersect.
2 parents b131b0a + 9eaf9a4 commit 7870283

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

library/src/scala/Enumeration.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
155155
* @throws NoSuchElementException if no `Value` with a matching
156156
* name is in this `Enumeration`
157157
*/
158-
final def withName(s: String): Value = values.find(_.toString == s).getOrElse(
158+
final def withName(s: String): Value = values.byName.getOrElse(s,
159159
throw new NoSuchElementException(s"No value found for '$s'"))
160160

161161
/** Creates a fresh value, part of this enumeration. */
@@ -208,7 +208,7 @@ abstract class Enumeration (initial: Int) extends Serializable {
208208
val value = m.invoke(this).asInstanceOf[Value]
209209
// verify that outer points to the correct Enumeration: ticket #3616.
210210
if (value.outerEnum eq thisenum) {
211-
val id = Int.unbox(classOf[Val] getMethod "id" invoke value)
211+
val id: Int = value.id
212212
nmap += ((id, name))
213213
}
214214
}
@@ -321,6 +321,8 @@ abstract class Enumeration (initial: Int) extends Serializable {
321321
super[SortedSet].zip[B](that)
322322
override def collect[B](pf: PartialFunction[Value, B])(implicit @implicitNotFound(ValueSet.ordMsg) ev: Ordering[B]): immutable.SortedSet[B] =
323323
super[SortedSet].collect[B](pf)
324+
325+
private[Enumeration] lazy val byName: Map[String, Value] = iterator.map( v => v.toString -> v).toMap
324326
}
325327

326328
/** A factory object for value sets */

0 commit comments

Comments
 (0)