-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhashsettest.c
More file actions
171 lines (144 loc) · 5.56 KB
/
Copy pathhashsettest.c
File metadata and controls
171 lines (144 loc) · 5.56 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
163
164
165
166
167
168
169
170
#include "hashset.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <assert.h>
const int kNumBuckets = 26;
struct frequency {
char ch; // a particular letter
int occurrences; // the number of times it occurs
};
/**
* Fucntion: HashFrequency
* -----------------------
* Hash function used to partition frequency structures into buckets. Our
* hash function is pretty simplistic, we simply mod the char by the
* number of buckets. This provides a number in the range 0 to numBuckets - 1,
* which is the proper range for our hashset.
*/
static int HashFrequency(const void *elem, int numBuckets)
{
struct frequency *freq = (struct frequency *)elem;
return (freq->ch % numBuckets);
}
/**
* Function: PrintFrequency
* -------------------------
* Mapping function used to print one frequency stored in a hashset. The
* file is passed as the client data, so that it can be used to print to
* any FILE *.
*/
static void PrintFrequency(void *elem, void *fp)
{
struct frequency *freq = (struct frequency *)elem;
fprintf((FILE *)fp, "Character %c occurred %4d times\n",
freq->ch, freq->occurrences);
}
/**
* Function: CompareLetter
* -----------------------
* Comparator function used to compare two frequency entries within hashset.
* Two frequencies are considered the same if they refer to the same char.
* This function is used in HashSetLookup to decide if a char already has been
* entered. This function has the same return semantics as strcmp:
* (negative if 1<2, zero if 1==2, positive if 1>2).
*/
static int CompareLetter(const void *elem1, const void *elem2)
{
struct frequency *freq1 = (struct frequency *)elem1;
struct frequency *freq2 = (struct frequency *)elem2;
return (freq1->ch - freq2->ch);
}
/**
* Function: CompareOccurrences
* -----------------------------
* Comparator function used to compare two frequency entries. This one sorts
* by occurrence as the primary key, and if occurrences are the same, it uses
* the char as the secondary key, thus the call to CompareLetter which
* compares the chars. It "reverse-sorts" so a higher frequency bubbles to
* the top.
*/
static int CompareOccurrences(const void *elem1, const void *elem2)
{
struct frequency *freq1 = (struct frequency *)elem1;
struct frequency *freq2 = (struct frequency *)elem2;
if (freq1->occurrences > freq2->occurrences) return -1;
else if (freq1->occurrences < freq2->occurrences) return 1;
else return CompareLetter(freq1, freq2);
}
/**
* Function: BuildTableOfLetterCounts
* ----------------------------------
* Opens a file (in this case, the hashsettest.c you are currently reading).
* and pulls characters out one by one and counts the number of times
* each character occurs. The frequecy information about the alphabetic
* characters is stored in the counts hashset passed in by address.
*/
static void BuildTableOfLetterCounts(hashset *counts)
{
struct frequency localFreq, *found;
int ch;
FILE *fp = fopen("hashsettest.c", "r"); // open self as file
assert(fp != NULL);
while ((ch = getc(fp)) != EOF) {
if (isalpha(ch)) { // only count letters
localFreq.ch = tolower(ch);
localFreq.occurrences = 1;
// See if we already have an entry for this char
found = (struct frequency *) HashSetLookup(counts, &localFreq);
if (found != NULL) // increment if already there
localFreq.occurrences = found->occurrences + 1;
HashSetEnter(counts, &localFreq); // enter should overwrite if needed
}
}
fclose(fp);
}
/**
* Function: AddFrequency
* ----------------------
* Mapping function used to take a frequency stored
* in a hashset and append it to an vector of frequencies.
* The address of the vector is passed in as the auxiliarydata.
*/
static void AddFrequency(void *elem, void *v)
{
VectorAppend((vector *) v, elem);
}
/**
* Function: TestHashTable
* -----------------------
* Runs a test of the hashset using a frequency structure as the element
* type. It will open a file, read each char, and count the number of
* times each char occurs. Tests enter, lookup, and mapping for the hashset.
* Prints contents of table to stdout. Then it dumps all the table elements
* into a vector and sorts them by frequency of occurrences and
* prints the array out. Note that this particular stress test passes
* 0 as the initialAllocation, which the vector is required to handle
* gracefully - be careful!
*/
static void TestHashTable(void)
{
hashset counts;
vector sortedCounts;
HashSetNew(&counts, sizeof(struct frequency), kNumBuckets, HashFrequency, CompareLetter, NULL);
fprintf(stdout, "\n\n ------------------------- Starting the HashTable test\n");
BuildTableOfLetterCounts(&counts);
fprintf(stdout, "Here is the unordered contents of the table:\n");
HashSetMap(&counts, PrintFrequency, stdout); // print contents of table
VectorNew(&sortedCounts, sizeof(struct frequency), NULL, 0);
HashSetMap(&counts, AddFrequency, &sortedCounts); // add all freq to array
VectorSort(&sortedCounts, CompareLetter); // sort by char
fprintf(stdout, "\nHere are the trials sorted by char: \n");
VectorMap(&sortedCounts, PrintFrequency, stdout);
VectorSort(&sortedCounts, CompareOccurrences); //sort by occurrences
fprintf(stdout, "\nHere are the trials sorted by occurrence & char: \n");
VectorMap(&sortedCounts, PrintFrequency, stdout); // print out array
VectorDispose(&sortedCounts); // free all storage
HashSetDispose(&counts);
}
int main(int ununsed, char **alsoUnused)
{
TestHashTable();
return 0;
}