-
Notifications
You must be signed in to change notification settings - Fork 0
/
print-stats.txt
68 lines (61 loc) · 2.43 KB
/
print-stats.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
/* -*- mode: c -*- */
/* These functions should be added to your htable.c file */
/**
* Prints out a table showing what the following attributes were like
* at regular intervals (as determined by num_stats) while the
* hashtable was being built.
*
* @li Percent At Home - how many keys were placed without a collision
* occurring.
* @li Average Collisions - how many collisions have occurred on
* average while placing all of the keys so far.
* @li Maximum Collisions - the most collisions that have occurred
* while placing a key.
*
* @param h the hashtable to print statistics summary from.
* @param stream the stream to send output to.
* @param num_stats the maximum number of statistical snapshots to print.
*/
void htable_print_stats(htable h, FILE *stream, int num_stats) {
int i;
fprintf(stream, "\n%s\n\n",
h->method == LINEAR_P ? "Linear Probing" : "Double Hashing");
fprintf(stream, "Percent Current Percent Average Maximum\n");
fprintf(stream, " Full Entries At Home Collisions Collisions\n");
fprintf(stream, "------------------------------------------------------\n");
for (i = 1; i <= num_stats; i++) {
print_stats_line(h, stream, 100 * i / num_stats);
}
fprintf(stream, "------------------------------------------------------\n\n");
}
/**
* Prints out a line of data from the hash table to reflect the state
* the table was in when it was a certain percentage full.
* Note: If the hashtable is less full than percent_full then no data
* will be printed.
*
* @param h - the hash table.
* @param stream - a stream to print the data to.
* @param percent_full - the point at which to show the data from.
*/
static void print_stats_line(htable h, FILE *stream, int percent_full) {
int current_entries = h->capacity * percent_full / 100;
double average_collisions = 0.0;
int at_home = 0;
int max_collisions = 0;
int i = 0;
if (current_entries > 0 && current_entries <= h->num_keys) {
for (i = 0; i < current_entries; i++) {
if (h->stats[i] == 0) {
at_home++;
}
if (h->stats[i] > max_collisions) {
max_collisions = h->stats[i];
}
average_collisions += h->stats[i];
}
fprintf(stream, "%4d %10d %11.1f %10.2f %11d\n", percent_full,
current_entries, at_home * 100.0 / current_entries,
average_collisions / current_entries, max_collisions);
}
}