기존의 자체 개발 알고리즘으로 일부 데이터를 압축/압축 해제해야합니다.스칼라의 비트 스트림 라이브러리
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.
스칼라에서 "Bitstrem"클래스 같은 사람 뭔가를 알고 :이 내가 좋아하는 작업을 많이 가지고? (난 아무것도 발견하고 나 자신에 의해 그것을 구현하지 않았다 희망하지 않았다.)
감사
을편집 : 내가 만약 http://www.scala-lang.org/node/8413과 대답 ("스칼라 컬렉션의 구조")를 결합 제가 알고있는 기존의 클래스 없다
abstract class Bit
object Bit {
val fromInt: Int => Bit = Array(Low, High)
val toInt: Bit => Int = Map(Low -> 0, High -> 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 < 0 || length <= idx)
throw new IndexOutOfBoundsException
Bit.fromInt(bits(idx/N) >> (idx % N) & 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 <- 0 until buf.length) {
bits(i/N) |= Bit.toInt(buf(i)) << (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
}
}
감사 등의 작업을 수행. 하지만 이해해야 할 문제가 있습니다. "(0/: ii) ((i, b) => (i << 1) | b)"Specialy는 "(0/: ii)"입니다./:는 foldLeft입니다. 그래서 그것은 "(0.foldLeft (ii))"로 해석 될 것입니다. (내 잘못한 부분은 어디입니까?) – Fabian
실제로'ii.foldLeft (0)'로 변환됩니다. 콜론으로 끝나는 연산자는 왼쪽 대신 _right_ 인수에서 메서드로 해석됩니다! 'i'와 새로운 비트 'b'가 주어진 결과, (i << 1) | b를 생성합니다. 즉, 왼쪽으로 한 비트 씩 'i'를 이동시키고 새로운 조금 아래에. 이 폴드는 이터레이터 전체에 적용됩니다. –