Repositories » libk0
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

Modified the function to generate hash tables stats to match the new
interface.

Changeset d19e9247ecd1

Parent 35a758ef931e

by Rup Palchowdhury

Changes to 4 files · Browse files at d19e9247ecd1 Showing diff from parent 35a758ef931e Diff from another changeset...

Change 1 of 2 Show Entire File khash.c Stacked
 
11
12
13
 
14
15
16
 
39
40
41
 
42
43
44
45
46
47
 
48
49
50
 
 
51
52
 
53
54
 
55
56
57
 
58
59
60
61
62
63
64
65
66
67
68
69
 
 
 
 
 
70
 
11
12
13
14
15
16
17
 
40
41
42
43
44
45
46
47
48
 
49
50
 
 
51
52
53
 
54
55
 
56
57
58
 
59
60
 
61
62
63
64
 
 
 
 
 
 
65
66
67
68
69
70
@@ -11,6 +11,7 @@
  for (int i = 0; i < n; i++)   h->tab[i] = NULL;   h->n = n; + h->nel = 0;   h->cmp = cmp;   h->hash = hash;   return h; @@ -39,32 +40,31 @@
  return np;   if (create == 1) {   h->tab[m] = addfront(h->tab[m], n); + h->nel++;   return h->tab[m];   }   return NULL;  }   -void hstats(Node *htab[], unsigned size) +void hstats(Hash *h, unsigned size)  { - float fill, avglen; - int c, maxlen, sumlen, chainlen; + float free, avglen; + int c, maxlen, chainlen;   Node *np; - c = maxlen = sumlen = 0; + c = maxlen = 0;   for (int i = 0; i < size; i++) { - if (htab[i] != NULL) { + if (h->tab[i] != NULL) {   c++;   chainlen = 0; - for (np = htab[i]; np != NULL; np = np->next) + for (np = h->tab[i]; np != NULL; np = np->next)   chainlen++; - sumlen += chainlen;   if (chainlen > maxlen) maxlen = chainlen;   }   }   - fill = (float)c / (float)size * 100; - avglen = (float)sumlen / (float)c; - fprintf(stderr, "Fill: %5.2f%% (%d/%d)\n", fill, c, size); - fprintf(stderr, "Avg chain length: %5.2f\n", avglen); - fprintf(stderr, "Max chain length: %d\n", maxlen); - fprintf(stderr, "Node count: %d\n", sumlen); + free = (float)(size - c) / (float)size * 100; + avglen = (float)h->nel / (float)c; + fprintf(stderr, "free space: %5.1f%% (%d/%d)\n", free, size - c, size); + fprintf(stderr, "#elements: %d\n", h->nel); + fprintf(stderr, "chain length (max/avg): %5.1f/%d\n", avglen, maxlen);  }
Change 1 of 2 Show Entire File khash.h Stacked
 
2
3
4
5
 
6
7
8
 
10
11
12
13
 
 
2
3
4
 
5
6
7
8
 
10
11
12
 
13
@@ -2,7 +2,7 @@
   struct Hash {   Node **tab; - unsigned n; + unsigned n, nel;   fn_cmp cmp;   fn_hash hash;  }; @@ -10,4 +10,4 @@
 Hash *newhash(unsigned, fn_cmp, fn_hash);  Node *hlookup(Hash*, Node*, int);  unsigned hash(void*, unsigned); -void hstats(Node *htab[], unsigned); +void hstats(Hash*, unsigned);
Change 1 of 2 Show Entire File kpost.c Stacked
 
7
8
9
10
11
 
12
13
14
 
26
27
28
29
30
31
32
33
34
35
 
36
37
38
 
7
8
9
 
 
10
11
12
13
 
25
26
27
 
 
 
 
 
 
 
28
29
30
31
@@ -7,8 +7,7 @@
 {   Post *t;   t = (Post *)malloc(sizeof(Post)); - t->s = (char *)malloc(strlen(s)); - strcpy(t->s, s); + t->s = s;   t->f = f;   return t;  } @@ -26,13 +25,7 @@
   void freepost(void *t)  { - Post *t_; - t_ = (Post *)t; - if (t_ != NULL) { - if (t_->s != NULL) - free(t_->s); - free(t_); - } + free(t);  }    void printpost(void *d, void *arg)
Change 1 of 1 Show Entire File test.c Stacked
 
38
39
40
41
 
 
 
 
42
43
 
44
45
46
 
38
39
40
 
41
42
43
44
45
 
46
47
48
49
@@ -38,9 +38,12 @@
  printf("\n");   }   } - np = hlookup(h, newnode(newpost(w[0], 0)), 0); + + hstats(h, NHASH); + + np = hlookup(h, newnode(newpost(w[3], 0)), 0);   if (np != NULL) { - pp = (Post *)np->data; + pp = (Post *)(np->data);   printf("Found: %s %d\n", pp->s, pp->f);   }   else