Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
dtstat.c
Go to the documentation of this file.
1#include <cdt/dthdr.h>
2#include <stddef.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 Dtlink_t* t;
22 int h;
23
24 for (h = data.ntab - 1; h >= 0; --h)
25 { size_t n = 0;
26 for (t = data.htab[h]; t; t = t->right)
27 n += 1;
28 if(count)
29 count[n] += 1;
30 else if(n > 0)
31 { ds->dt_n += 1;
32 if(n > ds->dt_max)
33 ds->dt_max = n;
34 }
35 }
36}
37
38int dtstat(Dt_t* dt, Dtstat_t* ds, int all)
39{
40 static size_t *Count;
41 static size_t Size;
42
43 UNFLATTEN(dt);
44
45 ds->dt_n = ds->dt_max = 0;
46 ds->dt_count = NULL;
47 ds->dt_size = dtsize(dt);
48 ds->dt_meth = dt->data.type & DT_METHODS;
49
50 if(!all)
51 return 0;
52
53 if (dt->data.type & DT_SET)
54 { dthstat(dt->data,ds,NULL);
55 if(ds->dt_max+1 > Size)
56 { if(Size > 0)
57 free(Count);
58 if(!(Count = malloc((ds->dt_max+1)*sizeof(int))) )
59 return -1;
60 Size = ds->dt_max+1;
61 }
62 for (size_t i = 0; i <= ds->dt_max; ++i)
63 Count[i] = 0;
64 dthstat(dt->data,ds,Count);
65 }
66 else if (dt->data.type & (DT_OSET|DT_OBAG))
67 { if (dt->data.here)
68 { dttstat(ds, dt->data.here, 0, NULL);
69 if(ds->dt_n+1 > Size)
70 { if(Size > 0)
71 free(Count);
72 if(!(Count = malloc((ds->dt_n+1)*sizeof(int))) )
73 return -1;
74 Size = ds->dt_n+1;
75 }
76
77 for (size_t i = 0; i <= ds->dt_n; ++i)
78 Count[i] = 0;
79 dttstat(ds, dt->data.here, 0, Count);
80 for(size_t i = 0; i <= ds->dt_n; ++i)
81 if(Count[i] > ds->dt_max)
82 ds->dt_max = Count[i];
83 }
84 }
85 ds->dt_count = Count;
86
87 return 0;
88}
#define DT_OBAG
Definition cdt.h:122
#define DT_SET
Definition cdt.h:120
CDT_API int dtsize(Dt_t *)
Definition dtsize.c:12
#define DT_METHODS
Definition cdt.h:123
#define DT_OSET
Definition cdt.h:121
#define UNFLATTEN(dt)
Definition dthdr.h:27
int dtstat(Dt_t *dt, Dtstat_t *ds, int all)
Definition dtstat.c:38
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 * malloc(YYSIZE_T)
void free(void *)
node NULL
Definition grammar.y:163
Definition cdt.h:72
Dtlink_t * here
Definition cdt.h:73
int type
Definition cdt.h:72
size_t dt_n
Definition cdt.h:114
int dt_size
Definition cdt.h:113
size_t dt_max
Definition cdt.h:115
int dt_meth
Definition cdt.h:112
size_t * dt_count
Definition cdt.h:116
Definition legal.c:50
Definition cdt.h:100
Dtdata_t data
sharable data
Definition cdt.h:102