Mercurial > hg > dmlib
comparison tools/fanalyze.c @ 2011:8e38fa3c4f98
Fix use of qsort().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sun, 08 Jul 2018 00:47:22 +0300 |
parents | 4a4c3e15b8c2 |
children | 8a9ef75fd3cd |
comparison
equal
deleted
inserted
replaced
2010:09d548b08150 | 2011:8e38fa3c4f98 |
---|---|
128 const DMStatValue *pa = va, *pb = vb; | 128 const DMStatValue *pa = va, *pb = vb; |
129 return pb->count - pa->count; | 129 return pb->count - pa->count; |
130 } | 130 } |
131 | 131 |
132 | 132 |
133 void dmSortStats(DMStats *stats) | |
134 { | |
135 qsort(&stats->cv, sizeof(DMStatValue), SET_MAX_ELEMS, dmCompareStatFunc); | |
136 } | |
137 | |
138 | |
139 void dmPrintStats(DMStats *stats, const int nmax, const size_t size) | 133 void dmPrintStats(DMStats *stats, const int nmax, const size_t size) |
140 { | 134 { |
135 qsort(&stats->cv, SET_MAX_ELEMS, sizeof(DMStatValue), dmCompareStatFunc); | |
136 | |
141 for (int n = 0; n < nmax; n++) | 137 for (int n = 0; n < nmax; n++) |
142 { | 138 { |
143 printf("$%02x (%1.2f%%), ", | 139 printf("$%02x (%d = %1.2f%%), ", |
144 stats->cv[n].value, | 140 stats->cv[n].value, |
141 stats->cv[n].count, | |
145 ((float) stats->cv[n].count * 100.0f) / (float) size); | 142 ((float) stats->cv[n].count * 100.0f) / (float) size); |
146 } | 143 } |
147 printf("\n\n"); | 144 printf("\n\n"); |
148 } | 145 } |
149 | 146 |
186 | 183 |
187 if (!compBufSize || file->size < compBufSize) | 184 if (!compBufSize || file->size < compBufSize) |
188 compBufSize = file->size; | 185 compBufSize = file->size; |
189 | 186 |
190 totalSize += file->size; | 187 totalSize += file->size; |
188 dmInitStats(&file->stats); | |
191 } | 189 } |
192 | 190 |
193 // Allocate comparision buffer | 191 // Allocate comparision buffer |
194 // XXX: integer overflow? | 192 // XXX: integer overflow? |
195 dmPrint(2, "Allocating %d element (%d bytes) comparision buffer.\n", | 193 dmPrint(2, "Allocating %d element (%d bytes) comparision buffer.\n", |
204 // Begin analyzing .. | 202 // Begin analyzing .. |
205 dmPrint(2, "Analyzing ..\n"); | 203 dmPrint(2, "Analyzing ..\n"); |
206 for (int nfile = 0; nfile < nsrcFiles; nfile++) | 204 for (int nfile = 0; nfile < nsrcFiles; nfile++) |
207 { | 205 { |
208 DMSourceFile *file = &srcFiles[nfile]; | 206 DMSourceFile *file = &srcFiles[nfile]; |
209 dmInitStats(&file->stats); | |
210 | 207 |
211 for (size_t offs = 0; offs < file->size; offs++) | 208 for (size_t offs = 0; offs < file->size; offs++) |
212 { | 209 { |
213 Uint8 bv = file->data[offs]; | 210 Uint8 bv = file->data[offs]; |
214 totalStats.cv[bv].count++; | 211 totalStats.cv[bv].count++; |
218 for (size_t offs = 0; offs < compBufSize; offs++) | 215 for (size_t offs = 0; offs < compBufSize; offs++) |
219 { | 216 { |
220 Uint8 data = offs < file->size ? file->data[offs] : 0; | 217 Uint8 data = offs < file->size ? file->data[offs] : 0; |
221 compBuf[offs].stats[data]++; | 218 compBuf[offs].stats[data]++; |
222 } | 219 } |
223 | 220 } |
224 dmSortStats(&file->stats); | |
225 } | |
226 dmSortStats(&totalStats); | |
227 | |
228 | 221 |
229 for (size_t offs = 0; offs < compBufSize; offs++) | 222 for (size_t offs = 0; offs < compBufSize; offs++) |
230 { | 223 { |
231 DMCompElem *el = &compBuf[offs]; | 224 DMCompElem *el = &compBuf[offs]; |
232 for (int n = 0; n < SET_MAX_ELEMS; n++) | 225 for (int n = 0; n < SET_MAX_ELEMS; n++) |