-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
261 lines (217 loc) · 5.67 KB
/
main.cpp
File metadata and controls
261 lines (217 loc) · 5.67 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//Author: Shawn 'sintaxerror' Rainey
//Do what you want with it.
#include <fstream>
#include <iostream>
#include <string.h>
#include <cmath>
//#include "hr_time.h"
#include <sys/time.h>
using namespace std;
const int numEntries = 50000;
const int numLookups = 10000;
unsigned int caseInsensitiveHash(const char *str);
const int tableSize = 0x3FFFF;
//Simple chained bucket entries
class tableEntry{
const char* word;
int value;
tableEntry* next;
public:
tableEntry(): word(0), value(0), next(0) {
}
//Returns the length of this bucket chain after insertion
//or 0 if duplicate
int insert(const char* word, const int value)
{
tableEntry* insertPoint = this;
int returnVal = 1;
//Until we reach the last
while(insertPoint->word != 0)
{
//String already in table. Replace value
if(!strcmp(this->word, word))
{
this->value = value;
return 0;
}
//make a new blank tableEntry as the last one
if(0 == insertPoint->next)
{
insertPoint->next = new tableEntry();
returnVal++;
}
insertPoint = insertPoint->next;
}
//insertPoint->word = new char[strlen(word)+1];
insertPoint->word = word;
//strcpy(insertPoint->word, word);
insertPoint->value = value;
return returnVal;
}
//Return value mapped to given string
//or -1 if not found.
int valueOf(const char* string)
{
if(0 == this->word) return -1;
int returnVal = 0;
tableEntry* foundPoint = this;
while(returnVal != -1)
{
if(!strcmp(foundPoint->word, string))
{
returnVal = foundPoint->value;
break;
}
else
{
if(foundPoint->next == 0)
{
returnVal = -1;
}
else
{
foundPoint = foundPoint->next;
}
}
}
return returnVal;
}
~tableEntry()
{
//delete[] word;
delete next;
//word = 0;
//value = 0;
//next = 0;
}
} hashTable[tableSize];
//Simple hash for single words, returns a number 0 through (tableLength-1)
unsigned int caseInsensitiveHash(const char* str)
{
//Length of a minimal representation of a letter: 5 bits
static const int sOfLetter = 5;
//Size of the hash in bits
//Note: 21 is not an arbitrary value. This length produces the least
//amount of collisions for the given input.
static const int sOfInt = 21;//(int)(log(tableSize)/log(2));
int offset = 0;
unsigned int hash = 0;//0x55555555;
unsigned int previousOffset = offset;
unsigned const int maxStartOffset = sOfInt - sOfLetter;
while(*str != 0)
{
hash ^= (unsigned(toupper(*str++))
- 64)
<< offset;
offset+=sOfLetter;
if(offset >= sOfInt-sOfLetter)
{
offset = (++previousOffset % (maxStartOffset));
}
}
if((hash & tableSize) == tableSize)
hash = tableSize-1;
return (hash & tableSize);
}
inline int AddWord(const char * text, int value)
{
return hashTable[caseInsensitiveHash(text)].insert(text, value);
}
inline int GetValue(const char * text)
{
return hashTable[caseInsensitiveHash(text)].valueOf(text);
}
//Reads a whole file into the contents buffer.
//Allocates a new contents array using new
void readFile(const char *const fname, char * &contents, long &size)
{
//Probably not the right thing to do
if(contents != 0) return;
filebuf *fbuf;
ifstream inReader(fname);
fbuf = inReader.rdbuf();
size = fbuf->pubseekoff(0, ios::end, ios::in);
fbuf->pubseekpos(0, ios::in);
contents = new char[size];
fbuf->sgetn(contents, size);
inReader.close();
}
int main()
{
char* buffer[2] = {0, 0};
long size50, size10;
//CStopWatch s;
timeval startTime, lookupTime, endTime;
readFile("50000words.txt", buffer[0], size50);
readFile("10000words.txt", buffer[1], size10);
char *entryList[numEntries],
*lookupList[numLookups];
int entryVals[numEntries],
lookupVals[numLookups];
const char delim[] = ", \n\r";
int i = 0;
entryList[0] = strtok(buffer[0], delim);
//cout << entryList[0] << endl; //Tetraheism
while(entryList[i] != NULL && i < numEntries-1)
{
entryVals[i] = atoi(strtok(NULL, delim));
entryList[++i] = strtok(NULL, delim);
}
//cout << (void*)entryList[0] << endl; //0x7c0020
//cout << entryList[1] << endl; //Chrisoms
lookupList[0] = strtok(buffer[1], delim);
//cout << (void*)entryList[0] << endl; //0x7c0020
//cout << entryList[1] << endl; //Chrisoms
i = 0;
while(lookupList[i] != NULL && i < numLookups-1)
{
lookupList[++i] = strtok(NULL, delim);
}
//cout << i << endl;
//entryList[0] = buffer[0];
//cout << (void*)entryList[0] << endl;
//cout << entryList[1] << endl; //Chrisoms
int numCollisions = 0;
gettimeofday(&startTime, NULL);
i = 0;
while(entryList[i] != NULL && i < numEntries)
{
//Collided if AddWord returns > 1
numCollisions += (AddWord(entryList[i], entryVals[i]) > 1? 1:0);
//entryList[i] = 0;
++i;
}
//DONE LOADING HERE - Start the timer!
gettimeofday(&lookupTime, NULL);
i = 0;
int sum = 0;
while(lookupList[i] != NULL && i < numLookups)
{
sum += lookupVals[i] = GetValue(lookupList[i]);
++i;
}
//DONE TIMING
gettimeofday(&endTime, NULL);
i=0;
while(lookupList[i] != NULL && i < numLookups)
{
//if(lookupVals[i] == -1)
//{
// cout << entryList[0] << " " << entryVals[0] << endl;
// cout << lookupList[i] << caseInsensitiveHash(lookupList[i]) << endl;
// cout << hashTable[caseInsensitiveHash(lookupList[i])].valueOf(lookupList[i]) << endl;
//}
//cout << lookupList[i] << " " << lookupVals[i] << endl;
lookupList[i] = 0;
++i;
}
cout << "Number of collisions: " << numCollisions << endl;
//useconds to seconds
cout << "Total time: " <<(endTime.tv_usec - startTime.tv_usec) / (1000.0 * 1000.0) << endl;
cout << "Time for lookups: " << (endTime.tv_usec - lookupTime.tv_usec) / (1000.0*1000.0) << endl;
//Should be 496637501
cout << sum << endl;
delete [] buffer[0];
delete [] buffer[1];
return 0;
}