Add nextNodeBefore primitive.
[SXSI/XMLTree.git] / libcds / src / static_sequence / wt_coder_huff.cpp
1 /* wt_coder_huff.cpp
2  * Copyright (C) 2008, Francisco Claude, all rights reserved.
3  *
4  * wt_coder_huff definition
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21  
22 #include <wt_coder_huff.h>
23
24 wt_coder_huff::wt_coder_huff(uint * symbs, uint n, alphabet_mapper * am) {
25   for(uint i=0;i<n;i++)
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;
30   for(uint i=0;i<n;i++)
31     symbs[i] = am->unmap(symbs[i]);
32 }
33
34 wt_coder_huff::wt_coder_huff(uchar * symbs, uint n, alphabet_mapper * am) {
35   for(uint i=0;i<n;i++)
36     symbs[i] = (uchar)am->map((uint)symbs[i]);
37         hc = new huffman_codes(symbs, n);
38   buffer = new uint[hc->max_length()/W+1]; 
39   s_len = 0; last_symbol = (uint)-1;
40   for(uint i=0;i<n;i++)
41     symbs[i] = (uchar)am->unmap((uint)symbs[i]);
42 }
43
44 wt_coder_huff::wt_coder_huff() {}
45
46 wt_coder_huff::~wt_coder_huff() {
47   delete hc;
48   delete [] buffer;
49 }
50
51 bool wt_coder_huff::is_set(uint symbol, uint l) {
52         if(symbol!=last_symbol) {
53     s_len = (uint)hc->encode(symbol, buffer, (ulong)0);
54     last_symbol = symbol;
55   }
56   return bitget(buffer,l);
57 }
58
59 bool wt_coder_huff::done(uint symbol, uint l) {
60   if(symbol!=last_symbol) {
61     s_len = (uint)hc->encode(symbol, buffer, (ulong)0);
62     last_symbol = symbol;
63   }
64   return l==s_len;
65 }
66
67 uint wt_coder_huff::size() {
68   return 2*sizeof(uint)+sizeof(wt_coder_huff)+hc->size()+(hc->max_length()/W+1)*sizeof(uint);
69 }
70
71 uint wt_coder_huff::save(FILE * fp) {
72   uint wr = WT_CODER_HUFF_HDR;
73   wr = fwrite(&wr,sizeof(uint),1,fp);
74   if(wr!=1) return 1;
75   if(hc->save(fp)) return 1;
76   //if(am->save(fp)) return 1;
77   return 0;
78 }
79
80 wt_coder_huff * wt_coder_huff::load(FILE *fp) {
81   uint rd;
82   if(fread(&rd,sizeof(uint),1,fp)!=1) return NULL;
83   if(rd!=WT_CODER_HUFF_HDR) return NULL;
84   wt_coder_huff * ret = new wt_coder_huff();
85   ret->hc = huffman_codes::load(fp);
86   ret->buffer = new uint[ret->hc->max_length()/W+1]; 
87   ret->s_len = 0; ret->last_symbol = (uint)-1;
88   return ret;
89 }