-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinvertedIndex.cpp
162 lines (126 loc) · 3.85 KB
/
invertedIndex.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
//
// Created by art on 03.05.19.
//
#include <algorithm>
#include "invertedIndex.h"
#include "simple9.h"
void Enc::Push(uint32_t &numOfText) {
if(lastNum != 0) { // not empty
if (lastNum != numOfText) { // no repeat
delay.push_back(numOfText - lastNum); // push delta
lastNum = numOfText;
}
else {
return;
}
}
else {
delay.push_back(numOfText);
lastNum = numOfText;
}
if(delay.size() == 28) {
simple9_encode(delay, encoded);
delay.clear();
}
}
std::vector<uint32_t > Enc::Decode() {
if(!delay.empty()) {
simple9_encode(delay, encoded);
delay.clear();
}
return simple9_decode(encoded);
}
void Enc::PushDelay() {
if (!delay.empty()) {
simple9_encode(delay,encoded);
delay.clear();
}
}
std::vector<std::string> splitStringBySpaceInVector(std::string &str) {
std::vector<std::string> vec;
std::istringstream iss(str);
for (std::string s; iss >> s;) {
vec.push_back(s);
}
return vec;
}
invertedIndex::invertedIndex(std::istream &in, uint32_t &n) {
getchar(); //\n
std::string str;
for (uint32_t numOfText = 1; numOfText <= n; ++numOfText) {
getline(std::cin, str);
readRow(numOfText, str);
}
for (auto &i : words) {
i.second.PushDelay();
}
}
void invertedIndex::readRow(uint32_t &numOfText, std::string& str) {
std::istringstream iss(str);
for (std::string s; iss >> s;) {
words[s].Push(numOfText);
}
}
std::vector<uint32_t> invertedIndex::queryIntersection(std::vector<uint32_t> &lhs, std::vector<uint32_t> &rhs) {
std::vector<uint32_t> ans;
int i = 0; // index in not decoded vector
int j = 0; // index in encoded vector
int k = 0; // index in selector
uint32_t lastElement = 0;
uint32_t element;
while ( i < lhs.size() && j < rhs.size()) {
uint32_t selector = rhs[j];
uint32_t typeSelector = selector & SELECTOR_MASK;
uint32_t countOfElements = selectors[typeSelector].nitems; // count of elements in one selector
uint32_t shift = selectors[typeSelector].nbits;
uint32_t mask = (1u << selectors[typeSelector].nbits) - 1;
selector = selector >> SELECTOR_BITS; // skip control bits
k = 0;
element = (selector & mask);
if (lastElement != 0) {
lastElement += element;
} else {
lastElement = element;
}
while ( k < countOfElements && i < lhs.size() ) {
if (element) {
if (lastElement == lhs[i]) {
ans.push_back(lastElement);
i++;
k++;
selector = selector >> shift;
element = (selector & mask);
lastElement += element;
}
else if (lhs[i] > lastElement) {
k++;
selector = selector >> shift;
element = (selector & mask);
lastElement += element;
}
else {
i++;
}
}
else {
k++;
selector = selector >> shift;
}
}
j++;
}
return ans;
}
void invertedIndex::query(std::string &str) {
std::vector<std::string> QueryWords = splitStringBySpaceInVector(str);
std::vector<uint32_t>::iterator it;
std::vector<uint32_t> ans = words[QueryWords[0]].Decode();
for (int i = 1; i < QueryWords.size(); ++i) {
ans = queryIntersection(ans, words[QueryWords[i]].encoded);
}
long long i = 0, sum = 0;
for (it = ans.begin(); it != ans.end(); ++it, ++i) {
sum = (sum + i * (*it)) % 1000000007;
}
std::cout << ans.size() << " " << sum << std::endl;
}