forked from scala/scala
-
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.
Merge pull request scala#37 from scala/bitset
Sketch mutable and immutable BitSets
- Loading branch information
Showing
8 changed files
with
145 additions
and
10 deletions.
There are no files selected for viewing
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,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 | ||
|
||
} |
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,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] |
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,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
14
src/main/scala/strawman/collection/immutable/SortedSet.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 |
---|---|---|
@@ -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] |
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,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] = ??? | ||
|
||
} |
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
12 changes: 12 additions & 0 deletions
12
src/main/scala/strawman/collection/mutable/SortedSet.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,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]] |
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