forked from mzz235711/Selectivity-Estimation-for-Queries-Containing-Predicates-over-Set-Valued-Attributes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset_map.cc
264 lines (251 loc) · 8.16 KB
/
set_map.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
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
#include <boost/functional/hash.hpp>
#include <vector>
#include <string>
#include <fstream>
#include <cctype>
#include <regex>
#include <unordered_map>
#include <ctime>
#include <stdlib.h>
#include <iostream>
#include <utility>
#include <limits>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils.h"
bool comp(std::pair<std::string,int> &a, std::pair<std::string,int> &b) {
return a.second > b.second;
}
int BinarySearch(std::vector<int> &arr, int l, int r, int &x) {
if (r >= l) {
int mid = l + (r - l) / 2;
if (arr[mid] == x) {
return 1;
} else if (arr[mid] > x) {
return BinarySearch(arr, l, mid - 1, x);
} else {
return BinarySearch(arr, mid + 1, r, x);
}
}
return 0;
}
void settrie_insert(TreeNode &root, std::vector<int> &set_key) {
auto *curr_node = &root;
for (auto &element : set_key) {
std::vector<TreeNode>::iterator it;
for (it = curr_node->children.begin(); it != curr_node->children.end(); it++) {
if (it->id == element) {
auto &new_node = *it;
curr_node = &new_node;
break;
} else if (it->id > element) {
TreeNode new_node;
new_node.id = element;
int index = it - curr_node->children.begin();
curr_node->children.insert(it, new_node);
curr_node = &(curr_node->children[index]);
break;
}
}
if (it == curr_node->children.end()) {
TreeNode new_node;
new_node.id = element;
curr_node->children.push_back(new_node);
curr_node = &(curr_node->children[curr_node->children.size() - 1]);
}
}
curr_node->last_flag = true;
}
void dfs_addvalue(TreeNode &curr_node, int &set_value, Vectormap &setmap, std::vector<int> &curr_set, int &node_num, int level) {
curr_node.range.first = set_value;
curr_node.level = level;
node_num++;
if (curr_node.last_flag == true) {
//if (set_map.find(curr_set) != set_map.end()) {
// auto set_value = set_map[curr_set];
//results.push_back(curr_node.last_flag);
//}
setmap[curr_set] = set_value;
set_value++;
}
for (auto &next_node : curr_node.children) {
//auto next_set = curr_set;
//next_set.push_back(next_node.id);
auto next_set = curr_set;
next_set.push_back(next_node.id);
dfs_addvalue(next_node, set_value, setmap, next_set, node_num, level + 1);
}
curr_node.range.second = set_value;
}
void serialize(TreeNode &curr_node, std::ofstream &file) {
file.write((char*)(&curr_node.level), sizeof(int));
file.write((char*)(&curr_node.id), sizeof(int));
file.write((char*)(&curr_node.last_flag), sizeof(bool));
file.write((char*)(&curr_node.range.first), sizeof(int));
file.write((char*)(&curr_node.range.second), sizeof(int));
for (auto &next_node : curr_node.children) {
serialize(next_node, file);
}
}
int main(int argc, char** argv) {
std::ifstream infile("./dataset/geotweet/geotweet_tags.csv");
std::string s;
getline(infile, s);
char delimiter = ',';
std::regex e("[a-z]+");
std::unordered_map<std::string, int> all_keywords;
std::vector<std::vector<std::string>> texts;
std::string pad = "ZZZ";
int tmpidx = 0;
while (getline(infile, s)) {
//size_t pos = s.find(delimiter);
//auto t = pos + delimiter.length();
//size_t pos2 = s.find(delimiter, t);
//std::string text = s.substr(pos + 1, pos2 - pos - 1);
std::string text = s;
if (s[0] == '"') {
text = text.substr(1, text.size() - 2);
}
std::string keyword;
//for(unsigned i = 0; i < text.size(); i++) {
// text[i] = std::tolower(text[i]);
//}
std::stringstream ss(text);
std::vector<std::string> new_text;
while (std::getline(ss, keyword, delimiter)) {
if (std::find(new_text.begin(), new_text.end(), keyword) == new_text.end()) {
if (all_keywords.find(keyword) == all_keywords.end()){
all_keywords.emplace(keyword, 1);
} else {
all_keywords[keyword] += 1;
}
new_text.push_back(keyword);
}
}
texts.push_back(new_text);
}
printf("file length %lu\n", texts.size());
/*
std::vector<std::pair<std::string, int>> freq_pair(all_keywords.begin(), all_keywords.end());
std::sort(freq_pair.begin(), freq_pair.end(), comp);
std::ofstream idxoutfile("idx.csv");
for (int i = 0; i < freq_pair.size(); i++) {
auto &k = freq_pair[i].first;
auto &v = freq_pair[i].second;
if (v >= 10) {
idxoutfile << k << "\t" << i << "\n";
}
}
idxoutfile.close();
return 0;
*/
std::vector<std::string> idx2word;
std::unordered_map<std::string, int> word2idx;
int index = 0;
std::ifstream idxinfile("idx.csv");
while (idxinfile >> s >> index) {
word2idx.emplace(s, index);
idx2word.push_back(s);
if (s == "abcxyz") {
std::cout << s << "\t" << index << "\t" << idx2word.size() << "\n";
}
}
word2idx[pad] = idx2word.size();
idx2word.push_back(pad);
printf("Number of keyword: %lu\n", idx2word.size());
//std::ofstream freqfile("frequency.csv");
//for (auto &w : idx2word) {
// freqfile << w << "\t" << all_keywords[w] << "\n";
//}
//freqfile.close();
std::vector<std::vector<int>> text_idx;
for (auto &text : texts) {
std::vector<int> new_text;
for (auto t: text) {
if (word2idx.find(t) != word2idx.end()) {
new_text.push_back(word2idx[t]);
}
}
std::sort(new_text.begin(), new_text.end());
text_idx.push_back(new_text);
}
int v_num = idx2word.size();
//std::cout << "vnum: " << vnum << "\n";
int partition_num = atoi(argv[1]);
//std::string folder_name = "./graph_color";
std::string folder_name = "color_partition_" + std::to_string(partition_num);
//std::string partfilename = folder_name + "/approx_clique_part_" + std::to_string(partition_num) + ".txt";
std::string partfilename = folder_name + "/part.txt";
std::ifstream partfile(partfilename);
std::vector<int> partition(v_num - 1);
int partid;
int maxpartid = 0;
for (int i = 0; i < v_num - 1; i++) {
partfile >> partid;
partition[i] = partid;
if (partid > maxpartid) {
maxpartid = partid;
}
}
//int partition_num = maxpartid + 1;
std::cout << "cp1\n";
std::vector<int> dis_num(partition_num, 0);
std::vector<std::unordered_map<std::vector<int>, int, container_hash<std::vector<int>>>> setmap(
partition_num, std::unordered_map<std::vector<int>, int, container_hash<std::vector<int>>>());
for (auto &idxs : text_idx) {
std::vector<std::vector<int>> part_idx(partition_num, std::vector<int>());
for (auto &idx : idxs) {
partid = partition[idx];
part_idx[partid].push_back(idx);
}
for (int i = 0; i < partition_num; i++) {
// Add pad to each column
if (part_idx[i].size() == 0) {
part_idx[i].push_back(word2idx[pad]);
}
if (part_idx[i].size() != 0) {
std::sort(part_idx[i].begin(), part_idx[i].end());
if (setmap[i].find(part_idx[i]) == setmap[i].end()) {
setmap[i][part_idx[i]] = dis_num[i];
dis_num[i]++;
}
}
}
}
std::vector<TreeNode> settrie(partition_num, TreeNode());
std::vector<Vectormap> set_map(partition_num, Vectormap());
std::string output_treename = folder_name +"/settrie.txt";
std::ofstream output_tree(output_treename, std::ios::out | std::ios::binary);
for (int i = 0; i < partition_num; i++) {
int node_num = 0;
for (auto &k_v : setmap[i]) {
auto set_key = k_v.first;
settrie_insert(settrie[i], set_key);
}
int init_value = 0;
std::vector<int> curr_set;
dfs_addvalue(settrie[i], init_value, set_map[i], curr_set, node_num, 0);
output_tree.write((char*)(&node_num), sizeof(int));
std::cout << "node num " << i << " " << node_num << "\n";
serialize(settrie[i], output_tree);
}
output_tree.close();
std::cout << "cp2\n";
std::string output_filename = folder_name + "/dis_set_map.txt";
std::ofstream output_file(output_filename);
for (int i = 0; i < partition_num; i++) {
output_file << i << " " << set_map[i].size() << "\n";
for (auto &k_v : set_map[i]) {
auto &dis_set = k_v.first;
auto dis_value = k_v.second;
output_file << dis_set.size() << " ";
for (auto idx : dis_set) {
output_file << idx << " ";
}
output_file << dis_value << "\n";
}
}
return 0;
}