diff options
author | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-19 13:49:03 +0000 |
---|---|---|
committer | Gilles Dubochet <gilles.dubochet@epfl.ch> | 2005-12-19 13:49:03 +0000 |
commit | ac849228490d5a0e2d3f048d649297d5c59b6ade (patch) | |
tree | 6314f2c06f37e67dec5827c3f94e25cf844a085c /src/library/scala/collection/mutable/BitSet.scala | |
parent | d6c0efe5b4b89a0337f1cdcdabf8c607d81f4ae1 (diff) | |
download | scala-ac849228490d5a0e2d3f048d649297d5c59b6ade.tar.gz scala-ac849228490d5a0e2d3f048d649297d5c59b6ade.tar.bz2 scala-ac849228490d5a0e2d3f048d649297d5c59b6ade.zip |
Switching to the new build system and to the ne...
Switching to the new build system and to the new build system. This is a
MAJOR commit, so be careful when updating.
Diffstat (limited to 'src/library/scala/collection/mutable/BitSet.scala')
-rw-r--r-- | src/library/scala/collection/mutable/BitSet.scala | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/src/library/scala/collection/mutable/BitSet.scala b/src/library/scala/collection/mutable/BitSet.scala new file mode 100644 index 0000000000..40d0bf0529 --- /dev/null +++ b/src/library/scala/collection/mutable/BitSet.scala @@ -0,0 +1,87 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2003, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +** $Id$ +\* */ + +package scala.collection.mutable ; + +/** mutable, resizable bit sets, to represent dense sets of small integers + * Bit indices are between 0..(size-1) inclusive + * @author Burak Emir + * @param initSize: initial size in nbits + */ +[serializable] +class BitSet(initSize: Int) extends scala.collection.BitSet { + import scala.runtime.compat.Platform.arraycopy; + + /** default constructor, initial size of 512 bits */ + def this() = this( 512 ); // ResizableArray.initialSize << 5 + + [serializable] + class ByteArray extends AnyRef with ResizableArray[Int] { + + final def ensureBits(nbits: Int): Unit = { + super[ResizableArray].ensureSize(memsize( nbits )); + } + final def and(j: Int, mask:Int): Unit = { + array.update( j, array(j) & mask ); + } + final def or(j: Int, mask:Int): Unit = { + array.update( j, array(j) | mask ); + } + def get(j:Int, mask:Int):Boolean = { + (array(j) & mask) != 0; + } + def freeze: Array[Int] = { + val arr = new Array[Int]( array.length ); + arraycopy(array, 0, arr, 0, arr.length); + arr + } + } + + /** size of this bitset in nbytes */ + var size: Int = initSize; + protected val internal = new ByteArray(); + internal.ensureBits( size ); + + /** ensure that this bitset can store at least nbits bits */ + def ensureSize(n: Int): Unit = { + internal.ensureBits( n ); + if( size < n ) size = n; + } + + /** 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); + val mask = (1 << (i & 0x1F)); + 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); + val mask = (1 << (i & 0x1F)); + 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)); + internal.get(j, mask); + } + + def toArray: Array[Int] = internal.freeze; + + def makeImmutable = new scala.collection.immutable.BitSet(this); + +} |