14887Schin /***********************************************************************
24887Schin * *
34887Schin * This software is part of the ast package *
4*12068SRoger.Faulkner@Oracle.COM * Copyright (c) 1985-2010 AT&T Intellectual Property *
54887Schin * and is licensed under the *
64887Schin * Common Public License, Version 1.0 *
78462SApril.Chin@Sun.COM * by AT&T Intellectual Property *
84887Schin * *
94887Schin * A copy of the License is available at *
104887Schin * http://www.opensource.org/licenses/cpl1.0.txt *
114887Schin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
124887Schin * *
134887Schin * Information and Software Systems Research *
144887Schin * AT&T Research *
154887Schin * Florham Park NJ *
164887Schin * *
174887Schin * Glenn Fowler <gsf@research.att.com> *
184887Schin * David Korn <dgk@research.att.com> *
194887Schin * Phong Vo <kpv@research.att.com> *
204887Schin * *
214887Schin ***********************************************************************/
224887Schin #include "dthdr.h"
234887Schin
244887Schin /* Get statistics of a dictionary
254887Schin **
264887Schin ** Written by Kiem-Phong Vo (5/25/96)
274887Schin */
284887Schin
294887Schin #if __STD_C
dttstat(Dtstat_t * ds,Dtlink_t * root,int depth,int * level)304887Schin static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level)
314887Schin #else
324887Schin static void dttstat(ds,root,depth,level)
334887Schin Dtstat_t* ds;
344887Schin Dtlink_t* root;
354887Schin int depth;
364887Schin int* level;
374887Schin #endif
384887Schin {
394887Schin if(root->left)
404887Schin dttstat(ds,root->left,depth+1,level);
414887Schin if(root->right)
424887Schin dttstat(ds,root->right,depth+1,level);
434887Schin if(depth > ds->dt_n)
444887Schin ds->dt_n = depth;
454887Schin if(level)
464887Schin level[depth] += 1;
474887Schin }
484887Schin
494887Schin #if __STD_C
dthstat(reg Dtdata_t * data,Dtstat_t * ds,reg int * count)504887Schin static void dthstat(reg Dtdata_t* data, Dtstat_t* ds, reg int* count)
514887Schin #else
524887Schin static void dthstat(data, ds, count)
534887Schin reg Dtdata_t* data;
544887Schin Dtstat_t* ds;
554887Schin reg int* count;
564887Schin #endif
574887Schin {
584887Schin reg Dtlink_t* t;
594887Schin reg int n, h;
604887Schin
614887Schin for(h = data->ntab-1; h >= 0; --h)
624887Schin { n = 0;
634887Schin for(t = data->htab[h]; t; t = t->right)
644887Schin n += 1;
654887Schin if(count)
664887Schin count[n] += 1;
674887Schin else if(n > 0)
684887Schin { ds->dt_n += 1;
694887Schin if(n > ds->dt_max)
704887Schin ds->dt_max = n;
714887Schin }
724887Schin }
734887Schin }
744887Schin
754887Schin #if __STD_C
dtstat(reg Dt_t * dt,Dtstat_t * ds,int all)764887Schin int dtstat(reg Dt_t* dt, Dtstat_t* ds, int all)
774887Schin #else
784887Schin int dtstat(dt, ds, all)
794887Schin reg Dt_t* dt;
804887Schin Dtstat_t* ds;
814887Schin int all;
824887Schin #endif
834887Schin {
844887Schin reg int i;
854887Schin static int *Count, Size;
864887Schin
874887Schin UNFLATTEN(dt);
884887Schin
894887Schin ds->dt_n = ds->dt_max = 0;
904887Schin ds->dt_count = NIL(int*);
914887Schin ds->dt_size = dtsize(dt);
924887Schin ds->dt_meth = dt->data->type&DT_METHODS;
934887Schin
944887Schin if(!all)
954887Schin return 0;
964887Schin
974887Schin if(dt->data->type&(DT_SET|DT_BAG))
984887Schin { dthstat(dt->data,ds,NIL(int*));
994887Schin if(ds->dt_max+1 > Size)
1004887Schin { if(Size > 0)
1014887Schin free(Count);
1024887Schin if(!(Count = (int*)malloc((ds->dt_max+1)*sizeof(int))) )
1034887Schin return -1;
1044887Schin Size = ds->dt_max+1;
1054887Schin }
1064887Schin for(i = ds->dt_max; i >= 0; --i)
1074887Schin Count[i] = 0;
1084887Schin dthstat(dt->data,ds,Count);
1094887Schin }
1104887Schin else if(dt->data->type&(DT_OSET|DT_OBAG))
1114887Schin { if(dt->data->here)
1124887Schin { dttstat(ds,dt->data->here,0,NIL(int*));
1134887Schin if(ds->dt_n+1 > Size)
1144887Schin { if(Size > 0)
1154887Schin free(Count);
1164887Schin if(!(Count = (int*)malloc((ds->dt_n+1)*sizeof(int))) )
1174887Schin return -1;
1184887Schin Size = ds->dt_n+1;
1194887Schin }
1204887Schin
1214887Schin for(i = ds->dt_n; i >= 0; --i)
1224887Schin Count[i] = 0;
1234887Schin dttstat(ds,dt->data->here,0,Count);
1244887Schin for(i = ds->dt_n; i >= 0; --i)
1254887Schin if(Count[i] > ds->dt_max)
1264887Schin ds->dt_max = Count[i];
1274887Schin }
1284887Schin }
1294887Schin ds->dt_count = Count;
1304887Schin
1314887Schin return 0;
1324887Schin }
133