-
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.
Move tag dict builder to dedicated namespace
- Loading branch information
Showing
4 changed files
with
135 additions
and
100 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
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,58 @@ | ||
(ns cljam.io.cram.encode.tag-dict) | ||
|
||
(defprotocol ITagDictionaryBuilder | ||
(assign-tags-id! [this tags]) | ||
(build-tag-dict [this])) | ||
|
||
(defn make-tag-dict-builder | ||
"TODO" | ||
[] | ||
(let [tags->id (volatile! {})] | ||
(reify ITagDictionaryBuilder | ||
(assign-tags-id! [_ tags] | ||
(let [tags' (into [] | ||
(keep (fn [opt] | ||
(let [[tag m] (first opt)] | ||
(when-not (= tag :RG) | ||
[tag (first (:type m))])))) | ||
tags)] | ||
(or (get @tags->id tags') | ||
(let [id (count @tags->id)] | ||
(vswap! tags->id assoc tags' id) | ||
id)))) | ||
(build-tag-dict [_] | ||
(->> (sort-by val @tags->id) | ||
(mapv (fn [[tags _]] | ||
(mapv (fn [[tag tag-type]] | ||
{:tag tag :type tag-type}) | ||
tags)))))))) | ||
|
||
(defn- build-tag-encoding [item] | ||
(letfn [(tag-id [item] | ||
(let [tag' (name (:tag item))] | ||
(bit-or (bit-shift-left (int (nth tag' 0)) 16) | ||
(bit-shift-left (int (nth tag' 1)) 8) | ||
(int (:type item))))) | ||
(tag-encoding-for-fixed-size [item size] | ||
{:codec :byte-array-len | ||
:len-encoding {:codec :huffman, :alphabet [size], :bit-len [0]} | ||
:val-encoding {:codec :external, :content-id (tag-id item)}})] | ||
(case (:type item) | ||
(\A \c \C) (tag-encoding-for-fixed-size item 1) | ||
(\s \S) (tag-encoding-for-fixed-size item 2) | ||
(\i \I \f) (tag-encoding-for-fixed-size item 4) | ||
(let [content-id (tag-id item)] | ||
{:codec :byte-array-len | ||
:len-encoding {:codec :external, :content-id content-id} | ||
:val-encoding {:codec :external, :content-id content-id}})))) | ||
|
||
(defn build-tag-encodings | ||
"TODO" | ||
[tag-dict] | ||
(reduce | ||
(fn [m entry] | ||
(reduce | ||
(fn [m {tag-type :type :as item}] | ||
(update-in m [(:tag item) tag-type] #(or % (build-tag-encoding item)))) | ||
m entry)) | ||
{} tag-dict)) |
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