xref: /onnv-gate/usr/src/lib/libast/common/cdt/dtstat.c (revision 4887:feebf9260c2e)
1*4887Schin /***********************************************************************
2*4887Schin *                                                                      *
3*4887Schin *               This software is part of the ast package               *
4*4887Schin *           Copyright (c) 1985-2007 AT&T Knowledge Ventures            *
5*4887Schin *                      and is licensed under the                       *
6*4887Schin *                  Common Public License, Version 1.0                  *
7*4887Schin *                      by AT&T Knowledge Ventures                      *
8*4887Schin *                                                                      *
9*4887Schin *                A copy of the License is available at                 *
10*4887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
11*4887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12*4887Schin *                                                                      *
13*4887Schin *              Information and Software Systems Research               *
14*4887Schin *                            AT&T Research                             *
15*4887Schin *                           Florham Park NJ                            *
16*4887Schin *                                                                      *
17*4887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
18*4887Schin *                  David Korn <dgk@research.att.com>                   *
19*4887Schin *                   Phong Vo <kpv@research.att.com>                    *
20*4887Schin *                                                                      *
21*4887Schin ***********************************************************************/
22*4887Schin #include	"dthdr.h"
23*4887Schin 
24*4887Schin /*	Get statistics of a dictionary
25*4887Schin **
26*4887Schin **	Written by Kiem-Phong Vo (5/25/96)
27*4887Schin */
28*4887Schin 
29*4887Schin #if __STD_C
30*4887Schin static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level)
31*4887Schin #else
32*4887Schin static void dttstat(ds,root,depth,level)
33*4887Schin Dtstat_t*	ds;
34*4887Schin Dtlink_t*	root;
35*4887Schin int		depth;
36*4887Schin int*		level;
37*4887Schin #endif
38*4887Schin {
39*4887Schin 	if(root->left)
40*4887Schin 		dttstat(ds,root->left,depth+1,level);
41*4887Schin 	if(root->right)
42*4887Schin 		dttstat(ds,root->right,depth+1,level);
43*4887Schin 	if(depth > ds->dt_n)
44*4887Schin 		ds->dt_n = depth;
45*4887Schin 	if(level)
46*4887Schin 		level[depth] += 1;
47*4887Schin }
48*4887Schin 
49*4887Schin #if __STD_C
50*4887Schin static void dthstat(reg Dtdata_t* data, Dtstat_t* ds, reg int* count)
51*4887Schin #else
52*4887Schin static void dthstat(data, ds, count)
53*4887Schin reg Dtdata_t*	data;
54*4887Schin Dtstat_t*	ds;
55*4887Schin reg int*	count;
56*4887Schin #endif
57*4887Schin {
58*4887Schin 	reg Dtlink_t*	t;
59*4887Schin 	reg int		n, h;
60*4887Schin 
61*4887Schin 	for(h = data->ntab-1; h >= 0; --h)
62*4887Schin 	{	n = 0;
63*4887Schin 		for(t = data->htab[h]; t; t = t->right)
64*4887Schin 			n += 1;
65*4887Schin 		if(count)
66*4887Schin 			count[n] += 1;
67*4887Schin 		else if(n > 0)
68*4887Schin 		{	ds->dt_n += 1;
69*4887Schin 			if(n > ds->dt_max)
70*4887Schin 				ds->dt_max = n;
71*4887Schin 		}
72*4887Schin 	}
73*4887Schin }
74*4887Schin 
75*4887Schin #if __STD_C
76*4887Schin int dtstat(reg Dt_t* dt, Dtstat_t* ds, int all)
77*4887Schin #else
78*4887Schin int dtstat(dt, ds, all)
79*4887Schin reg Dt_t*	dt;
80*4887Schin Dtstat_t*	ds;
81*4887Schin int		all;
82*4887Schin #endif
83*4887Schin {
84*4887Schin 	reg int		i;
85*4887Schin 	static int	*Count, Size;
86*4887Schin 
87*4887Schin 	UNFLATTEN(dt);
88*4887Schin 
89*4887Schin 	ds->dt_n = ds->dt_max = 0;
90*4887Schin 	ds->dt_count = NIL(int*);
91*4887Schin 	ds->dt_size = dtsize(dt);
92*4887Schin 	ds->dt_meth = dt->data->type&DT_METHODS;
93*4887Schin 
94*4887Schin 	if(!all)
95*4887Schin 		return 0;
96*4887Schin 
97*4887Schin 	if(dt->data->type&(DT_SET|DT_BAG))
98*4887Schin 	{	dthstat(dt->data,ds,NIL(int*));
99*4887Schin 		if(ds->dt_max+1 > Size)
100*4887Schin 		{	if(Size > 0)
101*4887Schin 				free(Count);
102*4887Schin 			if(!(Count = (int*)malloc((ds->dt_max+1)*sizeof(int))) )
103*4887Schin 				return -1;
104*4887Schin 			Size = ds->dt_max+1;
105*4887Schin 		}
106*4887Schin 		for(i = ds->dt_max; i >= 0; --i)
107*4887Schin 			Count[i] = 0;
108*4887Schin 		dthstat(dt->data,ds,Count);
109*4887Schin 	}
110*4887Schin 	else if(dt->data->type&(DT_OSET|DT_OBAG))
111*4887Schin 	{	if(dt->data->here)
112*4887Schin 		{	dttstat(ds,dt->data->here,0,NIL(int*));
113*4887Schin 			if(ds->dt_n+1 > Size)
114*4887Schin 			{	if(Size > 0)
115*4887Schin 					free(Count);
116*4887Schin 				if(!(Count = (int*)malloc((ds->dt_n+1)*sizeof(int))) )
117*4887Schin 					return -1;
118*4887Schin 				Size = ds->dt_n+1;
119*4887Schin 			}
120*4887Schin 
121*4887Schin 			for(i = ds->dt_n; i >= 0; --i)
122*4887Schin 				Count[i] = 0;
123*4887Schin 			dttstat(ds,dt->data->here,0,Count);
124*4887Schin 			for(i = ds->dt_n; i >= 0; --i)
125*4887Schin 				if(Count[i] > ds->dt_max)
126*4887Schin 					ds->dt_max = Count[i];
127*4887Schin 		}
128*4887Schin 	}
129*4887Schin 	ds->dt_count = Count;
130*4887Schin 
131*4887Schin 	return 0;
132*4887Schin }
133