Graphviz 14.1.2~dev.20260118.1035
Loading...
Searching...
No Matches
dtstat.c
Go to the documentation of this file.
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
11static 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
22static 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
37int 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}
#define DT_OBAG
Definition cdt.h:121
#define DT_SET
Definition cdt.h:119
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:14
#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:37
static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level)
Definition dtstat.c:11
static void dthstat(Dtdata_t data, Dtstat_t *ds, size_t *count)
Definition dtstat.c:22
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