diff options
author | michelou <michelou@epfl.ch> | 2004-09-24 16:42:24 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2004-09-24 16:42:24 +0000 |
commit | af63f742e8e0005fa8ccecdad77b05dc297b51af (patch) | |
tree | 907fb7c0eab76af57efe598462e73542b97855a6 | |
parent | 6f574e4004d22c22b323e1f4f99f86578e25fe84 (diff) | |
download | scala-af63f742e8e0005fa8ccecdad77b05dc297b51af.tar.gz scala-af63f742e8e0005fa8ccecdad77b05dc297b51af.tar.bz2 scala-af63f742e8e0005fa8ccecdad77b05dc297b51af.zip |
- added methods 'equals' and 'toString'.
-rw-r--r-- | sources/scala/collection/BitSet.scala | 47 |
1 files changed, 44 insertions, 3 deletions
diff --git a/sources/scala/collection/BitSet.scala b/sources/scala/collection/BitSet.scala index 522b93bf2a..55162c4022 100644 --- a/sources/scala/collection/BitSet.scala +++ b/sources/scala/collection/BitSet.scala @@ -11,7 +11,7 @@ package scala.collection; /** The class <code>BitSet</code> ... * - * @author Burak Emir + * @author Burak Emir, Stephane Micheloud * @version 1.0 */ abstract class BitSet with Function1[Int,Boolean] { @@ -29,8 +29,9 @@ abstract class BitSet with Function1[Int,Boolean] { def next: Boolean = { i = i + 1; apply(i-1) } } - /** returns the subset of <code>[0..size]</code> whose elements are - * indices of bits set to <code>v</code>. + /** + * Returns the subset of <code>[0..size]</code> whose elements are + * indices of bits set to <code>v</code>. * * @param v */ @@ -45,4 +46,44 @@ abstract class BitSet with Function1[Int,Boolean] { res } + /** + * Checks if two bitsets are structurally identical. + * + * @return true, iff both bitsets contain the same sequence of elements. + */ + override def equals(that: Any): Boolean = + that.isInstanceOf[BitSet] && + { val other = that.asInstanceOf[BitSet]; + (size == other.size) && + (Iterator.range(0, size) forall { i => apply(i) == other.apply(i)}); + }; + + /** + * Returns a string representation of this bitset in hexadecimal form, + * e.g. the bitset 001000001100 (12 bits) is represented as "20c" and + * the bitset 00100000110 (11 bits) as "106". + * + * @return the string representation for this bitset + */ + override def toString() = { + val sb = new StringBuffer(); + val n = size % 4; + if (n > 0) { + val x = (if (apply(0)) 4 else 0) + + (if (n > 1 && apply(1)) 2 else 0) + + (if (n > 2 && apply(2)) 1 else 0); + sb.append(Integer.toHexString(x)); + } + var i = n; + while (i < size) { + val x = (if (apply(i)) 8 else 0) + + (if (apply(i+1)) 4 else 0) + + (if (apply(i+2)) 2 else 0) + + (if (apply(i+3)) 1 else 0); + sb.append(Integer.toHexString(x)); + i = i + 4 + } + sb.toString() + } + } |