Debug swcsa
[SXSI/TextCollection.git] / swcsa / utils / huffDec.c
1
2 // implements canonical Huffman 
3
4 #include "huffDec.h"
5
6
7 //THuff createHuff (uint *fst, uint *num, uint depth) {
8 //      THuff H;
9 //      H.fst = fst;
10 //      H.num = num;
11 //      H.depth = depth;
12 //      return H;       
13 //}
14
15
16 void printCodeHuffDec (THuffDec H, uint symb)
17
18    { uint pos;
19      uint code;
20      int d;
21      //pos = H.s.spos[symb];
22      pos = symb;
23      code = 0;
24      d = H.depth;
25      
26     // fprintf(stderr,"\n H.depth= %ld and pos is %ld\n",H.depth,pos); 
27      while (pos >= H.num[d])
28        { code = (code + H.num[d]) >> 1;
29          pos -= H.num[d--];
30        }
31      code += pos;
32      if (d > W) {fprintf(stderr,"code larger than W"); d=W;}
33    
34          //show the code.
35      while (d--)
36         { if ((code >> d) & 1) 
37                 fprintf(stderr,"1");
38               else fprintf(stderr,"0");
39                 }
40    } 
41
42
43
44 //Decodes a code starting in position ptr from stream. Returns the ranking in the
45 //vector of symbols.
46 int decodeHuffDec (THuffDec *H, uint *symb, uint *stream, uint ptr)
47    { uint pos;
48      int d;
49      pos = 0;
50      d = 0;
51      while (pos < H->fst[d])
52         { pos = (pos << 1) | bitget(stream,ptr);
53           ptr++; d++;
54         }
55      *symb = H->num[d]+pos-H->fst[d];
56      return ptr;
57    }
58
59
60
61 // //Decodes a code starting in position ptr from stream. Returns the ranking in the
62 // //vector of symbols.
63 // int decodeHuffDecVariantWord (uint *fst , uint *symb, uint *stream, uint ptr, uint depth)
64 //    { uint pos;
65 //      int d;
66 //      pos = 0;
67 //      d = 0;
68 //      while ((d< depth) && (pos < fst[d*2])  )
69 //         { pos = (pos << 1) | bitget(stream,ptr);
70 //           ptr++; d++;
71 //         }
72 //      if (depth==d) 
73 //              *symb = pos;
74 //      else 
75 //              *symb = 
76 //                      pos  - 
77 //                      fst[d*2] + 
78 //                      fst[d*2+1]; 
79 //      //(*symb) = (depth==d) ? pos : fst[d*2+1] + pos - fst[d*2]; 
80 // 
81 //      return ptr;
82 //    }
83
84
85
86 // the bytes used by HuffDecman struct
87 uint sizeHuffDec (THuffDec H)
88    { return (1+ 2*(H.depth+1))*sizeof(uint);
89    }
90
91
92 void freeHuffDec (THuffDec H)
93
94   { free (H.fst); free (H.num);
95   }
96
97
98 THuffDec loadHuffDecAfterDecode (FILE *f, int enc)   //enc (0/1)-> do you only  want to perform encoding ??
99
100    { THuffDec H;
101 //     int i,d,dold,dact;
102 //
103 //     fread (&H.max,sizeof(uint),1,f); 
104 //     fread (&H.lim,sizeof(uint),1,f); 
105 //     fread (&H.depth,sizeof(uint),1,f); 
106 //
107 //     H.s.symb = malloc ((H.lim+1)*sizeof(uint));
108 //     fread (H.s.symb,sizeof(uint),H.lim+1,f); 
109 // 
110 //     H.fst = malloc ((H.depth+1)*sizeof(uint));
111 //     fread (H.fst,sizeof(uint),H.depth+1,f);
112 //
113 //     H.num = malloc ((H.depth+1)*sizeof(uint));
114 //     fread (H.num,sizeof(uint),H.depth+1,f); 
115 //     
116      return H;
117    }   
118         
119         
120 ////THuffDec loadHuffDec (FILE *f, int enc)
121 ////
122 ////   { THuffDec H;
123 ////     uint *symb;
124 ////     uint *num;
125 ////     int i,d,dold,dact;
126 ////     fread (&H.max,sizeof(uint),1,f); 
127 ////     fread (&H.lim,sizeof(uint),1,f); 
128 ////     fread (&H.depth,sizeof(uint),1,f); 
129 ////     symb = malloc ((H.lim+1)*sizeof(uint));
130 ////     fread (symb,sizeof(uint),H.lim+1,f); 
131 ////     if (enc) 
132 ////          { H.s.spos = malloc ((H.max+1)*sizeof(uint));
133 ////            for (i=0;i<=H.max;i++) H.s.spos[i] = ~0;
134 ////            for (i=0;i<=H.lim;i++) H.s.spos[symb[i]] = i;
135 ////            free (symb);
136 ////      }
137 ////     else H.s.symb = symb;
138 ////     H.num = malloc ((H.depth+1)*sizeof(uint));
139 ////     fread (H.num,sizeof(uint),H.depth+1,f); 
140 ////     if (!enc) 
141 ////          { H.fst = malloc ((H.depth+1)*sizeof(uint));
142 ////            H.fst[H.depth] = 0; dold = 0;
143 ////            for (d=H.depth-1;d>=0;d--)
144 ////            { dact = H.num[d+1];
145 ////              H.fst[d] = (H.fst[d+1]+dact) >> 1;
146 ////              H.num[d+1] = dold;
147 ////              dold += dact;
148 ////            }
149 ////        H.num[0] = dold;
150 ////      }
151 ////     return H;
152 ////   }        
153
154
155 void loadHuffDecAfterDecode2 (THuffDec *H, FILE *f, int enc)   //enc (0/1)-> do you only  want to perform encoding ??
156
157    { 
158      int i,d,dold,dact;
159
160 //     fread (&H->max,sizeof(uint),1,f); 
161 //     fread (&H->lim,sizeof(uint),1,f); 
162      fread (&H->depth,sizeof(uint),1,f); 
163
164 //     H->s.symb = malloc ((H->lim+1)*sizeof(uint));
165 //     fread (H->s.symb,sizeof(uint),H->lim+1,f); 
166  
167      H->fst = (uint *) malloc ((H->depth+1)*sizeof(uint));
168      fread (H->fst,sizeof(uint),H->depth+1,f);
169
170      H->num = (uint *) malloc ((H->depth+1)*sizeof(uint));
171      fread (H->num,sizeof(uint),H->depth+1,f); 
172    }   
173         
174      
175      
176      
177
178
179      
180