Graphviz 14.0.3~dev.20251029.0425
Loading...
Searching...
No Matches
dtstat.c
Go to the documentation of this file.
1#include <cdt/dthdr.h>
2#include <stdlib.h>
3
4/* Get statistics of a dictionary
5**
6** Written by Kiem-Phong Vo (5/25/96)
7*/
8
9static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level) {
10 if (root->left)
11 dttstat(ds, root->left, depth + 1, level);
12 if (root->right)
13 dttstat(ds, root->right, depth + 1, level);
14 if (depth > ds->dt_n)
15 ds->dt_n = depth;
16 if (level)
17 level[depth] += 1;
18}
19
20static void dthstat(Dtdata_t data, Dtstat_t *ds, size_t *count) {
21 for (int h = data.ntab - 1; h >= 0; --h) {
22 size_t n = 0;
23 for (Dtlink_t *t = data.htab[h]; t; t = t->right)
24 n += 1;
25 if (count)
26 count[n] += 1;
27 else if (n > 0) {
28 ds->dt_n += 1;
29 if (n > ds->dt_max)
30 ds->dt_max = n;
31 }
32 }
33}
34
35int dtstat(Dt_t *dt, Dtstat_t *ds, int all) {
36 static size_t *Count;
37
38 UNFLATTEN(dt);
39
40 ds->dt_n = ds->dt_max = 0;
41 ds->dt_count = NULL;
42 ds->dt_size = dtsize(dt);
43 ds->dt_meth = dt->data.type & DT_METHODS;
44
45 if (!all)
46 return 0;
47
48 if (dt->data.type & DT_SET) {
49 dthstat(dt->data, ds, NULL);
50 free(Count);
51 if (!(Count = calloc(ds->dt_max + 1, sizeof(size_t))))
52 return -1;
53 dthstat(dt->data, ds, Count);
54 } else if (dt->data.type & (DT_OSET | DT_OBAG)) {
55 if (dt->data.here) {
56 dttstat(ds, dt->data.here, 0, NULL);
57 free(Count);
58 if (!(Count = calloc(ds->dt_n + 1, sizeof(size_t))))
59 return -1;
60 dttstat(ds, dt->data.here, 0, Count);
61 for (size_t i = 0; i <= ds->dt_n; ++i)
62 if (Count[i] > ds->dt_max)
63 ds->dt_max = Count[i];
64 }
65 }
66 ds->dt_count = Count;
67
68 return 0;
69}
#define DT_OBAG
Definition cdt.h:121
#define DT_SET
Definition cdt.h:119
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:12
#define DT_METHODS
Definition cdt.h:122
#define DT_OSET
Definition cdt.h:120
#define UNFLATTEN(dt)
Definition dthdr.h:27
int dtstat(Dt_t *dt, Dtstat_t *ds, int all)
Definition dtstat.c:35
static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level)
Definition dtstat.c:9
static void dthstat(Dtdata_t data, Dtstat_t *ds, size_t *count)
Definition dtstat.c:20
void free(void *)
node NULL
Definition grammar.y:181
Definition cdt.h:71
Dtlink_t * here
Definition cdt.h:73
int type
Definition cdt.h:72
int ntab
Definition cdt.h:78
size_t dt_n
Definition cdt.h:113
int dt_size
Definition cdt.h:112
size_t dt_max
Definition cdt.h:114
int dt_meth
Definition cdt.h:111
size_t * dt_count
Definition cdt.h:115
Definition cdt.h:98
Dtdata_t data
sharable data
Definition cdt.h:101