2 * Copyright (C) 2008, Francisco Claude, all rights reserved.
4 * wt_coder_huff definition
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <wt_coder_huff.h>
24 wt_coder_huff::wt_coder_huff(uint * symbs, uint n, alphabet_mapper * am) {
26 symbs[i] = am->map(symbs[i]);
27 hc = new huffman_codes(symbs, n);
28 buffer = new uint[hc->max_length()/W+1];
29 s_len = 0; last_symbol = (uint)-1;
31 symbs[i] = am->unmap(symbs[i]);
34 wt_coder_huff::wt_coder_huff() {}
36 wt_coder_huff::~wt_coder_huff() {
41 bool wt_coder_huff::is_set(uint symbol, uint l) {
42 if(symbol!=last_symbol) {
43 s_len = (uint)hc->encode(symbol, buffer, (ulong)0);
46 return bitget(buffer,l);
49 bool wt_coder_huff::done(uint symbol, uint l) {
50 if(symbol!=last_symbol) {
51 s_len = (uint)hc->encode(symbol, buffer, (ulong)0);
57 uint wt_coder_huff::size() {
58 return 2*sizeof(uint)+sizeof(wt_coder_huff)+hc->size()+(hc->max_length()/W+1)*sizeof(uint);
61 uint wt_coder_huff::save(FILE * fp) {
62 uint wr = WT_CODER_HUFF_HDR;
63 wr = fwrite(&wr,sizeof(uint),1,fp);
65 if(hc->save(fp)) return 1;
66 //if(am->save(fp)) return 1;
70 wt_coder_huff * wt_coder_huff::load(FILE *fp) {
72 if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
73 if(rd!=WT_CODER_HUFF_HDR) return NULL;
74 wt_coder_huff * ret = new wt_coder_huff();
75 ret->hc = huffman_codes::load(fp);
76 ret->buffer = new uint[ret->hc->max_length()/W+1];
77 ret->s_len = 0; ret->last_symbol = (uint)-1;