-
Notifications
You must be signed in to change notification settings - Fork 18
/
parser_sparse.h
63 lines (52 loc) · 1.64 KB
/
parser_sparse.h
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
#pragma once
#include"config.h"
#include"logger.h"
#include<stdlib.h>
#include<memory>
#include<vector>
#include<functional>
class ParserSparse{
private:
const int MAX_LINE = 10000000;
std::function<void(idx_t,std::vector<std::pair<int,value_t>>)> consume;
std::vector<int> tokenize(char* buff){
std::vector<int> ret;
int i = 0;
while(*(buff + i) != '\0'){
if(*(buff + i) == ':' || *(buff + i) == ' ')
ret.push_back(i);
++i;
}
return ret;
}
std::vector<std::pair<int,value_t>> parse(std::vector<int> tokens,char* buff){
std::vector<std::pair<int,value_t>> ret;
ret.reserve(tokens.size() / 2);
for(int i = 0;i < tokens.size();i += 2){
int index;
value_t val;
sscanf(buff + tokens[i] + 1,"%d",&index);
sscanf(buff + tokens[i + 1] + 1,"%lf",&val);
ret.push_back(std::make_pair(index,val));
}
return ret;
}
public:
ParserSparse(const char* path,std::function<void(idx_t,std::vector<std::pair<int,value_t>>)> consume) : consume(consume){
auto fp = fopen(path,"r");
if(fp == NULL){
Logger::log(Logger::ERROR,"File not found at (%s)\n",path);
exit(1);
}
std::unique_ptr<char[]> buff(new char[MAX_LINE]);
std::vector<std::string> buffers;
idx_t idx = 0;
while(fgets(buff.get(),MAX_LINE,fp)){
auto tokens = tokenize(buff.get());
auto values = parse(tokens,buff.get());
consume(idx,values);
++idx;
}
fclose(fp);
}
};