12

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.

starblue
  • 55,348
  • 14
  • 97
  • 151
Petr
  • 62,528
  • 13
  • 153
  • 317
  • 1
    Unlike Java `enum`s, Scala's `Enumeration` values can have arbitrary numerical identifiers and that is why I suspect there is no direct equivalent for Java `EnumSet` and `EnumMap` in Scala. – ghik Nov 17 '12 at 19:01
  • @ghik While that's true I'd say this feature is rarely used. And when someone'd assign big numbers to values it'd be his responsibility to simply use a different `Set`/`Map` implementation. – Petr Nov 17 '12 at 20:14
  • @ghik In fact that is a source of bugs in 2.10 impl. – som-snytt Nov 17 '12 at 21:36

1 Answers1

5

Actually, in 2.10 Enumeration.ValueSet uses BitSet.

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

That would be here.

Roger
  • 563
  • 5
  • 12
som-snytt
  • 39,429
  • 2
  • 47
  • 129