forked from Preparation-Publication-BD2K/db_compress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.h
81 lines (72 loc) · 2.15 KB
/
base.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef BASE_H
#define BASE_H
#include <memory>
#include <string>
#include <vector>
namespace db_compress {
/*
* AttrValue is a virtual class for attribute values, all typed attribute
* values will be subclass of this class.
*/
class AttrValue {
public:
virtual ~AttrValue() = 0;
};
inline AttrValue::~AttrValue() {}
/*
* Tuple structure contains num_attr_ of attributes, the attr_ array stores the pointers to
* values of attributes, the attribute types can be determined by Schema class. Note that
* Tuple structures do not own the attribute value objects.
*/
struct Tuple {
Tuple(int cols) : attr(cols) {}
std::vector<const AttrValue*> attr;
};
/*
* Schema structure contains the attribute types information, which are used for type casting
* when we need to interpret the values in each tuple.
*/
struct Schema {
std::vector<int> attr_type;
Schema() {}
Schema(const std::vector<int>& attr_type_vec) : attr_type(attr_type_vec) {}
};
/*
* Structure used to represent a probability value
*/
struct Prob {
long long num;
char exp;
Prob() : num(0), exp(0) {}
Prob(long long num_, char exp_) : num(num_), exp(exp_) {}
void Reduce() {
if (num == 0) { exp = 0; return; }
while ((num & 0xff) == 0) { num >>= 8; exp -= 8; }
if ((num & 0xf) == 0) { num >>= 4; exp -= 4; }
if ((num & 3) == 0) { num >>= 2; exp -= 2; }
if ((num & 1) == 0) { num >>= 1; -- exp; }
}
};
/*
* Structure used to represent any probability interval between [0, 1].
*/
struct ProbInterval {
Prob l, r;
ProbInterval(const Prob& l_, const Prob& r_) : l(l_), r(r_) {}
};
/*
* Structure used to represent unit probability interval (i.e., [n/2^k, (n+1)/2^k])
*/
struct UnitProbInterval {
long long num;
char exp;
UnitProbInterval(int num_, char exp_) : num(num_), exp(exp_) {}
Prob Left() { return Prob(num, exp); }
Prob Right() { return Prob(num + 1, exp); }
Prob Mid() { return Prob((num << 1) + 1, exp + 1); }
ProbInterval GetProbInterval() { return ProbInterval(Left(),Right()); }
void GoLeft() { num <<= 1; ++ exp; }
void GoRight() { num <<= 1; ++ exp; ++ num; }
};
}
#endif