-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcritbit_node.h
140 lines (94 loc) · 3.48 KB
/
critbit_node.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
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
/**
* Author: kkzone
* Date: 10/18/2015
* Description: critbit_node_t,
**/
#ifndef CRITBIT_NODE_H
#define CRITBIT_NODE_H
#include <stdint.h>
#include <assert.h>
/*** NOTE:
* 1.) the significant(highest) position of child[0](left child) decide if the node is leaf node.
* If the significant position of node r's left child(child[0]) is 1, it is leaf node. otherwise is not.
*
* 2.) the child[]: in the internal node, it is its two children.
* 对于critbit-tree的叶子节点, child[0] 表示一个后缀值(但是最高位标识为1,表明是一个叶子节点).
* child[1] 表示这个叶子节点的index值。
*
* 3.) For internal node, child[0] and child[1] is it's left and right child.
* For leaf node, child[0] is suffix, child[1]即右孩子表示这个叶子节点在这颗critbit-tree的所有叶子节点中的index值,
不用因为critbit-tree的所有的叶子节点都指向一个后缀,而且都是有序的。我们需要知道一个叶子节点在这些所有的叶子节点的中的
位置,这样在串B-tree中进行查找的时候通过这个index值可以知道,孩子节点的位置,以及后缀范围,是至关重要的。
*
* 4.) The critpos: (critpos >> 3)表示需要比较的那个字符,(critpos & 0x7)表示这个字符中的第几个bit(从最高位开始数)。
当在critbit-tree中进行查找的时候,需要求模式P的某个字符ch的critpos值,如果这个值是0,走左孩子这条
道路,如果这个值是1,需要走右孩子这条道路。
The critpos of leaf node is zero.
*
* 5.) collect the leaf node from left to right is the suffix array. So the leaf ndoe are
* lexocically order sorted.
***/
#define CRITBIT_LEFTCHILD 0
#define CRITBIT_RIGHTCHILD 1
class critbit_node;
typedef critbit_node critbit_node_t;
class critbit_node {
private:
uint64_t critpos; /* Position of the critibit bit */
critbit_node* child[2]; /* Its two child For internal node*/
public:
void set_suffix(uint64_t sufpos){
this->child[CRITBIT_LEFTCHILD] = (critbit_node_t*)(sufpos | 0x8000000000000000);
}
void set_index(uint64_t index){
this->child[CRITBIT_RIGHTCHILD] = (critbit_node_t*)index;
}
uint64_t get_suffix(){
assert(is_leaf());
return (((uint64_t)(this->child[CRITBIT_LEFTCHILD])) & (~0x8000000000000000));
}
uint64_t get_index(){
assert(is_leaf());
return (uint64_t)(this->child[CRITBIT_RIGHTCHILD]);
}
bool is_leaf(){
return ((uint64_t)(this->child[CRITBIT_LEFTCHILD]) & 0x8000000000000000) >> 63;
}
void set_critpos(uint64_t pos){
this->critpos = pos;
}
void set_critpos(uint64_t diffChar, uint64_t diffPos) {
this->critpos = ((diffChar << 3) | diffPos);
}
uint64_t get_critpos(){
return this->critpos;
}
uint64_t get_char_len(){
return (this->critpos >> 3);
}
uint64_t get_diff_pos(){
return (this->critpos & 0x7);
}
critbit_node_t* get_left_child(){
assert(!is_leaf());
return this->child[CRITBIT_LEFTCHILD];
}
critbit_node_t* get_right_child(){
assert(!is_leaf());
return this->child[CRITBIT_RIGHTCHILD];
}
critbit_node_t* get_child(uint64_t direction){
assert(!is_leaf());
return this->child[direction];
}
void set_left_child(critbit_node_t* lchild){
this->child[CRITBIT_LEFTCHILD] = lchild;
}
void set_right_child(critbit_node_t* rchild){
this->child[CRITBIT_RIGHTCHILD] = rchild;
}
void set_child(uint64_t direction, critbit_node_t* val){
this->child[direction] = val;
}
};
#endif