| 1 |
#include "config.h" |
| 2 |
|
| 3 |
#include <cdt/dthdr.h> |
| 4 |
#include <stdlib.h> |
| 5 |
|
| 6 |
/* Get statistics of a dictionary |
| 7 |
** |
| 8 |
** Written by Kiem-Phong Vo (5/25/96) |
| 9 |
*/ |
| 10 |
|
| 11 |
static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level) { |
| 12 |
if (root->left) |
| 13 |
dttstat(ds, root->left, depth + 1, level); |
| 14 |
if (root->right) |
| 15 |
dttstat(ds, root->right, depth + 1, level); |
| 16 |
if (depth > ds->dt_n) |
| 17 |
ds->dt_n = depth; |
| 18 |
if (level) |
| 19 |
level[depth] += 1; |
| 20 |
} |
| 21 |
|
| 22 |
static void dthstat(Dtdata_t data, Dtstat_t *ds, size_t *count) { |
| 23 |
for (int h = data.ntab - 1; h >= 0; --h) { |
| 24 |
size_t n = 0; |
| 25 |
for (Dtlink_t *t = data.htab[h]; t; t = t->right) |
| 26 |
n += 1; |
| 27 |
if (count) |
| 28 |
count[n] += 1; |
| 29 |
else if (n > 0) { |
| 30 |
ds->dt_n += 1; |
| 31 |
if (n > ds->dt_max) |
| 32 |
ds->dt_max = n; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
int dtstat(Dt_t *dt, Dtstat_t *ds, int all) { |
| 38 |
static size_t *Count; |
| 39 |
|
| 40 |
UNFLATTEN(dt); |
| 41 |
|
| 42 |
ds->dt_n = ds->dt_max = 0; |
| 43 |
ds->dt_count = NULL; |
| 44 |
ds->dt_size = dtsize(dt); |
| 45 |
ds->dt_meth = dt->data.type & DT_METHODS; |
| 46 |
|
| 47 |
if (!all) |
| 48 |
return 0; |
| 49 |
|
| 50 |
if (dt->data.type & DT_SET) { |
| 51 |
dthstat(dt->data, ds, NULL); |
| 52 |
free(Count); |
| 53 |
if (!(Count = calloc(ds->dt_max + 1, sizeof(size_t)))) |
| 54 |
return -1; |
| 55 |
dthstat(dt->data, ds, Count); |
| 56 |
} else if (dt->data.type & (DT_OSET | DT_OBAG)) { |
| 57 |
if (dt->data.here) { |
| 58 |
dttstat(ds, dt->data.here, 0, NULL); |
| 59 |
free(Count); |
| 60 |
if (!(Count = calloc(ds->dt_n + 1, sizeof(size_t)))) |
| 61 |
return -1; |
| 62 |
dttstat(ds, dt->data.here, 0, Count); |
| 63 |
for (size_t i = 0; i <= ds->dt_n; ++i) |
| 64 |
if (Count[i] > ds->dt_max) |
| 65 |
ds->dt_max = Count[i]; |
| 66 |
} |
| 67 |
} |
| 68 |
ds->dt_count = Count; |
| 69 |
|
| 70 |
return 0; |
| 71 |
} |