Skip to content

Commit

Permalink
Merge pull request scala#37 from scala/bitset
Browse files Browse the repository at this point in the history
Sketch mutable and immutable BitSets
  • Loading branch information
julienrf committed Feb 27, 2017
2 parents b03e0e3 + 95f0bfa commit 8ef665b
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 10 deletions.
54 changes: 54 additions & 0 deletions src/main/scala/strawman/collection/BitSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package strawman
package collection

import strawman.collection.mutable.Builder

import scala.{Int, Ordering}

/** Base type of bitsets */
trait BitSet
extends SortedSet[Int]
with BitSetLike[BitSet]

/** Base implementation type of bitsets */
trait BitSetLike[+C <: BitSet]
extends SortedSetLike[Int, SortedSet]
with BitSetMonoTransforms[C] {

final def ordering: Ordering[Int] = Ordering.Int

}

trait BitSetMonoTransforms[+C <: BitSet]
extends SetMonoTransforms[Int, C] {

/**
* Computes the symmetric difference of this bitset and another bitset by performing a bitwise “exclusive-or”.
*
* @param other the other bitset to take part in the symmetric difference.
* @return a bitset containing those bits of this bitset or the other bitset that are not contained in both bitsets.
*/
def ^ (other: BitSet): C

/**
* Builds a new bitset by applying a function to all elements of this bitset
* @param f the function to apply to each element.
* @return a new bitset resulting from applying the given function ''f'' to
* each element of this bitset and collecting the results
*/
def map(f: Int => Int): C

}

/** Factory methods for unconstrained collections of kind `*` */
trait BitSetFactories[+C] {

def newBuilder: Builder[Int, C]

final def empty: C = newBuilder.result

final def apply(elems: Int*): C = newBuilder.++=(elems.toStrawman).result

implicit val canBuild: () => Builder[Int, C] = () => newBuilder

}
13 changes: 13 additions & 0 deletions src/main/scala/strawman/collection/SortedSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package strawman.collection

/** Base type of sorted sets */
trait SortedSet[A]
extends Set[A]
with Sorted[A]
with SortedSetLike[A, SortedSet] // Inherited SortedSet operations return a `SortedSet`

trait SortedSetLike[A, +C[X] <: SortedSet[X]]
extends SortedLike[A, C[A]]
with SortedPolyTransforms[A, C]
with SetLike[A, Set] // Inherited Set operations return a `Set`
with SetMonoTransforms[A, C[A]] // Override the return type of Set ops to return C[A]
20 changes: 20 additions & 0 deletions src/main/scala/strawman/collection/immutable/BitSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package strawman
package collection.immutable

import strawman.collection.BitSetFactories
import strawman.collection.mutable.Builder

import scala.Int
import scala.Predef.???

trait BitSet
extends collection.BitSet
with collection.BitSetLike[BitSet]
with SortedSet[Int]
with SetMonoTransforms[Int, BitSet] // Override mono transforms ops to return a BitSet rather than a SortedSet[Int]

object BitSet extends BitSetFactories[BitSet] {

def newBuilder: Builder[Int, BitSet] = ???

}
14 changes: 6 additions & 8 deletions src/main/scala/strawman/collection/immutable/SortedSet.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package strawman.collection.immutable

import strawman.collection.{Sorted, SortedLike, SortedPolyTransforms}
package strawman
package collection.immutable

/** Base trait for sorted sets */
trait SortedSet[A]
extends Set[A]
with Sorted[A]
with SortedSetLike[A, SortedSet] // Inherited SortedSet operations return a `SortedSet`
extends collection.SortedSet[A]
with Set[A]
with SortedSetLike[A, SortedSet]

trait SortedSetLike[A, +C[X] <: SortedSet[X]]
extends SortedLike[A, C[A]]
with SortedPolyTransforms[A, C]
extends collection.SortedSetLike[A, C]
with SetLike[A, Set] // Inherited Set operations return a `Set`
with SetMonoTransforms[A, C[A]] // Override the return type of Set ops to return C[A]
18 changes: 18 additions & 0 deletions src/main/scala/strawman/collection/mutable/BitSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package strawman
package collection.mutable

import strawman.collection.BitSetFactories

import scala.Int
import scala.Predef.???

trait BitSet
extends collection.BitSet
with collection.BitSetLike[BitSet]
with SortedSet[Int]

object BitSet extends BitSetFactories[BitSet] {

def newBuilder: Builder[Int, BitSet] = ???

}
6 changes: 5 additions & 1 deletion src/main/scala/strawman/collection/mutable/Set.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ object Set {
}

trait SetLike[A, +C[X] <: Set[X]]
extends collection.SetLike[A, C]
extends collection.SetLike[A, C]
with SetMonoTransforms[A, C[A]]

trait SetMonoTransforms[A, +Repr]
extends collection.SetMonoTransforms[A, Repr]
12 changes: 12 additions & 0 deletions src/main/scala/strawman/collection/mutable/SortedSet.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package strawman
package collection.mutable

trait SortedSet[A]
extends collection.SortedSet[A]
with Set[A]
with SortedSetLike[A, SortedSet]

trait SortedSetLike[A, +C[X] <: SortedSet[X]]
extends collection.SortedSetLike[A, C]
with SetLike[A, Set]
with SetMonoTransforms[A, C[A]]
18 changes: 17 additions & 1 deletion src/test/scala/strawman/collection/test/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package strawman
package collection.test

import java.lang.String
import scala.{Int, Unit, Array, Option, StringContext, Boolean, Any, Char}
import scala.{Either, Int, Left, Nothing, Unit, Array, Option, StringContext, Boolean, Any, Char}
import scala.Predef.{assert, println, charWrapper}

import collection._
Expand Down Expand Up @@ -333,6 +333,22 @@ class StrawmanTest {
val xs8: immutable.Map[Foo, Int] = xs7
}

def bitSets(xs: immutable.BitSet, ys: BitSet, zs: Set[Int]): Unit = {
val xs1 = xs & zs
val xs2: immutable.BitSet = xs1
val xs3 = xs ^ ys
val xs4: immutable.BitSet = xs3
val b = xs.subsetOf(zs)
val xs5 = xs.map((x: Int) => x + 1) // TODO Remove type annotation when SI-5708 is fixed
val xs6: immutable.BitSet = xs5
val xs7 = (xs: immutable.SortedSet[Int]).map((x: Int) => x.toString)
val xs8: immutable.SortedSet[String] = xs7
val xs9 = (xs: immutable.Set[Int]).map(x => Left(x): Either[Int, Nothing])
val xs10: immutable.Set[Either[Int, Nothing]] = xs9
val xs11 = xs + 42
val xs12: immutable.BitSet = xs11
}

@Test
def mainTest: Unit = {
val ints = List(1, 2, 3)
Expand Down

0 comments on commit 8ef665b

Please sign in to comment.