1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
//############################################################################
// Bitsets
//############################################################################
//############################################################################
import scala.language.postfixOps
object TestMutable {
import scala.collection.mutable.BitSet
val ms0 = new BitSet
val ms1 = new BitSet(8)
val ms2 = new BitSet(0)
ms0 += 2
ms1 ++= List(1, 2)
ms1 -= 1
ms1 --= List(1)
ms2(2) = true
ms2(3) = false
Console.println("ms0 = " + ms0)
Console.println("ms1 = " + ms1)
Console.println("ms2 = " + ms2)
Console.println("mb0 = " + ms0.contains(-1))
Console.println("mb1 = " + ms1.contains(2))
Console.println("mb2 = " + ms2.contains(3))
Console.println("xs0 = " + ms0.iterator.toList)
Console.println("xs1 = " + ms1.iterator.toList)
Console.println("xs2 = " + ms2.iterator.toList)
Console.println("ma0 = " + ms0.toList)
Console.println("ma1 = " + ms1.toList)
Console.println("ma2 = " + ms2.toList)
Console.println("mi0 = " + ms0.toImmutable)
Console.println("mi1 = " + ms1.toImmutable)
Console.println("mi2 = " + ms2.toImmutable)
Console.println
val N = 257
val gen = 3
val bs = BitSet((1 until N): _*)
(1 until N).foldLeft(gen) {
case (acc, i) =>
assert(bs.size == N-i, s"Bad size for $bs, expected ${N-i} actual ${bs.size}")
assert(!bs.isEmpty, s"Unexpected isEmpty for $bs")
bs -= acc
acc*gen % N
}
assert(bs.size == 0, s"Expected size == 0 for $bs")
assert(bs.isEmpty, s"Expected isEmpty for $bs")
}
object TestMutable2 {
import scala.collection.mutable.BitSet
import scala.collection.immutable.TreeSet
val l0 = 0 to 24 by 2 toList
val l1 = (190 to 255 toList) reverse
val l2 = (0 to 256 toList)
val l3 = (1 to 200 by 2 toList) reverse
val t0 = TreeSet(l0: _*)
val t1 = TreeSet(l1: _*)
val t2 = TreeSet(l2: _*)
val t3 = TreeSet(l3: _*)
val b0 = BitSet(l0: _*)
val b1 = BitSet(l1: _*)
val b2 = BitSet(l2: _*)
val b3 = BitSet(l3: _*)
println("m2_m0 = " + b0.toBitMask.toList.map(_.toBinaryString))
println("m2_m2 = " + b2.toBitMask.toList.map(_.toHexString))
println("m2_m0c = " + (BitSet.fromBitMask(b0.toBitMask) == b0))
println("m2_m1c = " + (BitSet.fromBitMask(b1.toBitMask) == b1))
println("m2_m2c = " + (BitSet.fromBitMask(b2.toBitMask) == b2))
println("m2_m3c = " + (BitSet.fromBitMask(b3.toBitMask) == b3))
println("m2_i0 = " + (t0 == b0))
println("m2_i1 = " + (t1 == b1))
println("m2_i2 = " + (t2 == b2))
println("m2_i3 = " + (t3 == b3))
println("m2_f0 = " + (t0.from(42) == b0.from(42)))
println("m2_f1 = " + (t1.from(42) == b1.from(42)))
println("m2_f2 = " + (t2.from(42) == b2.from(42)))
println("m2_f3 = " + (t3.from(42) == b3.from(42)))
println("m2_t0 = " + (t0.to(195) == b0.to(195)))
println("m2_t1 = " + (t1.to(195) == b1.to(195)))
println("m2_t2 = " + (t2.to(195) == b2.to(195)))
println("m2_t3 = " + (t3.to(195) == b3.to(195)))
println("m2_r0 = " + (t0.range(43,194) == b0.range(43,194)))
println("m2_r1 = " + (t1.range(43,194) == b1.range(43,194)))
println("m2_r2 = " + (t2.range(43,194) == b2.range(43,194)))
println("m2_r3 = " + (t3.range(43,194) == b3.range(43,194)))
println
}
object TestMutable3 {
import scala.collection.mutable.BitSet
val b0 = BitSet(5, 6)
val b1 = BitSet(7)
val b2 = BitSet(1, 5)
val b3 = BitSet(6, 7)
val b4 = BitSet(6, 7)
b1 |= b0
println(s"b1:$b1")
b2 &= b0
println(s"b2:$b2")
b3 ^= b0
println(s"b3:$b3")
b4 &~= b0
println(s"b4:$b4")
b0 ^= b0 |= b1
println(s"b0:$b0")
}
/***
The memory requirements here are way beyond
what a test should exercise.
object TestMutable4 {
import scala.collection.mutable.BitSet
val bMax = BitSet(Int.MaxValue)
println(s"bMax:$bMax")
bMax.foreach(println)
val bLarge = BitSet(2000000001)
println(s"bLarge:$bLarge")
println(bMax == bLarge)
}
***/
object TestImmutable {
import scala.collection.immutable.BitSet
val is0 = BitSet()
val is1 = BitSet.fromBitMask(Array())
val is2 = BitSet.fromBitMask(Array(4))
val is3 = BitSet.empty
Console.println("is0 = " + is0)
Console.println("is1 = " + is1)
Console.println("is2 = " + is2)
Console.println("is3 = " + is3)
Console.println("ib0 = " + is0.contains(-1))
Console.println("ib1 = " + is1.contains(0))
Console.println("ib2 = " + is2.contains(2))
Console.println("ib3 = " + is3.contains(2))
Console.println("ys0 = " + is0.iterator.toList)
Console.println("ys1 = " + is1.iterator.toList)
Console.println("ys2 = " + is2.iterator.toList)
Console.println("ys3 = " + is3.iterator.toList)
Console.println("ia0 = " + is0.toList)
Console.println("ia1 = " + is1.toList)
Console.println("ia2 = " + is2.toList)
Console.println("ia3 = " + is3.toList)
Console.println
}
object TestImmutable2 {
import scala.collection.immutable.{BitSet, TreeSet}
val l0 = 0 to 24 by 2 toList
val l1 = (190 to 255 toList) reverse
val l2 = (0 to 256 toList)
val l3 = (1 to 200 by 2 toList) reverse
val t0 = TreeSet(l0: _*)
val t1 = TreeSet(l1: _*)
val t2 = TreeSet(l2: _*)
val t3 = TreeSet(l3: _*)
val b0 = BitSet(l0: _*)
val b1 = BitSet(l1: _*)
val b2 = BitSet(l2: _*)
val b3 = BitSet(l3: _*)
println("i2_m0 = " + b0.toBitMask.toList.map(_.toBinaryString))
println("i2_m2 = " + b2.toBitMask.toList.map(_.toHexString))
println("i2_m0c = " + (BitSet.fromBitMask(b0.toBitMask) == b0))
println("i2_m1c = " + (BitSet.fromBitMask(b1.toBitMask) == b1))
println("i2_m2c = " + (BitSet.fromBitMask(b2.toBitMask) == b2))
println("i2_m3c = " + (BitSet.fromBitMask(b3.toBitMask) == b3))
println("i2_i0 = " + (t0 == b0))
println("i2_i1 = " + (t1 == b1))
println("i2_i2 = " + (t2 == b2))
println("i2_i3 = " + (t3 == b3))
println("i2_f0 = " + (t0.from(42) == b0.from(42)))
println("i2_f1 = " + (t1.from(42) == b1.from(42)))
println("i2_f2 = " + (t2.from(42) == b2.from(42)))
println("i2_f3 = " + (t3.from(42) == b3.from(42)))
println("i2_t0 = " + (t0.to(195) == b0.to(195)))
println("i2_t1 = " + (t1.to(195) == b1.to(195)))
println("i2_t2 = " + (t2.to(195) == b2.to(195)))
println("i2_t3 = " + (t3.to(195) == b3.to(195)))
println("i2_r0 = " + (t0.range(77,194) == b0.range(77,194)))
println("i2_r1 = " + (t1.range(77,194) == b1.range(77,194)))
println("i2_r2 = " + (t2.range(77,194) == b2.range(77,194)))
println("i2_r3 = " + (t3.range(77,194) == b3.range(77,194)))
println
}
object Test extends App {
TestMutable
TestMutable2
TestMutable3
// TestMutable4
TestImmutable
TestImmutable2
}
//############################################################################
|