Note that there are some explanatory texts on larger screens.

plurals
  1. POBitstream Library in Scala
    primarykey
    data
    text
    <p>I need to compress/decompress some Data with a old, in house developed Algorithm. There i have a lot of operations like:</p> <pre><code>if the next bit is 0 take the following 6 Bits and interpret them as an Int if the next bits are 10 take the following 9 Bits and interpret them as an Int etc. </code></pre> <p>Knows somebody something like a "Bitstrem" class in Scala? (I didn't found anything and hope that i didn't have to implement it by myself.)</p> <p>Thanks</p> <p>Edit: I combined the answer with <a href="http://www.scala-lang.org/node/8413" rel="nofollow">http://www.scala-lang.org/node/8413</a> ("The Architecture of Scala Collections") If somebody needs the samething:</p> <pre><code>abstract class Bit object Bit { val fromInt: Int =&gt; Bit = Array(Low, High) val toInt: Bit =&gt; Int = Map(Low -&gt; 0, High -&gt; 1) } case object High extends Bit case object Low extends Bit import collection.IndexedSeqLike import collection.mutable.{Builder, ArrayBuffer} import collection.generic.CanBuildFrom import collection.IndexedSeq // IndexedSeqLike implements all concrete methods of IndexedSeq // with newBuilder. (methods like take, filter, drop) final class BitSeq private (val bits: Array[Int], val length: Int) extends IndexedSeq[Bit] with IndexedSeqLike[Bit, BitSeq] { import BitSeq._ // Mandatory for IndexedSeqLike override protected[this] def newBuilder: Builder[Bit, BitSeq] = BitSeq.newBuilder //Mandatory for IndexedSeq def apply(idx: Int): Bit = { if(idx &lt; 0 || length &lt;= idx) throw new IndexOutOfBoundsException Bit.fromInt(bits(idx/N) &gt;&gt; (idx % N) &amp; M) } } object BitSeq { // Bits per Int private val N = 32 // Bitmask to isolate a bit private val M = 0x01 def fromSeq(buf: Seq[Bit]): BitSeq = { val bits = new Array[Int]((buf.length + N - 1) / N) for(i &lt;- 0 until buf.length) { bits(i/N) |= Bit.toInt(buf(i)) &lt;&lt; (i % N) } new BitSeq(bits, buf.length) } def apply(bits: Bit*) = fromSeq(bits) def newBuilder: Builder[Bit, BitSeq] = new ArrayBuffer mapResult fromSeq // Needed for map etc. (BitSeq map {:Bit} should return a BitSeq) implicit def canBuilderFrom: CanBuildFrom[BitSeq, Bit, BitSeq] = new CanBuildFrom[BitSeq, Bit, BitSeq] { def apply(): Builder[Bit, BitSeq] = newBuilder def apply(from: BitSeq): Builder[Bit, BitSeq] = newBuilder } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload