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