-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryTriples.hpp
114 lines (103 loc) · 2.87 KB
/
BinaryTriples.hpp
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
#ifndef BINARYTRIPLESHPP
#define BINARYTRIPLESHPP
#include "GPU.hpp"
#include "Types.h"
#include "Triple.hpp"
#include "Compressor.hpp"
#include "TreePattern.hpp"
#include <cstdint>
#include <memory>
#include <fstream>
#include <boost/noncopyable.hpp>
typedef std::unique_ptr<class AbstractIterator> PAbstractIterator;
inline int compare(uint32_t a, uint32_t b)
{
if(a<b)
return -1;
else if(a>b)
return 1;
else
return 0;
}
class BinaryTriple
{
public:
BinaryTriple(uint32_t s,uint32_t p, uint32_t o)
:_s(s),_p(p),_o(o)
{
}
uint32_t s() const
{
return _s;
}
uint32_t p() const
{
return _p;
}
uint32_t o() const
{
return _o;
}
static int compare(const BinaryTriple& a, const BinaryTriple& b)
{
int pc=::compare(a.p(),b.p());
if(pc!=0)
return pc;
int oc=::compare(a.o(),b.o());
if(oc!=0)
return oc;
int sc=::compare(a.s(),b.s());
return sc;
}
friend bool operator<(const BinaryTriple& a, const BinaryTriple& b)
{
return BinaryTriple::compare(a,b)<0;
}
private:
uint32_t _s,_p,_o;
};
typedef std::deque<BinaryTriple> RawBinaryTriples;
class BinaryTriple;
class BinaryTriples : private boost::noncopyable
{
public:
typedef ::Address Address;
static const std::pair<size,size> invalid;
void fill(PCodes soCodes, PCodes pCodes,RawBinaryTriples& triples);
void merge(const BinaryTriples& a, const BinaryTriples& b);
std::deque<std::string> answer(const TreePattern::Node* query) const;
size count(const TreePattern::Node* query) const;
bool ask(const TreePattern::Node* query, uint32_t s) const;
BinaryTriples();
~BinaryTriples();
void save(std::ofstream& f) const;
void load(std::ifstream& file);
private:
Address level2For1(uint32_t p) const;
Address level3For2(const Address& a, uint32_t s) const;
Address level3For12(uint32_t l1, uint32_t l2) const;
PAbstractIterator iteratorForQuery(const TreePattern::Node* query) const;
std::vector<uint32_t> answerCodes(const TreePattern::Node* query) const;
void add(uint32_t s, uint32_t p, uint32_t o);
void add(const BinaryTriple& t);
void finish();
void dump(const BinaryTriple& t) const;
void initGPU();
std::vector<uint32_t> flatten(PAbstractIterator i) const;
friend class TripleIterator;
PCodes soCodes,pCodes;
//id[1-4 bajty],pozycja w subjects[4 bajty]
uint8_t *level1;
size len1;
//id[1-4 bajty],pozycja w objects[4 bajty]
uint8_t *level2;
size len2;
//id[1-4 bajty]
uint8_t *level3;
size len3;
uint32_t prevP,prevO;
#if USE_GPU
GPU gpu;
#endif
};
#endif //BINARYTRIPLESHPP