00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "dthdr.h"
00018
00019 #ifdef DMALLOC
00020 #include "dmalloc.h"
00021 #endif
00022
00023
00024
00025
00026
00027
00028 #if __STD_C
00029 static void dttstat(Dtstat_t * ds, Dtlink_t * root, int depth, int *level)
00030 #else
00031 static void dttstat(ds, root, depth, level)
00032 Dtstat_t *ds;
00033 Dtlink_t *root;
00034 int depth;
00035 int *level;
00036 #endif
00037 {
00038 if (root->left)
00039 dttstat(ds, root->left, depth + 1, level);
00040 if (root->right)
00041 dttstat(ds, root->right, depth + 1, level);
00042 if (depth > ds->dt_n)
00043 ds->dt_n = depth;
00044 if (level)
00045 level[depth] += 1;
00046 }
00047
00048 #if __STD_C
00049 static void dthstat(reg Dtdata_t * data, Dtstat_t * ds, reg int *count)
00050 #else
00051 static void dthstat(data, ds, count)
00052 reg Dtdata_t *data;
00053 Dtstat_t *ds;
00054 reg int *count;
00055 #endif
00056 {
00057 reg Dtlink_t *t;
00058 reg int n, h;
00059
00060 for (h = data->ntab - 1; h >= 0; --h) {
00061 n = 0;
00062 for (t = data->htab[h]; t; t = t->right)
00063 n += 1;
00064 if (count)
00065 count[n] += 1;
00066 else if (n > 0) {
00067 ds->dt_n += 1;
00068 if (n > ds->dt_max)
00069 ds->dt_max = n;
00070 }
00071 }
00072 }
00073
00074 #if __STD_C
00075 int dtstat(reg Dt_t * dt, Dtstat_t * ds, int all)
00076 #else
00077 int dtstat(dt, ds, all)
00078 reg Dt_t *dt;
00079 Dtstat_t *ds;
00080 int all;
00081 #endif
00082 {
00083 reg int i;
00084 static int *Count, Size;
00085
00086 UNFLATTEN(dt);
00087
00088 ds->dt_n = ds->dt_max = 0;
00089 ds->dt_count = NIL(int *);
00090 ds->dt_size = dtsize(dt);
00091 ds->dt_meth = dt->data->type & DT_METHODS;
00092
00093 if (!all)
00094 return 0;
00095
00096 if (dt->data->type & (DT_SET | DT_BAG)) {
00097 dthstat(dt->data, ds, NIL(int *));
00098 if (ds->dt_max + 1 > Size) {
00099 if (Size > 0)
00100 free(Count);
00101 if (!(Count = (int *) malloc((ds->dt_max + 1) * sizeof(int))))
00102 return -1;
00103 Size = ds->dt_max + 1;
00104 }
00105 for (i = ds->dt_max; i >= 0; --i)
00106 Count[i] = 0;
00107 dthstat(dt->data, ds, Count);
00108 } else if (dt->data->type & (DT_OSET | DT_OBAG)) {
00109 if (dt->data->here) {
00110 dttstat(ds, dt->data->here, 0, NIL(int *));
00111 if (ds->dt_n + 1 > Size) {
00112 if (Size > 0)
00113 free(Count);
00114 if (!
00115 (Count = (int *) malloc((ds->dt_n + 1) * sizeof(int))))
00116 return -1;
00117 Size = ds->dt_n + 1;
00118 }
00119
00120 for (i = ds->dt_n; i >= 0; --i)
00121 Count[i] = 0;
00122 dttstat(ds, dt->data->here, 0, Count);
00123 for (i = ds->dt_n; i >= 0; --i)
00124 if (Count[i] > ds->dt_max)
00125 ds->dt_max = Count[i];
00126 }
00127 }
00128 ds->dt_count = Count;
00129
00130 return 0;
00131 }