-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
88 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
scala2/src/main/scala/jurisk/collections/mutable/MutableBitSet.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package jurisk.collections.mutable | ||
|
||
import jurisk.collections.mutable.BitSetKeySyntax.BitSetKeyOps1 | ||
import jurisk.collections.mutable.BitSetKeySyntax.BitSetKeyOps2 | ||
|
||
import scala.collection.mutable | ||
|
||
trait BitSetKey[T] { | ||
def toInt(value: T): Int | ||
def fromInt(value: Int): T | ||
} | ||
|
||
object BitSetKeyInstances { | ||
implicit val intBitSetKey: BitSetKey[Int] = new BitSetKey[Int] { | ||
def toInt(value: Int): Int = value | ||
def fromInt(value: Int): Int = value | ||
} | ||
} | ||
|
||
object BitSetKeySyntax { | ||
implicit class BitSetKeyOps1[T](value: T)(implicit ev: BitSetKey[T]) { | ||
def toInt: Int = ev.toInt(value) | ||
} | ||
|
||
implicit class BitSetKeyOps2[T](value: Int)(implicit ev: BitSetKey[T]) { | ||
def fromInt: T = ev.fromInt(value) | ||
} | ||
} | ||
|
||
final class MutableBitSet[T: BitSetKey]( | ||
private val underlying: mutable.BitSet | ||
) { | ||
private def this() = | ||
this(mutable.BitSet.empty) | ||
|
||
def add(value: T): Boolean = | ||
underlying.add(value.toInt) | ||
|
||
def size: Int = | ||
underlying.size | ||
|
||
def filterNot( | ||
predicate: T => Boolean | ||
): MutableBitSet[T] = | ||
new MutableBitSet(underlying.filterNot(x => predicate(x.fromInt))) | ||
|
||
def count( | ||
predicate: T => Boolean | ||
): Int = | ||
underlying.count(x => predicate(x.fromInt)) | ||
} | ||
|
||
object MutableBitSet { | ||
def apply[T: BitSetKey](values: T*): MutableBitSet[T] = { | ||
val result = new MutableBitSet[T]() | ||
values.foreach(result.add) | ||
result | ||
} | ||
} |