]> granicus.if.org Git - graphviz/commitdiff
cdt: use 'size_t' types for counting stats instead of 'int'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 3 Nov 2022 05:02:42 +0000 (22:02 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 4 Nov 2022 04:18:04 +0000 (21:18 -0700)
This squashes two compiler warnings:

  dtstat.c: In function ‘dtstat’:
  dtstat.c:60:59: warning: conversion to ‘long unsigned int’ from ‘int’ may
    change the sign of the result [-Wsign-conversion]
     60 |  if(!(Count = malloc((ds->dt_max+1)*sizeof(int))) )
        |                                    ^
  dtstat.c:74:65: warning: conversion to ‘long unsigned int’ from ‘int’ may
    change the sign of the result [-Wsign-conversion]
     74 |  if(!(Count = malloc((ds->dt_n+1)*sizeof(int))) )
        |                                  ^

It also allows these functions to now count beyond 2³¹ - 1.

lib/cdt/cdt.h
lib/cdt/dtstat.c

index 6e3aa44c8eefdace6e14756cbc31580dfdb4d75d..a285ac26b2fd1f3b0cf51fe01a2b4e067e37b0b3 100644 (file)
@@ -119,9 +119,9 @@ struct _dt_s
 struct _dtstat_s
 {      int     dt_meth;        /* method type                          */
        int     dt_size;        /* number of elements                   */
-       int     dt_n;           /* number of chains or levels           */
-       int     dt_max;         /* max size of a chain or a level       */
-       int*    dt_count;       /* counts of chains or levels by size   */
+       size_t dt_n; // number of chains or levels
+       size_t dt_max; // max size of a chain or a level
+       size_t* dt_count; // counts of chains or levels by size
 };
 
 /* flag set if the last search operation actually found the object */
index 5c40fb1dcacc415b81756f0477fabf29fa48873c..91e7138285a5d24024eace07150ea7790ffa2d11 100644 (file)
@@ -6,8 +6,7 @@
 **     Written by Kiem-Phong Vo (5/25/96)
 */
 
-static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level)
-{
+static void dttstat(Dtstat_t *ds, Dtlink_t *root, size_t depth, size_t *level) {
        if(root->left)
                dttstat(ds,root->left,depth+1,level);
        if(root->right)
@@ -18,13 +17,12 @@ static void dttstat(Dtstat_t* ds, Dtlink_t* root, int depth, int* level)
                level[depth] += 1;
 }
 
-static void dthstat(Dtdata_t* data, Dtstat_t* ds, int* count)
-{
+static void dthstat(Dtdata_t *data, Dtstat_t *ds, size_t *count) {
        Dtlink_t*       t;
-       int             n, h;
+       int             h;
 
        for(h = data->ntab-1; h >= 0; --h)
-       {       n = 0;
+       {       size_t n = 0;
                for(t = data->htab[h]; t; t = t->right)
                        n += 1;
                if(count)
@@ -39,8 +37,8 @@ static void dthstat(Dtdata_t* data, Dtstat_t* ds, int* count)
 
 int dtstat(Dt_t* dt, Dtstat_t* ds, int all)
 {
-       int             i;
-       static int      *Count, Size;
+       static size_t *Count;
+       static size_t Size;
 
        UNFLATTEN(dt);
 
@@ -61,7 +59,7 @@ int dtstat(Dt_t* dt, Dtstat_t* ds, int all)
                                return -1;
                        Size = ds->dt_max+1;
                }
-               for(i = ds->dt_max; i >= 0; --i)
+               for (size_t i = 0; i <= ds->dt_max; ++i)
                        Count[i] = 0;
                dthstat(dt->data,ds,Count);
        }
@@ -76,10 +74,10 @@ int dtstat(Dt_t* dt, Dtstat_t* ds, int all)
                                Size = ds->dt_n+1;
                        }
 
-                       for(i = ds->dt_n; i >= 0; --i)
+                       for (size_t i = 0; i <= ds->dt_n; ++i)
                                Count[i] = 0;
                        dttstat(ds,dt->data->here,0,Count);
-                       for(i = ds->dt_n; i >= 0; --i)
+                       for(size_t i = 0; i <= ds->dt_n; ++i)
                                if(Count[i] > ds->dt_max)
                                        ds->dt_max = Count[i];
                }