Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Scala equivalent of EnumSet/EnumMap?

In Java we have two nice classes: EnumSet for sets of enums and EnumMap for a map whose keys are enums. EnumSet is represented as a 64-bit word (or an array of 64-bit words) and EnumMap as an array of values, both indexed by the ordinal numbers of enums. So insert/lookup/remove/... operations take just O(1) time.

Do we have something like that in Scala - mutable or immutable?

I found BitSet (both mutable and immutable) which operates on integers, so I assumed there would be an efficient implementation of sets of Enumeration.Values backed up by it. But I found only Enumeration.ValueSet, which is backed up by SortedSet[Int]. While that's not so bad, BitSet seems to be quite more efficient for this purpose.

I didn't find any optimized implementation of maps with Enumeration.Value as keys similar to EnumMap.

like image 234
Petr Avatar asked Nov 17 '12 17:11

Petr


1 Answers

Actually, in 2.10 Enumeration.ValueSet uses BitSet.

class ValueSet private[ValueSet] (private[this] var nnIds: immutable.BitSet)

That would be here.

like image 157
som-snytt Avatar answered Nov 04 '22 16:11

som-snytt