Skip to content
This repository
Browse code

Improved BitSet implementations

- Mutable and immutable BitSets now extend SortedSet, using a fixed
  Ordering.Int and an efficient bit mask based rangeImpl()
- fromArray methods in both implementations are deprecated in favor of
  new fromBitMask and fromBitMaskNoCopy methods
- New toBitMask method for converting bit sets back to Array[Long] bit
  masks
- immutable.BitSet uses a more efficient Builder, based on
  mutable.BitSet (closes SI-4647)
- Delete scala.tools.nsc.util.BitSet (not used anywhere)

Review by @odersky
  • Loading branch information...
commit 2d0f82898dc6e3998db425dbab571ba297deda41 1 parent 51f5831
Stefan Zeiger authored December 01, 2011
159  src/compiler/scala/tools/nsc/util/BitSet.scala
... ...
@@ -1,159 +0,0 @@
1  
-package scala.tools.nsc
2  
-package util
3  
-
4  
-import BitSet._
5  
-
6  
-abstract class BitSet {
7  
-
8  
-  protected def nwords: Int
9  
-  protected def word(idx: Int): Long
10  
-  protected def updateWord(idx: Int, w: Long): BitSet
11  
-
12  
-  def + (elem: Int): BitSet = {
13  
-    require(elem >= 0)
14  
-    if (contains(elem)) this
15  
-    else {
16  
-      val idx = elem >> LogWL
17  
-      updateWord(idx, word(idx) | (1L << elem))
18  
-    }
19  
-  }
20  
-
21  
-  def - (elem: Int): BitSet = {
22  
-    require(elem >= 0)
23  
-    if (contains(elem)) {
24  
-      val idx = elem >> LogWL
25  
-      updateWord(idx, word(idx) & ~(1L << elem))
26  
-    } else this
27  
-  }
28  
-
29  
-  def | (other: BitSet): BitSet = {
30  
-    val len = this.nwords max other.nwords
31  
-    val words = new Array[Long](len)
32  
-    for (idx <- 0 until len)
33  
-      words(idx) = this.word(idx) | other.word(idx)
34  
-    fromArray(words)
35  
-  }
36  
-
37  
-  def & (other: BitSet): BitSet = {
38  
-    val len = this.nwords min other.nwords
39  
-    val words = new Array[Long](len)
40  
-    for (idx <- 0 until len)
41  
-      words(idx) = this.word(idx) & other.word(idx)
42  
-    fromArray(words)
43  
-  }
44  
-
45  
-  def &~ (other: BitSet): BitSet = {
46  
-    val len = this.nwords
47  
-    val words = new Array[Long](len)
48  
-    for (idx <- 0 until len)
49  
-      words(idx) = this.word(idx) & ~other.word(idx)
50  
-    fromArray(words)
51  
-  }
52  
-
53  
-  def ^ (other: BitSet): BitSet = {
54  
-    val len = this.nwords max other.nwords
55  
-    val words = new Array[Long](len)
56  
-    for (idx <- 0 until len)
57  
-      words(idx) = this.word(idx) ^ other.word(idx)
58  
-    fromArray(words)
59  
-  }
60  
-
61  
-  def contains(elem: Int): Boolean =
62  
-    0 <= elem && (word(elem >> LogWL) & (1L << elem)) != 0L
63  
-
64  
-  def subSet(other: BitSet): Boolean =
65  
-    (0 until nwords) forall (idx => (this.word(idx) & ~ other.word(idx)) == 0L)
66  
-
67  
-  override def equals(other: Any) = other match {
68  
-    case that: BitSet =>
69  
-      (0 until (this.nwords max that.nwords)) forall (idx => this.word(idx) == that.word(idx))
70  
-    case _ =>
71  
-      false
72  
-  }
73  
-
74  
-  override def hashCode: Int = {
75  
-    import scala.util.MurmurHash3._
76  
-    var h = hashSeed
77  
-    var i = 0; while(i < nwords) {
78  
-      val w = word(i)
79  
-      h = mix(h, (w >>> 32).toInt)
80  
-      h = mix(h, w.toInt)
81  
-      i += 1
82  
-    }
83  
-    finalizeHash(h, nwords)
84  
-  }
85  
-
86  
-  def addString(sb: StringBuilder, start: String, sep: String, end: String) {
87  
-    sb append start
88  
-    var pre = ""
89  
-    for (i <- 0 until nwords * WordLength)
90  
-      if (contains(i)) {
91  
-        sb append pre append i
92  
-        pre = sep
93  
-      }
94  
-    sb append end
95  
-  }
96  
-
97  
-  def mkString(start: String, sep: String, end: String) = {
98  
-    val sb = new StringBuilder
99  
-    addString(sb, start, sep, end)
100  
-    sb.toString
101  
-  }
102  
-
103  
-  override def toString = mkString("BitSet(", ", ", ")")
104  
-}
105  
-
106  
-object BitSet {
107  
-
108  
-  private final val WordLength = 64
109  
-  private final val LogWL = 6
110  
-  private val hashSeed = "BitSet".hashCode
111  
-
112  
-  val empty: BitSet = new BitSet1(0L)
113  
-
114  
-  def apply(elems: Int*) = (empty /: elems) (_ + _)
115  
-
116  
-  def fromArray(elems: Array[Long]) = {
117  
-    val len = elems.length
118  
-    if (len == 0) empty
119  
-    else if (len == 1) new BitSet1(elems(0))
120  
-    else if (len == 2) new BitSet2(elems(0), elems(1))
121  
-    else new BitSetN(elems)
122  
-  }
123  
-
124  
-  private def updateArray(elems: Array[Long], idx: Int, w: Long): BitSet = {
125  
-    var len = elems.length
126  
-    while (len > 0 && (elems(len - 1) == 0L || w == 0L && idx == len - 1)) len -= 1
127  
-    var newlen = len
128  
-    if (idx >= newlen && w != 0L) newlen = idx + 1
129  
-    val newelems = new Array[Long](newlen)
130  
-    Array.copy(elems, 0, newelems, 0, len)
131  
-    if (idx < newlen) newelems(idx) = w
132  
-    else assert(w == 0L)
133  
-    fromArray(newelems)
134  
-  }
135  
-
136  
-  class BitSet1(val elems: Long) extends BitSet {
137  
-    protected def nwords = 1
138  
-    protected def word(idx: Int) = if (idx == 0) elems else 0L
139  
-    protected def updateWord(idx: Int, w: Long): BitSet =
140  
-      if (idx == 0) new BitSet1(w)
141  
-      else if (idx == 1) new BitSet2(elems, w)
142  
-      else updateArray(Array(elems), idx, w)
143  
-  }
144  
-
145  
-  class BitSet2(val elems0: Long, elems1: Long) extends BitSet {
146  
-    protected def nwords = 2
147  
-    protected def word(idx: Int) = if (idx == 0) elems0 else if (idx == 1) elems1 else 0L
148  
-    protected def updateWord(idx: Int, w: Long): BitSet =
149  
-      if (idx == 0) new BitSet2(w, elems1)
150  
-      else if (idx == 1) new BitSet2(elems0, w)
151  
-      else updateArray(Array(elems0, elems1), idx, w)
152  
-  }
153  
-
154  
-  class BitSetN(val elems: Array[Long]) extends BitSet {
155  
-    protected def nwords = elems.length
156  
-    protected def word(idx: Int) = if (idx < nwords) elems(idx) else 0L
157  
-    protected def updateWord(idx: Int, w: Long): BitSet = updateArray(elems, idx, w)
158  
-  }
159  
-}
2  src/library/scala/collection/BitSet.scala
@@ -15,7 +15,7 @@ import generic._
15 15
 /** A common base class for mutable and immutable bitsets.
16 16
  *  $bitsetinfo
17 17
  */
