blocks.c
3.03 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define START_LOC 19
#define HASH_TABLE_SIZE 32768
struct names {
char *name;
int count;
};
struct hash_entry {
struct names *name_ptr;
struct hash_entry *next;
};
struct hash_entry hash_table[HASH_TABLE_SIZE];
/* Hash table instrumentation */
#define MAXCHAIN 200
int chain_distribution[MAXCHAIN+1];
unsigned long getblockname(char *);
main(int argc, char *argv[])
{
FILE *filep;
char line[256];
char *string;
struct hash_entry *hp;
struct names *namep;
int i;
long value;
if (argc != 2) {
fprintf(stderr, "usage: %s file\n", argv[0]);
exit(1);
}
filep = fopen(argv[1], "r");
if (filep == NULL) {
fprintf(stderr, "cannot open %s\n", argv[1]);
exit(2);
}
while (1) {
if (fgets(line, 256, filep) == NULL)
break;
/* find the sting after 3 numbers */
string = line;
for (i = 0; i < 3; i++) {
while (isspace(*string))
string++;
if (!*string)
break;
if (!isdigit(*string))
break;
while (isdigit(*string))
string++;
if (!*string)
break;
}
while (isspace(*string))
string++;
if (!*string || i != 3)
continue;
value = getblockname(string);
/* find or place string in hash table */
hp = &hash_table[value];
if (hp->name_ptr == 0) {
namep = (struct names *)malloc(sizeof(struct names));
namep->name = strdup(string);
namep->count = 1;
hp->name_ptr = namep;
} else {
/* walk down the chain looking for a match */
while (hp != 0) {
if (strcmp(string, hp->name_ptr->name) == 0) {
hp->name_ptr->count++;
break;
} else {
hp = hp->next;
}
}
if (hp == NULL) {
/* did not find match in chain */
namep = (struct names *)malloc(sizeof(struct names));
namep->name = strdup(string);
namep->count = 1;
hp = (struct hash_entry *)malloc(sizeof(struct hash_entry));
hp->name_ptr = hash_table[value].name_ptr;
hp->next = hash_table[value].next;
hash_table[value].name_ptr = namep;
hash_table[value].next = hp;
}
}
}
/* now print out the list */
for (i = 0; i < HASH_TABLE_SIZE; i++) {
int chain;
chain = 0;
hp = &hash_table[i];
while (hp) {
namep = hp->name_ptr;
if (namep) {
printf("%5d %s\n", namep->count, namep->name);
chain++;
}
hp = hp->next;
}
if (chain >= MAXCHAIN) {
chain = MAXCHAIN;
}
chain_distribution[chain]++;
}
for (i=0; i < MAXCHAIN+1; i++) {
if (chain_distribution[i])
fprintf(stderr, "chain_distribution[%d] is %d\n", i, chain_distribution[i]);
}
}
/*
* retrive the block name from the entire netname. Put a 0 (ie
* end the string) at the '.'
* Also calculate the hash value
*/
unsigned long
getblockname(char *s)
{
unsigned long value;
unsigned char c;
value = 0;
while (1) {
c = (unsigned)*s;
if (c == '.' || c == '\n' || c == 0) {
*s = 0;
return (value % 32768);
}
value <<= 1;
value += c;
s++;
}
}