-
Notifications
You must be signed in to change notification settings - Fork 0
/
Indexer.cpp
277 lines (254 loc) · 7.12 KB
/
Indexer.cpp
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "provided.h"
#include "MyMap.h"
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;
struct bucket;
const int MAX_MAP_SIZE=20351;
class IndexerImpl
{
public:
IndexerImpl();
~IndexerImpl();
bool incorporate(string url, WordBag& wb);
vector<UrlCount> getUrlCounts(string word);
bool save(string filenameBase);
bool load(string filenameBase);
private:
struct point
{
point(int hash, int count):m_hash(hash),m_count(count){};
int m_hash;
int m_count;
};
//clears index
void clear();
//hashes a string and copies value into hash, returns hashed index
int hashurl(string &url,int &hash);
//idToUrl maps a hash to its string value
MyMap <int,string>* idToUrl[MAX_MAP_SIZE];
MyMap <string, vector<point> >* urlToCount;
vector<int> indexes;
};
IndexerImpl::IndexerImpl()
{
urlToCount=new MyMap<string, vector<point> >;
}
IndexerImpl::~IndexerImpl()
{
//clear();
}
void IndexerImpl::clear()
{
delete urlToCount;
vector<int>::iterator it = indexes.begin();
while(it!=indexes.end())
{
delete idToUrl[(*it)];
it++;
}
}
int IndexerImpl::hashurl(string &url, int &hash){
int i, total=0;
for (i=0;i<url.length();i++)
total=total + (i+1)*url[i];
hash = total;
total = total % MAX_MAP_SIZE;
return total;
}
bool IndexerImpl::incorporate(string url, WordBag& wb)
{
int hash;
//Generates a hash of url as well as index for linear probing
int modhash = hashurl(url,hash);
//If the head bucket of the array is not initialized, initialize it
if (idToUrl[modhash]==NULL)
idToUrl[modhash]=new MyMap<int,string>;
//Checks to see if this url has already been passed to incorporate
if (idToUrl[modhash]->find(hash)!=NULL)
return false;
//Adds url to hash-url map
if (idToUrl[modhash]->size()==0)
indexes.push_back(modhash);
idToUrl[modhash]->associate(hash,url);
string word;
int count;
//Iterates through W distinct words in wordbag. O(W)
bool gotAWord = wb.getFirstWord(word, count);
while (gotAWord)
{
//Accounts for case-insensitive indexing.
strToLower(word);
point bucket = point(hash,count);
//Find if word already exists. O(log N).
vector<point>* temp = urlToCount->find(word);
if (temp==NULL)
{
//The word does not yet exist in the index
//Create a temp vector
temp = new vector<point>;
}
temp->push_back(bucket);
//Map word to temp Map of urlhash to wordcount
urlToCount->associate(word, *temp);
gotAWord = wb.getNextWord(word,count);
}
//Final BigO is O(WlogN) which is less than O(WN)
return true;
}
vector<UrlCount> IndexerImpl::getUrlCounts(string word)
{
strToLower(word);
vector<UrlCount> output;
//First locates the word in urlToCount Map. O(log N) steps
vector<point>* temp = urlToCount->find(word);
//If word is not found in index, return empty vector
if (temp==NULL)
return output;
vector<point>::iterator it = temp->begin();
//Iterates through the vector mapped to the word. O(log N) + V steps where V is the size of the vector
while (it!=temp->end())
{
UrlCount copy;
copy.count=it->m_count;
int id = it->m_hash;
string url = *(idToUrl[id%MAX_MAP_SIZE]->find(id));
copy.url=url;
output.push_back(copy);
it++;
}
//Final BigO is O(logN + V) which is less than O(N + V)
return output;
}
bool IndexerImpl::save(string filenameBase)
{
ofstream outfile(filenameBase+".txt");
if ( ! outfile )
{
cerr << "Error: Cannot create data.txt!" << endl;
return false;
}
//Map to store locations to grab from array
MyMap<int, int>* h2pull = new MyMap<int, int>;
//Save size of urlToCount to outfile
outfile << urlToCount->size() << endl;
string word;
vector<point>* value = urlToCount->getFirst(word);
while (value!=NULL)
{
//save word and size of vector
outfile << word << " " << value->size() << endl;
vector<point>::iterator it = value->begin();
while (it!=value->end())
{
int hash = it->m_hash;
outfile << hash << " " << it->m_count << endl;
h2pull->associate(hash%MAX_MAP_SIZE, hash%MAX_MAP_SIZE);
it++;
}
value = urlToCount->getNext(word);
}
//Save size of h2pull (number of elements in array to visit)
outfile << h2pull->size() << endl;
int temp;
int* location = h2pull->getFirst(temp);
while (location!=NULL)
{
int size = idToUrl[temp]->size();
outfile << temp << " " << size <<endl;
int hash;
string* url = idToUrl[temp]->getFirst(hash);
while(url!=NULL)
{
outfile << hash << " "<< *url << endl;
url = idToUrl[temp]->getNext(hash);
}
location = h2pull->getNext(temp);
}
delete h2pull;
return true;
}
bool IndexerImpl::load(string filenameBase)
{
//Deletes existing map of url to count
clear();
urlToCount = new MyMap<string, vector<point> >;
ifstream infile(filenameBase+".txt");
if ( ! infile )
{
cerr << "Error: Cannot open data.txt!" << endl;
return false;
}
int urlToCountsize;
infile >> urlToCountsize;
infile.ignore(10000, '\n');
string tempword;
int vectorsize;
for (int i=0;i<urlToCountsize;i++)
{
infile >> tempword;
infile >> vectorsize;
infile.ignore(10000, '\n');
vector<point> tempvec;
for (int j=0;j<vectorsize;j++)
{
int hash, count;
infile >> hash;
infile >> count;
infile.ignore(10000, '\n');
tempvec.push_back(point(hash,count));
}
urlToCount->associate(tempword, tempvec);
}
int idToUrlsize;
infile >> idToUrlsize;
infile.ignore(10000, '\n');
for (int j=0;j<idToUrlsize;j++)
{
int index, count;
infile >> index;
infile >> count;
infile.ignore(10000, '\n');
for (int i=0;i<count;i++)
{
int key;
string url;
infile >> key;
infile >> url;
infile.ignore(10000, '\n');
if (idToUrl[index]==NULL)
idToUrl[index]=new MyMap<int, string>;
idToUrl[index]->associate(key, url);
}
}
return true;
}
//******************** Indexer functions *******************************
// These functions simply delegate to IndexerImpl's functions.
// You probably don't want to change any of this code.
Indexer::Indexer()
{
m_impl = new IndexerImpl;
}
Indexer::~Indexer()
{
delete m_impl;
}
bool Indexer::incorporate(string url, WordBag& wb)
{
return m_impl->incorporate(url, wb);
}
vector<UrlCount> Indexer::getUrlCounts(string word)
{
return m_impl->getUrlCounts(word);
}
bool Indexer::save(string filenameBase)
{
return m_impl->save(filenameBase);
}
bool Indexer::load(string filenameBase)
{
return m_impl->load(filenameBase);
}