patternMinor
Scala BitSet implemented with Java BitSet, for use in Scala Enumerations to replace ValueSet
Viewed 0 times
enumerationswithimplementedreplacejavascalaforvaluesetusebitset
Problem
Due to a performance profiling hotspot detailed here, I implemented my own
In my enumeration objects, I have to have code like this:
Elsewhere, I need to do things like this:
For
```
package alder
import scala.language.implicitConversions
class BitSet[E this.bits equals that.bits
case _ => false
}
def union(that: BitSet[E]): BitSet[E] = {
var newBits = bits
newBits.or(that.bits)
new BitSetE
}
def |(that: BitSet[E]): BitSet[E] = union(that)
def |(v: E#Value): BitSet[E] = union(BitSet(v))
def intersection(that: BitSet[E]): BitSet[E] = {
var newBits = bits
newBits.and(that.bits)
new BitSet(newBits)
}
def &(that: BitSet[E]): BitSet[E] = intersection(that)
def &(v: E#Value): BitSet[E] = intersection(BitSet(v))
def contains(v: E#Value): Boolean = bits.get(v.id)
def containsAll(that: BitSet[E]): Boolean = intersection(that) == that
def containsAny(that: BitSet[E]): Boolean = intersection(that).nonEmpty
def toBitMask(): Array[Long] = bits.toLongArray
def mkString(e: E, sep: String): String =
{
val vs = e.ValueSet.fromBitMask(toBitMask())
vs.mkString(sep)
}
}
object BitSet {
def apply[E <: Enumeration](): BitSet[E] = new BitSetE
def applyE <: Enumeration: BitSet[E] = {
var bits = new java.util.BitSet
for (v <- vs) {
bits.set(v.id)
}
new BitSetE
BitSet using Java's BitSet. This is intended to replace the Enumeration.ValueSet. However, it's a bit awkward to use, primarily due to my likely misunderstanding of the relationships between the Enumeration class, Enumeration type and concrete Enumeration object.In my enumeration objects, I have to have code like this:
type BitSet = alder.BitSet[this.type]
val empty = alder.BitSet[this.type]()Elsewhere, I need to do things like this:
alder.BitSet.fromBitMask[SomeEnumeration.type](...)For
mkString I need to actually pass in the enumeration object itself. Is there any way to make this entire edifice a little more user-friendly?```
package alder
import scala.language.implicitConversions
class BitSet[E this.bits equals that.bits
case _ => false
}
def union(that: BitSet[E]): BitSet[E] = {
var newBits = bits
newBits.or(that.bits)
new BitSetE
}
def |(that: BitSet[E]): BitSet[E] = union(that)
def |(v: E#Value): BitSet[E] = union(BitSet(v))
def intersection(that: BitSet[E]): BitSet[E] = {
var newBits = bits
newBits.and(that.bits)
new BitSet(newBits)
}
def &(that: BitSet[E]): BitSet[E] = intersection(that)
def &(v: E#Value): BitSet[E] = intersection(BitSet(v))
def contains(v: E#Value): Boolean = bits.get(v.id)
def containsAll(that: BitSet[E]): Boolean = intersection(that) == that
def containsAny(that: BitSet[E]): Boolean = intersection(that).nonEmpty
def toBitMask(): Array[Long] = bits.toLongArray
def mkString(e: E, sep: String): String =
{
val vs = e.ValueSet.fromBitMask(toBitMask())
vs.mkString(sep)
}
}
object BitSet {
def apply[E <: Enumeration](): BitSet[E] = new BitSetE
def applyE <: Enumeration: BitSet[E] = {
var bits = new java.util.BitSet
for (v <- vs) {
bits.set(v.id)
}
new BitSetE
Solution
After studying your code for a good long while, I realized that the reason you haven't gotten an answer is that there isn't really much to say about your code. Well done!
I would recommend adding some thorough ScalaDoc though. It has been my experience that Scala library developers have far too much faith in the ability of library users to magically understand what they really meant. Examples are wonderful things.
I would recommend adding some thorough ScalaDoc though. It has been my experience that Scala library developers have far too much faith in the ability of library users to magically understand what they really meant. Examples are wonderful things.
Context
StackExchange Code Review Q#74795, answer score: 8
Revisions (0)
No revisions yet.