I’m looking for a way to represent a set of integers with a bit vector (which would be the characteristic function of that set of integers) and be able to perform bitwise operations on this set.
Initially I thought scala’s BitSet would be the ideal candidate. However, it seems BitSet doesn’t support shifting operations according to the documentation 1. Upon further investigation I also found that the related Java BitSet implementation doesn’t support shift operations either 2.
Am I left with the only option of implementing my own BitSet class which supports shift operations? Moreover, according to the description given in 3 it doesn’t sound that difficult to support shift operations on the Scala’s BitSet implementation, or have I misunderstood something here?
Thanks in advance.
The usual trick when faced with a need for retrofitting new functionality is the “Pimp My Library” pattern. Implicitly convert the BitSet to a dedicated type intended to perform the added operation:
Alter
shiftLeftto whatever name and with whatever arguments you prefer.UPDATE
If you know for certain that you’ll have an immutable
BitSet, then a (slightly hacky) approach to access the raw underlying array is to pattern match. Not too painful either, as there are only 3 possible concrete subclasses for an immutableBitSet:Annoyingly, the
elems1param toBitSet2isn’t a val, and theelemsparam to a mutable BitSet is marked protected. So it’s not perfect, but should do the trick if your set is non-trivial and immutable. For the trivial cases, “normal” access to the set won’t be too expensive.And yes, this technique would be used within the wrapper as described above.