| 1 | /* Huffman codes for digitized alphabets |
| 2 | */ |
| 3 | #ifndef eslHUFFMAN_INCLUDED |
| 4 | #define eslHUFFMAN_INCLUDED |
| 5 | #include <esl_config.h> |
| 6 | |
| 7 | typedef struct huffman_s { |
| 8 | int *len; // [0..K-1] = codelength 0..127. L[i]=0: symbol i is not coded. |
| 9 | uint32_t *code; // [0..K-1] = Huffman encoding for symbol [i], right flushed. |
| 10 | int K; // total # of symbols in alphabet |
| 11 | |
| 12 | /* Canonical Huffman sorting */ |
| 13 | int *sorted_at; // [0..Ku-1] = in current sort, rank #i is symidx = sorted_at[i] |
| 14 | int Ku; // how many symbols actually encoded & in sort; 0 < Ku <= K |
| 15 | |
| 16 | /* Decoding table. */ |
| 17 | int *dt_len; // [0..D-1] = each used codelength [d] |
| 18 | uint32_t *dt_lcode; // [0..D-1] = leftshifted first code for codelength [d] |
| 19 | int *dt_rank; // [0..D-1] = rank (in sorted_at[]) of 1st code for each codelength [d]. |
| 20 | int D; // Number of different code lengths; size of decoding table. |
| 21 | int Lmax; // max code length: \max_i L[i] |
| 22 | } ESL_HUFFMAN; |
| 23 | |
| 24 | #define eslHUFFMAN_MAXCODE 32 // Maximum <code> length in bits: uint32_t |
| 25 | |
| 26 | extern int esl_huffman_Build(const float *fq, int K, ESL_HUFFMAN **ret_hc); |
| 27 | extern void esl_huffman_Destroy(ESL_HUFFMAN *hc); |
| 28 | |
| 29 | extern int esl_huffman_Encode(const ESL_HUFFMAN *hc, const char *T, int n, uint32_t **ret_X, int *ret_nb); |
| 30 | extern int esl_huffman_Decode(const ESL_HUFFMAN *hc, const uint32_t *X, int nb, char **ret_T, int *ret_n); |
| 31 | |
| 32 | extern int esl_huffman_Dump(FILE *fp, ESL_HUFFMAN *hc); |
| 33 | |
| 34 | #endif /*eslHUFFMAN_INCLUDED*/ |
| 35 | |
| 36 | /* Example canonical code (Ku=7, Lmax=4) |
| 37 | * r L code |
| 38 | * 0 1 0 |
| 39 | * 1 3 100 |
| 40 | * 2 3 101 |
| 41 | * 3 4 1100 |
| 42 | * 4 4 1101 |
| 43 | * 5 4 1110 |
| 44 | * 6 4 1111 |
| 45 | * |
| 46 | * Example decoding table (D=3) |
| 47 | * d dt_L dt_lcode dt_rank |
| 48 | * 0 1 0000 0 |
| 49 | * 1 3 1000 1 |
| 50 | * 2 4 1100 3 |
| 51 | * |
| 52 | */ |