annotate ezBAMQC/src/htslib/cram/cram_stats.c @ 20:9de3bbec2479 draft default tip

Uploaded
author youngkim
date Thu, 31 Mar 2016 10:10:37 -0400
parents dfa3745e5fd8
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
1 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
2 Copyright (c) 2012-2013 Genome Research Ltd.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
3 Author: James Bonfield <jkb@sanger.ac.uk>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
4
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
6 modification, are permitted provided that the following conditions are met:
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
7
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
8 1. Redistributions of source code must retain the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
9 this list of conditions and the following disclaimer.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
10
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright notice,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
12 this list of conditions and the following disclaimer in the documentation
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
13 and/or other materials provided with the distribution.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
14
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
15 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
16 Institute nor the names of its contributors may be used to endorse or promote
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
17 products derived from this software without specific prior written permission.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
18
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
19 THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS IS" AND
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
22 DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH LTD OR CONTRIBUTORS BE LIABLE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
23 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
24 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
25 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
26 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
27 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
28 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
29 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
30
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
31 #ifdef HAVE_CONFIG_H
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
32 #include "io_lib_config.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
33 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
34
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
35 #include <stdio.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
36 #include <errno.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
37 #include <assert.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
38 #include <stdlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
39 #include <string.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
40 #include <zlib.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
41 #include <sys/types.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
42 #include <sys/stat.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
43 #include <math.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
44 #include <ctype.h>
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
45
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
46 #include "cram/cram.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
47 #include "cram/os.h"
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
48
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
49 cram_stats *cram_stats_create(void) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
50 return calloc(1, sizeof(cram_stats));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
51 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
52
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
53 void cram_stats_add(cram_stats *st, int32_t val) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
54 st->nsamp++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
55
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
56 //assert(val >= 0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
57
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
58 if (val < MAX_STAT_VAL && val >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
59 st->freqs[val]++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
60 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
61 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
62 int r;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
63
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
64 if (!st->h) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
65 st->h = kh_init(m_i2i);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
66 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
67
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
68 k = kh_put(m_i2i, st->h, val, &r);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
69 if (r == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
70 kh_val(st->h, k)++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
71 else if (r != -1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
72 kh_val(st->h, k) = 1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
73 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
74 ; // FIXME: handle error
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
75 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
76 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
77
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
78 void cram_stats_del(cram_stats *st, int32_t val) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
79 st->nsamp--;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
80
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
81 //assert(val >= 0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
82
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
83 if (val < MAX_STAT_VAL && val >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
84 st->freqs[val]--;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
85 assert(st->freqs[val] >= 0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
86 } else if (st->h) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
87 khint_t k = kh_get(m_i2i, st->h, val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
88
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
89 if (k != kh_end(st->h)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
90 if (--kh_val(st->h, k) == 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
91 kh_del(m_i2i, st->h, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
92 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
93 fprintf(stderr, "Failed to remove val %d from cram_stats\n", val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
94 st->nsamp++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
95 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
96 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
97 fprintf(stderr, "Failed to remove val %d from cram_stats\n", val);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
98 st->nsamp++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
99 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
100 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
101
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
102 void cram_stats_dump(cram_stats *st) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
103 int i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
104 fprintf(stderr, "cram_stats:\n");
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
105 for (i = 0; i < MAX_STAT_VAL; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
106 if (!st->freqs[i])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
107 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
108 fprintf(stderr, "\t%d\t%d\n", i, st->freqs[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
109 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
110 if (st->h) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
111 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
112 for (k = kh_begin(st->h); k != kh_end(st->h); k++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
113 if (!kh_exist(st->h, k))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
114 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
115
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
116 fprintf(stderr, "\t%d\t%d\n", kh_key(st->h, k), kh_val(st->h, k));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
117 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
118 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
119 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
120
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
121 #if 1
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
122 /* Returns the number of bits set in val; it the highest bit used */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
123 static int nbits(int v) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
124 static const int MultiplyDeBruijnBitPosition[32] = {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
125 1, 10, 2, 11, 14, 22, 3, 30, 12, 15, 17, 19, 23, 26, 4, 31,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
126 9, 13, 21, 29, 16, 18, 25, 8, 20, 28, 24, 7, 27, 6, 5, 32
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
127 };
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
128
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
129 v |= v >> 1; // first up to set all bits 1 after the first 1 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
130 v |= v >> 2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
131 v |= v >> 4;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
132 v |= v >> 8;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
133 v |= v >> 16;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
134
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
135 // DeBruijn magic to find top bit
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
136 return MultiplyDeBruijnBitPosition[(uint32_t)(v * 0x07C4ACDDU) >> 27];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
137 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
138 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
139
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
140 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
141 * Computes entropy from integer frequencies for various encoding methods and
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
142 * picks the best encoding.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
143 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
144 * FIXME: we could reuse some of the code here for the actual encoding
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
145 * parameters too. Eg the best 'k' for SUBEXP or the code lengths for huffman.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
146 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
147 * Returns the best codec to use.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
148 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
149 enum cram_encoding cram_stats_encoding(cram_fd *fd, cram_stats *st) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
150 enum cram_encoding best_encoding = E_NULL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
151 int best_size = INT_MAX, bits;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
152 int nvals, i, ntot = 0, max_val = 0, min_val = INT_MAX, k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
153 int *vals = NULL, *freqs = NULL, vals_alloc = 0, *codes;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
154
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
155 //cram_stats_dump(st);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
156
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
157 /* Count number of unique symbols */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
158 for (nvals = i = 0; i < MAX_STAT_VAL; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
159 if (!st->freqs[i])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
160 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
161 if (nvals >= vals_alloc) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
162 vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
163 vals = realloc(vals, vals_alloc * sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
164 freqs = realloc(freqs, vals_alloc * sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
165 if (!vals || !freqs) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
166 if (vals) free(vals);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
167 if (freqs) free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
168 return E_HUFFMAN; // Cannot do much else atm
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
169 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
170 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
171 vals[nvals] = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
172 freqs[nvals] = st->freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
173 ntot += freqs[nvals];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
174 if (max_val < i) max_val = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
175 if (min_val > i) min_val = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
176 nvals++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
177 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
178 if (st->h) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
179 khint_t k;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
180 int i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
181
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
182 for (k = kh_begin(st->h); k != kh_end(st->h); k++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
183 if (!kh_exist(st->h, k))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
184 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
185
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
186 if (nvals >= vals_alloc) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
187 vals_alloc = vals_alloc ? vals_alloc*2 : 1024;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
188 vals = realloc(vals, vals_alloc * sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
189 freqs = realloc(freqs, vals_alloc * sizeof(int));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
190 if (!vals || !freqs)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
191 return E_HUFFMAN; // Cannot do much else atm
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
192 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
193 i = kh_key(st->h, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
194 vals[nvals]=i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
195 freqs[nvals] = kh_val(st->h, k);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
196 ntot += freqs[nvals];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
197 if (max_val < i) max_val = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
198 if (min_val > i) min_val = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
199 nvals++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
200 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
201 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
202
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
203 st->nvals = nvals;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
204 assert(ntot == st->nsamp);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
205
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
206 if (nvals <= 1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
207 free(vals);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
208 free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
209 return E_HUFFMAN;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
210 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
211
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
212 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
213 fprintf(stderr, "Range = %d..%d, nvals=%d, ntot=%d\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
214 min_val, max_val, nvals, ntot);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
215
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
216 /* Theoretical entropy */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
217 // if (fd->verbose > 1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
218 // double dbits = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
219 // for (i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
220 // dbits += freqs[i] * log((double)freqs[i]/ntot);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
221 // }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
222 // dbits /= -log(2);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
223 // if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
224 // fprintf(stderr, "Entropy = %f\n", dbits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
225 // }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
226
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
227 if (nvals > 1 && ntot > 256) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
228 #if 0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
229 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
230 * CRUDE huffman estimator. Round to closest and round up from 0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
231 * to 1 bit.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
232 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
233 * With and without ITF8 incase we have a few discrete values but with
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
234 * large magnitude.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
235 *
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
236 * Note rans0/arith0 and Z_HUFFMAN_ONLY vs internal huffman can be
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
237 * compared in this way, but order-1 (eg rans1) or maybe LZ77 modes
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
238 * may detect the correlation of high bytes to low bytes in multi-
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
239 * byte values. So this predictor breaks down.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
240 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
241 double dbits = 0; // entropy + ~huffman
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
242 double dbitsH = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
243 double dbitsE = 0; // external entropy + ~huffman
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
244 double dbitsEH = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
245 int F[256] = {0}, n = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
246 double e = 0; // accumulated error bits
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
247 for (i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
248 double x; int X;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
249 unsigned int v = vals[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
250
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
251 //Better encoding would cope with sign.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
252 //v = ABS(vals[i])*2+(vals[i]<0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
253
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
254 if (!(v & ~0x7f)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
255 F[v] += freqs[i], n+=freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
256 } else if (!(v & ~0x3fff)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
257 F[(v>>8) |0x80] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
258 F[ v &0xff] += freqs[i], n+=2*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
259 } else if (!(v & ~0x1fffff)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
260 F[(v>>16)|0xc0] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
261 F[(v>>8 )&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
262 F[ v &0xff] += freqs[i], n+=3*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
263 } else if (!(v & ~0x0fffffff)) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
264 F[(v>>24)|0xe0] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
265 F[(v>>16)&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
266 F[(v>>8 )&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
267 F[ v &0xff] += freqs[i], n+=4*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
268 } else {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
269 F[(v>>28)|0xf0] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
270 F[(v>>20)&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
271 F[(v>>12)&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
272 F[(v>>4 )&0xff] += freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
273 F[ v &0x0f] += freqs[i], n+=5*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
274 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
275
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
276 x = -log((double)freqs[i]/ntot)/.69314718055994530941;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
277 X = x+0.5;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
278 if ((int)(x+((double)e/freqs[i])+.5)>X) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
279 X++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
280 } else if ((int)(x+((double)e/freqs[i])+.5)<X) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
281 X--;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
282 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
283 e-=freqs[i]*(X-x);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
284 X += (X==0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
285
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
286 //fprintf(stderr, "Val %d = %d x %d (ent %f, %d) e %f\n", i, v, freqs[i], x, X, e);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
287
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
288 dbits += freqs[i] * x;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
289 dbitsH += freqs[i] * X;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
290 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
291
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
292 for (i = 0; i < 256; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
293 if (F[i]) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
294 double x = -log((double)F[i]/n)/.69314718055994530941;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
295 int X = x+0.5;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
296 X += (X==0);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
297 dbitsE += F[i] * x;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
298 dbitsEH += F[i] * X;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
299
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
300 //fprintf(stderr, "Val %d = %d x %d (e %f, %d)\n", i, i, F[i], x, X);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
301 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
302 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
303
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
304 //fprintf(stderr, "CORE Entropy = %f, %f\n", dbits/8, dbitsH/8);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
305 //fprintf(stderr, "Ext. Entropy = %f, %f\n", dbitsE/8, dbitsEH/8);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
306
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
307 if (dbitsE < 1000 || dbitsE / dbits > 1.1) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
308 //fprintf(stderr, "=> %d < 200 ? E_HUFFMAN : E_BETA\n", nvals);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
309 free(vals); free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
310 return nvals < 200 ? E_HUFFMAN : E_BETA;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
311 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
312 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
313 free(vals); free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
314 return E_EXTERNAL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
315 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
316
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
317 /*
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
318 * Avoid complex stats for now, just do heuristic of HUFFMAN for small
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
319 * alphabets and BETA for anything large.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
320 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
321 free(vals); free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
322 return nvals < 200 ? E_HUFFMAN : E_BETA;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
323 //return E_HUFFMAN;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
324 //return E_EXTERNAL;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
325
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
326
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
327 /* We only support huffman now anyway... */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
328 //free(vals); free(freqs); return E_HUFFMAN;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
329
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
330 /* Beta */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
331 bits = nbits(max_val - min_val) * ntot;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
332 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
333 fprintf(stderr, "BETA = %d\n", bits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
334 if (best_size > bits)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
335 best_size = bits, best_encoding = E_BETA;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
336
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
337 #if 0
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
338 /* Unary */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
339 if (min_val >= 0) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
340 for (bits = i = 0; i < nvals; i++)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
341 bits += freqs[i]*(vals[i]+1);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
342 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
343 fprintf(stderr, "UNARY = %d\n", bits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
344 if (best_size > bits)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
345 best_size = bits, best_encoding = E_NULL; //E_UNARY;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
346 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
347
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
348 /* Gamma */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
349 for (bits = i = 0; i < nvals; i++)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
350 bits += ((nbits(vals[i]-min_val+1)-1) + nbits(vals[i]-min_val+1)) * freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
351 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
352 fprintf(stderr, "GAMMA = %d\n", bits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
353 if (best_size > bits)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
354 best_size = bits, best_encoding = E_GAMMA;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
355
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
356 /* Subexponential */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
357 for (k = 0; k < 10; k++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
358 for (bits = i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
359 if (vals[i]-min_val < (1<<k))
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
360 bits += (1 + k)*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
361 else
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
362 bits += (nbits(vals[i]-min_val)*2-k)*freqs[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
363 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
364
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
365 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
366 fprintf(stderr, "SUBEXP%d = %d\n", k, bits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
367 if (best_size > bits)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
368 best_size = bits, best_encoding = E_SUBEXP;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
369 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
370 #endif
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
371
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
372 /* byte array len */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
373
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
374 /* byte array stop */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
375
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
376 /* External? Guesswork! */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
377
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
378 /* Huffman */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
379 // qsort(freqs, nvals, sizeof(freqs[0]), sort_freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
380 // for (i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
381 // fprintf(stderr, "%d = %d\n", i, freqs[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
382 // vals[i] = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
383 // }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
384
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
385 /* Grow freqs to 2*freqs, to store sums */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
386 /* Vals holds link data */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
387 freqs = realloc(freqs, 2*nvals*sizeof(*freqs));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
388 codes = calloc(2*nvals, sizeof(*codes));
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
389 if (!freqs || !codes)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
390 return E_HUFFMAN; // Cannot do much else atm
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
391
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
392 /* Inefficient, use pointers to form chain so we can insert and maintain
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
393 * a sorted list? This is currently O(nvals^2) complexity.
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
394 */
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
395 for (;;) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
396 int low1 = INT_MAX, low2 = INT_MAX;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
397 int ind1 = 0, ind2 = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
398 for (i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
399 if (freqs[i] < 0)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
400 continue;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
401 if (low1 > freqs[i])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
402 low2 = low1, ind2 = ind1, low1 = freqs[i], ind1 = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
403 else if (low2 > freqs[i])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
404 low2 = freqs[i], ind2 = i;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
405 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
406 if (low2 == INT_MAX)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
407 break;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
408
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
409 //fprintf(stderr, "Merge ind %d (%d), %d (%d) = %d+%d, => %d=%d\n",
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
410 // ind1, vals[ind1], ind2, vals[ind2], low1, low2,
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
411 // nvals, low1+low2);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
412
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
413 freqs[nvals] = low1 + low2;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
414 codes[ind1] = nvals;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
415 codes[ind2] = nvals;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
416 freqs[ind1] *= -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
417 freqs[ind2] *= -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
418 nvals++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
419 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
420 nvals = nvals/2+1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
421
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
422 for (i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
423 int code_len = 0;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
424 for (k = codes[i]; k; k = codes[k])
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
425 code_len++;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
426 codes[i] = code_len;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
427 freqs[i] *= -1;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
428 //fprintf(stderr, "%d / %d => %d\n", vals[i], freqs[i], codes[i]);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
429 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
430
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
431 for (bits = i = 0; i < nvals; i++) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
432 bits += freqs[i] * codes[i];
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
433 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
434 if (fd->verbose > 1)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
435 fprintf(stderr, "HUFFMAN = %d\n", bits);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
436 if (best_size >= bits)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
437 best_size = bits, best_encoding = E_HUFFMAN;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
438 free(codes);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
439
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
440 free(vals);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
441 free(freqs);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
442
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
443 return best_encoding;
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
444 }
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
445
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
446 void cram_stats_free(cram_stats *st) {
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
447 if (st->h)
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
448 kh_destroy(m_i2i, st->h);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
449 free(st);
dfa3745e5fd8 Uploaded
youngkim
parents:
diff changeset
450 }