-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #309 from chrovis/feature/cram-bit-encoding-support
Support for CRAM beta coding
- Loading branch information
Showing
5 changed files
with
105 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
(ns cljam.io.cram.bit-stream | ||
(:import [java.nio ByteBuffer])) | ||
|
||
(defprotocol IBitStreamDecoder | ||
(read-bits [_ m])) | ||
|
||
(definline ^:private right-nbits-of [x nbits] | ||
`(bit-and (long ~x) (dec (bit-shift-left 1 (long ~nbits))))) | ||
|
||
(deftype BitStreamDecoder | ||
[^ByteBuffer bb | ||
^:unsynchronized-mutable ^long buffer | ||
^:unsynchronized-mutable ^long nbits] | ||
IBitStreamDecoder | ||
(read-bits [_ m] | ||
(let [m (long m)] | ||
(if (zero? m) | ||
m | ||
(loop [m m, buf buffer, n nbits, acc 0] | ||
(if (<= m n) | ||
(do (set! buffer buf) | ||
(set! nbits (- n m)) | ||
(bit-or acc (right-nbits-of (unsigned-bit-shift-right buf nbits) m))) | ||
(let [m' (- m n) | ||
acc' (bit-or acc (bit-shift-left (right-nbits-of buf n) m'))] | ||
(recur m' (long (.get bb)) 8 acc')))))))) | ||
|
||
(defn make-bit-stream-decoder | ||
"Creates a new bit stream decoder based on the given byte buffer." | ||
[bb] | ||
(->BitStreamDecoder bb 0 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(ns cljam.io.cram.bit-stream-test | ||
(:require [cljam.io.cram.bit-stream :as bs] | ||
[cljam.io.util.byte-buffer :as bb] | ||
[clojure.test :refer [deftest is]])) | ||
|
||
(deftest read-bits-test | ||
(let [bb (bb/make-lsb-byte-buffer (byte-array [0x31 0x41 0x59 0x26])) | ||
bs-decoder (bs/make-bit-stream-decoder bb)] | ||
(is (= [3 1 4 1 5 9 2 6] (repeatedly 8 #(bs/read-bits bs-decoder 4))))) | ||
(let [bb (bb/make-lsb-byte-buffer (byte-array [2r11001110 2r10101110])) | ||
bs-decoder (bs/make-bit-stream-decoder bb)] | ||
(is (= [6 3 5 2 7] (repeatedly 5 #(bs/read-bits bs-decoder 3)))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters