summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorburaq <buraq@epfl.ch>2004-09-28 15:41:00 +0000
committerburaq <buraq@epfl.ch>2004-09-28 15:41:00 +0000
commite6a955c2fca7fb56450616c1c5463624a728543a (patch)
tree3ebee9b2f7f2850cf7c1aac93a2c7bd331692c1c /sources
parentd214dd6c6ca7f409139270cf3b0c80598d07f56e (diff)
downloadscala-e6a955c2fca7fb56450616c1c5463624a728543a.tar.gz
scala-e6a955c2fca7fb56450616c1c5463624a728543a.tar.bz2
scala-e6a955c2fca7fb56450616c1c5463624a728543a.zip
bitset addition, unit test addition
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/collection/BitSet.scala3
-rw-r--r--sources/scala/collection/immutable/BitSet.scala4
-rw-r--r--sources/scala/collection/mutable/BitSet.scala12
-rw-r--r--sources/scala/testing/UnitTest.scala4
4 files changed, 16 insertions, 7 deletions
diff --git a/sources/scala/collection/BitSet.scala b/sources/scala/collection/BitSet.scala
index 55162c4022..9f32a62716 100644
--- a/sources/scala/collection/BitSet.scala
+++ b/sources/scala/collection/BitSet.scala
@@ -86,4 +86,7 @@ abstract class BitSet with Function1[Int,Boolean] {
sb.toString()
}
+ /** returns number of Int cells needed to store n bits */
+ protected def memsize(n:Int) = size >>> 5 + { if(size < 32) 1 else 0 };
+
}
diff --git a/sources/scala/collection/immutable/BitSet.scala b/sources/scala/collection/immutable/BitSet.scala
index e0a52c43a0..8677be1beb 100644
--- a/sources/scala/collection/immutable/BitSet.scala
+++ b/sources/scala/collection/immutable/BitSet.scala
@@ -43,10 +43,10 @@ class BitSet(n:Int, ba: Array[Int], copy: Boolean) extends collection.BitSet {
that.isInstanceOf[BitSet] &&
{ val other = that.asInstanceOf[BitSet];
(size == other.size) && ( size == 0 || {
- var len = size>>>5;
+ var len = memsize( size );
var i = 0;
var res=true;
- while(( i<= len ) && res ) { // 32 x faster equality check
+ while(( i< len ) && res ) { // 32 x faster equality check
res = array(i) == other.array(i);
i = i + 1;
}
diff --git a/sources/scala/collection/mutable/BitSet.scala b/sources/scala/collection/mutable/BitSet.scala
index e9d4dd6774..278f6d5965 100644
--- a/sources/scala/collection/mutable/BitSet.scala
+++ b/sources/scala/collection/mutable/BitSet.scala
@@ -17,16 +17,14 @@ package scala.collection.mutable ;
class BitSet(initSize: Int) extends scala.collection.BitSet {
/** default constructor, initial size of 16 bits */
- def this() = this( 16 );
-
- final def intSize(size:Int) = { (size >>> 5) + (if( (size & 0x1F)!= 0 ) 1 else 0) };
+ def this() = this( 32 );
class ByteArray with ResizableArray[Int] {
- override protected val initialSize: Int = intSize( initSize );
+ override protected val initialSize: Int = memsize( initSize );
override protected var array: Array[Int] = new Array[Int](initialSize);
/** size of this bitset in nbits */
- def ensureBits(nbits: Int): Unit = ensureSize( intSize( nbits ));
+ def ensureBits(nbits: Int): Unit = ensureSize(memsize( nbits ));
final def and(j: Int, mask:Int): Unit = {
array.update( j, array(j) & mask );
@@ -56,8 +54,10 @@ class BitSet(initSize: Int) extends scala.collection.BitSet {
if( size < nbits ) size = nbits;
}
+ /** calls set or clear for i-th bit */
final def set(i: Int, b: Boolean): Unit = if( b ) set(i) else clear(i);
+ /** sets i-th bit to true. Grows to size i+1 if needed. */
final def set(i: Int): Unit = {
ensureSize(i+1);
val j = (i >>> 5);
@@ -65,6 +65,7 @@ class BitSet(initSize: Int) extends scala.collection.BitSet {
internal.or(j, mask);
}
+ /** clears i-th bit. Grows to size i+1 if needed. */
def clear(i: Int): Unit = {
ensureSize(i+1);
val j = (i >>> 5);
@@ -72,6 +73,7 @@ class BitSet(initSize: Int) extends scala.collection.BitSet {
internal.and(j, ~mask);
}
+ /** gets i-th bit. Grows to size i+1 if needed. */
def apply(i: Int):Boolean = {
val j = (i >>> 5);
val mask = (1 << (i & 0x1F));
diff --git a/sources/scala/testing/UnitTest.scala b/sources/scala/testing/UnitTest.scala
index bfaf311786..2f8878b2eb 100644
--- a/sources/scala/testing/UnitTest.scala
+++ b/sources/scala/testing/UnitTest.scala
@@ -43,6 +43,10 @@ object UnitTest {
else
report.fail( actual.toString(), expected.toString() );
+ def assertTrue( actual: Boolean ):Unit = assertEquals(actual, true);
+ def assertFalse( actual: Boolean ):Unit = assertEquals(actual, false);
+
+
def assertNotEquals[a]( actual: a, expected: a ):Unit =
if( actual != expected )
report.ok