18  
-trait BitSet extends Set[Int]
  18
+trait BitSet extends SortedSet[Int]
19 19
                 with BitSetLike[BitSet] {
20 20
   override def empty: BitSet = BitSet.empty
21 21
 }
53  src/library/scala/collection/BitSetLike.scala
@@ -32,7 +32,7 @@ import mutable.StringBuilder
32 32
  *  @define coll bitset
33 33
  *  @define Coll BitSet
34 34
  */
35  
-trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, This] { self =>
  35
+trait BitSetLike[+This <: BitSetLike[This] with SortedSet[Int]] extends SortedSetLike[Int, This] { self =>
36 36
 
37 37
   def empty: This
38 38
 
@@ -46,7 +46,19 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
46 46
 
47 47
   /** Creates a new set of this kind from an array of longs
48 48
    */
49  
-  protected def fromArray(elems: Array[Long]): This
  49
+  protected def fromBitMaskNoCopy(elems: Array[Long]): This
  50
+
  51
+  /** Creates a bit mask for this set as a new array of longs
  52
+   */
  53
+  def toBitMask: Array[Long] = {
  54
+    val a = new Array[Long](nwords)
  55
+    var i = a.length
  56
+    while(i > 0) {
  57
+      i -= 1
  58
+      a(i) = word(i)
  59
+    }
  60
+    a
  61
+  }
50 62
 
51 63
   override def size: Int = {
52 64
     var s = 0
@@ -58,6 +70,35 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
58 70
     s
59 71
   }
60 72
 
  73
+  implicit def ordering: Ordering[Int] = Ordering.Int
  74
+
  75
+  def rangeImpl(from: Option[Int], until: Option[Int]): This = {
  76
+    val a = toBitMask
  77
+    val len = a.length
  78
+    if(from.isDefined) {
  79
+      var f = from.get
  80
+      var pos = 0
  81
+      while(f >= 64 && pos < len) {
  82
+        f -= 64
  83
+        a(pos) = 0
  84
+        pos += 1
  85
+      }
  86
+      if(f > 0 && pos < len) a(pos) &= ~((1L << f)-1)
  87
+    }
  88
+    if(until.isDefined) {
  89
+      val u = until.get
  90
+      val w = u / 64
  91
+      val b = u % 64
  92
+      var clearw = w+1
  93
+      while(clearw < len) {
  94
+        a(clearw) = 0
  95
+        clearw += 1
  96
+      }
  97
+      if(w < len) a(w) &= (1L << b)-1
  98
+    }
  99
+    fromBitMaskNoCopy(a)
  100
+  }
  101
+
61 102
   def iterator: Iterator[Int] = new AbstractIterator[Int] {
62 103
     private var current = 0
63 104
     private val end = nwords * WordLength
@@ -91,7 +132,7 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
91 132
     val words = new Array[Long](len)
92 133
     for (idx <- 0 until len)
93 134
       words(idx) = this.word(idx) | other.word(idx)
94  
-    fromArray(words)
  135
+    fromBitMaskNoCopy(words)
95 136
   }
96 137
 
97 138
   /** Computes the intersection between this bitset and another bitset by performing
@@ -105,7 +146,7 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
105 146
     val words = new Array[Long](len)
106 147
     for (idx <- 0 until len)
107 148
       words(idx) = this.word(idx) & other.word(idx)
108  
-    fromArray(words)
  149
+    fromBitMaskNoCopy(words)
109 150
   }
110 151
 
111 152
   /** Computes the difference of this bitset and another bitset by performing
@@ -120,7 +161,7 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
120 161
     val words = new Array[Long](len)
121 162
     for (idx <- 0 until len)
122 163
       words(idx) = this.word(idx) & ~other.word(idx)
123  
-    fromArray(words)
  164
+    fromBitMaskNoCopy(words)
124 165
   }
125 166
 
126 167
   /** Computes the symmetric difference of this bitset and another bitset by performing
@@ -135,7 +176,7 @@ trait BitSetLike[+This <: BitSetLike[This] with Set[Int]] extends SetLike[Int, T
135 176
     val words = new Array[Long](len)
136 177
     for (idx <- 0 until len)
137 178
       words(idx) = this.word(idx) ^ other.word(idx)
138  
-    fromArray(words)
  179
+    fromBitMaskNoCopy(words)
139 180
   }
140 181
 
141 182
   def contains(elem: Int): Boolean =
4  src/library/scala/collection/SortedSet.scala
@@ -27,5 +27,7 @@ trait SortedSet[A] extends Set[A] with SortedSetLike[A, SortedSet[A]] {
27 27
  */
28 28
 object SortedSet extends SortedSetFactory[SortedSet] {
29 29
   def empty[A](implicit ord: Ordering[A]): immutable.SortedSet[A] = immutable.SortedSet.empty[A](ord)
30  
-  implicit def canBuildFrom[A](implicit ord: Ordering[A]): CanBuildFrom[Coll, A, SortedSet[A]] = new SortedSetCanBuildFrom[A]
  30
+  def canBuildFrom[A](implicit ord: Ordering[A]): CanBuildFrom[Coll, A, SortedSet[A]] = newCanBuildFrom[A]
  31
+  // Force a declaration here so that BitSet's (which does not inherit from SortedSetFactory) can be more specific
  32
+  override implicit def newCanBuildFrom[A](implicit ord : Ordering[A]) : CanBuildFrom[Coll, A, SortedSet[A]] = super.newCanBuildFrom
31 33
 }
49  src/library/scala/collection/immutable/BitSet.scala
@@ -22,13 +22,16 @@ import mutable.{ Builder, SetBuilder }
22 22
  */
23 23
 @SerialVersionUID(1611436763290191562L)
24 24
 abstract class BitSet extends scala.collection.AbstractSet[Int]
25  
-                         with Set[Int]
  25
+                         with SortedSet[Int]
26 26
                          with scala.collection.BitSet
27 27
                          with BitSetLike[BitSet]
28 28
                          with Serializable {
29 29
   override def empty = BitSet.empty
30 30
 
31  
-  def fromArray(elems: Array[Long]): BitSet = BitSet.fromArray(elems)
  31
+  @deprecated("Use BitSet.fromBitMask[NoCopy] instead of fromArray", "2.10")
  32
+  def fromArray(elems: Array[Long]): BitSet = fromBitMaskNoCopy(elems)
  33
+
  34
+  protected def fromBitMaskNoCopy(elems: Array[Long]): BitSet = BitSet.fromBitMaskNoCopy(elems)
32 35
 
33 36
   /** Update word at index `idx`; enlarge set if `idx` outside range of set.
34 37
    */
@@ -64,14 +67,38 @@ object BitSet extends BitSetFactory[BitSet] {
64 67
   /** The empty bitset */
65 68
   val empty: BitSet = new BitSet1(0L)
66 69
 
67  
-  /** An adding builder for immutable Sets. */
68  
-  def newBuilder: Builder[Int, BitSet] = new SetBuilder[Int, BitSet](empty)
  70
+  /** A builder that takes advantage of mutable BitSets. */
  71
+  def newBuilder: Builder[Int, BitSet] = new Builder[Int, BitSet] {
  72
+    private[this] val b = new mutable.BitSet
  73
+    def += (x: Int) = { b += x; this }
  74
+    def clear() = b.clear
  75
+    def result() = b.toImmutable
  76
+  }
69 77
 
70 78
   /** $bitsetCanBuildFrom */
71 79
   implicit def canBuildFrom: CanBuildFrom[BitSet, Int, BitSet] = bitsetCanBuildFrom
72 80
 
73 81
   /** A bitset containing all the bits in an array */
74  
-  def fromArray(elems: Array[Long]): BitSet = {
  82
+  @deprecated("Use fromBitMask[NoCopy] instead of fromArray", "2.10")
  83
+  def fromArray(elems: Array[Long]): BitSet = fromBitMaskNoCopy(elems)
  84
+
  85
+  /** A bitset containing all the bits in an array */
  86
+  def fromBitMask(elems: Array[Long]): BitSet = {
  87
+    val len = elems.length
  88
+    if (len == 0) empty
  89
+    else if (len == 1) new BitSet1(elems(0))
  90
+    else if (len == 2) new BitSet2(elems(0), elems(1))
  91
+    else {
  92
+      val a = new Array[Long](len)
  93
+      Array.copy(elems, 0, a, 0, len)
  94
+      new BitSetN(a)
  95
+    }
  96
+  }
  97
+
  98
+  /** A bitset containing all the bits in an array, wrapping the existing
  99
+   *  array without copying.
  100
+   */
  101
+  def fromBitMaskNoCopy(elems: Array[Long]): BitSet = {
75 102
     val len = elems.length
76 103
     if (len == 0) empty
77 104
     else if (len == 1) new BitSet1(elems(0))
@@ -85,7 +112,7 @@ object BitSet extends BitSetFactory[BitSet] {
85 112
     protected def updateWord(idx: Int, w: Long): BitSet =
86 113
       if (idx == 0) new BitSet1(w)
87 114
       else if (idx == 1) new BitSet2(elems, w)
88  
-      else fromArray(updateArray(Array(elems), idx, w))
  115
+      else fromBitMaskNoCopy(updateArray(Array(elems), idx, w))
89 116
   }
90 117
 
91 118
   class BitSet2(val elems0: Long, elems1: Long) extends BitSet {
@@ -94,12 +121,18 @@ object BitSet extends BitSetFactory[BitSet] {
94 121
     protected def updateWord(idx: Int, w: Long): BitSet =
95 122
       if (idx == 0) new BitSet2(w, elems1)
96 123
       else if (idx == 1) new BitSet2(elems0, w)
97  
-      else fromArray(updateArray(Array(elems0, elems1), idx, w))
  124
+      else fromBitMaskNoCopy(updateArray(Array(elems0, elems1), idx, w))
98 125
   }
99 126
 
  127
+  /** The implementing class for bit sets with elements >= 128 (exceeding
  128
+   *  the capacity of two long values). The constructor wraps an existing
  129
+   *  bit mask without copying, thus exposing a mutable part of the internal
  130
+   *  implementation. Care needs to be taken not to modify the exposed
  131
+   *  array.  
  132
+   */
100 133
   class BitSetN(val elems: Array[Long]) extends BitSet {
101 134
     protected def nwords = elems.length
102 135
     protected def word(idx: Int) = if (idx < nwords) elems(idx) else 0L
103  
-    protected def updateWord(idx: Int, w: Long): BitSet = fromArray(updateArray(elems, idx, w))
  136
+    protected def updateWord(idx: Int, w: Long): BitSet = fromBitMaskNoCopy(updateArray(elems, idx, w))
104 137
   }
105 138
 }
4  src/library/scala/collection/immutable/SortedSet.scala
@@ -35,6 +35,8 @@ trait SortedSet[A] extends Set[A] with scala.collection.SortedSet[A] with Sorted
35 35
  */
36 36
 object SortedSet extends ImmutableSortedSetFactory[SortedSet] {
37 37
   /** $sortedSetCanBuildFromInfo */
38  
-  implicit def canBuildFrom[A](implicit ord: Ordering[A]): CanBuildFrom[Coll, A, SortedSet[A]] = new SortedSetCanBuildFrom[A]
  38
+  def canBuildFrom[A](implicit ord: Ordering[A]): CanBuildFrom[Coll, A, SortedSet[A]] = newCanBuildFrom[A]
39 39
   def empty[A](implicit ord: Ordering[A]): SortedSet[A] = TreeSet.empty[A]
  40
+  // Force a declaration here so that BitSet's (which does not inherit from SortedSetFactory) can be more specific
  41
+  override implicit def newCanBuildFrom[A](implicit ord : Ordering[A]) : CanBuildFrom[Coll, A, SortedSet[A]] = super.newCanBuildFrom
40 42
 }
19  src/library/scala/collection/mutable/BitSet.scala
@@ -34,7 +34,7 @@ import BitSetLike.{LogWL, updateArray}
34 34
  */
35 35
 @SerialVersionUID(8483111450368547763L)
36 36
 class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
37  
-                                                  with Set[Int]
  37
+                                                  with SortedSet[Int]
38 38
                                                   with scala.collection.BitSet
39 39
                                                   with BitSetLike[BitSet]
40 40
                                                   with SetLike[Int, BitSet]
@@ -65,7 +65,7 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
65 65
     elems(idx) = w
66 66
   }
67 67
 
68  
-  protected def fromArray(words: Array[Long]): BitSet = new BitSet(words)
  68
+  protected def fromBitMaskNoCopy(words: Array[Long]): BitSet = new BitSet(words)
69 69
 
70 70
   override def add(elem: Int): Boolean = {
71 71
     require(elem >= 0)
@@ -100,7 +100,7 @@ class BitSet(protected var elems: Array[Long]) extends AbstractSet[Int]
100 100
    *
101 101
    *  @return an immutable set containing all the elements of this set.
102 102
    */
103  
-  def toImmutable = immutable.BitSet.fromArray(elems)
  103
+  def toImmutable = immutable.BitSet.fromBitMaskNoCopy(elems)
104 104
 
105 105
   override def clone(): BitSet = {
106 106
     val elems1 = new Array[Long](elems.length)
@@ -121,4 +121,17 @@ object BitSet extends BitSetFactory[BitSet] {
121 121
 
122 122
   /** $bitsetCanBuildFrom */
123 123
   implicit def canBuildFrom: CanBuildFrom[BitSet, Int, BitSet] = bitsetCanBuildFrom
  124
+
  125
+  /** A bitset containing all the bits in an array */
  126
+  def fromBitMask(elems: Array[Long]): BitSet = {
  127
+    val len = elems.length
  128
+    val a = new Array[Long](len)
  129
+    Array.copy(elems, 0, a, 0, len)
  130
+    new BitSet(a)
  131
+  }
  132
+
  133
+  /** A bitset containing all the bits in an array, wrapping the existing
  134
+   *  array without copying.
  135
+   */
  136
+  def fromBitMaskNoCopy(elems: Array[Long]): BitSet = new BitSet(elems)
124 137
 }
46  test/files/run/bitsets.check
@@ -14,6 +14,29 @@ mi0 = BitSet(2)
14 14
 mi1 = BitSet(2)
15 15
 mi2 = BitSet(2)
16 16
 
  17
+m2_m0 = List(1010101010101010101010101)
  18
+m2_m2 = List(ffffffffffffffff, ffffffffffffffff, ffffffffffffffff, ffffffffffffffff, 1, 0, 0, 0)
  19
+m2_m0c = true
  20
+m2_m1c = true
  21
+m2_m2c = true
  22
+m2_m3c = true
  23
+m2_i0 = true
  24
+m2_i1 = true
  25
+m2_i2 = true
  26
+m2_i3 = true
  27
+m2_f0 = true
  28
+m2_f1 = true
  29
+m2_f2 = true
  30
+m2_f3 = true
  31
+m2_t0 = true
  32
+m2_t1 = true
  33
+m2_t2 = true
  34
+m2_t3 = true
  35
+m2_r0 = true
  36
+m2_r1 = true
  37
+m2_r2 = true
  38
+m2_r3 = true
  39
+
17 40
 is0 = BitSet()
18 41
 is1 = BitSet()
19 42
 is2 = BitSet(2)
@@ -31,3 +54,26 @@ ia1 = List()
31 54
 ia2 = List(2)
32 55
 ia3 = List()
33 56
 
  57
+i2_m0 = List(1010101010101010101010101)
  58
+i2_m2 = List(ffffffffffffffff, ffffffffffffffff, ffffffffffffffff, ffffffffffffffff, 1)
  59
+i2_m0c = true
  60
+i2_m1c = true
  61
+i2_m2c = true
  62
+i2_m3c = true
  63
+i2_i0 = true
  64
+i2_i1 = true
  65
+i2_i2 = true
  66
+i2_i3 = true
  67
+i2_f0 = true
  68
+i2_f1 = true
  69
+i2_f2 = true
  70
+i2_f3 = true
  71
+i2_t0 = true
  72
+i2_t1 = true
  73
+i2_t2 = true
  74
+i2_t3 = true
  75
+i2_r0 = true
  76
+i2_r1 = true
  77
+i2_r2 = true
  78
+i2_r3 = true
  79
+
85  test/files/run/bitsets.scala
@@ -39,6 +39,48 @@ object TestMutable {
39 39
   Console.println
40 40
 }
41 41
 
  42
+object TestMutable2 {
  43
+  import scala.collection.mutable.BitSet
  44
+  import scala.collection.immutable.TreeSet
  45
+
  46
+  val l0 = 0 to 24 by 2 toList
  47
+  val l1 = (190 to 255 toList) reverse
  48
+  val l2 = (0 to 256 toList)
  49
+  val l3 = (1 to 200 by 2 toList) reverse
  50
+  val t0 = TreeSet(l0: _*)
  51
+  val t1 = TreeSet(l1: _*)
  52
+  val t2 = TreeSet(l2: _*)
  53
+  val t3 = TreeSet(l3: _*)
  54
+  val b0 = BitSet(l0: _*)
  55
+  val b1 = BitSet(l1: _*)
  56
+  val b2 = BitSet(l2: _*)
  57
+  val b3 = BitSet(l3: _*)
  58
+
  59
+  println("m2_m0 = " + b0.toBitMask.toList.map(_.toBinaryString))
  60
+  println("m2_m2 = " + b2.toBitMask.toList.map(_.toHexString))
  61
+  println("m2_m0c = " + (BitSet.fromBitMask(b0.toBitMask) == b0))
  62
+  println("m2_m1c = " + (BitSet.fromBitMask(b1.toBitMask) == b1))
  63
+  println("m2_m2c = " + (BitSet.fromBitMask(b2.toBitMask) == b2))
  64
+  println("m2_m3c = " + (BitSet.fromBitMask(b3.toBitMask) == b3))
  65
+  println("m2_i0 = " + (t0 == b0))
  66
+  println("m2_i1 = " + (t1 == b1))
  67
+  println("m2_i2 = " + (t2 == b2))
  68
+  println("m2_i3 = " + (t3 == b3))
  69
+  println("m2_f0 = " + (t0.from(42) == b0.from(42)))
  70
+  println("m2_f1 = " + (t1.from(42) == b1.from(42)))
  71
+  println("m2_f2 = " + (t2.from(42) == b2.from(42)))
  72
+  println("m2_f3 = " + (t3.from(42) == b3.from(42)))
  73
+  println("m2_t0 = " + (t0.to(195) == b0.to(195)))
  74
+  println("m2_t1 = " + (t1.to(195) == b1.to(195)))
  75
+  println("m2_t2 = " + (t2.to(195) == b2.to(195)))
  76
+  println("m2_t3 = " + (t3.to(195) == b3.to(195)))
  77
+  println("m2_r0 = " + (t0.range(43,194) == b0.range(43,194)))
  78
+  println("m2_r1 = " + (t1.range(43,194) == b1.range(43,194)))
  79
+  println("m2_r2 = " + (t2.range(43,194) == b2.range(43,194)))
  80
+  println("m2_r3 = " + (t3.range(43,194) == b3.range(43,194)))
  81
+  println
  82
+}
  83
+
42 84
 object TestImmutable {
43 85
   import scala.collection.immutable.BitSet
44 86
 
@@ -69,9 +111,52 @@ object TestImmutable {
69 111
   Console.println
70 112
 }
71 113
 
  114
+object TestImmutable2 {
  115
+  import scala.collection.immutable.{BitSet, TreeSet}
  116
+
  117
+  val l0 = 0 to 24 by 2 toList
  118
+  val l1 = (190 to 255 toList) reverse
  119
+  val l2 = (0 to 256 toList)
  120
+  val l3 = (1 to 200 by 2 toList) reverse
  121
+  val t0 = TreeSet(l0: _*)
  122
+  val t1 = TreeSet(l1: _*)
  123
+  val t2 = TreeSet(l2: _*)
  124
+  val t3 = TreeSet(l3: _*)
  125
+  val b0 = BitSet(l0: _*)
  126
+  val b1 = BitSet(l1: _*)
  127
+  val b2 = BitSet(l2: _*)
  128
+  val b3 = BitSet(l3: _*)
  129
+
  130
+  println("i2_m0 = " + b0.toBitMask.toList.map(_.toBinaryString))
  131
+  println("i2_m2 = " + b2.toBitMask.toList.map(_.toHexString))
  132
+  println("i2_m0c = " + (BitSet.fromBitMask(b0.toBitMask) == b0))
  133
+  println("i2_m1c = " + (BitSet.fromBitMask(b1.toBitMask) == b1))
  134
+  println("i2_m2c = " + (BitSet.fromBitMask(b2.toBitMask) == b2))
  135
+  println("i2_m3c = " + (BitSet.fromBitMask(b3.toBitMask) == b3))
  136
+  println("i2_i0 = " + (t0 == b0))
  137
+  println("i2_i1 = " + (t1 == b1))
  138
+  println("i2_i2 = " + (t2 == b2))
  139
+  println("i2_i3 = " + (t3 == b3))
  140
+  println("i2_f0 = " + (t0.from(42) == b0.from(42)))
  141
+  println("i2_f1 = " + (t1.from(42) == b1.from(42)))
  142
+  println("i2_f2 = " + (t2.from(42) == b2.from(42)))
  143
+  println("i2_f3 = " + (t3.from(42) == b3.from(42)))
  144
+  println("i2_t0 = " + (t0.to(195) == b0.to(195)))
  145
+  println("i2_t1 = " + (t1.to(195) == b1.to(195)))
  146
+  println("i2_t2 = " + (t2.to(195) == b2.to(195)))
  147
+  println("i2_t3 = " + (t3.to(195) == b3.to(195)))
  148
+  println("i2_r0 = " + (t0.range(77,194) == b0.range(77,194)))
  149
+  println("i2_r1 = " + (t1.range(77,194) == b1.range(77,194)))
  150
+  println("i2_r2 = " + (t2.range(77,194) == b2.range(77,194)))
  151
+  println("i2_r3 = " + (t3.range(77,194) == b3.range(77,194)))
  152
+  println
  153
+}
  154
+
72 155
 object Test extends App {
73 156
   TestMutable
  157
+  TestMutable2
74 158
   TestImmutable
  159
+  TestImmutable2
75 160
 }
76 161
 
77 162
 //############################################################################

1 note on commit 2d0f828

What about https://issues.scala-lang.org/browse/SI-2196, “Use java.lang.Long.bitCount in BitSetLike”?

Please sign in to comment.
Something went wrong with that request. Please try again.