-
Notifications
You must be signed in to change notification settings - Fork 4
/
windex.cc
178 lines (158 loc) · 4.48 KB
/
windex.cc
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
#include <string>
#include <string.h>
#include <stdexcept>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <algorithm>
#include <unistd.h>
#include "bytell_hash_map.hpp"
#include <set>
std::string stringerror()
{
return strerror(errno);
}
struct SmartFP
{
SmartFP(const char* fname, const char* mode)
{
d_fp = fopen(fname, mode);
if(!d_fp)
throw std::runtime_error("Can't open file: " + stringerror());
}
~SmartFP()
{
fclose(d_fp);
}
FILE* d_fp;
};
bool getWord(FILE* fp, std::string& str, size_t& offset)
{
str.clear();
int c;
for(;;) {
c = getc_unlocked(fp);
++offset;
if(c == EOF)
return !str.empty();
if(!isalnum((char)c)) {
if(!str.empty())
return true;
else
continue;
}
str.append(1, tolower((char)c));
}
return true;
}
using namespace std;
struct Location
{
unsigned int fileno;
size_t offset;
} __attribute__((packed));
int main(int argc, char** argv)
{
vector<string> filenames;
ifstream ifs(argv[1]);
std::string line;
while(getline(ifs, line))
filenames.push_back(line);
cout<<"Have "<<filenames.size()<<" files"<<endl;
unsigned long wordcount=0, bytecount=0;
//std::unordered_map<string, std::vector<Location>> allWords;
ska::bytell_hash_map<string, std::vector<Location>> allWords;
unsigned int fileno=0;
for(const auto& fname : filenames) {
cout<<"\r"<<(fileno+1)<<"/"<<filenames.size()<<" "<<fname<<", "<<wordcount<<" words, "<<allWords.size()<<" different\x1B[K";
cout.flush();
SmartFP sfp(fname.c_str(), "r");
std::string word;
size_t offset=0;
while(getWord(sfp.d_fp, word, offset)) {
++wordcount;
allWords[word].push_back({fileno, offset});
// cout<<word<<"\n";
}
bytecount += ftell(sfp.d_fp);
++fileno;
}
cout<<"\nRead "<<bytecount<<" bytes"<<endl;
// this is where we take all the unique words and sort them so we can
// do prefix searches too
std::vector<string> owords;
owords.reserve(allWords.size());
for(const auto& w : allWords) {
owords.push_back(w.first);
}
sort(owords.begin(), owords.end());
cout<<"Done indexing"<<endl;
vector<pair<std::string, size_t> > popcount;
for(const auto& w : allWords)
popcount.push_back({w.first, w.second.size()});
auto cmp = [](const auto& a, const auto& b)
{
return b.second < a.second;
};
int top = std::min(popcount.size(), (size_t)20);
nth_element(popcount.begin(), popcount.begin() + top, popcount.end(), cmp);
sort(popcount.begin(), popcount.begin() + top, cmp);
int count=0;
for(const auto& e : popcount) {
if(++count > top)
break;
cout << count << "\t" << e.first << "\t" << e.second << endl;
}
cout<<"Words present in most files: "<<endl;
popcount.clear();
for(const auto& w : allWords) {
set<uint32_t> uni;
for(const auto& f : w.second)
uni.insert(f.fileno);
popcount.push_back({w.first, uni.size()});
}
sort(popcount.begin(), popcount.end(), cmp);
count=0;
for(const auto& e : popcount) {
cout<<count<<"\t"<<e.first<<"\t"<<e.second<<endl;
if(++count > 19)
break;
}
// normal word: search, if ends on '?', list matching words
// if ends on '*', search for all those words
while(getline(cin, line)) {
if(line.empty())
continue;
char lastchar =*line.rbegin(); // rbegin() == reverse begin
if(lastchar =='?' || lastchar=='*') {
line.resize(line.size()-1);
if(line.empty())
continue;
cout<<"Looking for words starting with '"<<line<<"'"<<endl;
auto iter = lower_bound(owords.begin(), owords.end(), line);
for(; iter != owords.end() && !iter->compare(0, line.size(), line); ++iter) {
if(lastchar=='?')
cout<<" "<<*iter<<endl;
else {
for(const auto& l : allWords.find(*iter)->second) {
cout<<"\t"<<*iter<<": File "<<filenames[l.fileno]<<", offset "<<l.offset<<endl;
}
}
}
}
else {
auto iter = allWords.find(line);
if(iter == allWords.end()) {
cout<<"Word '"<<line<<"' was not found"<<endl;
continue;
}
cout<<"Word '"<<line<<"' occurred "<<iter->second.size()<<" times: "<<endl;
for(const auto& l : iter->second) {
cout<<"\tFile "<<filenames[l.fileno]<<", offset "<<l.offset<<endl;
}
cout<<"\tEnd of "<<iter->second.size()<<" hits"<<endl;
}
}
_exit(0); // cheating a bit for the benchmark, saves cleanup time
}