dtstat.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <cdt/dthdr.h>
  2. #include <stddef.h>
  3. /* Get statistics of a dictionary
  4. **
  5. ** Written by Kiem-Phong Vo (5/25/96)
  6. */
  7. static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level) {
  8. if(root->left)
  9. dttstat(ds,root->left,depth+1,level);
  10. if(root->right)
  11. dttstat(ds,root->right,depth+1,level);
  12. if(depth > ds->dt_n)
  13. ds->dt_n = depth;
  14. if(level)
  15. level[depth] += 1;
  16. }
  17. static void dthstat(Dtdata_t *data, Dtstat_t *ds, size_t *count) {
  18. Dtlink_t* t;
  19. int h;
  20. for(h = data->ntab-1; h >= 0; --h)
  21. { size_t n = 0;
  22. for(t = data->htab[h]; t; t = t->right)
  23. n += 1;
  24. if(count)
  25. count[n] += 1;
  26. else if(n > 0)
  27. { ds->dt_n += 1;
  28. if(n > ds->dt_max)
  29. ds->dt_max = n;
  30. }
  31. }
  32. }
  33. int dtstat(Dt_t* dt, Dtstat_t* ds, int all)
  34. {
  35. static size_t *Count;
  36. static size_t Size;
  37. UNFLATTEN(dt);
  38. ds->dt_n = ds->dt_max = 0;
  39. ds->dt_count = NULL;
  40. ds->dt_size = dtsize(dt);
  41. ds->dt_meth = dt->data->type&DT_METHODS;
  42. if(!all)
  43. return 0;
  44. if(dt->data->type&DT_SET)
  45. { dthstat(dt->data,ds,NULL);
  46. if(ds->dt_max+1 > Size)
  47. { if(Size > 0)
  48. free(Count);
  49. if(!(Count = malloc((ds->dt_max+1)*sizeof(int))) )
  50. return -1;
  51. Size = ds->dt_max+1;
  52. }
  53. for (size_t i = 0; i <= ds->dt_max; ++i)
  54. Count[i] = 0;
  55. dthstat(dt->data,ds,Count);
  56. }
  57. else if(dt->data->type&(DT_OSET|DT_OBAG))
  58. { if(dt->data->here)
  59. { dttstat(ds,dt->data->here,0,NULL);
  60. if(ds->dt_n+1 > Size)
  61. { if(Size > 0)
  62. free(Count);
  63. if(!(Count = malloc((ds->dt_n+1)*sizeof(int))) )
  64. return -1;
  65. Size = ds->dt_n+1;
  66. }
  67. for (size_t i = 0; i <= ds->dt_n; ++i)
  68. Count[i] = 0;
  69. dttstat(ds,dt->data->here,0,Count);
  70. for(size_t i = 0; i <= ds->dt_n; ++i)
  71. if(Count[i] > ds->dt_max)
  72. ds->dt_max = Count[i];
  73. }
  74. }
  75. ds->dt_count = Count;
  76. return 0;
  77. }